blob: 18264254b8373cca4b0e8734d2a2ee6695dc481f [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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 Murdochb0fe1622011-05-05 13:52:32 +010031#include "ast.h"
32#include "deoptimizer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033#include "execution.h"
34#include "factory.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010035#include "safepoint-table.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036#include "scopeinfo.h"
37#include "top.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39namespace v8 {
40namespace internal {
41
42
43template <class C>
44static 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 Reck59135872010-11-02 12:39:01 -070056MaybeObject* Accessors::IllegalSetter(JSObject*, Object*, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +000057 UNREACHABLE();
58 return NULL;
59}
60
61
62Object* Accessors::IllegalGetAccessor(Object* object, void*) {
63 UNREACHABLE();
64 return object;
65}
66
67
John Reck59135872010-11-02 12:39:01 -070068MaybeObject* Accessors::ReadOnlySetAccessor(JSObject*, Object* value, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +000069 // 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 Reck59135872010-11-02 12:39:01 -070080MaybeObject* Accessors::ArrayGetLength(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +000081 // 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.
90Object* 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 Reck59135872010-11-02 12:39:01 -0700102MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 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.
Ben Murdoch086aeea2011-05-13 15:57:08 +0100129 return object->SetLocalPropertyIgnoreAttributes(Heap::length_symbol(),
130 value, NONE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 }
132 }
133 return Top::Throw(*Factory::NewRangeError("invalid_array_length",
134 HandleVector<Object>(NULL, 0)));
135}
136
137
138const AccessorDescriptor Accessors::ArrayLength = {
139 ArrayGetLength,
140 ArraySetLength,
141 0
142};
143
144
145//
146// Accessors::StringLength
147//
148
149
John Reck59135872010-11-02 12:39:01 -0700150MaybeObject* Accessors::StringGetLength(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 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
160const AccessorDescriptor Accessors::StringLength = {
161 StringGetLength,
162 IllegalSetter,
163 0
164};
165
166
167//
168// Accessors::ScriptSource
169//
170
171
John Reck59135872010-11-02 12:39:01 -0700172MaybeObject* Accessors::ScriptGetSource(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000173 Object* script = JSValue::cast(object)->value();
174 return Script::cast(script)->source();
175}
176
177
178const AccessorDescriptor Accessors::ScriptSource = {
179 ScriptGetSource,
180 IllegalSetter,
181 0
182};
183
184
185//
186// Accessors::ScriptName
187//
188
189
John Reck59135872010-11-02 12:39:01 -0700190MaybeObject* Accessors::ScriptGetName(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 Object* script = JSValue::cast(object)->value();
192 return Script::cast(script)->name();
193}
194
195
196const AccessorDescriptor Accessors::ScriptName = {
197 ScriptGetName,
198 IllegalSetter,
199 0
200};
201
202
203//
204// Accessors::ScriptId
205//
206
207
John Reck59135872010-11-02 12:39:01 -0700208MaybeObject* Accessors::ScriptGetId(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000209 Object* script = JSValue::cast(object)->value();
210 return Script::cast(script)->id();
211}
212
213
214const AccessorDescriptor Accessors::ScriptId = {
215 ScriptGetId,
216 IllegalSetter,
217 0
218};
219
220
221//
222// Accessors::ScriptLineOffset
223//
224
225
John Reck59135872010-11-02 12:39:01 -0700226MaybeObject* Accessors::ScriptGetLineOffset(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 Object* script = JSValue::cast(object)->value();
228 return Script::cast(script)->line_offset();
229}
230
231
232const AccessorDescriptor Accessors::ScriptLineOffset = {
233 ScriptGetLineOffset,
234 IllegalSetter,
235 0
236};
237
238
239//
240// Accessors::ScriptColumnOffset
241//
242
243
John Reck59135872010-11-02 12:39:01 -0700244MaybeObject* Accessors::ScriptGetColumnOffset(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 Object* script = JSValue::cast(object)->value();
246 return Script::cast(script)->column_offset();
247}
248
249
250const AccessorDescriptor Accessors::ScriptColumnOffset = {
251 ScriptGetColumnOffset,
252 IllegalSetter,
253 0
254};
255
256
257//
258// Accessors::ScriptData
259//
260
261
John Reck59135872010-11-02 12:39:01 -0700262MaybeObject* Accessors::ScriptGetData(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 Object* script = JSValue::cast(object)->value();
264 return Script::cast(script)->data();
265}
266
267
268const AccessorDescriptor Accessors::ScriptData = {
269 ScriptGetData,
270 IllegalSetter,
271 0
272};
273
274
275//
276// Accessors::ScriptType
277//
278
279
John Reck59135872010-11-02 12:39:01 -0700280MaybeObject* Accessors::ScriptGetType(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 Object* script = JSValue::cast(object)->value();
282 return Script::cast(script)->type();
283}
284
285
286const AccessorDescriptor Accessors::ScriptType = {
287 ScriptGetType,
288 IllegalSetter,
289 0
290};
291
292
293//
294// Accessors::ScriptCompilationType
295//
296
297
John Reck59135872010-11-02 12:39:01 -0700298MaybeObject* Accessors::ScriptGetCompilationType(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 Object* script = JSValue::cast(object)->value();
300 return Script::cast(script)->compilation_type();
301}
302
303
304const AccessorDescriptor Accessors::ScriptCompilationType = {
305 ScriptGetCompilationType,
306 IllegalSetter,
307 0
308};
309
310
311//
312// Accessors::ScriptGetLineEnds
313//
314
315
John Reck59135872010-11-02 12:39:01 -0700316MaybeObject* Accessors::ScriptGetLineEnds(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 HandleScope scope;
318 Handle<Script> script(Script::cast(JSValue::cast(object)->value()));
319 InitScriptLineEnds(script);
Steve Blockd0582a62009-12-15 09:54:21 +0000320 ASSERT(script->line_ends()->IsFixedArray());
321 Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends()));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800322 // 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 Blockd0582a62009-12-15 09:54:21 +0000326 return *js_array;
Steve Blocka7e24c12009-10-30 11:49:00 +0000327}
328
329
330const AccessorDescriptor Accessors::ScriptLineEnds = {
331 ScriptGetLineEnds,
332 IllegalSetter,
333 0
334};
335
336
337//
338// Accessors::ScriptGetContextData
339//
340
341
John Reck59135872010-11-02 12:39:01 -0700342MaybeObject* Accessors::ScriptGetContextData(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000343 Object* script = JSValue::cast(object)->value();
344 return Script::cast(script)->context_data();
345}
346
347
348const AccessorDescriptor Accessors::ScriptContextData = {
349 ScriptGetContextData,
350 IllegalSetter,
351 0
352};
353
354
355//
Steve Blockd0582a62009-12-15 09:54:21 +0000356// Accessors::ScriptGetEvalFromScript
Steve Blocka7e24c12009-10-30 11:49:00 +0000357//
358
359
John Reck59135872010-11-02 12:39:01 -0700360MaybeObject* Accessors::ScriptGetEvalFromScript(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 Object* script = JSValue::cast(object)->value();
Steve Blockd0582a62009-12-15 09:54:21 +0000362 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 Blocka7e24c12009-10-30 11:49:00 +0000372}
373
374
Steve Blockd0582a62009-12-15 09:54:21 +0000375const AccessorDescriptor Accessors::ScriptEvalFromScript = {
376 ScriptGetEvalFromScript,
Steve Blocka7e24c12009-10-30 11:49:00 +0000377 IllegalSetter,
378 0
379};
380
381
382//
Steve Blockd0582a62009-12-15 09:54:21 +0000383// Accessors::ScriptGetEvalFromScriptPosition
Steve Blocka7e24c12009-10-30 11:49:00 +0000384//
385
386
John Reck59135872010-11-02 12:39:01 -0700387MaybeObject* Accessors::ScriptGetEvalFromScriptPosition(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 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 Blockd0582a62009-12-15 09:54:21 +0000399 Handle<Code> code(SharedFunctionInfo::cast(
400 script->eval_from_shared())->code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000401 return Smi::FromInt(code->SourcePosition(code->instruction_start() +
402 script->eval_from_instructions_offset()->value()));
403}
404
405
Steve Blockd0582a62009-12-15 09:54:21 +0000406const AccessorDescriptor Accessors::ScriptEvalFromScriptPosition = {
407 ScriptGetEvalFromScriptPosition,
408 IllegalSetter,
409 0
410};
411
412
413//
414// Accessors::ScriptGetEvalFromFunctionName
415//
416
417
John Reck59135872010-11-02 12:39:01 -0700418MaybeObject* Accessors::ScriptGetEvalFromFunctionName(Object* object, void*) {
Steve Blockd0582a62009-12-15 09:54:21 +0000419 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
433const AccessorDescriptor Accessors::ScriptEvalFromFunctionName = {
434 ScriptGetEvalFromFunctionName,
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 IllegalSetter,
436 0
437};
438
439
440//
441// Accessors::FunctionPrototype
442//
443
444
John Reck59135872010-11-02 12:39:01 -0700445MaybeObject* Accessors::FunctionGetPrototype(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000446 bool found_it = false;
447 JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it);
448 if (!found_it) return Heap::undefined_value();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100449 while (!function->should_have_prototype()) {
450 found_it = false;
451 function = FindInPrototypeChain<JSFunction>(object->GetPrototype(),
452 &found_it);
453 // There has to be one because we hit the getter.
454 ASSERT(found_it);
455 }
456
Steve Blocka7e24c12009-10-30 11:49:00 +0000457 if (!function->has_prototype()) {
John Reck59135872010-11-02 12:39:01 -0700458 Object* prototype;
459 { MaybeObject* maybe_prototype = Heap::AllocateFunctionPrototype(function);
460 if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype;
461 }
462 Object* result;
463 { MaybeObject* maybe_result = function->SetPrototype(prototype);
464 if (!maybe_result->ToObject(&result)) return maybe_result;
465 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000466 }
467 return function->prototype();
468}
469
470
John Reck59135872010-11-02 12:39:01 -0700471MaybeObject* Accessors::FunctionSetPrototype(JSObject* object,
472 Object* value,
473 void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000474 bool found_it = false;
475 JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it);
476 if (!found_it) return Heap::undefined_value();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100477 if (!function->should_have_prototype()) {
478 // Since we hit this accessor, object will have no prototype property.
479 return object->SetLocalPropertyIgnoreAttributes(Heap::prototype_symbol(),
480 value,
481 NONE);
482 }
483
Steve Blocka7e24c12009-10-30 11:49:00 +0000484 if (function->has_initial_map()) {
485 // If the function has allocated the initial map
486 // replace it with a copy containing the new prototype.
John Reck59135872010-11-02 12:39:01 -0700487 Object* new_map;
488 { MaybeObject* maybe_new_map =
489 function->initial_map()->CopyDropTransitions();
490 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
491 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000492 function->set_initial_map(Map::cast(new_map));
493 }
John Reck59135872010-11-02 12:39:01 -0700494 Object* prototype;
495 { MaybeObject* maybe_prototype = function->SetPrototype(value);
496 if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype;
497 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 ASSERT(function->prototype() == value);
499 return function;
500}
501
502
503const AccessorDescriptor Accessors::FunctionPrototype = {
504 FunctionGetPrototype,
505 FunctionSetPrototype,
506 0
507};
508
509
510//
511// Accessors::FunctionLength
512//
513
514
John Reck59135872010-11-02 12:39:01 -0700515MaybeObject* Accessors::FunctionGetLength(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000516 bool found_it = false;
517 JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it);
518 if (!found_it) return Smi::FromInt(0);
519 // Check if already compiled.
Iain Merrick75681382010-08-19 15:07:18 +0100520 if (!function->shared()->is_compiled()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000521 // If the function isn't compiled yet, the length is not computed
522 // correctly yet. Compile it now and return the right length.
523 HandleScope scope;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100524 Handle<JSFunction> handle(function);
525 if (!CompileLazy(handle, KEEP_EXCEPTION)) return Failure::Exception();
526 return Smi::FromInt(handle->shared()->length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 } else {
528 return Smi::FromInt(function->shared()->length());
529 }
530}
531
532
533const AccessorDescriptor Accessors::FunctionLength = {
534 FunctionGetLength,
535 ReadOnlySetAccessor,
536 0
537};
538
539
540//
541// Accessors::FunctionName
542//
543
544
John Reck59135872010-11-02 12:39:01 -0700545MaybeObject* Accessors::FunctionGetName(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 bool found_it = false;
547 JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it);
548 if (!found_it) return Heap::undefined_value();
549 return holder->shared()->name();
550}
551
552
553const AccessorDescriptor Accessors::FunctionName = {
554 FunctionGetName,
555 ReadOnlySetAccessor,
556 0
557};
558
559
560//
561// Accessors::FunctionArguments
562//
563
Ben Murdochb0fe1622011-05-05 13:52:32 +0100564static Address SlotAddress(JavaScriptFrame* frame, int slot_index) {
565 if (slot_index >= 0) {
566 const int offset = JavaScriptFrameConstants::kLocal0Offset;
567 return frame->fp() + offset - (slot_index * kPointerSize);
568 } else {
569 const int offset = JavaScriptFrameConstants::kReceiverOffset;
570 return frame->caller_sp() + offset + (slot_index * kPointerSize);
571 }
572}
573
574
575// We can't intermix stack decoding and allocations because
576// deoptimization infrastracture is not GC safe.
577// Thus we build a temporary structure in malloced space.
578class SlotRef BASE_EMBEDDED {
579 public:
580 enum SlotRepresentation {
581 UNKNOWN,
582 TAGGED,
583 INT32,
584 DOUBLE,
585 LITERAL
586 };
587
588 SlotRef()
589 : addr_(NULL), representation_(UNKNOWN) { }
590
591 SlotRef(Address addr, SlotRepresentation representation)
592 : addr_(addr), representation_(representation) { }
593
594 explicit SlotRef(Object* literal)
595 : literal_(literal), representation_(LITERAL) { }
596
597 Handle<Object> GetValue() {
598 switch (representation_) {
599 case TAGGED:
600 return Handle<Object>(Memory::Object_at(addr_));
601
602 case INT32: {
603 int value = Memory::int32_at(addr_);
604 if (Smi::IsValid(value)) {
605 return Handle<Object>(Smi::FromInt(value));
606 } else {
607 return Factory::NewNumberFromInt(value);
608 }
609 }
610
611 case DOUBLE: {
612 double value = Memory::double_at(addr_);
613 return Factory::NewNumber(value);
614 }
615
616 case LITERAL:
617 return literal_;
618
619 default:
620 UNREACHABLE();
621 return Handle<Object>::null();
622 }
623 }
624
625 private:
626 Address addr_;
627 Handle<Object> literal_;
628 SlotRepresentation representation_;
629};
630
631
632static SlotRef ComputeSlotForNextArgument(TranslationIterator* iterator,
633 DeoptimizationInputData* data,
634 JavaScriptFrame* frame) {
635 Translation::Opcode opcode =
636 static_cast<Translation::Opcode>(iterator->Next());
637
638 switch (opcode) {
639 case Translation::BEGIN:
640 case Translation::FRAME:
641 // Peeled off before getting here.
642 break;
643
644 case Translation::ARGUMENTS_OBJECT:
645 // This can be only emitted for local slots not for argument slots.
646 break;
647
648 case Translation::REGISTER:
649 case Translation::INT32_REGISTER:
650 case Translation::DOUBLE_REGISTER:
651 case Translation::DUPLICATE:
652 // We are at safepoint which corresponds to call. All registers are
653 // saved by caller so there would be no live registers at this
654 // point. Thus these translation commands should not be used.
655 break;
656
657 case Translation::STACK_SLOT: {
658 int slot_index = iterator->Next();
659 Address slot_addr = SlotAddress(frame, slot_index);
660 return SlotRef(slot_addr, SlotRef::TAGGED);
661 }
662
663 case Translation::INT32_STACK_SLOT: {
664 int slot_index = iterator->Next();
665 Address slot_addr = SlotAddress(frame, slot_index);
666 return SlotRef(slot_addr, SlotRef::INT32);
667 }
668
669 case Translation::DOUBLE_STACK_SLOT: {
670 int slot_index = iterator->Next();
671 Address slot_addr = SlotAddress(frame, slot_index);
672 return SlotRef(slot_addr, SlotRef::DOUBLE);
673 }
674
675 case Translation::LITERAL: {
676 int literal_index = iterator->Next();
677 return SlotRef(data->LiteralArray()->get(literal_index));
678 }
679 }
680
681 UNREACHABLE();
682 return SlotRef();
683}
684
685
686
687
688
689static void ComputeSlotMappingForArguments(JavaScriptFrame* frame,
690 int inlined_frame_index,
691 Vector<SlotRef>* args_slots) {
692 AssertNoAllocation no_gc;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100693 int deopt_index = AstNode::kNoNumber;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100694 DeoptimizationInputData* data =
695 static_cast<OptimizedFrame*>(frame)->GetDeoptimizationData(&deopt_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100696 TranslationIterator it(data->TranslationByteArray(),
697 data->TranslationIndex(deopt_index)->value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100698 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
699 ASSERT(opcode == Translation::BEGIN);
700 int frame_count = it.Next();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100701 USE(frame_count);
702 ASSERT(frame_count > inlined_frame_index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100703 int frames_to_skip = inlined_frame_index;
704 while (true) {
705 opcode = static_cast<Translation::Opcode>(it.Next());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100706 // Skip over operands to advance to the next opcode.
707 it.Skip(Translation::NumberOfOperandsFor(opcode));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100708 if (opcode == Translation::FRAME) {
709 if (frames_to_skip == 0) {
Steve Block1e0659c2011-05-24 12:43:12 +0100710 // We reached the frame corresponding to the inlined function
711 // in question. Process the translation commands for the
712 // arguments.
713 //
714 // Skip the translation command for the receiver.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100715 it.Skip(Translation::NumberOfOperandsFor(
716 static_cast<Translation::Opcode>(it.Next())));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100717 // Compute slots for arguments.
718 for (int i = 0; i < args_slots->length(); ++i) {
719 (*args_slots)[i] = ComputeSlotForNextArgument(&it, data, frame);
720 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100721 return;
722 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100723 frames_to_skip--;
724 }
725 }
726
727 UNREACHABLE();
728}
729
730
731static MaybeObject* ConstructArgumentsObjectForInlinedFunction(
732 JavaScriptFrame* frame,
733 Handle<JSFunction> inlined_function,
734 int inlined_frame_index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100735 int args_count = inlined_function->shared()->formal_parameter_count();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100736 ScopedVector<SlotRef> args_slots(args_count);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100737 ComputeSlotMappingForArguments(frame, inlined_frame_index, &args_slots);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100738 Handle<JSObject> arguments =
739 Factory::NewArgumentsObject(inlined_function, args_count);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100740 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 Blocka7e24c12009-10-30 11:49:00 +0000751
John Reck59135872010-11-02 12:39:01 -0700752MaybeObject* Accessors::FunctionGetArguments(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000753 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 Murdochb0fe1622011-05-05 13:52:32 +0100760 List<JSFunction*> functions(2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 for (JavaScriptFrameIterator it; !it.done(); it.Advance()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000762 JavaScriptFrame* frame = it.frame();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100763 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 Blocka7e24c12009-10-30 11:49:00 +0000767
Ben Murdochb0fe1622011-05-05 13:52:32 +0100768 if (i > 0) {
Steve Block1e0659c2011-05-24 12:43:12 +0100769 // The function in question was inlined. Inlined functions have the
770 // correct number of arguments and no allocated arguments object, so
771 // we can construct a fresh one by interpreting the function's
772 // deoptimization input data.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100773 return ConstructArgumentsObjectForInlinedFunction(frame, function, i);
Steve Block1e0659c2011-05-24 12:43:12 +0100774 }
775
776 if (!frame->is_optimized()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100777 // If there is an arguments variable in the stack, we return that.
Steve Block1e0659c2011-05-24 12:43:12 +0100778 Handle<SerializedScopeInfo> info(function->shared()->scope_info());
779 int index = info->StackSlotIndex(Heap::arguments_symbol());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100780 if (index >= 0) {
Steve Block1e0659c2011-05-24 12:43:12 +0100781 Handle<Object> arguments(frame->GetExpression(index));
Ben Murdoch086aeea2011-05-13 15:57:08 +0100782 if (!arguments->IsArgumentsMarker()) return *arguments;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100783 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100784 }
Steve Block1e0659c2011-05-24 12:43:12 +0100785
786 // If there is no arguments variable in the stack or we have an
787 // optimized frame, we find the frame that holds the actual arguments
788 // passed to the function.
789 it.AdvanceToArgumentsFrame();
790 frame = it.frame();
791
792 // Get the number of arguments and construct an arguments object
793 // mirror for the right frame.
794 const int length = frame->GetProvidedParametersCount();
795 Handle<JSObject> arguments = Factory::NewArgumentsObject(function,
796 length);
797 Handle<FixedArray> array = Factory::NewFixedArray(length);
798
799 // Copy the parameters to the arguments object.
800 ASSERT(array->length() == length);
801 for (int i = 0; i < length; i++) array->set(i, frame->GetParameter(i));
802 arguments->set_elements(*array);
803
804 // Return the freshly allocated arguments object.
805 return *arguments;
Steve Blocka7e24c12009-10-30 11:49:00 +0000806 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100807 functions.Rewind(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000808 }
809
810 // No frame corresponding to the given function found. Return null.
811 return Heap::null_value();
812}
813
814
815const AccessorDescriptor Accessors::FunctionArguments = {
816 FunctionGetArguments,
817 ReadOnlySetAccessor,
818 0
819};
820
821
822//
823// Accessors::FunctionCaller
824//
825
826
John Reck59135872010-11-02 12:39:01 -0700827MaybeObject* Accessors::FunctionGetCaller(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000828 HandleScope scope;
John Reck59135872010-11-02 12:39:01 -0700829 AssertNoAllocation no_alloc;
Steve Blocka7e24c12009-10-30 11:49:00 +0000830 bool found_it = false;
831 JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it);
832 if (!found_it) return Heap::undefined_value();
833 Handle<JSFunction> function(holder);
834
Ben Murdochb0fe1622011-05-05 13:52:32 +0100835 List<JSFunction*> functions(2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000836 for (JavaScriptFrameIterator it; !it.done(); it.Advance()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100837 JavaScriptFrame* frame = it.frame();
838 frame->GetFunctions(&functions);
839 for (int i = functions.length() - 1; i >= 0; i--) {
840 if (functions[i] == *function) {
841 // Once we have found the frame, we need to go to the caller
842 // frame. This may require skipping through a number of top-level
843 // frames, e.g. frames for scripts not functions.
844 if (i > 0) {
845 ASSERT(!functions[i - 1]->shared()->is_toplevel());
846 return functions[i - 1];
847 } else {
848 for (it.Advance(); !it.done(); it.Advance()) {
849 frame = it.frame();
850 functions.Rewind(0);
851 frame->GetFunctions(&functions);
852 if (!functions.last()->shared()->is_toplevel()) {
853 return functions.last();
854 }
855 ASSERT(functions.length() == 1);
856 }
857 if (it.done()) return Heap::null_value();
858 break;
859 }
860 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000861 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100862 functions.Rewind(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000863 }
864
865 // No frame corresponding to the given function found. Return null.
866 return Heap::null_value();
867}
868
869
870const AccessorDescriptor Accessors::FunctionCaller = {
871 FunctionGetCaller,
872 ReadOnlySetAccessor,
873 0
874};
875
876
877//
878// Accessors::ObjectPrototype
879//
880
881
John Reck59135872010-11-02 12:39:01 -0700882MaybeObject* Accessors::ObjectGetPrototype(Object* receiver, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 Object* current = receiver->GetPrototype();
884 while (current->IsJSObject() &&
885 JSObject::cast(current)->map()->is_hidden_prototype()) {
886 current = current->GetPrototype();
887 }
888 return current;
889}
890
891
John Reck59135872010-11-02 12:39:01 -0700892MaybeObject* Accessors::ObjectSetPrototype(JSObject* receiver,
893 Object* value,
894 void*) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000895 const bool skip_hidden_prototypes = true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000896 // To be consistent with other Set functions, return the value.
Andrei Popescu402d9372010-02-26 13:31:12 +0000897 return receiver->SetPrototype(value, skip_hidden_prototypes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000898}
899
900
901const AccessorDescriptor Accessors::ObjectPrototype = {
902 ObjectGetPrototype,
903 ObjectSetPrototype,
904 0
905};
906
907} } // namespace v8::internal