yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 29 | #include "accessors.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 30 | |
| 31 | #include "contexts.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 32 | #include "deoptimizer.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "execution.h" |
| 34 | #include "factory.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 35 | #include "frames-inl.h" |
| 36 | #include "isolate.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 37 | #include "list-inl.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 38 | #include "property-details.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | template <class C> |
| 45 | static C* FindInPrototypeChain(Object* obj, bool* found_it) { |
| 46 | ASSERT(!*found_it); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 47 | Heap* heap = HEAP; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | while (!Is<C>(obj)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 49 | if (obj == heap->null_value()) return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | obj = obj->GetPrototype(); |
| 51 | } |
| 52 | *found_it = true; |
| 53 | return C::cast(obj); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | // Entry point that never should be called. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 58 | MaybeObject* Accessors::IllegalSetter(JSObject*, Object*, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | UNREACHABLE(); |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | Object* Accessors::IllegalGetAccessor(Object* object, void*) { |
| 65 | UNREACHABLE(); |
| 66 | return object; |
| 67 | } |
| 68 | |
| 69 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 70 | MaybeObject* Accessors::ReadOnlySetAccessor(JSObject*, Object* value, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | // According to ECMA-262, section 8.6.2.2, page 28, setting |
| 72 | // read-only properties must be silently ignored. |
| 73 | return value; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | // |
| 78 | // Accessors::ArrayLength |
| 79 | // |
| 80 | |
| 81 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 82 | MaybeObject* Accessors::ArrayGetLength(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | // Traverse the prototype chain until we reach an array. |
| 84 | bool found_it = false; |
| 85 | JSArray* holder = FindInPrototypeChain<JSArray>(object, &found_it); |
| 86 | if (!found_it) return Smi::FromInt(0); |
| 87 | return holder->length(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | // The helper function will 'flatten' Number objects. |
| 92 | Object* Accessors::FlattenNumber(Object* value) { |
| 93 | if (value->IsNumber() || !value->IsJSValue()) return value; |
| 94 | JSValue* wrapper = JSValue::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 95 | ASSERT(Isolate::Current()->context()->global_context()->number_function()-> |
| 96 | has_initial_map()); |
| 97 | Map* number_map = Isolate::Current()->context()->global_context()-> |
| 98 | number_function()->initial_map(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | if (wrapper->map() == number_map) return wrapper->value(); |
| 100 | return value; |
| 101 | } |
| 102 | |
| 103 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 104 | MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 105 | Isolate* isolate = object->GetIsolate(); |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 106 | |
| 107 | // This means one of the object's prototypes is a JSArray and the |
| 108 | // object does not have a 'length' property. Calling SetProperty |
| 109 | // causes an infinite loop. |
| 110 | if (!object->IsJSArray()) { |
| 111 | return object->SetLocalPropertyIgnoreAttributes( |
| 112 | isolate->heap()->length_symbol(), value, NONE); |
| 113 | } |
| 114 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | value = FlattenNumber(value); |
| 116 | |
| 117 | // Need to call methods that may trigger GC. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 118 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | |
| 120 | // Protect raw pointers. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 121 | Handle<JSObject> object_handle(object, isolate); |
| 122 | Handle<Object> value_handle(value, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | |
| 124 | bool has_exception; |
| 125 | Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception); |
| 126 | if (has_exception) return Failure::Exception(); |
| 127 | Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception); |
| 128 | if (has_exception) return Failure::Exception(); |
| 129 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | if (uint32_v->Number() == number_v->Number()) { |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 131 | return Handle<JSArray>::cast(object_handle)->SetElementsLength(*uint32_v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 132 | } |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 133 | return isolate->Throw( |
| 134 | *isolate->factory()->NewRangeError("invalid_array_length", |
| 135 | HandleVector<Object>(NULL, 0))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | const AccessorDescriptor Accessors::ArrayLength = { |
| 140 | ArrayGetLength, |
| 141 | ArraySetLength, |
| 142 | 0 |
| 143 | }; |
| 144 | |
| 145 | |
| 146 | // |
| 147 | // Accessors::StringLength |
| 148 | // |
| 149 | |
| 150 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 151 | MaybeObject* Accessors::StringGetLength(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | Object* value = object; |
| 153 | if (object->IsJSValue()) value = JSValue::cast(object)->value(); |
| 154 | if (value->IsString()) return Smi::FromInt(String::cast(value)->length()); |
| 155 | // If object is not a string we return 0 to be compatible with WebKit. |
| 156 | // Note: Firefox returns the length of ToString(object). |
| 157 | return Smi::FromInt(0); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | const AccessorDescriptor Accessors::StringLength = { |
| 162 | StringGetLength, |
| 163 | IllegalSetter, |
| 164 | 0 |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | // |
| 169 | // Accessors::ScriptSource |
| 170 | // |
| 171 | |
| 172 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 173 | MaybeObject* Accessors::ScriptGetSource(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | Object* script = JSValue::cast(object)->value(); |
| 175 | return Script::cast(script)->source(); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | const AccessorDescriptor Accessors::ScriptSource = { |
| 180 | ScriptGetSource, |
| 181 | IllegalSetter, |
| 182 | 0 |
| 183 | }; |
| 184 | |
| 185 | |
| 186 | // |
| 187 | // Accessors::ScriptName |
| 188 | // |
| 189 | |
| 190 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 191 | MaybeObject* Accessors::ScriptGetName(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | Object* script = JSValue::cast(object)->value(); |
| 193 | return Script::cast(script)->name(); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | const AccessorDescriptor Accessors::ScriptName = { |
| 198 | ScriptGetName, |
| 199 | IllegalSetter, |
| 200 | 0 |
| 201 | }; |
| 202 | |
| 203 | |
| 204 | // |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 205 | // Accessors::ScriptId |
| 206 | // |
| 207 | |
| 208 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 209 | MaybeObject* Accessors::ScriptGetId(Object* object, void*) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 210 | Object* script = JSValue::cast(object)->value(); |
| 211 | return Script::cast(script)->id(); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | const AccessorDescriptor Accessors::ScriptId = { |
| 216 | ScriptGetId, |
| 217 | IllegalSetter, |
| 218 | 0 |
| 219 | }; |
| 220 | |
| 221 | |
| 222 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | // Accessors::ScriptLineOffset |
| 224 | // |
| 225 | |
| 226 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 227 | MaybeObject* Accessors::ScriptGetLineOffset(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | Object* script = JSValue::cast(object)->value(); |
| 229 | return Script::cast(script)->line_offset(); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | const AccessorDescriptor Accessors::ScriptLineOffset = { |
| 234 | ScriptGetLineOffset, |
| 235 | IllegalSetter, |
| 236 | 0 |
| 237 | }; |
| 238 | |
| 239 | |
| 240 | // |
| 241 | // Accessors::ScriptColumnOffset |
| 242 | // |
| 243 | |
| 244 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 245 | MaybeObject* Accessors::ScriptGetColumnOffset(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | Object* script = JSValue::cast(object)->value(); |
| 247 | return Script::cast(script)->column_offset(); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | const AccessorDescriptor Accessors::ScriptColumnOffset = { |
| 252 | ScriptGetColumnOffset, |
| 253 | IllegalSetter, |
| 254 | 0 |
| 255 | }; |
| 256 | |
| 257 | |
| 258 | // |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 259 | // Accessors::ScriptData |
| 260 | // |
| 261 | |
| 262 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 263 | MaybeObject* Accessors::ScriptGetData(Object* object, void*) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 264 | Object* script = JSValue::cast(object)->value(); |
| 265 | return Script::cast(script)->data(); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | const AccessorDescriptor Accessors::ScriptData = { |
| 270 | ScriptGetData, |
| 271 | IllegalSetter, |
| 272 | 0 |
| 273 | }; |
| 274 | |
| 275 | |
| 276 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | // Accessors::ScriptType |
| 278 | // |
| 279 | |
| 280 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 281 | MaybeObject* Accessors::ScriptGetType(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | Object* script = JSValue::cast(object)->value(); |
| 283 | return Script::cast(script)->type(); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | const AccessorDescriptor Accessors::ScriptType = { |
| 288 | ScriptGetType, |
| 289 | IllegalSetter, |
| 290 | 0 |
| 291 | }; |
| 292 | |
| 293 | |
| 294 | // |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 295 | // Accessors::ScriptCompilationType |
| 296 | // |
| 297 | |
| 298 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 299 | MaybeObject* Accessors::ScriptGetCompilationType(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 300 | Object* script = JSValue::cast(object)->value(); |
| 301 | return Script::cast(script)->compilation_type(); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | const AccessorDescriptor Accessors::ScriptCompilationType = { |
| 306 | ScriptGetCompilationType, |
| 307 | IllegalSetter, |
| 308 | 0 |
| 309 | }; |
| 310 | |
| 311 | |
| 312 | // |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 313 | // Accessors::ScriptGetLineEnds |
| 314 | // |
| 315 | |
| 316 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 317 | MaybeObject* Accessors::ScriptGetLineEnds(Object* object, void*) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 318 | JSValue* wrapper = JSValue::cast(object); |
| 319 | Isolate* isolate = wrapper->GetIsolate(); |
| 320 | HandleScope scope(isolate); |
| 321 | Handle<Script> script(Script::cast(wrapper->value()), isolate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 322 | InitScriptLineEnds(script); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 323 | ASSERT(script->line_ends()->IsFixedArray()); |
| 324 | Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends())); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 325 | // We do not want anyone to modify this array from JS. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 326 | ASSERT(*line_ends == isolate->heap()->empty_fixed_array() || |
| 327 | line_ends->map() == isolate->heap()->fixed_cow_array_map()); |
| 328 | Handle<JSArray> js_array = |
| 329 | isolate->factory()->NewJSArrayWithElements(line_ends); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 330 | return *js_array; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | |
| 334 | const AccessorDescriptor Accessors::ScriptLineEnds = { |
| 335 | ScriptGetLineEnds, |
| 336 | IllegalSetter, |
| 337 | 0 |
| 338 | }; |
| 339 | |
| 340 | |
| 341 | // |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 342 | // Accessors::ScriptGetContextData |
| 343 | // |
| 344 | |
| 345 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 346 | MaybeObject* Accessors::ScriptGetContextData(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 347 | Object* script = JSValue::cast(object)->value(); |
| 348 | return Script::cast(script)->context_data(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
| 352 | const AccessorDescriptor Accessors::ScriptContextData = { |
| 353 | ScriptGetContextData, |
| 354 | IllegalSetter, |
| 355 | 0 |
| 356 | }; |
| 357 | |
| 358 | |
| 359 | // |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 360 | // Accessors::ScriptGetEvalFromScript |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 361 | // |
| 362 | |
| 363 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 364 | MaybeObject* Accessors::ScriptGetEvalFromScript(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 365 | Object* script = JSValue::cast(object)->value(); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 366 | if (!Script::cast(script)->eval_from_shared()->IsUndefined()) { |
| 367 | Handle<SharedFunctionInfo> eval_from_shared( |
| 368 | SharedFunctionInfo::cast(Script::cast(script)->eval_from_shared())); |
| 369 | |
| 370 | if (eval_from_shared->script()->IsScript()) { |
| 371 | Handle<Script> eval_from_script(Script::cast(eval_from_shared->script())); |
| 372 | return *GetScriptWrapper(eval_from_script); |
| 373 | } |
| 374 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 375 | return HEAP->undefined_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 379 | const AccessorDescriptor Accessors::ScriptEvalFromScript = { |
| 380 | ScriptGetEvalFromScript, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 381 | IllegalSetter, |
| 382 | 0 |
| 383 | }; |
| 384 | |
| 385 | |
| 386 | // |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 387 | // Accessors::ScriptGetEvalFromScriptPosition |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 388 | // |
| 389 | |
| 390 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 391 | MaybeObject* Accessors::ScriptGetEvalFromScriptPosition(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 392 | HandleScope scope; |
| 393 | Handle<Script> script(Script::cast(JSValue::cast(object)->value())); |
| 394 | |
| 395 | // If this is not a script compiled through eval there is no eval position. |
| 396 | int compilation_type = Smi::cast(script->compilation_type())->value(); |
| 397 | if (compilation_type != Script::COMPILATION_TYPE_EVAL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 398 | return HEAP->undefined_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // Get the function from where eval was called and find the source position |
| 402 | // from the instruction offset. |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 403 | Handle<Code> code(SharedFunctionInfo::cast( |
| 404 | script->eval_from_shared())->code()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 405 | return Smi::FromInt(code->SourcePosition(code->instruction_start() + |
| 406 | script->eval_from_instructions_offset()->value())); |
| 407 | } |
| 408 | |
| 409 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 410 | const AccessorDescriptor Accessors::ScriptEvalFromScriptPosition = { |
| 411 | ScriptGetEvalFromScriptPosition, |
| 412 | IllegalSetter, |
| 413 | 0 |
| 414 | }; |
| 415 | |
| 416 | |
| 417 | // |
| 418 | // Accessors::ScriptGetEvalFromFunctionName |
| 419 | // |
| 420 | |
| 421 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 422 | MaybeObject* Accessors::ScriptGetEvalFromFunctionName(Object* object, void*) { |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 423 | Object* script = JSValue::cast(object)->value(); |
| 424 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast( |
| 425 | Script::cast(script)->eval_from_shared())); |
| 426 | |
| 427 | |
| 428 | // Find the name of the function calling eval. |
| 429 | if (!shared->name()->IsUndefined()) { |
| 430 | return shared->name(); |
| 431 | } else { |
| 432 | return shared->inferred_name(); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | |
| 437 | const AccessorDescriptor Accessors::ScriptEvalFromFunctionName = { |
| 438 | ScriptGetEvalFromFunctionName, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 439 | IllegalSetter, |
| 440 | 0 |
| 441 | }; |
| 442 | |
| 443 | |
| 444 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 445 | // Accessors::FunctionPrototype |
| 446 | // |
| 447 | |
| 448 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 449 | MaybeObject* Accessors::FunctionGetPrototype(Object* object, void*) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 450 | Heap* heap = Isolate::Current()->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 451 | bool found_it = false; |
| 452 | JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 453 | if (!found_it) return heap->undefined_value(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 454 | while (!function->should_have_prototype()) { |
| 455 | found_it = false; |
| 456 | function = FindInPrototypeChain<JSFunction>(object->GetPrototype(), |
| 457 | &found_it); |
| 458 | // There has to be one because we hit the getter. |
| 459 | ASSERT(found_it); |
| 460 | } |
| 461 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 462 | if (!function->has_prototype()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 463 | Object* prototype; |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 464 | { MaybeObject* maybe_prototype = heap->AllocateFunctionPrototype(function); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 465 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 466 | } |
| 467 | Object* result; |
| 468 | { MaybeObject* maybe_result = function->SetPrototype(prototype); |
| 469 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 470 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 471 | } |
| 472 | return function->prototype(); |
| 473 | } |
| 474 | |
| 475 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 476 | MaybeObject* Accessors::FunctionSetPrototype(JSObject* object, |
| 477 | Object* value, |
| 478 | void*) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 479 | Heap* heap = object->GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 480 | bool found_it = false; |
| 481 | JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 482 | if (!found_it) return heap->undefined_value(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 483 | if (!function->should_have_prototype()) { |
| 484 | // Since we hit this accessor, object will have no prototype property. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 485 | return object->SetLocalPropertyIgnoreAttributes(heap->prototype_symbol(), |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 486 | value, |
| 487 | NONE); |
| 488 | } |
| 489 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 490 | Object* prototype; |
| 491 | { MaybeObject* maybe_prototype = function->SetPrototype(value); |
| 492 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 493 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 494 | ASSERT(function->prototype() == value); |
| 495 | return function; |
| 496 | } |
| 497 | |
| 498 | |
| 499 | const AccessorDescriptor Accessors::FunctionPrototype = { |
| 500 | FunctionGetPrototype, |
| 501 | FunctionSetPrototype, |
| 502 | 0 |
| 503 | }; |
| 504 | |
| 505 | |
| 506 | // |
| 507 | // Accessors::FunctionLength |
| 508 | // |
| 509 | |
| 510 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 511 | MaybeObject* Accessors::FunctionGetLength(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 512 | bool found_it = false; |
| 513 | JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it); |
| 514 | if (!found_it) return Smi::FromInt(0); |
| 515 | // Check if already compiled. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 516 | if (!function->shared()->is_compiled()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 517 | // If the function isn't compiled yet, the length is not computed |
| 518 | // correctly yet. Compile it now and return the right length. |
| 519 | HandleScope scope; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 520 | Handle<JSFunction> handle(function); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 521 | if (!JSFunction::CompileLazy(handle, KEEP_EXCEPTION)) { |
| 522 | return Failure::Exception(); |
| 523 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 524 | return Smi::FromInt(handle->shared()->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 525 | } else { |
| 526 | return Smi::FromInt(function->shared()->length()); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | |
| 531 | const AccessorDescriptor Accessors::FunctionLength = { |
| 532 | FunctionGetLength, |
| 533 | ReadOnlySetAccessor, |
| 534 | 0 |
| 535 | }; |
| 536 | |
| 537 | |
| 538 | // |
| 539 | // Accessors::FunctionName |
| 540 | // |
| 541 | |
| 542 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 543 | MaybeObject* Accessors::FunctionGetName(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 544 | bool found_it = false; |
| 545 | JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 546 | if (!found_it) return HEAP->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 547 | return holder->shared()->name(); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | const AccessorDescriptor Accessors::FunctionName = { |
| 552 | FunctionGetName, |
| 553 | ReadOnlySetAccessor, |
| 554 | 0 |
| 555 | }; |
| 556 | |
| 557 | |
| 558 | // |
| 559 | // Accessors::FunctionArguments |
| 560 | // |
| 561 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 562 | |
| 563 | static MaybeObject* ConstructArgumentsObjectForInlinedFunction( |
| 564 | JavaScriptFrame* frame, |
| 565 | Handle<JSFunction> inlined_function, |
| 566 | int inlined_frame_index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 567 | Factory* factory = Isolate::Current()->factory(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 568 | Vector<SlotRef> args_slots = |
| 569 | SlotRef::ComputeSlotMappingForArguments( |
| 570 | frame, |
| 571 | inlined_frame_index, |
| 572 | inlined_function->shared()->formal_parameter_count()); |
| 573 | int args_count = args_slots.length(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 574 | Handle<JSObject> arguments = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 575 | factory->NewArgumentsObject(inlined_function, args_count); |
| 576 | Handle<FixedArray> array = factory->NewFixedArray(args_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 577 | for (int i = 0; i < args_count; ++i) { |
| 578 | Handle<Object> value = args_slots[i].GetValue(); |
| 579 | array->set(i, *value); |
| 580 | } |
| 581 | arguments->set_elements(*array); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 582 | args_slots.Dispose(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 583 | |
| 584 | // Return the freshly allocated arguments object. |
| 585 | return *arguments; |
| 586 | } |
| 587 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 588 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 589 | MaybeObject* Accessors::FunctionGetArguments(Object* object, void*) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 590 | Isolate* isolate = Isolate::Current(); |
| 591 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 592 | bool found_it = false; |
| 593 | JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 594 | if (!found_it) return isolate->heap()->undefined_value(); |
| 595 | Handle<JSFunction> function(holder, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 596 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 597 | if (function->shared()->native()) return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 598 | // Find the top invocation of the function by traversing frames. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 599 | List<JSFunction*> functions(2); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 600 | for (JavaScriptFrameIterator it(isolate); !it.done(); it.Advance()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 601 | JavaScriptFrame* frame = it.frame(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 602 | frame->GetFunctions(&functions); |
| 603 | for (int i = functions.length() - 1; i >= 0; i--) { |
| 604 | // Skip all frames that aren't invocations of the given function. |
| 605 | if (functions[i] != *function) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 606 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 607 | if (i > 0) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 608 | // The function in question was inlined. Inlined functions have the |
| 609 | // correct number of arguments and no allocated arguments object, so |
| 610 | // we can construct a fresh one by interpreting the function's |
| 611 | // deoptimization input data. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 612 | return ConstructArgumentsObjectForInlinedFunction(frame, function, i); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | if (!frame->is_optimized()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 616 | // If there is an arguments variable in the stack, we return that. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 617 | Handle<ScopeInfo> scope_info(function->shared()->scope_info()); |
| 618 | int index = scope_info->StackSlotIndex( |
| 619 | isolate->heap()->arguments_symbol()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 620 | if (index >= 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 621 | Handle<Object> arguments(frame->GetExpression(index), isolate); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 622 | if (!arguments->IsArgumentsMarker()) return *arguments; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 623 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 624 | } |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 625 | |
| 626 | // If there is no arguments variable in the stack or we have an |
| 627 | // optimized frame, we find the frame that holds the actual arguments |
| 628 | // passed to the function. |
| 629 | it.AdvanceToArgumentsFrame(); |
| 630 | frame = it.frame(); |
| 631 | |
| 632 | // Get the number of arguments and construct an arguments object |
| 633 | // mirror for the right frame. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 634 | const int length = frame->ComputeParametersCount(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 635 | Handle<JSObject> arguments = isolate->factory()->NewArgumentsObject( |
| 636 | function, length); |
| 637 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 638 | |
| 639 | // Copy the parameters to the arguments object. |
| 640 | ASSERT(array->length() == length); |
| 641 | for (int i = 0; i < length; i++) array->set(i, frame->GetParameter(i)); |
| 642 | arguments->set_elements(*array); |
| 643 | |
| 644 | // Return the freshly allocated arguments object. |
| 645 | return *arguments; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 646 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 647 | functions.Rewind(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | // No frame corresponding to the given function found. Return null. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 651 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
| 655 | const AccessorDescriptor Accessors::FunctionArguments = { |
| 656 | FunctionGetArguments, |
| 657 | ReadOnlySetAccessor, |
| 658 | 0 |
| 659 | }; |
| 660 | |
| 661 | |
| 662 | // |
| 663 | // Accessors::FunctionCaller |
| 664 | // |
| 665 | |
| 666 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 667 | static MaybeObject* CheckNonStrictCallerOrThrow( |
| 668 | Isolate* isolate, |
| 669 | JSFunction* caller) { |
| 670 | DisableAssertNoAllocation enable_allocation; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 671 | if (!caller->shared()->is_classic_mode()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 672 | return isolate->Throw( |
| 673 | *isolate->factory()->NewTypeError("strict_caller", |
| 674 | HandleVector<Object>(NULL, 0))); |
| 675 | } |
| 676 | return caller; |
| 677 | } |
| 678 | |
| 679 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 680 | class FrameFunctionIterator { |
| 681 | public: |
| 682 | FrameFunctionIterator(Isolate* isolate, const AssertNoAllocation& promise) |
| 683 | : frame_iterator_(isolate), |
| 684 | functions_(2), |
| 685 | index_(0) { |
| 686 | GetFunctions(); |
| 687 | } |
| 688 | JSFunction* next() { |
| 689 | if (functions_.length() == 0) return NULL; |
| 690 | JSFunction* next_function = functions_[index_]; |
| 691 | index_--; |
| 692 | if (index_ < 0) { |
| 693 | GetFunctions(); |
| 694 | } |
| 695 | return next_function; |
| 696 | } |
| 697 | |
| 698 | // Iterate through functions until the first occurence of 'function'. |
| 699 | // Returns true if 'function' is found, and false if the iterator ends |
| 700 | // without finding it. |
| 701 | bool Find(JSFunction* function) { |
| 702 | JSFunction* next_function; |
| 703 | do { |
| 704 | next_function = next(); |
| 705 | if (next_function == function) return true; |
| 706 | } while (next_function != NULL); |
| 707 | return false; |
| 708 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 709 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 710 | private: |
| 711 | void GetFunctions() { |
| 712 | functions_.Rewind(0); |
| 713 | if (frame_iterator_.done()) return; |
| 714 | JavaScriptFrame* frame = frame_iterator_.frame(); |
| 715 | frame->GetFunctions(&functions_); |
| 716 | ASSERT(functions_.length() > 0); |
| 717 | frame_iterator_.Advance(); |
| 718 | index_ = functions_.length() - 1; |
| 719 | } |
| 720 | JavaScriptFrameIterator frame_iterator_; |
| 721 | List<JSFunction*> functions_; |
| 722 | int index_; |
| 723 | }; |
| 724 | |
| 725 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 726 | MaybeObject* Accessors::FunctionGetCaller(Object* object, void*) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 727 | Isolate* isolate = Isolate::Current(); |
| 728 | HandleScope scope(isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 729 | AssertNoAllocation no_alloc; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 730 | bool found_it = false; |
| 731 | JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 732 | if (!found_it) return isolate->heap()->undefined_value(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 733 | if (holder->shared()->native()) return isolate->heap()->null_value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 734 | Handle<JSFunction> function(holder, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 735 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 736 | FrameFunctionIterator it(isolate, no_alloc); |
| 737 | |
| 738 | // Find the function from the frames. |
| 739 | if (!it.Find(*function)) { |
| 740 | // No frame corresponding to the given function found. Return null. |
| 741 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 742 | } |
| 743 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 744 | // Find previously called non-toplevel function. |
| 745 | JSFunction* caller; |
| 746 | do { |
| 747 | caller = it.next(); |
| 748 | if (caller == NULL) return isolate->heap()->null_value(); |
| 749 | } while (caller->shared()->is_toplevel()); |
| 750 | |
| 751 | // If caller is a built-in function and caller's caller is also built-in, |
| 752 | // use that instead. |
| 753 | JSFunction* potential_caller = caller; |
| 754 | while (potential_caller != NULL && potential_caller->IsBuiltin()) { |
| 755 | caller = potential_caller; |
| 756 | potential_caller = it.next(); |
| 757 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 758 | // If caller is bound, return null. This is compatible with JSC, and |
| 759 | // allows us to make bound functions use the strict function map |
| 760 | // and its associated throwing caller and arguments. |
| 761 | if (caller->shared()->bound()) { |
| 762 | return isolate->heap()->null_value(); |
| 763 | } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 764 | return CheckNonStrictCallerOrThrow(isolate, caller); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | |
| 768 | const AccessorDescriptor Accessors::FunctionCaller = { |
| 769 | FunctionGetCaller, |
| 770 | ReadOnlySetAccessor, |
| 771 | 0 |
| 772 | }; |
| 773 | |
| 774 | |
| 775 | // |
| 776 | // Accessors::ObjectPrototype |
| 777 | // |
| 778 | |
| 779 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 780 | MaybeObject* Accessors::ObjectGetPrototype(Object* receiver, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 781 | Object* current = receiver->GetPrototype(); |
| 782 | while (current->IsJSObject() && |
| 783 | JSObject::cast(current)->map()->is_hidden_prototype()) { |
| 784 | current = current->GetPrototype(); |
| 785 | } |
| 786 | return current; |
| 787 | } |
| 788 | |
| 789 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 790 | MaybeObject* Accessors::ObjectSetPrototype(JSObject* receiver, |
| 791 | Object* value, |
| 792 | void*) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 793 | const bool skip_hidden_prototypes = true; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 794 | // To be consistent with other Set functions, return the value. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 795 | return receiver->SetPrototype(value, skip_hidden_prototypes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | |
| 799 | const AccessorDescriptor Accessors::ObjectPrototype = { |
| 800 | ObjectGetPrototype, |
| 801 | ObjectSetPrototype, |
| 802 | 0 |
| 803 | }; |
| 804 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 805 | |
| 806 | // |
| 807 | // Accessors::MakeModuleExport |
| 808 | // |
| 809 | |
| 810 | static v8::Handle<v8::Value> ModuleGetExport( |
| 811 | v8::Local<v8::String> property, |
| 812 | const v8::AccessorInfo& info) { |
| 813 | JSModule* instance = JSModule::cast(*v8::Utils::OpenHandle(*info.Holder())); |
| 814 | Context* context = Context::cast(instance->context()); |
| 815 | ASSERT(context->IsModuleContext()); |
| 816 | int slot = info.Data()->Int32Value(); |
| 817 | Object* value = context->get(slot); |
| 818 | if (value->IsTheHole()) { |
| 819 | Handle<String> name = v8::Utils::OpenHandle(*property); |
| 820 | Isolate* isolate = instance->GetIsolate(); |
| 821 | isolate->ScheduleThrow( |
| 822 | *isolate->factory()->NewReferenceError("not_defined", |
| 823 | HandleVector(&name, 1))); |
| 824 | return v8::Handle<v8::Value>(); |
| 825 | } |
| 826 | return v8::Utils::ToLocal(Handle<Object>(value)); |
| 827 | } |
| 828 | |
| 829 | |
| 830 | static void ModuleSetExport( |
| 831 | v8::Local<v8::String> property, |
| 832 | v8::Local<v8::Value> value, |
| 833 | const v8::AccessorInfo& info) { |
| 834 | JSModule* instance = JSModule::cast(*v8::Utils::OpenHandle(*info.Holder())); |
| 835 | Context* context = Context::cast(instance->context()); |
| 836 | ASSERT(context->IsModuleContext()); |
| 837 | int slot = info.Data()->Int32Value(); |
| 838 | Object* old_value = context->get(slot); |
| 839 | if (old_value->IsTheHole()) { |
| 840 | Handle<String> name = v8::Utils::OpenHandle(*property); |
| 841 | Isolate* isolate = instance->GetIsolate(); |
| 842 | isolate->ScheduleThrow( |
| 843 | *isolate->factory()->NewReferenceError("not_defined", |
| 844 | HandleVector(&name, 1))); |
| 845 | return; |
| 846 | } |
| 847 | context->set(slot, *v8::Utils::OpenHandle(*value)); |
| 848 | } |
| 849 | |
| 850 | |
| 851 | Handle<AccessorInfo> Accessors::MakeModuleExport( |
| 852 | Handle<String> name, |
| 853 | int index, |
| 854 | PropertyAttributes attributes) { |
| 855 | Factory* factory = name->GetIsolate()->factory(); |
| 856 | Handle<AccessorInfo> info = factory->NewAccessorInfo(); |
| 857 | info->set_property_attributes(attributes); |
| 858 | info->set_all_can_read(true); |
| 859 | info->set_all_can_write(true); |
| 860 | info->set_name(*name); |
| 861 | info->set_data(Smi::FromInt(index)); |
| 862 | Handle<Object> getter = v8::FromCData(&ModuleGetExport); |
| 863 | Handle<Object> setter = v8::FromCData(&ModuleSetExport); |
| 864 | info->set_getter(*getter); |
| 865 | if (!(attributes & ReadOnly)) info->set_setter(*setter); |
| 866 | return info; |
| 867 | } |
| 868 | |
| 869 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 870 | } } // namespace v8::internal |