OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
673 DisableAssertNoAllocation enable_allocation; | 673 DisableAssertNoAllocation enable_allocation; |
674 if (caller->shared()->strict_mode()) { | 674 if (caller->shared()->strict_mode()) { |
675 return isolate->Throw( | 675 return isolate->Throw( |
676 *isolate->factory()->NewTypeError("strict_caller", | 676 *isolate->factory()->NewTypeError("strict_caller", |
677 HandleVector<Object>(NULL, 0))); | 677 HandleVector<Object>(NULL, 0))); |
678 } | 678 } |
679 return caller; | 679 return caller; |
680 } | 680 } |
681 | 681 |
682 | 682 |
683 class FrameFunctionIterator { | |
684 public: | |
685 FrameFunctionIterator(Isolate* isolate, const AssertNoAllocation& promise) | |
686 : frame_iterator_(isolate), | |
687 functions_(2), | |
688 index_(0) { | |
689 GetFunctions(); | |
690 } | |
691 JSFunction* next() { | |
692 if (functions_.length() == 0) return NULL; | |
693 JSFunction* next_function = functions_[index_]; | |
694 index_--; | |
695 if (index_ < 0) { | |
696 GetFunctions(); | |
697 } | |
698 return next_function; | |
699 } | |
700 | |
701 bool Find(JSFunction* function) { | |
702 JSFunction* next_function; | |
703 do { | |
704 next_function = next(); | |
705 if (next_function == function) return true; | |
706 } while (next_function != NULL); | |
707 return false; | |
708 } | |
709 private: | |
710 void GetFunctions() { | |
711 functions_.Rewind(0); | |
712 if (frame_iterator_.done()) return; | |
713 JavaScriptFrame* frame = frame_iterator_.frame(); | |
714 frame->GetFunctions(&functions_); | |
715 ASSERT(functions_.length() > 0); | |
716 frame_iterator_.Advance(); | |
717 index_ = functions_.length() - 1; | |
718 } | |
719 JavaScriptFrameIterator frame_iterator_; | |
720 List<JSFunction*> functions_; | |
721 int index_; | |
722 }; | |
723 | |
724 | |
683 MaybeObject* Accessors::FunctionGetCaller(Object* object, void*) { | 725 MaybeObject* Accessors::FunctionGetCaller(Object* object, void*) { |
684 Isolate* isolate = Isolate::Current(); | 726 Isolate* isolate = Isolate::Current(); |
685 HandleScope scope(isolate); | 727 HandleScope scope(isolate); |
686 AssertNoAllocation no_alloc; | 728 AssertNoAllocation no_alloc; |
687 bool found_it = false; | 729 bool found_it = false; |
688 JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); | 730 JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
689 if (!found_it) return isolate->heap()->undefined_value(); | 731 if (!found_it) return isolate->heap()->undefined_value(); |
690 Handle<JSFunction> function(holder, isolate); | 732 Handle<JSFunction> function(holder, isolate); |
691 | 733 |
692 List<JSFunction*> functions(2); | 734 FrameFunctionIterator it(isolate, no_alloc); |
693 for (JavaScriptFrameIterator it(isolate); !it.done(); it.Advance()) { | 735 |
694 JavaScriptFrame* frame = it.frame(); | 736 // Find the function from the frames. |
695 frame->GetFunctions(&functions); | 737 if (!it.Find(*function)) { |
696 for (int i = functions.length() - 1; i >= 0; i--) { | 738 // No frame corresponding to the given function found. Return null. |
697 if (functions[i] == *function) { | 739 return isolate->heap()->null_value(); |
698 // Once we have found the frame, we need to go to the caller | |
699 // frame. This may require skipping through a number of top-level | |
700 // frames, e.g. frames for scripts not functions. | |
701 if (i > 0) { | |
702 ASSERT(!functions[i - 1]->shared()->is_toplevel()); | |
703 return CheckNonStrictCallerOrThrow(isolate, functions[i - 1]); | |
704 } else { | |
705 for (it.Advance(); !it.done(); it.Advance()) { | |
706 frame = it.frame(); | |
707 functions.Rewind(0); | |
708 frame->GetFunctions(&functions); | |
709 if (!functions.last()->shared()->is_toplevel()) { | |
710 return CheckNonStrictCallerOrThrow(isolate, functions.last()); | |
711 } | |
712 ASSERT(functions.length() == 1); | |
713 } | |
714 if (it.done()) return isolate->heap()->null_value(); | |
715 break; | |
716 } | |
717 } | |
718 } | |
719 functions.Rewind(0); | |
720 } | 740 } |
721 | 741 |
722 // No frame corresponding to the given function found. Return null. | 742 // Find previously called non-toplevel function. |
723 return isolate->heap()->null_value(); | 743 JSFunction* caller; |
744 do { | |
745 caller = it.next(); | |
746 if (caller == NULL) return isolate->heap()->null_value(); | |
747 } while (caller->shared()->is_toplevel()); | |
748 | |
749 // If caller is a built-in function and caller's caller is also built-in, | |
750 // use that instead. | |
751 for (JSFunction* potential_caller = caller; | |
752 potential_caller != NULL && potential_caller->IsBuiltin(); | |
William Hesse
2011/08/25 13:29:48
I think this is clearer as a while loop, or anythi
Lasse Reichstein
2011/08/25 13:34:12
Done.
| |
753 potential_caller = it.next()) { | |
754 caller = potential_caller; | |
755 } | |
756 | |
757 return CheckNonStrictCallerOrThrow(isolate, caller); | |
724 } | 758 } |
725 | 759 |
726 | 760 |
727 const AccessorDescriptor Accessors::FunctionCaller = { | 761 const AccessorDescriptor Accessors::FunctionCaller = { |
728 FunctionGetCaller, | 762 FunctionGetCaller, |
729 ReadOnlySetAccessor, | 763 ReadOnlySetAccessor, |
730 0 | 764 0 |
731 }; | 765 }; |
732 | 766 |
733 | 767 |
(...skipping 21 matching lines...) Expand all Loading... | |
755 } | 789 } |
756 | 790 |
757 | 791 |
758 const AccessorDescriptor Accessors::ObjectPrototype = { | 792 const AccessorDescriptor Accessors::ObjectPrototype = { |
759 ObjectGetPrototype, | 793 ObjectGetPrototype, |
760 ObjectSetPrototype, | 794 ObjectSetPrototype, |
761 0 | 795 0 |
762 }; | 796 }; |
763 | 797 |
764 } } // namespace v8::internal | 798 } } // namespace v8::internal |
OLD | NEW |