Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: src/isolate.cc

Issue 6969042: Isolates cleanup: move top.cc to isolate.cc. (Closed)
Patch Set: Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/flag-definitions.h ('k') | src/top.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 22 matching lines...) Expand all
33 #include "bootstrapper.h" 33 #include "bootstrapper.h"
34 #include "codegen.h" 34 #include "codegen.h"
35 #include "compilation-cache.h" 35 #include "compilation-cache.h"
36 #include "debug.h" 36 #include "debug.h"
37 #include "deoptimizer.h" 37 #include "deoptimizer.h"
38 #include "heap-profiler.h" 38 #include "heap-profiler.h"
39 #include "hydrogen.h" 39 #include "hydrogen.h"
40 #include "isolate.h" 40 #include "isolate.h"
41 #include "lithium-allocator.h" 41 #include "lithium-allocator.h"
42 #include "log.h" 42 #include "log.h"
43 #include "messages.h"
43 #include "regexp-stack.h" 44 #include "regexp-stack.h"
44 #include "runtime-profiler.h" 45 #include "runtime-profiler.h"
45 #include "scanner.h" 46 #include "scanner.h"
46 #include "scopeinfo.h" 47 #include "scopeinfo.h"
47 #include "serialize.h" 48 #include "serialize.h"
48 #include "simulator.h" 49 #include "simulator.h"
49 #include "spaces.h" 50 #include "spaces.h"
50 #include "stub-cache.h" 51 #include "stub-cache.h"
51 #include "version.h" 52 #include "version.h"
53 #include "vm-state-inl.h"
52 54
53 55
54 namespace v8 { 56 namespace v8 {
55 namespace internal { 57 namespace internal {
56 58
57 Atomic32 ThreadId::highest_thread_id_ = 0; 59 Atomic32 ThreadId::highest_thread_id_ = 0;
58 60
59 int ThreadId::AllocateThreadId() { 61 int ThreadId::AllocateThreadId() {
60 int new_id = NoBarrier_AtomicIncrement(&highest_thread_id_, 1); 62 int new_id = NoBarrier_AtomicIncrement(&highest_thread_id_, 1);
61 return new_id; 63 return new_id;
62 } 64 }
63 65
66
64 int ThreadId::GetCurrentThreadId() { 67 int ThreadId::GetCurrentThreadId() {
65 int thread_id = Thread::GetThreadLocalInt(Isolate::thread_id_key_); 68 int thread_id = Thread::GetThreadLocalInt(Isolate::thread_id_key_);
66 if (thread_id == 0) { 69 if (thread_id == 0) {
67 thread_id = AllocateThreadId(); 70 thread_id = AllocateThreadId();
68 Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id); 71 Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id);
69 } 72 }
70 return thread_id; 73 return thread_id;
71 } 74 }
72 75
76
77 ThreadLocalTop::ThreadLocalTop() {
78 InitializeInternal();
79 }
80
81
82 void ThreadLocalTop::InitializeInternal() {
83 c_entry_fp_ = 0;
84 handler_ = 0;
85 #ifdef USE_SIMULATOR
86 simulator_ = NULL;
87 #endif
88 #ifdef ENABLE_LOGGING_AND_PROFILING
89 js_entry_sp_ = NULL;
90 external_callback_ = NULL;
91 #endif
92 #ifdef ENABLE_VMSTATE_TRACKING
93 current_vm_state_ = EXTERNAL;
94 #endif
95 try_catch_handler_address_ = NULL;
96 context_ = NULL;
97 thread_id_ = ThreadId::Invalid();
98 external_caught_exception_ = false;
99 failed_access_check_callback_ = NULL;
100 save_context_ = NULL;
101 catcher_ = NULL;
102 }
103
104
105 void ThreadLocalTop::Initialize() {
106 InitializeInternal();
107 #ifdef USE_SIMULATOR
108 #ifdef V8_TARGET_ARCH_ARM
109 simulator_ = Simulator::current(isolate_);
110 #elif V8_TARGET_ARCH_MIPS
111 simulator_ = Simulator::current(isolate_);
112 #endif
113 #endif
114 thread_id_ = ThreadId::Current();
115 }
116
117
118 v8::TryCatch* ThreadLocalTop::TryCatchHandler() {
119 return TRY_CATCH_FROM_ADDRESS(try_catch_handler_address());
120 }
121
122
73 // Create a dummy thread that will wait forever on a semaphore. The only 123 // Create a dummy thread that will wait forever on a semaphore. The only
74 // purpose for this thread is to have some stack area to save essential data 124 // purpose for this thread is to have some stack area to save essential data
75 // into for use by a stacks only core dump (aka minidump). 125 // into for use by a stacks only core dump (aka minidump).
76 class PreallocatedMemoryThread: public Thread { 126 class PreallocatedMemoryThread: public Thread {
77 public: 127 public:
78 char* data() { 128 char* data() {
79 if (data_ready_semaphore_ != NULL) { 129 if (data_ready_semaphore_ != NULL) {
80 // Initial access is guarded until the data has been published. 130 // Initial access is guarded until the data has been published.
81 data_ready_semaphore_->Wait(); 131 data_ready_semaphore_->Wait();
82 delete data_ready_semaphore_; 132 delete data_ready_semaphore_;
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 } 415 }
366 } 416 }
367 417
368 418
369 Isolate* Isolate::GetDefaultIsolateForLocking() { 419 Isolate* Isolate::GetDefaultIsolateForLocking() {
370 EnsureDefaultIsolate(); 420 EnsureDefaultIsolate();
371 return default_isolate_; 421 return default_isolate_;
372 } 422 }
373 423
374 424
425 Address Isolate::get_address_from_id(Isolate::AddressId id) {
426 return isolate_addresses_[id];
427 }
428
429
430 char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) {
431 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage);
432 Iterate(v, thread);
433 return thread_storage + sizeof(ThreadLocalTop);
434 }
435
436
437 void Isolate::IterateThread(ThreadVisitor* v) {
438 v->VisitThread(this, thread_local_top());
439 }
440
441
442 void Isolate::IterateThread(ThreadVisitor* v, char* t) {
443 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(t);
444 v->VisitThread(this, thread);
445 }
446
447
448 void Isolate::Iterate(ObjectVisitor* v, ThreadLocalTop* thread) {
449 // Visit the roots from the top for a given thread.
450 Object* pending;
451 // The pending exception can sometimes be a failure. We can't show
452 // that to the GC, which only understands objects.
453 if (thread->pending_exception_->ToObject(&pending)) {
454 v->VisitPointer(&pending);
455 thread->pending_exception_ = pending; // In case GC updated it.
456 }
457 v->VisitPointer(&(thread->pending_message_obj_));
458 v->VisitPointer(BitCast<Object**>(&(thread->pending_message_script_)));
459 v->VisitPointer(BitCast<Object**>(&(thread->context_)));
460 Object* scheduled;
461 if (thread->scheduled_exception_->ToObject(&scheduled)) {
462 v->VisitPointer(&scheduled);
463 thread->scheduled_exception_ = scheduled;
464 }
465
466 for (v8::TryCatch* block = thread->TryCatchHandler();
467 block != NULL;
468 block = TRY_CATCH_FROM_ADDRESS(block->next_)) {
469 v->VisitPointer(BitCast<Object**>(&(block->exception_)));
470 v->VisitPointer(BitCast<Object**>(&(block->message_)));
471 }
472
473 // Iterate over pointers on native execution stack.
474 for (StackFrameIterator it(this, thread); !it.done(); it.Advance()) {
475 it.frame()->Iterate(v);
476 }
477 }
478
479
480 void Isolate::Iterate(ObjectVisitor* v) {
481 ThreadLocalTop* current_t = thread_local_top();
482 Iterate(v, current_t);
483 }
484
485
486 void Isolate::RegisterTryCatchHandler(v8::TryCatch* that) {
487 // The ARM simulator has a separate JS stack. We therefore register
488 // the C++ try catch handler with the simulator and get back an
489 // address that can be used for comparisons with addresses into the
490 // JS stack. When running without the simulator, the address
491 // returned will be the address of the C++ try catch handler itself.
492 Address address = reinterpret_cast<Address>(
493 SimulatorStack::RegisterCTryCatch(reinterpret_cast<uintptr_t>(that)));
494 thread_local_top()->set_try_catch_handler_address(address);
495 }
496
497
498 void Isolate::UnregisterTryCatchHandler(v8::TryCatch* that) {
499 ASSERT(thread_local_top()->TryCatchHandler() == that);
500 thread_local_top()->set_try_catch_handler_address(
501 reinterpret_cast<Address>(that->next_));
502 thread_local_top()->catcher_ = NULL;
503 SimulatorStack::UnregisterCTryCatch();
504 }
505
506
507 Handle<String> Isolate::StackTraceString() {
508 if (stack_trace_nesting_level_ == 0) {
509 stack_trace_nesting_level_++;
510 HeapStringAllocator allocator;
511 StringStream::ClearMentionedObjectCache();
512 StringStream accumulator(&allocator);
513 incomplete_message_ = &accumulator;
514 PrintStack(&accumulator);
515 Handle<String> stack_trace = accumulator.ToString();
516 incomplete_message_ = NULL;
517 stack_trace_nesting_level_ = 0;
518 return stack_trace;
519 } else if (stack_trace_nesting_level_ == 1) {
520 stack_trace_nesting_level_++;
521 OS::PrintError(
522 "\n\nAttempt to print stack while printing stack (double fault)\n");
523 OS::PrintError(
524 "If you are lucky you may find a partial stack dump on stdout.\n\n");
525 incomplete_message_->OutputToStdOut();
526 return factory()->empty_symbol();
527 } else {
528 OS::Abort();
529 // Unreachable
530 return factory()->empty_symbol();
531 }
532 }
533
534
535 Handle<JSArray> Isolate::CaptureCurrentStackTrace(
536 int frame_limit, StackTrace::StackTraceOptions options) {
537 // Ensure no negative values.
538 int limit = Max(frame_limit, 0);
539 Handle<JSArray> stack_trace = factory()->NewJSArray(frame_limit);
540
541 Handle<String> column_key = factory()->LookupAsciiSymbol("column");
542 Handle<String> line_key = factory()->LookupAsciiSymbol("lineNumber");
543 Handle<String> script_key = factory()->LookupAsciiSymbol("scriptName");
544 Handle<String> name_or_source_url_key =
545 factory()->LookupAsciiSymbol("nameOrSourceURL");
546 Handle<String> script_name_or_source_url_key =
547 factory()->LookupAsciiSymbol("scriptNameOrSourceURL");
548 Handle<String> function_key = factory()->LookupAsciiSymbol("functionName");
549 Handle<String> eval_key = factory()->LookupAsciiSymbol("isEval");
550 Handle<String> constructor_key =
551 factory()->LookupAsciiSymbol("isConstructor");
552
553 StackTraceFrameIterator it(this);
554 int frames_seen = 0;
555 while (!it.done() && (frames_seen < limit)) {
556 JavaScriptFrame* frame = it.frame();
557 // Set initial size to the maximum inlining level + 1 for the outermost
558 // function.
559 List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1);
560 frame->Summarize(&frames);
561 for (int i = frames.length() - 1; i >= 0 && frames_seen < limit; i--) {
562 // Create a JSObject to hold the information for the StackFrame.
563 Handle<JSObject> stackFrame = factory()->NewJSObject(object_function());
564
565 Handle<JSFunction> fun = frames[i].function();
566 Handle<Script> script(Script::cast(fun->shared()->script()));
567
568 if (options & StackTrace::kLineNumber) {
569 int script_line_offset = script->line_offset()->value();
570 int position = frames[i].code()->SourcePosition(frames[i].pc());
571 int line_number = GetScriptLineNumber(script, position);
572 // line_number is already shifted by the script_line_offset.
573 int relative_line_number = line_number - script_line_offset;
574 if (options & StackTrace::kColumnOffset && relative_line_number >= 0) {
575 Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends()));
576 int start = (relative_line_number == 0) ? 0 :
577 Smi::cast(line_ends->get(relative_line_number - 1))->value() + 1;
578 int column_offset = position - start;
579 if (relative_line_number == 0) {
580 // For the case where the code is on the same line as the script
581 // tag.
582 column_offset += script->column_offset()->value();
583 }
584 SetLocalPropertyNoThrow(stackFrame, column_key,
585 Handle<Smi>(Smi::FromInt(column_offset + 1)));
586 }
587 SetLocalPropertyNoThrow(stackFrame, line_key,
588 Handle<Smi>(Smi::FromInt(line_number + 1)));
589 }
590
591 if (options & StackTrace::kScriptName) {
592 Handle<Object> script_name(script->name(), this);
593 SetLocalPropertyNoThrow(stackFrame, script_key, script_name);
594 }
595
596 if (options & StackTrace::kScriptNameOrSourceURL) {
597 Handle<Object> script_name(script->name(), this);
598 Handle<JSValue> script_wrapper = GetScriptWrapper(script);
599 Handle<Object> property = GetProperty(script_wrapper,
600 name_or_source_url_key);
601 ASSERT(property->IsJSFunction());
602 Handle<JSFunction> method = Handle<JSFunction>::cast(property);
603 bool caught_exception;
604 Handle<Object> result = Execution::TryCall(method, script_wrapper, 0,
605 NULL, &caught_exception);
606 if (caught_exception) {
607 result = factory()->undefined_value();
608 }
609 SetLocalPropertyNoThrow(stackFrame, script_name_or_source_url_key,
610 result);
611 }
612
613 if (options & StackTrace::kFunctionName) {
614 Handle<Object> fun_name(fun->shared()->name(), this);
615 if (fun_name->ToBoolean()->IsFalse()) {
616 fun_name = Handle<Object>(fun->shared()->inferred_name(), this);
617 }
618 SetLocalPropertyNoThrow(stackFrame, function_key, fun_name);
619 }
620
621 if (options & StackTrace::kIsEval) {
622 int type = Smi::cast(script->compilation_type())->value();
623 Handle<Object> is_eval = (type == Script::COMPILATION_TYPE_EVAL) ?
624 factory()->true_value() : factory()->false_value();
625 SetLocalPropertyNoThrow(stackFrame, eval_key, is_eval);
626 }
627
628 if (options & StackTrace::kIsConstructor) {
629 Handle<Object> is_constructor = (frames[i].is_constructor()) ?
630 factory()->true_value() : factory()->false_value();
631 SetLocalPropertyNoThrow(stackFrame, constructor_key, is_constructor);
632 }
633
634 FixedArray::cast(stack_trace->elements())->set(frames_seen, *stackFrame);
635 frames_seen++;
636 }
637 it.Advance();
638 }
639
640 stack_trace->set_length(Smi::FromInt(frames_seen));
641 return stack_trace;
642 }
643
644
645 void Isolate::PrintStack() {
646 if (stack_trace_nesting_level_ == 0) {
647 stack_trace_nesting_level_++;
648
649 StringAllocator* allocator;
650 if (preallocated_message_space_ == NULL) {
651 allocator = new HeapStringAllocator();
652 } else {
653 allocator = preallocated_message_space_;
654 }
655
656 StringStream::ClearMentionedObjectCache();
657 StringStream accumulator(allocator);
658 incomplete_message_ = &accumulator;
659 PrintStack(&accumulator);
660 accumulator.OutputToStdOut();
661 accumulator.Log();
662 incomplete_message_ = NULL;
663 stack_trace_nesting_level_ = 0;
664 if (preallocated_message_space_ == NULL) {
665 // Remove the HeapStringAllocator created above.
666 delete allocator;
667 }
668 } else if (stack_trace_nesting_level_ == 1) {
669 stack_trace_nesting_level_++;
670 OS::PrintError(
671 "\n\nAttempt to print stack while printing stack (double fault)\n");
672 OS::PrintError(
673 "If you are lucky you may find a partial stack dump on stdout.\n\n");
674 incomplete_message_->OutputToStdOut();
675 }
676 }
677
678
679 static void PrintFrames(StringStream* accumulator,
680 StackFrame::PrintMode mode) {
681 StackFrameIterator it;
682 for (int i = 0; !it.done(); it.Advance()) {
683 it.frame()->Print(accumulator, mode, i++);
684 }
685 }
686
687
688 void Isolate::PrintStack(StringStream* accumulator) {
689 if (!IsInitialized()) {
690 accumulator->Add(
691 "\n==== Stack trace is not available ==========================\n\n");
692 accumulator->Add(
693 "\n==== Isolate for the thread is not initialized =============\n\n");
694 return;
695 }
696 // The MentionedObjectCache is not GC-proof at the moment.
697 AssertNoAllocation nogc;
698 ASSERT(StringStream::IsMentionedObjectCacheClear());
699
700 // Avoid printing anything if there are no frames.
701 if (c_entry_fp(thread_local_top()) == 0) return;
702
703 accumulator->Add(
704 "\n==== Stack trace ============================================\n\n");
705 PrintFrames(accumulator, StackFrame::OVERVIEW);
706
707 accumulator->Add(
708 "\n==== Details ================================================\n\n");
709 PrintFrames(accumulator, StackFrame::DETAILS);
710
711 accumulator->PrintMentionedObjectCache();
712 accumulator->Add("=====================\n\n");
713 }
714
715
716 void Isolate::SetFailedAccessCheckCallback(
717 v8::FailedAccessCheckCallback callback) {
718 thread_local_top()->failed_access_check_callback_ = callback;
719 }
720
721
722 void Isolate::ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type) {
723 if (!thread_local_top()->failed_access_check_callback_) return;
724
725 ASSERT(receiver->IsAccessCheckNeeded());
726 ASSERT(context());
727
728 // Get the data object from access check info.
729 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
730 if (!constructor->shared()->IsApiFunction()) return;
731 Object* data_obj =
732 constructor->shared()->get_api_func_data()->access_check_info();
733 if (data_obj == heap_.undefined_value()) return;
734
735 HandleScope scope;
736 Handle<JSObject> receiver_handle(receiver);
737 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data());
738 thread_local_top()->failed_access_check_callback_(
739 v8::Utils::ToLocal(receiver_handle),
740 type,
741 v8::Utils::ToLocal(data));
742 }
743
744
745 enum MayAccessDecision {
746 YES, NO, UNKNOWN
747 };
748
749
750 static MayAccessDecision MayAccessPreCheck(Isolate* isolate,
751 JSObject* receiver,
752 v8::AccessType type) {
753 // During bootstrapping, callback functions are not enabled yet.
754 if (isolate->bootstrapper()->IsActive()) return YES;
755
756 if (receiver->IsJSGlobalProxy()) {
757 Object* receiver_context = JSGlobalProxy::cast(receiver)->context();
758 if (!receiver_context->IsContext()) return NO;
759
760 // Get the global context of current top context.
761 // avoid using Isolate::global_context() because it uses Handle.
762 Context* global_context = isolate->context()->global()->global_context();
763 if (receiver_context == global_context) return YES;
764
765 if (Context::cast(receiver_context)->security_token() ==
766 global_context->security_token())
767 return YES;
768 }
769
770 return UNKNOWN;
771 }
772
773
774 bool Isolate::MayNamedAccess(JSObject* receiver, Object* key,
775 v8::AccessType type) {
776 ASSERT(receiver->IsAccessCheckNeeded());
777
778 // The callers of this method are not expecting a GC.
779 AssertNoAllocation no_gc;
780
781 // Skip checks for hidden properties access. Note, we do not
782 // require existence of a context in this case.
783 if (key == heap_.hidden_symbol()) return true;
784
785 // Check for compatibility between the security tokens in the
786 // current lexical context and the accessed object.
787 ASSERT(context());
788
789 MayAccessDecision decision = MayAccessPreCheck(this, receiver, type);
790 if (decision != UNKNOWN) return decision == YES;
791
792 // Get named access check callback
793 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
794 if (!constructor->shared()->IsApiFunction()) return false;
795
796 Object* data_obj =
797 constructor->shared()->get_api_func_data()->access_check_info();
798 if (data_obj == heap_.undefined_value()) return false;
799
800 Object* fun_obj = AccessCheckInfo::cast(data_obj)->named_callback();
801 v8::NamedSecurityCallback callback =
802 v8::ToCData<v8::NamedSecurityCallback>(fun_obj);
803
804 if (!callback) return false;
805
806 HandleScope scope(this);
807 Handle<JSObject> receiver_handle(receiver, this);
808 Handle<Object> key_handle(key, this);
809 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
810 LOG(this, ApiNamedSecurityCheck(key));
811 bool result = false;
812 {
813 // Leaving JavaScript.
814 VMState state(this, EXTERNAL);
815 result = callback(v8::Utils::ToLocal(receiver_handle),
816 v8::Utils::ToLocal(key_handle),
817 type,
818 v8::Utils::ToLocal(data));
819 }
820 return result;
821 }
822
823
824 bool Isolate::MayIndexedAccess(JSObject* receiver,
825 uint32_t index,
826 v8::AccessType type) {
827 ASSERT(receiver->IsAccessCheckNeeded());
828 // Check for compatibility between the security tokens in the
829 // current lexical context and the accessed object.
830 ASSERT(context());
831
832 MayAccessDecision decision = MayAccessPreCheck(this, receiver, type);
833 if (decision != UNKNOWN) return decision == YES;
834
835 // Get indexed access check callback
836 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
837 if (!constructor->shared()->IsApiFunction()) return false;
838
839 Object* data_obj =
840 constructor->shared()->get_api_func_data()->access_check_info();
841 if (data_obj == heap_.undefined_value()) return false;
842
843 Object* fun_obj = AccessCheckInfo::cast(data_obj)->indexed_callback();
844 v8::IndexedSecurityCallback callback =
845 v8::ToCData<v8::IndexedSecurityCallback>(fun_obj);
846
847 if (!callback) return false;
848
849 HandleScope scope(this);
850 Handle<JSObject> receiver_handle(receiver, this);
851 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
852 LOG(this, ApiIndexedSecurityCheck(index));
853 bool result = false;
854 {
855 // Leaving JavaScript.
856 VMState state(this, EXTERNAL);
857 result = callback(v8::Utils::ToLocal(receiver_handle),
858 index,
859 type,
860 v8::Utils::ToLocal(data));
861 }
862 return result;
863 }
864
865
866 const char* const Isolate::kStackOverflowMessage =
867 "Uncaught RangeError: Maximum call stack size exceeded";
868
869
870 Failure* Isolate::StackOverflow() {
871 HandleScope scope;
872 Handle<String> key = factory()->stack_overflow_symbol();
873 Handle<JSObject> boilerplate =
874 Handle<JSObject>::cast(GetProperty(js_builtins_object(), key));
875 Handle<Object> exception = Copy(boilerplate);
876 // TODO(1240995): To avoid having to call JavaScript code to compute
877 // the message for stack overflow exceptions which is very likely to
878 // double fault with another stack overflow exception, we use a
879 // precomputed message.
880 DoThrow(*exception, NULL);
881 return Failure::Exception();
882 }
883
884
885 Failure* Isolate::TerminateExecution() {
886 DoThrow(heap_.termination_exception(), NULL);
887 return Failure::Exception();
888 }
889
890
891 Failure* Isolate::Throw(Object* exception, MessageLocation* location) {
892 DoThrow(exception, location);
893 return Failure::Exception();
894 }
895
896
897 Failure* Isolate::ReThrow(MaybeObject* exception, MessageLocation* location) {
898 bool can_be_caught_externally = false;
899 ShouldReportException(&can_be_caught_externally,
900 is_catchable_by_javascript(exception));
901 thread_local_top()->catcher_ = can_be_caught_externally ?
902 try_catch_handler() : NULL;
903
904 // Set the exception being re-thrown.
905 set_pending_exception(exception);
906 return Failure::Exception();
907 }
908
909
910 Failure* Isolate::ThrowIllegalOperation() {
911 return Throw(heap_.illegal_access_symbol());
912 }
913
914
915 void Isolate::ScheduleThrow(Object* exception) {
916 // When scheduling a throw we first throw the exception to get the
917 // error reporting if it is uncaught before rescheduling it.
918 Throw(exception);
919 thread_local_top()->scheduled_exception_ = pending_exception();
920 thread_local_top()->external_caught_exception_ = false;
921 clear_pending_exception();
922 }
923
924
925 Failure* Isolate::PromoteScheduledException() {
926 MaybeObject* thrown = scheduled_exception();
927 clear_scheduled_exception();
928 // Re-throw the exception to avoid getting repeated error reporting.
929 return ReThrow(thrown);
930 }
931
932
933 void Isolate::PrintCurrentStackTrace(FILE* out) {
934 StackTraceFrameIterator it(this);
935 while (!it.done()) {
936 HandleScope scope;
937 // Find code position if recorded in relocation info.
938 JavaScriptFrame* frame = it.frame();
939 int pos = frame->LookupCode()->SourcePosition(frame->pc());
940 Handle<Object> pos_obj(Smi::FromInt(pos));
941 // Fetch function and receiver.
942 Handle<JSFunction> fun(JSFunction::cast(frame->function()));
943 Handle<Object> recv(frame->receiver());
944 // Advance to the next JavaScript frame and determine if the
945 // current frame is the top-level frame.
946 it.Advance();
947 Handle<Object> is_top_level = it.done()
948 ? factory()->true_value()
949 : factory()->false_value();
950 // Generate and print stack trace line.
951 Handle<String> line =
952 Execution::GetStackTraceLine(recv, fun, pos_obj, is_top_level);
953 if (line->length() > 0) {
954 line->PrintOn(out);
955 fprintf(out, "\n");
956 }
957 }
958 }
959
960
961 void Isolate::ComputeLocation(MessageLocation* target) {
962 *target = MessageLocation(Handle<Script>(heap_.empty_script()), -1, -1);
963 StackTraceFrameIterator it(this);
964 if (!it.done()) {
965 JavaScriptFrame* frame = it.frame();
966 JSFunction* fun = JSFunction::cast(frame->function());
967 Object* script = fun->shared()->script();
968 if (script->IsScript() &&
969 !(Script::cast(script)->source()->IsUndefined())) {
970 int pos = frame->LookupCode()->SourcePosition(frame->pc());
971 // Compute the location from the function and the reloc info.
972 Handle<Script> casted_script(Script::cast(script));
973 *target = MessageLocation(casted_script, pos, pos + 1);
974 }
975 }
976 }
977
978
979 bool Isolate::ShouldReportException(bool* can_be_caught_externally,
980 bool catchable_by_javascript) {
981 // Find the top-most try-catch handler.
982 StackHandler* handler =
983 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
984 while (handler != NULL && !handler->is_try_catch()) {
985 handler = handler->next();
986 }
987
988 // Get the address of the external handler so we can compare the address to
989 // determine which one is closer to the top of the stack.
990 Address external_handler_address =
991 thread_local_top()->try_catch_handler_address();
992
993 // The exception has been externally caught if and only if there is
994 // an external handler which is on top of the top-most try-catch
995 // handler.
996 *can_be_caught_externally = external_handler_address != NULL &&
997 (handler == NULL || handler->address() > external_handler_address ||
998 !catchable_by_javascript);
999
1000 if (*can_be_caught_externally) {
1001 // Only report the exception if the external handler is verbose.
1002 return try_catch_handler()->is_verbose_;
1003 } else {
1004 // Report the exception if it isn't caught by JavaScript code.
1005 return handler == NULL;
1006 }
1007 }
1008
1009
1010 void Isolate::DoThrow(MaybeObject* exception, MessageLocation* location) {
1011 ASSERT(!has_pending_exception());
1012
1013 HandleScope scope;
1014 Object* exception_object = Smi::FromInt(0);
1015 bool is_object = exception->ToObject(&exception_object);
1016 Handle<Object> exception_handle(exception_object);
1017
1018 // Determine reporting and whether the exception is caught externally.
1019 bool catchable_by_javascript = is_catchable_by_javascript(exception);
1020 // Only real objects can be caught by JS.
1021 ASSERT(!catchable_by_javascript || is_object);
1022 bool can_be_caught_externally = false;
1023 bool should_report_exception =
1024 ShouldReportException(&can_be_caught_externally, catchable_by_javascript);
1025 bool report_exception = catchable_by_javascript && should_report_exception;
1026
1027 #ifdef ENABLE_DEBUGGER_SUPPORT
1028 // Notify debugger of exception.
1029 if (catchable_by_javascript) {
1030 debugger_->OnException(exception_handle, report_exception);
1031 }
1032 #endif
1033
1034 // Generate the message.
1035 Handle<Object> message_obj;
1036 MessageLocation potential_computed_location;
1037 bool try_catch_needs_message =
1038 can_be_caught_externally &&
1039 try_catch_handler()->capture_message_;
1040 if (report_exception || try_catch_needs_message) {
1041 if (location == NULL) {
1042 // If no location was specified we use a computed one instead
1043 ComputeLocation(&potential_computed_location);
1044 location = &potential_computed_location;
1045 }
1046 if (!bootstrapper()->IsActive()) {
1047 // It's not safe to try to make message objects or collect stack
1048 // traces while the bootstrapper is active since the infrastructure
1049 // may not have been properly initialized.
1050 Handle<String> stack_trace;
1051 if (FLAG_trace_exception) stack_trace = StackTraceString();
1052 Handle<JSArray> stack_trace_object;
1053 if (report_exception && capture_stack_trace_for_uncaught_exceptions_) {
1054 stack_trace_object = CaptureCurrentStackTrace(
1055 stack_trace_for_uncaught_exceptions_frame_limit_,
1056 stack_trace_for_uncaught_exceptions_options_);
1057 }
1058 ASSERT(is_object); // Can't use the handle unless there's a real object.
1059 message_obj = MessageHandler::MakeMessageObject("uncaught_exception",
1060 location, HandleVector<Object>(&exception_handle, 1), stack_trace,
1061 stack_trace_object);
1062 }
1063 }
1064
1065 // Save the message for reporting if the the exception remains uncaught.
1066 thread_local_top()->has_pending_message_ = report_exception;
1067 if (!message_obj.is_null()) {
1068 thread_local_top()->pending_message_obj_ = *message_obj;
1069 if (location != NULL) {
1070 thread_local_top()->pending_message_script_ = *location->script();
1071 thread_local_top()->pending_message_start_pos_ = location->start_pos();
1072 thread_local_top()->pending_message_end_pos_ = location->end_pos();
1073 }
1074 }
1075
1076 // Do not forget to clean catcher_ if currently thrown exception cannot
1077 // be caught. If necessary, ReThrow will update the catcher.
1078 thread_local_top()->catcher_ = can_be_caught_externally ?
1079 try_catch_handler() : NULL;
1080
1081 // NOTE: Notifying the debugger or generating the message
1082 // may have caused new exceptions. For now, we just ignore
1083 // that and set the pending exception to the original one.
1084 if (is_object) {
1085 set_pending_exception(*exception_handle);
1086 } else {
1087 // Failures are not on the heap so they neither need nor work with handles.
1088 ASSERT(exception_handle->IsFailure());
1089 set_pending_exception(exception);
1090 }
1091 }
1092
1093
1094 bool Isolate::IsExternallyCaught() {
1095 ASSERT(has_pending_exception());
1096
1097 if ((thread_local_top()->catcher_ == NULL) ||
1098 (try_catch_handler() != thread_local_top()->catcher_)) {
1099 // When throwing the exception, we found no v8::TryCatch
1100 // which should care about this exception.
1101 return false;
1102 }
1103
1104 if (!is_catchable_by_javascript(pending_exception())) {
1105 return true;
1106 }
1107
1108 // Get the address of the external handler so we can compare the address to
1109 // determine which one is closer to the top of the stack.
1110 Address external_handler_address =
1111 thread_local_top()->try_catch_handler_address();
1112 ASSERT(external_handler_address != NULL);
1113
1114 // The exception has been externally caught if and only if there is
1115 // an external handler which is on top of the top-most try-finally
1116 // handler.
1117 // There should be no try-catch blocks as they would prohibit us from
1118 // finding external catcher in the first place (see catcher_ check above).
1119 //
1120 // Note, that finally clause would rethrow an exception unless it's
1121 // aborted by jumps in control flow like return, break, etc. and we'll
1122 // have another chances to set proper v8::TryCatch.
1123 StackHandler* handler =
1124 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
1125 while (handler != NULL && handler->address() < external_handler_address) {
1126 ASSERT(!handler->is_try_catch());
1127 if (handler->is_try_finally()) return false;
1128
1129 handler = handler->next();
1130 }
1131
1132 return true;
1133 }
1134
1135
1136 void Isolate::ReportPendingMessages() {
1137 ASSERT(has_pending_exception());
1138 PropagatePendingExceptionToExternalTryCatch();
1139
1140 // If the pending exception is OutOfMemoryException set out_of_memory in
1141 // the global context. Note: We have to mark the global context here
1142 // since the GenerateThrowOutOfMemory stub cannot make a RuntimeCall to
1143 // set it.
1144 HandleScope scope;
1145 if (thread_local_top_.pending_exception_ == Failure::OutOfMemoryException()) {
1146 context()->mark_out_of_memory();
1147 } else if (thread_local_top_.pending_exception_ ==
1148 heap()->termination_exception()) {
1149 // Do nothing: if needed, the exception has been already propagated to
1150 // v8::TryCatch.
1151 } else {
1152 if (thread_local_top_.has_pending_message_) {
1153 thread_local_top_.has_pending_message_ = false;
1154 if (!thread_local_top_.pending_message_obj_->IsTheHole()) {
1155 HandleScope scope;
1156 Handle<Object> message_obj(thread_local_top_.pending_message_obj_);
1157 if (thread_local_top_.pending_message_script_ != NULL) {
1158 Handle<Script> script(thread_local_top_.pending_message_script_);
1159 int start_pos = thread_local_top_.pending_message_start_pos_;
1160 int end_pos = thread_local_top_.pending_message_end_pos_;
1161 MessageLocation location(script, start_pos, end_pos);
1162 MessageHandler::ReportMessage(this, &location, message_obj);
1163 } else {
1164 MessageHandler::ReportMessage(this, NULL, message_obj);
1165 }
1166 }
1167 }
1168 }
1169 clear_pending_message();
1170 }
1171
1172
1173 void Isolate::TraceException(bool flag) {
1174 FLAG_trace_exception = flag; // TODO(isolates): This is an unfortunate use.
1175 }
1176
1177
1178 bool Isolate::OptionalRescheduleException(bool is_bottom_call) {
1179 ASSERT(has_pending_exception());
1180 PropagatePendingExceptionToExternalTryCatch();
1181
1182 // Allways reschedule out of memory exceptions.
1183 if (!is_out_of_memory()) {
1184 bool is_termination_exception =
1185 pending_exception() == heap_.termination_exception();
1186
1187 // Do not reschedule the exception if this is the bottom call.
1188 bool clear_exception = is_bottom_call;
1189
1190 if (is_termination_exception) {
1191 if (is_bottom_call) {
1192 thread_local_top()->external_caught_exception_ = false;
1193 clear_pending_exception();
1194 return false;
1195 }
1196 } else if (thread_local_top()->external_caught_exception_) {
1197 // If the exception is externally caught, clear it if there are no
1198 // JavaScript frames on the way to the C++ frame that has the
1199 // external handler.
1200 ASSERT(thread_local_top()->try_catch_handler_address() != NULL);
1201 Address external_handler_address =
1202 thread_local_top()->try_catch_handler_address();
1203 JavaScriptFrameIterator it;
1204 if (it.done() || (it.frame()->sp() > external_handler_address)) {
1205 clear_exception = true;
1206 }
1207 }
1208
1209 // Clear the exception if needed.
1210 if (clear_exception) {
1211 thread_local_top()->external_caught_exception_ = false;
1212 clear_pending_exception();
1213 return false;
1214 }
1215 }
1216
1217 // Reschedule the exception.
1218 thread_local_top()->scheduled_exception_ = pending_exception();
1219 clear_pending_exception();
1220 return true;
1221 }
1222
1223
1224 void Isolate::SetCaptureStackTraceForUncaughtExceptions(
1225 bool capture,
1226 int frame_limit,
1227 StackTrace::StackTraceOptions options) {
1228 capture_stack_trace_for_uncaught_exceptions_ = capture;
1229 stack_trace_for_uncaught_exceptions_frame_limit_ = frame_limit;
1230 stack_trace_for_uncaught_exceptions_options_ = options;
1231 }
1232
1233
1234 bool Isolate::is_out_of_memory() {
1235 if (has_pending_exception()) {
1236 MaybeObject* e = pending_exception();
1237 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1238 return true;
1239 }
1240 }
1241 if (has_scheduled_exception()) {
1242 MaybeObject* e = scheduled_exception();
1243 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1244 return true;
1245 }
1246 }
1247 return false;
1248 }
1249
1250
1251 Handle<Context> Isolate::global_context() {
1252 GlobalObject* global = thread_local_top()->context_->global();
1253 return Handle<Context>(global->global_context());
1254 }
1255
1256
1257 Handle<Context> Isolate::GetCallingGlobalContext() {
1258 JavaScriptFrameIterator it;
1259 #ifdef ENABLE_DEBUGGER_SUPPORT
1260 if (debug_->InDebugger()) {
1261 while (!it.done()) {
1262 JavaScriptFrame* frame = it.frame();
1263 Context* context = Context::cast(frame->context());
1264 if (context->global_context() == *debug_->debug_context()) {
1265 it.Advance();
1266 } else {
1267 break;
1268 }
1269 }
1270 }
1271 #endif // ENABLE_DEBUGGER_SUPPORT
1272 if (it.done()) return Handle<Context>::null();
1273 JavaScriptFrame* frame = it.frame();
1274 Context* context = Context::cast(frame->context());
1275 return Handle<Context>(context->global_context());
1276 }
1277
1278
1279 char* Isolate::ArchiveThread(char* to) {
1280 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1281 RuntimeProfiler::IsolateExitedJS(this);
1282 }
1283 memcpy(to, reinterpret_cast<char*>(thread_local_top()),
1284 sizeof(ThreadLocalTop));
1285 InitializeThreadLocal();
1286 return to + sizeof(ThreadLocalTop);
1287 }
1288
1289
1290 char* Isolate::RestoreThread(char* from) {
1291 memcpy(reinterpret_cast<char*>(thread_local_top()), from,
1292 sizeof(ThreadLocalTop));
1293 // This might be just paranoia, but it seems to be needed in case a
1294 // thread_local_top_ is restored on a separate OS thread.
1295 #ifdef USE_SIMULATOR
1296 #ifdef V8_TARGET_ARCH_ARM
1297 thread_local_top()->simulator_ = Simulator::current(this);
1298 #elif V8_TARGET_ARCH_MIPS
1299 thread_local_top()->simulator_ = Simulator::current(this);
1300 #endif
1301 #endif
1302 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1303 RuntimeProfiler::IsolateEnteredJS(this);
1304 }
1305 return from + sizeof(ThreadLocalTop);
1306 }
1307
1308
375 Isolate::ThreadDataTable::ThreadDataTable() 1309 Isolate::ThreadDataTable::ThreadDataTable()
376 : list_(NULL) { 1310 : list_(NULL) {
377 } 1311 }
378 1312
379 1313
380 Isolate::PerIsolateThreadData* 1314 Isolate::PerIsolateThreadData*
381 Isolate::ThreadDataTable::Lookup(Isolate* isolate, 1315 Isolate::ThreadDataTable::Lookup(Isolate* isolate,
382 ThreadId thread_id) { 1316 ThreadId thread_id) {
383 for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) { 1317 for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) {
384 if (data->Matches(isolate, thread_id)) return data; 1318 if (data->Matches(isolate, thread_id)) return data;
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 1857
924 #ifdef DEBUG 1858 #ifdef DEBUG
925 #define ISOLATE_FIELD_OFFSET(type, name, ignored) \ 1859 #define ISOLATE_FIELD_OFFSET(type, name, ignored) \
926 const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_); 1860 const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_);
927 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET) 1861 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
928 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET) 1862 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
929 #undef ISOLATE_FIELD_OFFSET 1863 #undef ISOLATE_FIELD_OFFSET
930 #endif 1864 #endif
931 1865
932 } } // namespace v8::internal 1866 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/flag-definitions.h ('k') | src/top.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698