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