Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 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" |
| 29 | |
| 30 | #include "accessors.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 31 | #include "ast.h" |
| 32 | #include "deoptimizer.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | #include "execution.h" |
| 34 | #include "factory.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 35 | #include "safepoint-table.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | #include "scopeinfo.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | |
| 38 | namespace v8 { |
| 39 | namespace internal { |
| 40 | |
| 41 | |
| 42 | template <class C> |
| 43 | static C* FindInPrototypeChain(Object* obj, bool* found_it) { |
| 44 | ASSERT(!*found_it); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 45 | Heap* heap = HEAP; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | while (!Is<C>(obj)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 47 | if (obj == heap->null_value()) return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | obj = obj->GetPrototype(); |
| 49 | } |
| 50 | *found_it = true; |
| 51 | return C::cast(obj); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | // Entry point that never should be called. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 56 | MaybeObject* Accessors::IllegalSetter(JSObject*, Object*, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | UNREACHABLE(); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | Object* Accessors::IllegalGetAccessor(Object* object, void*) { |
| 63 | UNREACHABLE(); |
| 64 | return object; |
| 65 | } |
| 66 | |
| 67 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 68 | MaybeObject* Accessors::ReadOnlySetAccessor(JSObject*, Object* value, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | // According to ECMA-262, section 8.6.2.2, page 28, setting |
| 70 | // read-only properties must be silently ignored. |
| 71 | return value; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | // |
| 76 | // Accessors::ArrayLength |
| 77 | // |
| 78 | |
| 79 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 80 | MaybeObject* Accessors::ArrayGetLength(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | // Traverse the prototype chain until we reach an array. |
| 82 | bool found_it = false; |
| 83 | JSArray* holder = FindInPrototypeChain<JSArray>(object, &found_it); |
| 84 | if (!found_it) return Smi::FromInt(0); |
| 85 | return holder->length(); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | // The helper function will 'flatten' Number objects. |
| 90 | Object* Accessors::FlattenNumber(Object* value) { |
| 91 | if (value->IsNumber() || !value->IsJSValue()) return value; |
| 92 | JSValue* wrapper = JSValue::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 93 | ASSERT(Isolate::Current()->context()->global_context()->number_function()-> |
| 94 | has_initial_map()); |
| 95 | Map* number_map = Isolate::Current()->context()->global_context()-> |
| 96 | number_function()->initial_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | if (wrapper->map() == number_map) return wrapper->value(); |
| 98 | return value; |
| 99 | } |
| 100 | |
| 101 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 102 | MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 103 | Isolate* isolate = object->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 104 | value = FlattenNumber(value); |
| 105 | |
| 106 | // Need to call methods that may trigger GC. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 107 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | |
| 109 | // Protect raw pointers. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 110 | Handle<JSObject> object_handle(object, isolate); |
| 111 | Handle<Object> value_handle(value, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | |
| 113 | bool has_exception; |
| 114 | Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception); |
| 115 | if (has_exception) return Failure::Exception(); |
| 116 | Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception); |
| 117 | if (has_exception) return Failure::Exception(); |
| 118 | |
| 119 | // Restore raw pointers, |
| 120 | object = *object_handle; |
| 121 | value = *value_handle; |
| 122 | |
| 123 | if (uint32_v->Number() == number_v->Number()) { |
| 124 | if (object->IsJSArray()) { |
| 125 | return JSArray::cast(object)->SetElementsLength(*uint32_v); |
| 126 | } else { |
| 127 | // This means one of the object's prototypes is a JSArray and |
| 128 | // the object does not have a 'length' property. |
| 129 | // Calling SetProperty causes an infinite loop. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 130 | return object->SetLocalPropertyIgnoreAttributes( |
| 131 | isolate->heap()->length_symbol(), value, NONE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 134 | return isolate->Throw( |
| 135 | *isolate->factory()->NewRangeError("invalid_array_length", |
| 136 | HandleVector<Object>(NULL, 0))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | const AccessorDescriptor Accessors::ArrayLength = { |
| 141 | ArrayGetLength, |
| 142 | ArraySetLength, |
| 143 | 0 |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | // |
| 148 | // Accessors::StringLength |
| 149 | // |
| 150 | |
| 151 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 152 | MaybeObject* Accessors::StringGetLength(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | Object* value = object; |
| 154 | if (object->IsJSValue()) value = JSValue::cast(object)->value(); |
| 155 | if (value->IsString()) return Smi::FromInt(String::cast(value)->length()); |
| 156 | // If object is not a string we return 0 to be compatible with WebKit. |
| 157 | // Note: Firefox returns the length of ToString(object). |
| 158 | return Smi::FromInt(0); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | const AccessorDescriptor Accessors::StringLength = { |
| 163 | StringGetLength, |
| 164 | IllegalSetter, |
| 165 | 0 |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | // |
| 170 | // Accessors::ScriptSource |
| 171 | // |
| 172 | |
| 173 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 174 | MaybeObject* Accessors::ScriptGetSource(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | Object* script = JSValue::cast(object)->value(); |
| 176 | return Script::cast(script)->source(); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | const AccessorDescriptor Accessors::ScriptSource = { |
| 181 | ScriptGetSource, |
| 182 | IllegalSetter, |
| 183 | 0 |
| 184 | }; |
| 185 | |
| 186 | |
| 187 | // |
| 188 | // Accessors::ScriptName |
| 189 | // |
| 190 | |
| 191 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 192 | MaybeObject* Accessors::ScriptGetName(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | Object* script = JSValue::cast(object)->value(); |
| 194 | return Script::cast(script)->name(); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | const AccessorDescriptor Accessors::ScriptName = { |
| 199 | ScriptGetName, |
| 200 | IllegalSetter, |
| 201 | 0 |
| 202 | }; |
| 203 | |
| 204 | |
| 205 | // |
| 206 | // Accessors::ScriptId |
| 207 | // |
| 208 | |
| 209 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 210 | MaybeObject* Accessors::ScriptGetId(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | Object* script = JSValue::cast(object)->value(); |
| 212 | return Script::cast(script)->id(); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | const AccessorDescriptor Accessors::ScriptId = { |
| 217 | ScriptGetId, |
| 218 | IllegalSetter, |
| 219 | 0 |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | // |
| 224 | // Accessors::ScriptLineOffset |
| 225 | // |
| 226 | |
| 227 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 228 | MaybeObject* Accessors::ScriptGetLineOffset(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | Object* script = JSValue::cast(object)->value(); |
| 230 | return Script::cast(script)->line_offset(); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | const AccessorDescriptor Accessors::ScriptLineOffset = { |
| 235 | ScriptGetLineOffset, |
| 236 | IllegalSetter, |
| 237 | 0 |
| 238 | }; |
| 239 | |
| 240 | |
| 241 | // |
| 242 | // Accessors::ScriptColumnOffset |
| 243 | // |
| 244 | |
| 245 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 246 | MaybeObject* Accessors::ScriptGetColumnOffset(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 247 | Object* script = JSValue::cast(object)->value(); |
| 248 | return Script::cast(script)->column_offset(); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | const AccessorDescriptor Accessors::ScriptColumnOffset = { |
| 253 | ScriptGetColumnOffset, |
| 254 | IllegalSetter, |
| 255 | 0 |
| 256 | }; |
| 257 | |
| 258 | |
| 259 | // |
| 260 | // Accessors::ScriptData |
| 261 | // |
| 262 | |
| 263 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 264 | MaybeObject* Accessors::ScriptGetData(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | Object* script = JSValue::cast(object)->value(); |
| 266 | return Script::cast(script)->data(); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | const AccessorDescriptor Accessors::ScriptData = { |
| 271 | ScriptGetData, |
| 272 | IllegalSetter, |
| 273 | 0 |
| 274 | }; |
| 275 | |
| 276 | |
| 277 | // |
| 278 | // Accessors::ScriptType |
| 279 | // |
| 280 | |
| 281 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 282 | MaybeObject* Accessors::ScriptGetType(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 283 | Object* script = JSValue::cast(object)->value(); |
| 284 | return Script::cast(script)->type(); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | const AccessorDescriptor Accessors::ScriptType = { |
| 289 | ScriptGetType, |
| 290 | IllegalSetter, |
| 291 | 0 |
| 292 | }; |
| 293 | |
| 294 | |
| 295 | // |
| 296 | // Accessors::ScriptCompilationType |
| 297 | // |
| 298 | |
| 299 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 300 | MaybeObject* Accessors::ScriptGetCompilationType(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 301 | Object* script = JSValue::cast(object)->value(); |
| 302 | return Script::cast(script)->compilation_type(); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | const AccessorDescriptor Accessors::ScriptCompilationType = { |
| 307 | ScriptGetCompilationType, |
| 308 | IllegalSetter, |
| 309 | 0 |
| 310 | }; |
| 311 | |
| 312 | |
| 313 | // |
| 314 | // Accessors::ScriptGetLineEnds |
| 315 | // |
| 316 | |
| 317 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 318 | MaybeObject* Accessors::ScriptGetLineEnds(Object* object, void*) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 319 | JSValue* wrapper = JSValue::cast(object); |
| 320 | Isolate* isolate = wrapper->GetIsolate(); |
| 321 | HandleScope scope(isolate); |
| 322 | Handle<Script> script(Script::cast(wrapper->value()), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | InitScriptLineEnds(script); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 324 | ASSERT(script->line_ends()->IsFixedArray()); |
| 325 | Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends())); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 326 | // We do not want anyone to modify this array from JS. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 327 | ASSERT(*line_ends == isolate->heap()->empty_fixed_array() || |
| 328 | line_ends->map() == isolate->heap()->fixed_cow_array_map()); |
| 329 | Handle<JSArray> js_array = |
| 330 | isolate->factory()->NewJSArrayWithElements(line_ends); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 331 | return *js_array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
| 335 | const AccessorDescriptor Accessors::ScriptLineEnds = { |
| 336 | ScriptGetLineEnds, |
| 337 | IllegalSetter, |
| 338 | 0 |
| 339 | }; |
| 340 | |
| 341 | |
| 342 | // |
| 343 | // Accessors::ScriptGetContextData |
| 344 | // |
| 345 | |
| 346 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 347 | MaybeObject* Accessors::ScriptGetContextData(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 348 | Object* script = JSValue::cast(object)->value(); |
| 349 | return Script::cast(script)->context_data(); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | const AccessorDescriptor Accessors::ScriptContextData = { |
| 354 | ScriptGetContextData, |
| 355 | IllegalSetter, |
| 356 | 0 |
| 357 | }; |
| 358 | |
| 359 | |
| 360 | // |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 361 | // Accessors::ScriptGetEvalFromScript |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 362 | // |
| 363 | |
| 364 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 365 | MaybeObject* Accessors::ScriptGetEvalFromScript(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | Object* script = JSValue::cast(object)->value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 367 | if (!Script::cast(script)->eval_from_shared()->IsUndefined()) { |
| 368 | Handle<SharedFunctionInfo> eval_from_shared( |
| 369 | SharedFunctionInfo::cast(Script::cast(script)->eval_from_shared())); |
| 370 | |
| 371 | if (eval_from_shared->script()->IsScript()) { |
| 372 | Handle<Script> eval_from_script(Script::cast(eval_from_shared->script())); |
| 373 | return *GetScriptWrapper(eval_from_script); |
| 374 | } |
| 375 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 376 | return HEAP->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 380 | const AccessorDescriptor Accessors::ScriptEvalFromScript = { |
| 381 | ScriptGetEvalFromScript, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 382 | IllegalSetter, |
| 383 | 0 |
| 384 | }; |
| 385 | |
| 386 | |
| 387 | // |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 388 | // Accessors::ScriptGetEvalFromScriptPosition |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | // |
| 390 | |
| 391 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 392 | MaybeObject* Accessors::ScriptGetEvalFromScriptPosition(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 393 | HandleScope scope; |
| 394 | Handle<Script> script(Script::cast(JSValue::cast(object)->value())); |
| 395 | |
| 396 | // If this is not a script compiled through eval there is no eval position. |
| 397 | int compilation_type = Smi::cast(script->compilation_type())->value(); |
| 398 | if (compilation_type != Script::COMPILATION_TYPE_EVAL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 399 | return HEAP->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // Get the function from where eval was called and find the source position |
| 403 | // from the instruction offset. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 404 | Handle<Code> code(SharedFunctionInfo::cast( |
| 405 | script->eval_from_shared())->code()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | return Smi::FromInt(code->SourcePosition(code->instruction_start() + |
| 407 | script->eval_from_instructions_offset()->value())); |
| 408 | } |
| 409 | |
| 410 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 411 | const AccessorDescriptor Accessors::ScriptEvalFromScriptPosition = { |
| 412 | ScriptGetEvalFromScriptPosition, |
| 413 | IllegalSetter, |
| 414 | 0 |
| 415 | }; |
| 416 | |
| 417 | |
| 418 | // |
| 419 | // Accessors::ScriptGetEvalFromFunctionName |
| 420 | // |
| 421 | |
| 422 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 423 | MaybeObject* Accessors::ScriptGetEvalFromFunctionName(Object* object, void*) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 424 | Object* script = JSValue::cast(object)->value(); |
| 425 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast( |
| 426 | Script::cast(script)->eval_from_shared())); |
| 427 | |
| 428 | |
| 429 | // Find the name of the function calling eval. |
| 430 | if (!shared->name()->IsUndefined()) { |
| 431 | return shared->name(); |
| 432 | } else { |
| 433 | return shared->inferred_name(); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | |
| 438 | const AccessorDescriptor Accessors::ScriptEvalFromFunctionName = { |
| 439 | ScriptGetEvalFromFunctionName, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 440 | IllegalSetter, |
| 441 | 0 |
| 442 | }; |
| 443 | |
| 444 | |
| 445 | // |
| 446 | // Accessors::FunctionPrototype |
| 447 | // |
| 448 | |
| 449 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 450 | MaybeObject* Accessors::FunctionGetPrototype(Object* object, void*) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 451 | Heap* heap = Isolate::Current()->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 452 | bool found_it = false; |
| 453 | JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 454 | if (!found_it) return heap->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 455 | while (!function->should_have_prototype()) { |
| 456 | found_it = false; |
| 457 | function = FindInPrototypeChain<JSFunction>(object->GetPrototype(), |
| 458 | &found_it); |
| 459 | // There has to be one because we hit the getter. |
| 460 | ASSERT(found_it); |
| 461 | } |
| 462 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | if (!function->has_prototype()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 464 | Object* prototype; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 465 | { MaybeObject* maybe_prototype = heap->AllocateFunctionPrototype(function); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 466 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 467 | } |
| 468 | Object* result; |
| 469 | { MaybeObject* maybe_result = function->SetPrototype(prototype); |
| 470 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 471 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 472 | } |
| 473 | return function->prototype(); |
| 474 | } |
| 475 | |
| 476 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 477 | MaybeObject* Accessors::FunctionSetPrototype(JSObject* object, |
| 478 | Object* value, |
| 479 | void*) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 480 | Heap* heap = object->GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 481 | bool found_it = false; |
| 482 | JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 483 | if (!found_it) return heap->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 484 | if (!function->should_have_prototype()) { |
| 485 | // Since we hit this accessor, object will have no prototype property. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 486 | return object->SetLocalPropertyIgnoreAttributes(heap->prototype_symbol(), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 487 | value, |
| 488 | NONE); |
| 489 | } |
| 490 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 491 | if (function->has_initial_map()) { |
| 492 | // If the function has allocated the initial map |
| 493 | // replace it with a copy containing the new prototype. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 494 | Object* new_map; |
| 495 | { MaybeObject* maybe_new_map = |
| 496 | function->initial_map()->CopyDropTransitions(); |
| 497 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 498 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 499 | function->set_initial_map(Map::cast(new_map)); |
| 500 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 501 | Object* prototype; |
| 502 | { MaybeObject* maybe_prototype = function->SetPrototype(value); |
| 503 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 504 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 505 | ASSERT(function->prototype() == value); |
| 506 | return function; |
| 507 | } |
| 508 | |
| 509 | |
| 510 | const AccessorDescriptor Accessors::FunctionPrototype = { |
| 511 | FunctionGetPrototype, |
| 512 | FunctionSetPrototype, |
| 513 | 0 |
| 514 | }; |
| 515 | |
| 516 | |
| 517 | // |
| 518 | // Accessors::FunctionLength |
| 519 | // |
| 520 | |
| 521 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 522 | MaybeObject* Accessors::FunctionGetLength(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 523 | bool found_it = false; |
| 524 | JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it); |
| 525 | if (!found_it) return Smi::FromInt(0); |
| 526 | // Check if already compiled. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 527 | if (!function->shared()->is_compiled()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 528 | // If the function isn't compiled yet, the length is not computed |
| 529 | // correctly yet. Compile it now and return the right length. |
| 530 | HandleScope scope; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 531 | Handle<JSFunction> handle(function); |
| 532 | if (!CompileLazy(handle, KEEP_EXCEPTION)) return Failure::Exception(); |
| 533 | return Smi::FromInt(handle->shared()->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 534 | } else { |
| 535 | return Smi::FromInt(function->shared()->length()); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | |
| 540 | const AccessorDescriptor Accessors::FunctionLength = { |
| 541 | FunctionGetLength, |
| 542 | ReadOnlySetAccessor, |
| 543 | 0 |
| 544 | }; |
| 545 | |
| 546 | |
| 547 | // |
| 548 | // Accessors::FunctionName |
| 549 | // |
| 550 | |
| 551 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 552 | MaybeObject* Accessors::FunctionGetName(Object* object, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 553 | bool found_it = false; |
| 554 | JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 555 | if (!found_it) return HEAP->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 556 | return holder->shared()->name(); |
| 557 | } |
| 558 | |
| 559 | |
| 560 | const AccessorDescriptor Accessors::FunctionName = { |
| 561 | FunctionGetName, |
| 562 | ReadOnlySetAccessor, |
| 563 | 0 |
| 564 | }; |
| 565 | |
| 566 | |
| 567 | // |
| 568 | // Accessors::FunctionArguments |
| 569 | // |
| 570 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 571 | static Address SlotAddress(JavaScriptFrame* frame, int slot_index) { |
| 572 | if (slot_index >= 0) { |
| 573 | const int offset = JavaScriptFrameConstants::kLocal0Offset; |
| 574 | return frame->fp() + offset - (slot_index * kPointerSize); |
| 575 | } else { |
| 576 | const int offset = JavaScriptFrameConstants::kReceiverOffset; |
| 577 | return frame->caller_sp() + offset + (slot_index * kPointerSize); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | |
| 582 | // We can't intermix stack decoding and allocations because |
| 583 | // deoptimization infrastracture is not GC safe. |
| 584 | // Thus we build a temporary structure in malloced space. |
| 585 | class SlotRef BASE_EMBEDDED { |
| 586 | public: |
| 587 | enum SlotRepresentation { |
| 588 | UNKNOWN, |
| 589 | TAGGED, |
| 590 | INT32, |
| 591 | DOUBLE, |
| 592 | LITERAL |
| 593 | }; |
| 594 | |
| 595 | SlotRef() |
| 596 | : addr_(NULL), representation_(UNKNOWN) { } |
| 597 | |
| 598 | SlotRef(Address addr, SlotRepresentation representation) |
| 599 | : addr_(addr), representation_(representation) { } |
| 600 | |
| 601 | explicit SlotRef(Object* literal) |
| 602 | : literal_(literal), representation_(LITERAL) { } |
| 603 | |
| 604 | Handle<Object> GetValue() { |
| 605 | switch (representation_) { |
| 606 | case TAGGED: |
| 607 | return Handle<Object>(Memory::Object_at(addr_)); |
| 608 | |
| 609 | case INT32: { |
| 610 | int value = Memory::int32_at(addr_); |
| 611 | if (Smi::IsValid(value)) { |
| 612 | return Handle<Object>(Smi::FromInt(value)); |
| 613 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 614 | return Isolate::Current()->factory()->NewNumberFromInt(value); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
| 618 | case DOUBLE: { |
| 619 | double value = Memory::double_at(addr_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 620 | return Isolate::Current()->factory()->NewNumber(value); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | case LITERAL: |
| 624 | return literal_; |
| 625 | |
| 626 | default: |
| 627 | UNREACHABLE(); |
| 628 | return Handle<Object>::null(); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | private: |
| 633 | Address addr_; |
| 634 | Handle<Object> literal_; |
| 635 | SlotRepresentation representation_; |
| 636 | }; |
| 637 | |
| 638 | |
| 639 | static SlotRef ComputeSlotForNextArgument(TranslationIterator* iterator, |
| 640 | DeoptimizationInputData* data, |
| 641 | JavaScriptFrame* frame) { |
| 642 | Translation::Opcode opcode = |
| 643 | static_cast<Translation::Opcode>(iterator->Next()); |
| 644 | |
| 645 | switch (opcode) { |
| 646 | case Translation::BEGIN: |
| 647 | case Translation::FRAME: |
| 648 | // Peeled off before getting here. |
| 649 | break; |
| 650 | |
| 651 | case Translation::ARGUMENTS_OBJECT: |
| 652 | // This can be only emitted for local slots not for argument slots. |
| 653 | break; |
| 654 | |
| 655 | case Translation::REGISTER: |
| 656 | case Translation::INT32_REGISTER: |
| 657 | case Translation::DOUBLE_REGISTER: |
| 658 | case Translation::DUPLICATE: |
| 659 | // We are at safepoint which corresponds to call. All registers are |
| 660 | // saved by caller so there would be no live registers at this |
| 661 | // point. Thus these translation commands should not be used. |
| 662 | break; |
| 663 | |
| 664 | case Translation::STACK_SLOT: { |
| 665 | int slot_index = iterator->Next(); |
| 666 | Address slot_addr = SlotAddress(frame, slot_index); |
| 667 | return SlotRef(slot_addr, SlotRef::TAGGED); |
| 668 | } |
| 669 | |
| 670 | case Translation::INT32_STACK_SLOT: { |
| 671 | int slot_index = iterator->Next(); |
| 672 | Address slot_addr = SlotAddress(frame, slot_index); |
| 673 | return SlotRef(slot_addr, SlotRef::INT32); |
| 674 | } |
| 675 | |
| 676 | case Translation::DOUBLE_STACK_SLOT: { |
| 677 | int slot_index = iterator->Next(); |
| 678 | Address slot_addr = SlotAddress(frame, slot_index); |
| 679 | return SlotRef(slot_addr, SlotRef::DOUBLE); |
| 680 | } |
| 681 | |
| 682 | case Translation::LITERAL: { |
| 683 | int literal_index = iterator->Next(); |
| 684 | return SlotRef(data->LiteralArray()->get(literal_index)); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | UNREACHABLE(); |
| 689 | return SlotRef(); |
| 690 | } |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | static void ComputeSlotMappingForArguments(JavaScriptFrame* frame, |
| 697 | int inlined_frame_index, |
| 698 | Vector<SlotRef>* args_slots) { |
| 699 | AssertNoAllocation no_gc; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 700 | int deopt_index = AstNode::kNoNumber; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 701 | DeoptimizationInputData* data = |
| 702 | static_cast<OptimizedFrame*>(frame)->GetDeoptimizationData(&deopt_index); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 703 | TranslationIterator it(data->TranslationByteArray(), |
| 704 | data->TranslationIndex(deopt_index)->value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 705 | Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next()); |
| 706 | ASSERT(opcode == Translation::BEGIN); |
| 707 | int frame_count = it.Next(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 708 | USE(frame_count); |
| 709 | ASSERT(frame_count > inlined_frame_index); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 710 | int frames_to_skip = inlined_frame_index; |
| 711 | while (true) { |
| 712 | opcode = static_cast<Translation::Opcode>(it.Next()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 713 | // Skip over operands to advance to the next opcode. |
| 714 | it.Skip(Translation::NumberOfOperandsFor(opcode)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 715 | if (opcode == Translation::FRAME) { |
| 716 | if (frames_to_skip == 0) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 717 | // We reached the frame corresponding to the inlined function |
| 718 | // in question. Process the translation commands for the |
| 719 | // arguments. |
| 720 | // |
| 721 | // Skip the translation command for the receiver. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 722 | it.Skip(Translation::NumberOfOperandsFor( |
| 723 | static_cast<Translation::Opcode>(it.Next()))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 724 | // Compute slots for arguments. |
| 725 | for (int i = 0; i < args_slots->length(); ++i) { |
| 726 | (*args_slots)[i] = ComputeSlotForNextArgument(&it, data, frame); |
| 727 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 728 | return; |
| 729 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 730 | frames_to_skip--; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | UNREACHABLE(); |
| 735 | } |
| 736 | |
| 737 | |
| 738 | static MaybeObject* ConstructArgumentsObjectForInlinedFunction( |
| 739 | JavaScriptFrame* frame, |
| 740 | Handle<JSFunction> inlined_function, |
| 741 | int inlined_frame_index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 742 | Factory* factory = Isolate::Current()->factory(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 743 | int args_count = inlined_function->shared()->formal_parameter_count(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 744 | ScopedVector<SlotRef> args_slots(args_count); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 745 | ComputeSlotMappingForArguments(frame, inlined_frame_index, &args_slots); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 746 | Handle<JSObject> arguments = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 747 | factory->NewArgumentsObject(inlined_function, args_count); |
| 748 | Handle<FixedArray> array = factory->NewFixedArray(args_count); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 749 | for (int i = 0; i < args_count; ++i) { |
| 750 | Handle<Object> value = args_slots[i].GetValue(); |
| 751 | array->set(i, *value); |
| 752 | } |
| 753 | arguments->set_elements(*array); |
| 754 | |
| 755 | // Return the freshly allocated arguments object. |
| 756 | return *arguments; |
| 757 | } |
| 758 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 759 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 760 | MaybeObject* Accessors::FunctionGetArguments(Object* object, void*) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 761 | Isolate* isolate = Isolate::Current(); |
| 762 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 763 | bool found_it = false; |
| 764 | JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 765 | if (!found_it) return isolate->heap()->undefined_value(); |
| 766 | Handle<JSFunction> function(holder, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 767 | |
| 768 | // Find the top invocation of the function by traversing frames. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 769 | List<JSFunction*> functions(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 770 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 771 | JavaScriptFrame* frame = it.frame(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 772 | frame->GetFunctions(&functions); |
| 773 | for (int i = functions.length() - 1; i >= 0; i--) { |
| 774 | // Skip all frames that aren't invocations of the given function. |
| 775 | if (functions[i] != *function) continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 776 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 777 | if (i > 0) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 778 | // The function in question was inlined. Inlined functions have the |
| 779 | // correct number of arguments and no allocated arguments object, so |
| 780 | // we can construct a fresh one by interpreting the function's |
| 781 | // deoptimization input data. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 782 | return ConstructArgumentsObjectForInlinedFunction(frame, function, i); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | if (!frame->is_optimized()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 786 | // If there is an arguments variable in the stack, we return that. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 787 | Handle<SerializedScopeInfo> info(function->shared()->scope_info()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 788 | int index = info->StackSlotIndex(isolate->heap()->arguments_symbol()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 789 | if (index >= 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 790 | Handle<Object> arguments(frame->GetExpression(index), isolate); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 791 | if (!arguments->IsArgumentsMarker()) return *arguments; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 792 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 793 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 794 | |
| 795 | // If there is no arguments variable in the stack or we have an |
| 796 | // optimized frame, we find the frame that holds the actual arguments |
| 797 | // passed to the function. |
| 798 | it.AdvanceToArgumentsFrame(); |
| 799 | frame = it.frame(); |
| 800 | |
| 801 | // Get the number of arguments and construct an arguments object |
| 802 | // mirror for the right frame. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 803 | const int length = frame->ComputeParametersCount(); |
| 804 | Handle<JSObject> arguments = isolate->factory()->NewArgumentsObject( |
| 805 | function, length); |
| 806 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 807 | |
| 808 | // Copy the parameters to the arguments object. |
| 809 | ASSERT(array->length() == length); |
| 810 | for (int i = 0; i < length; i++) array->set(i, frame->GetParameter(i)); |
| 811 | arguments->set_elements(*array); |
| 812 | |
| 813 | // Return the freshly allocated arguments object. |
| 814 | return *arguments; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 815 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 816 | functions.Rewind(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | // No frame corresponding to the given function found. Return null. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 820 | return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | |
| 824 | const AccessorDescriptor Accessors::FunctionArguments = { |
| 825 | FunctionGetArguments, |
| 826 | ReadOnlySetAccessor, |
| 827 | 0 |
| 828 | }; |
| 829 | |
| 830 | |
| 831 | // |
| 832 | // Accessors::FunctionCaller |
| 833 | // |
| 834 | |
| 835 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 836 | static MaybeObject* CheckNonStrictCallerOrThrow( |
| 837 | Isolate* isolate, |
| 838 | JSFunction* caller) { |
| 839 | DisableAssertNoAllocation enable_allocation; |
| 840 | if (caller->shared()->strict_mode()) { |
| 841 | return isolate->Throw( |
| 842 | *isolate->factory()->NewTypeError("strict_caller", |
| 843 | HandleVector<Object>(NULL, 0))); |
| 844 | } |
| 845 | return caller; |
| 846 | } |
| 847 | |
| 848 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 849 | MaybeObject* Accessors::FunctionGetCaller(Object* object, void*) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 850 | Isolate* isolate = Isolate::Current(); |
| 851 | HandleScope scope(isolate); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 852 | AssertNoAllocation no_alloc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 853 | bool found_it = false; |
| 854 | JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 855 | if (!found_it) return isolate->heap()->undefined_value(); |
| 856 | Handle<JSFunction> function(holder, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 857 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 858 | List<JSFunction*> functions(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 859 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 860 | JavaScriptFrame* frame = it.frame(); |
| 861 | frame->GetFunctions(&functions); |
| 862 | for (int i = functions.length() - 1; i >= 0; i--) { |
| 863 | if (functions[i] == *function) { |
| 864 | // Once we have found the frame, we need to go to the caller |
| 865 | // frame. This may require skipping through a number of top-level |
| 866 | // frames, e.g. frames for scripts not functions. |
| 867 | if (i > 0) { |
| 868 | ASSERT(!functions[i - 1]->shared()->is_toplevel()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 869 | return CheckNonStrictCallerOrThrow(isolate, functions[i - 1]); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 870 | } else { |
| 871 | for (it.Advance(); !it.done(); it.Advance()) { |
| 872 | frame = it.frame(); |
| 873 | functions.Rewind(0); |
| 874 | frame->GetFunctions(&functions); |
| 875 | if (!functions.last()->shared()->is_toplevel()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 876 | return CheckNonStrictCallerOrThrow(isolate, functions.last()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 877 | } |
| 878 | ASSERT(functions.length() == 1); |
| 879 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 880 | if (it.done()) return isolate->heap()->null_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 881 | break; |
| 882 | } |
| 883 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 884 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 885 | functions.Rewind(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | // No frame corresponding to the given function found. Return null. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 889 | return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | |
| 893 | const AccessorDescriptor Accessors::FunctionCaller = { |
| 894 | FunctionGetCaller, |
| 895 | ReadOnlySetAccessor, |
| 896 | 0 |
| 897 | }; |
| 898 | |
| 899 | |
| 900 | // |
| 901 | // Accessors::ObjectPrototype |
| 902 | // |
| 903 | |
| 904 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 905 | MaybeObject* Accessors::ObjectGetPrototype(Object* receiver, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 906 | Object* current = receiver->GetPrototype(); |
| 907 | while (current->IsJSObject() && |
| 908 | JSObject::cast(current)->map()->is_hidden_prototype()) { |
| 909 | current = current->GetPrototype(); |
| 910 | } |
| 911 | return current; |
| 912 | } |
| 913 | |
| 914 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 915 | MaybeObject* Accessors::ObjectSetPrototype(JSObject* receiver, |
| 916 | Object* value, |
| 917 | void*) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 918 | const bool skip_hidden_prototypes = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 919 | // To be consistent with other Set functions, return the value. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 920 | return receiver->SetPrototype(value, skip_hidden_prototypes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | |
| 924 | const AccessorDescriptor Accessors::ObjectPrototype = { |
| 925 | ObjectGetPrototype, |
| 926 | ObjectSetPrototype, |
| 927 | 0 |
| 928 | }; |
| 929 | |
| 930 | } } // namespace v8::internal |