blob: 43d54fe47482846303ec46d03d3b5e2126a7dfeb [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.
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
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();
449 if (!function->has_prototype()) {
John Reck59135872010-11-02 12:39:01 -0700450 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 Blocka7e24c12009-10-30 11:49:00 +0000458 }
459 return function->prototype();
460}
461
462
John Reck59135872010-11-02 12:39:01 -0700463MaybeObject* Accessors::FunctionSetPrototype(JSObject* object,
464 Object* value,
465 void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000466 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 Reck59135872010-11-02 12:39:01 -0700472 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 Blocka7e24c12009-10-30 11:49:00 +0000477 function->set_initial_map(Map::cast(new_map));
478 }
John Reck59135872010-11-02 12:39:01 -0700479 Object* prototype;
480 { MaybeObject* maybe_prototype = function->SetPrototype(value);
481 if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype;
482 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000483 ASSERT(function->prototype() == value);
484 return function;
485}
486
487
488const AccessorDescriptor Accessors::FunctionPrototype = {
489 FunctionGetPrototype,
490 FunctionSetPrototype,
491 0
492};
493
494
495//
496// Accessors::FunctionLength
497//
498
499
John Reck59135872010-11-02 12:39:01 -0700500MaybeObject* Accessors::FunctionGetLength(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 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 Merrick75681382010-08-19 15:07:18 +0100505 if (!function->shared()->is_compiled()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000506 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100509 Handle<JSFunction> handle(function);
510 if (!CompileLazy(handle, KEEP_EXCEPTION)) return Failure::Exception();
511 return Smi::FromInt(handle->shared()->length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000512 } else {
513 return Smi::FromInt(function->shared()->length());
514 }
515}
516
517
518const AccessorDescriptor Accessors::FunctionLength = {
519 FunctionGetLength,
520 ReadOnlySetAccessor,
521 0
522};
523
524
525//
526// Accessors::FunctionName
527//
528
529
John Reck59135872010-11-02 12:39:01 -0700530MaybeObject* Accessors::FunctionGetName(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 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
538const AccessorDescriptor Accessors::FunctionName = {
539 FunctionGetName,
540 ReadOnlySetAccessor,
541 0
542};
543
544
545//
546// Accessors::FunctionArguments
547//
548
Ben Murdochb0fe1622011-05-05 13:52:32 +0100549static 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.
563class 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
617static 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
674static 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
726static 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 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) {
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 Blocka7e24c12009-10-30 11:49:00 +0000802 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100803 functions.Rewind(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000804 }
805
806 // No frame corresponding to the given function found. Return null.
807 return Heap::null_value();
808}
809
810
811const AccessorDescriptor Accessors::FunctionArguments = {
812 FunctionGetArguments,
813 ReadOnlySetAccessor,
814 0
815};
816
817
818//
819// Accessors::FunctionCaller
820//
821
822
John Reck59135872010-11-02 12:39:01 -0700823MaybeObject* Accessors::FunctionGetCaller(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000824 HandleScope scope;
John Reck59135872010-11-02 12:39:01 -0700825 AssertNoAllocation no_alloc;
Steve Blocka7e24c12009-10-30 11:49:00 +0000826 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 Murdochb0fe1622011-05-05 13:52:32 +0100831 List<JSFunction*> functions(2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000832 for (JavaScriptFrameIterator it; !it.done(); it.Advance()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100833 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 Blocka7e24c12009-10-30 11:49:00 +0000857 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100858 functions.Rewind(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000859 }
860
861 // No frame corresponding to the given function found. Return null.
862 return Heap::null_value();
863}
864
865
866const AccessorDescriptor Accessors::FunctionCaller = {
867 FunctionGetCaller,
868 ReadOnlySetAccessor,
869 0
870};
871
872
873//
874// Accessors::ObjectPrototype
875//
876
877
John Reck59135872010-11-02 12:39:01 -0700878MaybeObject* Accessors::ObjectGetPrototype(Object* receiver, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000879 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 Reck59135872010-11-02 12:39:01 -0700888MaybeObject* Accessors::ObjectSetPrototype(JSObject* receiver,
889 Object* value,
890 void*) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000891 const bool skip_hidden_prototypes = true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000892 // To be consistent with other Set functions, return the value.
Andrei Popescu402d9372010-02-26 13:31:12 +0000893 return receiver->SetPrototype(value, skip_hidden_prototypes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000894}
895
896
897const AccessorDescriptor Accessors::ObjectPrototype = {
898 ObjectGetPrototype,
899 ObjectSetPrototype,
900 0
901};
902
903} } // namespace v8::internal