From 53310ac152bd58c9d9c297084345f6163307436c Mon Sep 17 00:00:00 2001 From: "svenpanne@chromium.org" Date: Fri, 15 Mar 2013 12:06:53 +0000 Subject: [PATCH] Added a version of the v8::HandleScope constructor with an Isolate and use that consistently. I tried to limit the use of v8::Isolate::GetCurrent() and v8::internal::Isolate::Current() as much as possible, but sometimes this would have involved restructuring tests quite a bit, which is better left for a separate CL. BUG=v8:2487 Review URL: https://codereview.chromium.org/12716010 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13953 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- include/v8.h | 4 + samples/lineprocessor.cc | 38 +- samples/process.cc | 49 +- samples/shell.cc | 51 +- src/api.cc | 45 +- src/d8-debug.cc | 32 +- src/d8-debug.h | 8 +- src/d8-posix.cc | 2 +- src/d8-readline.cc | 2 +- src/d8.cc | 63 +- src/d8.h | 8 +- src/debug.cc | 3 +- src/factory.cc | 8 +- src/mksnapshot.cc | 5 +- test/cctest/test-accessors.cc | 46 +- test/cctest/test-alloc.cc | 4 +- test/cctest/test-api.cc | 980 +++++++++++++++--------------- test/cctest/test-assembler-arm.cc | 57 +- test/cctest/test-assembler-ia32.cc | 44 +- test/cctest/test-assembler-mips.cc | 80 ++- test/cctest/test-assembler-x64.cc | 5 +- test/cctest/test-compiler.cc | 28 +- test/cctest/test-cpu-profiler.cc | 6 +- test/cctest/test-date.cc | 6 +- test/cctest/test-debug.cc | 230 +++---- test/cctest/test-declarative-accessors.cc | 13 +- test/cctest/test-decls.cc | 28 +- test/cctest/test-deoptimization.cc | 32 +- test/cctest/test-dictionary.cc | 6 +- test/cctest/test-disasm-arm.cc | 7 +- test/cctest/test-disasm-ia32.cc | 12 +- test/cctest/test-disasm-mips.cc | 7 +- test/cctest/test-func-name-inference.cc | 44 +- test/cctest/test-global-object.cc | 2 +- test/cctest/test-heap-profiler.cc | 86 +-- test/cctest/test-heap.cc | 170 +++--- test/cctest/test-lockers.cc | 40 +- test/cctest/test-log-stack-tracer.cc | 10 +- test/cctest/test-log.cc | 4 +- test/cctest/test-mark-compact.cc | 11 +- test/cctest/test-object-observe.cc | 22 +- test/cctest/test-parsing.cc | 12 +- test/cctest/test-profile-generator.cc | 8 +- test/cctest/test-random.cc | 2 +- test/cctest/test-regexp.cc | 17 +- test/cctest/test-serialize.cc | 16 +- test/cctest/test-strings.cc | 27 +- test/cctest/test-symbols.cc | 2 - test/cctest/test-thread-termination.cc | 12 +- test/cctest/test-threads.cc | 6 +- test/cctest/test-weakmaps.cc | 18 +- 51 files changed, 1239 insertions(+), 1179 deletions(-) diff --git a/include/v8.h b/include/v8.h index 8fab761..1e4d2f9 100644 --- a/include/v8.h +++ b/include/v8.h @@ -514,8 +514,11 @@ template class Persistent : public Handle { */ class V8EXPORT HandleScope { public: + // TODO(svenpanne) Deprecate me when Chrome is fixed! HandleScope(); + HandleScope(Isolate* isolate); + ~HandleScope(); /** @@ -559,6 +562,7 @@ class V8EXPORT HandleScope { } }; + void Initialize(Isolate* isolate); void Leave(); internal::Isolate* isolate_; diff --git a/samples/lineprocessor.cc b/samples/lineprocessor.cc index 6549f4c..b5b6367 100644 --- a/samples/lineprocessor.cc +++ b/samples/lineprocessor.cc @@ -98,13 +98,14 @@ enum MainCycleType { }; const char* ToCString(const v8::String::Utf8Value& value); -void ReportException(v8::TryCatch* handler); +void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); v8::Handle ReadFile(const char* name); v8::Handle ReadLine(); v8::Handle Print(const v8::Arguments& args); v8::Handle ReadLine(const v8::Arguments& args); -bool RunCppCycle(v8::Handle script, v8::Local context, +bool RunCppCycle(v8::Handle script, + v8::Local context, bool report_exceptions); @@ -132,7 +133,8 @@ void DispatchDebugMessages() { int RunMain(int argc, char* argv[]) { v8::V8::SetFlagsFromCommandLine(&argc, argv, true); - v8::HandleScope handle_scope; + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::HandleScope handle_scope(isolate); v8::Handle script_source(NULL); v8::Handle script_name(NULL); @@ -213,9 +215,9 @@ int RunMain(int argc, char* argv[]) { #ifdef ENABLE_DEBUGGER_SUPPORT debug_message_context = - v8::Persistent::New(context->GetIsolate(), context); + v8::Persistent::New(isolate, context); - v8::Locker locker(context->GetIsolate()); + v8::Locker locker(isolate); if (support_callback) { v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); @@ -236,7 +238,7 @@ int RunMain(int argc, char* argv[]) { if (script.IsEmpty()) { // Print errors that happened during compilation. if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return 1; } } @@ -247,13 +249,14 @@ int RunMain(int argc, char* argv[]) { script->Run(); if (try_catch.HasCaught()) { if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return 1; } } if (cycle_type == CycleInCpp) { - bool res = RunCppCycle(script, v8::Context::GetCurrent(), + bool res = RunCppCycle(script, + v8::Context::GetCurrent(), report_exceptions); return !res; } else { @@ -263,15 +266,16 @@ int RunMain(int argc, char* argv[]) { } -bool RunCppCycle(v8::Handle script, v8::Local context, +bool RunCppCycle(v8::Handle script, + v8::Local context, bool report_exceptions) { + v8::Isolate* isolate = context->GetIsolate(); #ifdef ENABLE_DEBUGGER_SUPPORT - v8::Locker lock(v8::Isolate::GetCurrent()); + v8::Locker lock(isolate); #endif // ENABLE_DEBUGGER_SUPPORT v8::Handle fun_name = v8::String::New("ProcessLine"); - v8::Handle process_val = - v8::Context::GetCurrent()->Global()->Get(fun_name); + v8::Handle process_val = context->Global()->Get(fun_name); // If there is no Process function, or if it is not a function, // bail out @@ -286,7 +290,7 @@ bool RunCppCycle(v8::Handle script, v8::Local context, while (!feof(stdin)) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(isolate); v8::Handle input_line = ReadLine(); if (input_line == v8::Undefined()) { @@ -303,7 +307,7 @@ bool RunCppCycle(v8::Handle script, v8::Local context, argc, argv); if (try_catch.HasCaught()) { if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return false; } } @@ -350,8 +354,8 @@ v8::Handle ReadFile(const char* name) { } -void ReportException(v8::TryCatch* try_catch) { - v8::HandleScope handle_scope; +void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) { + v8::HandleScope handle_scope(isolate); v8::String::Utf8Value exception(try_catch->Exception()); const char* exception_string = ToCString(exception); v8::Handle message = try_catch->Message(); @@ -389,7 +393,7 @@ void ReportException(v8::TryCatch* try_catch) { v8::Handle Print(const v8::Arguments& args) { bool first = true; for (int i = 0; i < args.Length(); i++) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(args.GetIsolate()); if (first) { first = false; } else { diff --git a/samples/process.cc b/samples/process.cc index c3d1773..4dcc09a 100644 --- a/samples/process.cc +++ b/samples/process.cc @@ -79,7 +79,8 @@ class JsHttpRequestProcessor : public HttpRequestProcessor { public: // Creates a new processor that processes requests by invoking the // Process function of the JavaScript script given as an argument. - explicit JsHttpRequestProcessor(Handle script) : script_(script) { } + JsHttpRequestProcessor(Isolate* isolate, Handle script) + : isolate_(isolate), script_(script) { } virtual ~JsHttpRequestProcessor(); virtual bool Initialize(map* opts, @@ -97,8 +98,8 @@ class JsHttpRequestProcessor : public HttpRequestProcessor { // Constructs the template that describes the JavaScript wrapper // type for requests. - static Handle MakeRequestTemplate(); - static Handle MakeMapTemplate(); + static Handle MakeRequestTemplate(Isolate* isolate); + static Handle MakeMapTemplate(Isolate* isolate); // Callbacks that access the individual fields of request objects. static Handle GetPath(Local name, const AccessorInfo& info); @@ -121,8 +122,9 @@ class JsHttpRequestProcessor : public HttpRequestProcessor { Handle WrapRequest(HttpRequest* obj); static HttpRequest* UnwrapRequest(Handle obj); - Isolate* GetIsolate() { return context_->GetIsolate(); } + Isolate* GetIsolate() { return isolate_; } + Isolate* isolate_; Handle script_; Persistent context_; Persistent process_; @@ -136,12 +138,12 @@ class JsHttpRequestProcessor : public HttpRequestProcessor { static Handle LogCallback(const Arguments& args) { - if (args.Length() < 1) return v8::Undefined(); - HandleScope scope; + if (args.Length() < 1) return Undefined(); + HandleScope scope(args.GetIsolate()); Handle arg = args[0]; String::Utf8Value value(arg); HttpRequestProcessor::Log(*value); - return v8::Undefined(); + return Undefined(); } @@ -149,7 +151,7 @@ static Handle LogCallback(const Arguments& args) { bool JsHttpRequestProcessor::Initialize(map* opts, map* output) { // Create a handle scope to hold the temporary references. - HandleScope handle_scope; + HandleScope handle_scope(GetIsolate()); // Create a template for the global object where we set the // built-in global functions. @@ -197,7 +199,7 @@ bool JsHttpRequestProcessor::Initialize(map* opts, bool JsHttpRequestProcessor::ExecuteScript(Handle script) { - HandleScope handle_scope; + HandleScope handle_scope(GetIsolate()); // We're just about to compile the script; set up an error handler to // catch any exceptions the script might throw. @@ -227,7 +229,7 @@ bool JsHttpRequestProcessor::ExecuteScript(Handle script) { bool JsHttpRequestProcessor::InstallMaps(map* opts, map* output) { - HandleScope handle_scope; + HandleScope handle_scope(GetIsolate()); // Wrap the map object in a JavaScript wrapper Handle opts_obj = WrapMap(opts); @@ -244,7 +246,7 @@ bool JsHttpRequestProcessor::InstallMaps(map* opts, bool JsHttpRequestProcessor::Process(HttpRequest* request) { // Create a handle scope to keep the temporary object references. - HandleScope handle_scope; + HandleScope handle_scope(GetIsolate()); // Enter this processor's context so all the remaining operations // take place there @@ -275,7 +277,7 @@ JsHttpRequestProcessor::~JsHttpRequestProcessor() { // Dispose the persistent handles. When noone else has any // references to the objects stored in the handles they will be // automatically reclaimed. - v8::Isolate* isolate = GetIsolate(); + Isolate* isolate = GetIsolate(); context_.Dispose(isolate); process_.Dispose(isolate); } @@ -293,12 +295,12 @@ Persistent JsHttpRequestProcessor::map_template_; // JavaScript object. Handle JsHttpRequestProcessor::WrapMap(map* obj) { // Handle scope for temporary handles. - HandleScope handle_scope; + HandleScope handle_scope(GetIsolate()); // Fetch the template for creating JavaScript map wrappers. // It only has to be created once, which we do on demand. if (map_template_.IsEmpty()) { - Handle raw_template = MakeMapTemplate(); + Handle raw_template = MakeMapTemplate(GetIsolate()); map_template_ = Persistent::New(GetIsolate(), raw_template); } Handle templ = map_template_; @@ -376,8 +378,9 @@ Handle JsHttpRequestProcessor::MapSet(Local name, } -Handle JsHttpRequestProcessor::MakeMapTemplate() { - HandleScope handle_scope; +Handle JsHttpRequestProcessor::MakeMapTemplate( + Isolate* isolate) { + HandleScope handle_scope(isolate); Handle result = ObjectTemplate::New(); result->SetInternalFieldCount(1); @@ -398,12 +401,12 @@ Handle JsHttpRequestProcessor::MakeMapTemplate() { */ Handle JsHttpRequestProcessor::WrapRequest(HttpRequest* request) { // Handle scope for temporary handles. - HandleScope handle_scope; + HandleScope handle_scope(GetIsolate()); // Fetch the template for creating JavaScript http request wrappers. // It only has to be created once, which we do on demand. if (request_template_.IsEmpty()) { - Handle raw_template = MakeRequestTemplate(); + Handle raw_template = MakeRequestTemplate(GetIsolate()); request_template_ = Persistent::New(GetIsolate(), raw_template); } @@ -475,8 +478,9 @@ Handle JsHttpRequestProcessor::GetUserAgent(Local name, } -Handle JsHttpRequestProcessor::MakeRequestTemplate() { - HandleScope handle_scope; +Handle JsHttpRequestProcessor::MakeRequestTemplate( + Isolate* isolate) { + HandleScope handle_scope(isolate); Handle result = ObjectTemplate::New(); result->SetInternalFieldCount(1); @@ -608,13 +612,14 @@ int main(int argc, char* argv[]) { fprintf(stderr, "No script was specified.\n"); return 1; } - HandleScope scope; + Isolate* isolate = Isolate::GetCurrent(); + HandleScope scope(isolate); Handle source = ReadFile(file); if (source.IsEmpty()) { fprintf(stderr, "Error reading '%s'.\n", file.c_str()); return 1; } - JsHttpRequestProcessor processor(source); + JsHttpRequestProcessor processor(isolate, source); map output; if (!processor.Initialize(&options, &output)) { fprintf(stderr, "Error initializing processor.\n"); diff --git a/samples/shell.cc b/samples/shell.cc index e9057f9..0b71c2c 100644 --- a/samples/shell.cc +++ b/samples/shell.cc @@ -47,8 +47,9 @@ v8::Persistent CreateShellContext(); void RunShell(v8::Handle context); -int RunMain(int argc, char* argv[]); -bool ExecuteString(v8::Handle source, +int RunMain(v8::Isolate* isolate, int argc, char* argv[]); +bool ExecuteString(v8::Isolate* isolate, + v8::Handle source, v8::Handle name, bool print_result, bool report_exceptions); @@ -58,7 +59,7 @@ v8::Handle Load(const v8::Arguments& args); v8::Handle Quit(const v8::Arguments& args); v8::Handle Version(const v8::Arguments& args); v8::Handle ReadFile(const char* name); -void ReportException(v8::TryCatch* handler); +void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); static bool run_shell; @@ -66,20 +67,21 @@ static bool run_shell; int main(int argc, char* argv[]) { v8::V8::SetFlagsFromCommandLine(&argc, argv, true); + v8::Isolate* isolate = v8::Isolate::GetCurrent(); run_shell = (argc == 1); int result; { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(isolate); v8::Persistent context = CreateShellContext(); if (context.IsEmpty()) { fprintf(stderr, "Error creating context\n"); return 1; } context->Enter(); - result = RunMain(argc, argv); + result = RunMain(isolate, argc, argv); if (run_shell) RunShell(context); context->Exit(); - context.Dispose(context->GetIsolate()); + context.Dispose(isolate); } v8::V8::Dispose(); return result; @@ -118,7 +120,7 @@ v8::Persistent CreateShellContext() { v8::Handle Print(const v8::Arguments& args) { bool first = true; for (int i = 0; i < args.Length(); i++) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(args.GetIsolate()); if (first) { first = false; } else { @@ -158,7 +160,7 @@ v8::Handle Read(const v8::Arguments& args) { // JavaScript file. v8::Handle Load(const v8::Arguments& args) { for (int i = 0; i < args.Length(); i++) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(args.GetIsolate()); v8::String::Utf8Value file(args[i]); if (*file == NULL) { return v8::ThrowException(v8::String::New("Error loading file")); @@ -167,7 +169,11 @@ v8::Handle Load(const v8::Arguments& args) { if (source.IsEmpty()) { return v8::ThrowException(v8::String::New("Error loading file")); } - if (!ExecuteString(source, v8::String::New(*file), false, false)) { + if (!ExecuteString(args.GetIsolate(), + source, + v8::String::New(*file), + false, + false)) { return v8::ThrowException(v8::String::New("Error executing file")); } } @@ -216,7 +222,7 @@ v8::Handle ReadFile(const char* name) { // Process remaining command line arguments and execute files -int RunMain(int argc, char* argv[]) { +int RunMain(v8::Isolate* isolate, int argc, char* argv[]) { for (int i = 1; i < argc; i++) { const char* str = argv[i]; if (strcmp(str, "--shell") == 0) { @@ -232,7 +238,7 @@ int RunMain(int argc, char* argv[]) { // Execute argument given to -e option directly. v8::Handle file_name = v8::String::New("unnamed"); v8::Handle source = v8::String::New(argv[++i]); - if (!ExecuteString(source, file_name, false, true)) return 1; + if (!ExecuteString(isolate, source, file_name, false, true)) return 1; } else { // Use all other arguments as names of files to load and run. v8::Handle file_name = v8::String::New(str); @@ -241,7 +247,7 @@ int RunMain(int argc, char* argv[]) { fprintf(stderr, "Error reading '%s'\n", str); continue; } - if (!ExecuteString(source, file_name, false, true)) return 1; + if (!ExecuteString(isolate, source, file_name, false, true)) return 1; } } return 0; @@ -260,25 +266,30 @@ void RunShell(v8::Handle context) { fprintf(stderr, "> "); char* str = fgets(buffer, kBufferSize, stdin); if (str == NULL) break; - v8::HandleScope handle_scope; - ExecuteString(v8::String::New(str), name, true, true); + v8::HandleScope handle_scope(context->GetIsolate()); + ExecuteString(context->GetIsolate(), + v8::String::New(str), + name, + true, + true); } fprintf(stderr, "\n"); } // Executes a string within the current v8 context. -bool ExecuteString(v8::Handle source, +bool ExecuteString(v8::Isolate* isolate, + v8::Handle source, v8::Handle name, bool print_result, bool report_exceptions) { - v8::HandleScope handle_scope; + v8::HandleScope handle_scope(isolate); v8::TryCatch try_catch; v8::Handle script = v8::Script::Compile(source, name); if (script.IsEmpty()) { // Print errors that happened during compilation. if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return false; } else { v8::Handle result = script->Run(); @@ -286,7 +297,7 @@ bool ExecuteString(v8::Handle source, assert(try_catch.HasCaught()); // Print errors that happened during execution. if (report_exceptions) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return false; } else { assert(!try_catch.HasCaught()); @@ -303,8 +314,8 @@ bool ExecuteString(v8::Handle source, } -void ReportException(v8::TryCatch* try_catch) { - v8::HandleScope handle_scope; +void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) { + v8::HandleScope handle_scope(isolate); v8::String::Utf8Value exception(try_catch->Exception()); const char* exception_string = ToCString(exception); v8::Handle message = try_catch->Message(); diff --git a/src/api.cc b/src/api.cc index faba6de..47928a6 100644 --- a/src/api.cc +++ b/src/api.cc @@ -666,11 +666,21 @@ void V8::DisposeGlobal(i::Isolate* isolate, i::Object** obj) { HandleScope::HandleScope() { - i::Isolate* isolate = i::Isolate::Current(); - API_ENTRY_CHECK(isolate, "HandleScope::HandleScope"); + Initialize(reinterpret_cast(i::Isolate::Current())); +} + + +HandleScope::HandleScope(Isolate* isolate) { + Initialize(isolate); +} + + +void HandleScope::Initialize(Isolate* isolate) { + i::Isolate* internal_isolate = reinterpret_cast(isolate); + API_ENTRY_CHECK(internal_isolate, "HandleScope::HandleScope"); v8::ImplementationUtilities::HandleScopeData* current = - isolate->handle_scope_data(); - isolate_ = isolate; + internal_isolate->handle_scope_data(); + isolate_ = internal_isolate; prev_next_ = current->next; prev_limit_ = current->limit; is_closed_ = false; @@ -686,7 +696,6 @@ HandleScope::~HandleScope() { void HandleScope::Leave() { - ASSERT(isolate_ == i::Isolate::Current()); v8::ImplementationUtilities::HandleScopeData* current = isolate_->handle_scope_data(); current->level--; @@ -1884,7 +1893,7 @@ v8::TryCatch::TryCatch() v8::TryCatch::~TryCatch() { ASSERT(isolate_ == i::Isolate::Current()); if (rethrow_) { - v8::HandleScope scope; + v8::HandleScope scope(reinterpret_cast(isolate_)); v8::Local exc = v8::Local::New(Exception()); isolate_->UnregisterTryCatchHandler(this); v8::ThrowException(exc); @@ -1976,7 +1985,7 @@ Local Message::Get() const { i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); ON_BAILOUT(isolate, "v8::Message::Get()", return Local()); ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle obj = Utils::OpenHandle(this); i::Handle raw_result = i::MessageHandler::GetMessage(isolate, obj); Local result = Utils::ToLocal(raw_result); @@ -1990,7 +1999,7 @@ v8::Handle Message::GetScriptResourceName() const { return Local(); } ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle message = i::Handle::cast(Utils::OpenHandle(this)); // Return this.script.name. @@ -2009,7 +2018,7 @@ v8::Handle Message::GetScriptData() const { return Local(); } ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle message = i::Handle::cast(Utils::OpenHandle(this)); // Return this.script.data. @@ -2027,7 +2036,7 @@ v8::Handle Message::GetStackTrace() const { return Local(); } ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle message = i::Handle::cast(Utils::OpenHandle(this)); i::Handle stackFramesObj(message->stack_frames(), isolate); @@ -2147,7 +2156,7 @@ Local Message::GetSourceLine() const { i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); ON_BAILOUT(isolate, "v8::Message::GetSourceLine()", return Local()); ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); EXCEPTION_PREAMBLE(isolate); i::Handle result = CallV8HeapFunction("GetSourceLine", Utils::OpenHandle(this), @@ -2177,7 +2186,7 @@ Local StackTrace::GetFrame(uint32_t index) const { return Local(); } ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle self = Utils::OpenHandle(this); i::Object* raw_object = self->GetElementNoExceptionThrown(index); i::Handle obj(i::JSObject::cast(raw_object)); @@ -2254,7 +2263,7 @@ Local StackFrame::GetScriptName() const { return Local(); } ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle self = Utils::OpenHandle(this); i::Handle name = GetProperty(self, "scriptName"); if (!name->IsString()) { @@ -2270,7 +2279,7 @@ Local StackFrame::GetScriptNameOrSourceURL() const { return Local(); } ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle self = Utils::OpenHandle(this); i::Handle name = GetProperty(self, "scriptNameOrSourceURL"); if (!name->IsString()) { @@ -2286,7 +2295,7 @@ Local StackFrame::GetFunctionName() const { return Local(); } ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle self = Utils::OpenHandle(this); i::Handle name = GetProperty(self, "functionName"); if (!name->IsString()) { @@ -3280,7 +3289,7 @@ bool v8::Object::Delete(uint32_t index) { ON_BAILOUT(isolate, "v8::Object::DeleteProperty()", return false); ENTER_V8(isolate); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle self = Utils::OpenHandle(this); return i::JSObject::DeleteElement(self, index)->IsTrue(); } @@ -3858,7 +3867,7 @@ Local Function::NewInstance(int argc, ENTER_V8(isolate); i::Logger::TimerEventScope timer_scope( isolate, i::Logger::TimerEventScope::v8_execute); - HandleScope scope; + HandleScope scope(reinterpret_cast(isolate)); i::Handle function = Utils::OpenHandle(this); STATIC_ASSERT(sizeof(v8::Handle) == sizeof(i::Object**)); i::Handle* args = reinterpret_cast*>(argv); @@ -6318,7 +6327,7 @@ Local Debug::GetMirror(v8::Handle obj) { if (!isolate->IsInitialized()) return Local(); ON_BAILOUT(isolate, "v8::Debug::GetMirror()", return Local()); ENTER_V8(isolate); - v8::HandleScope scope; + v8::HandleScope scope(reinterpret_cast(isolate)); i::Debug* isolate_debug = isolate->debug(); isolate_debug->Load(); i::Handle debug(isolate_debug->debug_context()->global_object()); diff --git a/src/d8-debug.cc b/src/d8-debug.cc index f044328..a20de43 100644 --- a/src/d8-debug.cc +++ b/src/d8-debug.cc @@ -54,7 +54,9 @@ void HandleDebugEvent(DebugEvent event, Handle exec_state, Handle event_data, Handle data) { - HandleScope scope; + // TODO(svenpanne) There should be a way to retrieve this in the callback. + Isolate* isolate = Isolate::GetCurrent(); + HandleScope scope(isolate); // Check for handled event. if (event != Break && event != Exception && event != AfterCompile) { @@ -69,7 +71,7 @@ void HandleDebugEvent(DebugEvent event, Function::Cast(*event_data->Get(to_json_fun_name)); Local event_json = to_json_fun->Call(event_data, 0, NULL); if (try_catch.HasCaught()) { - Shell::ReportException(&try_catch); + Shell::ReportException(isolate, &try_catch); return; } @@ -77,7 +79,7 @@ void HandleDebugEvent(DebugEvent event, Handle details = Shell::DebugMessageDetails(Handle::Cast(event_json)); if (try_catch.HasCaught()) { - Shell::ReportException(&try_catch); + Shell::ReportException(isolate, &try_catch); return; } String::Utf8Value str(details->Get(String::New("text"))); @@ -93,7 +95,7 @@ void HandleDebugEvent(DebugEvent event, Local cmd_processor = Object::Cast(*fun->Call(exec_state, 0, NULL)); if (try_catch.HasCaught()) { - Shell::ReportException(&try_catch); + Shell::ReportException(isolate, &try_catch); return; } @@ -114,7 +116,7 @@ void HandleDebugEvent(DebugEvent event, Handle request = Shell::DebugCommandToJSONRequest(String::New(command)); if (try_catch.HasCaught()) { - Shell::ReportException(&try_catch); + Shell::ReportException(isolate, &try_catch); continue; } @@ -138,7 +140,7 @@ void HandleDebugEvent(DebugEvent event, args[0] = request; Handle response_val = fun->Call(cmd_processor, kArgc, args); if (try_catch.HasCaught()) { - Shell::ReportException(&try_catch); + Shell::ReportException(isolate, &try_catch); continue; } Handle response = Handle::Cast(response_val); @@ -146,7 +148,7 @@ void HandleDebugEvent(DebugEvent event, // Convert the debugger response into text details and the running state. Handle response_details = Shell::DebugMessageDetails(response); if (try_catch.HasCaught()) { - Shell::ReportException(&try_catch); + Shell::ReportException(isolate, &try_catch); continue; } String::Utf8Value text_str(response_details->Get(String::New("text"))); @@ -159,8 +161,8 @@ void HandleDebugEvent(DebugEvent event, } -void RunRemoteDebugger(int port) { - RemoteDebugger debugger(port); +void RunRemoteDebugger(Isolate* isolate, int port) { + RemoteDebugger debugger(isolate, port); debugger.Run(); } @@ -273,15 +275,15 @@ RemoteDebuggerEvent* RemoteDebugger::GetEvent() { void RemoteDebugger::HandleMessageReceived(char* message) { - Locker lock(v8::Isolate::GetCurrent()); - HandleScope scope; + Locker lock(isolate_); + HandleScope scope(isolate_); // Print the event details. TryCatch try_catch; Handle details = Shell::DebugMessageDetails(Handle::Cast(String::New(message))); if (try_catch.HasCaught()) { - Shell::ReportException(&try_catch); + Shell::ReportException(isolate_, &try_catch); PrintPrompt(); return; } @@ -302,15 +304,15 @@ void RemoteDebugger::HandleMessageReceived(char* message) { void RemoteDebugger::HandleKeyboardCommand(char* command) { - Locker lock(v8::Isolate::GetCurrent()); - HandleScope scope; + Locker lock(isolate_); + HandleScope scope(isolate_); // Convert the debugger command to a JSON debugger request. TryCatch try_catch; Handle request = Shell::DebugCommandToJSONRequest(String::New(command)); if (try_catch.HasCaught()) { - Shell::ReportException(&try_catch); + Shell::ReportException(isolate_, &try_catch); PrintPrompt(); return; } diff --git a/src/d8-debug.h b/src/d8-debug.h index aeff3c1..a6cea2a 100644 --- a/src/d8-debug.h +++ b/src/d8-debug.h @@ -43,7 +43,7 @@ void HandleDebugEvent(DebugEvent event, // Start the remove debugger connecting to a V8 debugger agent on the specified // port. -void RunRemoteDebugger(int port); +void RunRemoteDebugger(Isolate* isolate, int port); // Forward declerations. class RemoteDebuggerEvent; @@ -53,8 +53,9 @@ class ReceiverThread; // Remote debugging class. class RemoteDebugger { public: - explicit RemoteDebugger(int port) - : port_(port), + explicit RemoteDebugger(Isolate* isolate, int port) + : isolate_(isolate), + port_(port), event_access_(i::OS::CreateMutex()), event_available_(i::OS::CreateSemaphore(0)), head_(NULL), tail_(NULL) {} @@ -79,6 +80,7 @@ class RemoteDebugger { // Get connection to agent in debugged V8. i::Socket* conn() { return conn_; } + Isolate* isolate_; int port_; // Port used to connect to debugger V8. i::Socket* conn_; // Connection to debugger agent in debugged V8. diff --git a/src/d8-posix.cc b/src/d8-posix.cc index 8a278e4..1be782a 100644 --- a/src/d8-posix.cc +++ b/src/d8-posix.cc @@ -449,7 +449,7 @@ static bool WaitForChild(int pid, // Implementation of the system() function (see d8.h for details). Handle Shell::System(const Arguments& args) { - HandleScope scope; + HandleScope scope(args.GetIsolate()); int read_timeout = -1; int total_timeout = -1; if (!GetTimeouts(args, &read_timeout, &total_timeout)) return v8::Undefined(); diff --git a/src/d8-readline.cc b/src/d8-readline.cc index cc7a3a6..5226364 100644 --- a/src/d8-readline.cc +++ b/src/d8-readline.cc @@ -154,7 +154,7 @@ char* ReadLineEditor::CompletionGenerator(const char* text, int state) { HandleScope scope; Local full_text = String::New(rl_line_buffer, rl_point); Handle completions = - Shell::GetCompletions(String::New(text), full_text); + Shell::GetCompletions(isolate, String::New(text), full_text); current_completions = Persistent::New(isolate, completions); current_index = 0; } diff --git a/src/d8.cc b/src/d8.cc index 5010acb..eaebd90 100644 --- a/src/d8.cc +++ b/src/d8.cc @@ -93,7 +93,7 @@ const char kArrayMarkerPropName[] = "d8::_is_typed_array_"; class Symbols { public: explicit Symbols(Isolate* isolate) : isolate_(isolate) { - HandleScope scope; + HandleScope scope(isolate); #define INIT_SYMBOL(name, value) \ name##_ = Persistent::New(isolate, String::NewSymbol(value)); FOR_EACH_SYMBOL(INIT_SYMBOL) @@ -181,7 +181,8 @@ const char* Shell::ToCString(const v8::String::Utf8Value& value) { // Executes a string within the current v8 context. -bool Shell::ExecuteString(Handle source, +bool Shell::ExecuteString(Isolate* isolate, + Handle source, Handle name, bool print_result, bool report_exceptions) { @@ -190,7 +191,7 @@ bool Shell::ExecuteString(Handle source, #else bool FLAG_debugger = false; #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT - HandleScope handle_scope; + HandleScope handle_scope(isolate); TryCatch try_catch; options.script_executed = true; if (FLAG_debugger) { @@ -201,7 +202,7 @@ bool Shell::ExecuteString(Handle source, if (script.IsEmpty()) { // Print errors that happened during compilation. if (report_exceptions && !FLAG_debugger) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return false; } else { Handle result = script->Run(); @@ -209,7 +210,7 @@ bool Shell::ExecuteString(Handle source, ASSERT(try_catch.HasCaught()); // Print errors that happened during execution. if (report_exceptions && !FLAG_debugger) - ReportException(&try_catch); + ReportException(isolate, &try_catch); return false; } else { ASSERT(!try_catch.HasCaught()); @@ -237,7 +238,7 @@ Handle Shell::Print(const Arguments& args) { Handle Shell::Write(const Arguments& args) { for (int i = 0; i < args.Length(); i++) { - HandleScope handle_scope; + HandleScope handle_scope(args.GetIsolate()); if (i != 0) { printf(" "); } @@ -315,7 +316,7 @@ Handle Shell::ReadFromStdin(Isolate* isolate) { Handle Shell::Load(const Arguments& args) { for (int i = 0; i < args.Length(); i++) { - HandleScope handle_scope; + HandleScope handle_scope(args.GetIsolate()); String::Utf8Value file(args[i]); if (*file == NULL) { return Throw("Error loading file"); @@ -324,7 +325,11 @@ Handle Shell::Load(const Arguments& args) { if (source.IsEmpty()) { return Throw("Error loading file"); } - if (!ExecuteString(source, String::New(*file), false, true)) { + if (!ExecuteString(args.GetIsolate(), + source, + String::New(*file), + false, + true)) { return Throw("Error executing file"); } } @@ -827,7 +832,7 @@ Handle Shell::ArraySet(const Arguments& args) { void Shell::ExternalArrayWeakCallback(v8::Isolate* isolate, Persistent object, void* data) { - HandleScope scope; + HandleScope scope(isolate); int32_t length = object->ToObject()->Get(Symbols::byteLength(isolate))->Uint32Value(); isolate->AdjustAmountOfExternalAllocatedMemory(-length); @@ -903,8 +908,8 @@ Handle Shell::Version(const Arguments& args) { } -void Shell::ReportException(v8::TryCatch* try_catch) { - HandleScope handle_scope; +void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { + HandleScope handle_scope(isolate); #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) bool enter_context = !Context::InContext(); if (enter_context) utility_context_->Enter(); @@ -950,8 +955,10 @@ void Shell::ReportException(v8::TryCatch* try_catch) { #ifndef V8_SHARED -Handle Shell::GetCompletions(Handle text, Handle full) { - HandleScope handle_scope; +Handle Shell::GetCompletions(Isolate* isolate, + Handle text, + Handle full) { + HandleScope handle_scope(isolate); Context::Scope context_scope(utility_context_); Handle global = utility_context_->Global(); Handle fun = global->Get(String::New("GetCompletions")); @@ -1094,7 +1101,7 @@ void Shell::AddHistogramSample(void* histogram, int sample) { void Shell::InstallUtilityScript(Isolate* isolate) { Locker lock(isolate); - HandleScope scope; + HandleScope scope(isolate); // If we use the utility context, we have to set the security tokens so that // utility, evaluation and debug context can all access each other. utility_context_->SetSecurityToken(Undefined(isolate)); @@ -1272,7 +1279,7 @@ void Shell::InitializeDebugger(Isolate* isolate) { if (options.test_shell) return; #ifndef V8_SHARED Locker lock(isolate); - HandleScope scope; + HandleScope scope(isolate); Handle global_template = CreateGlobalTemplate(isolate); utility_context_ = Context::New(NULL, global_template); @@ -1493,16 +1500,16 @@ Handle Shell::ReadFile(Isolate* isolate, const char* name) { void Shell::RunShell(Isolate* isolate) { Locker locker(isolate); Context::Scope context_scope(evaluation_context_); - HandleScope outer_scope; + HandleScope outer_scope(isolate); Handle name = String::New("(d8)"); LineEditor* console = LineEditor::Get(); printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); console->Open(isolate); while (true) { - HandleScope inner_scope; + HandleScope inner_scope(isolate); Handle input = console->Prompt(Shell::kPrompt); if (input.IsEmpty()) break; - ExecuteString(input, name, true, true); + ExecuteString(isolate, input, name, true, true); } printf("\n"); } @@ -1541,13 +1548,13 @@ void ShellThread::Run() { // Prepare the context for this thread. Locker locker(isolate_); - HandleScope outer_scope; + HandleScope outer_scope(isolate_); Persistent thread_context = Shell::CreateEvaluationContext(isolate_); Context::Scope context_scope(thread_context); while ((ptr != NULL) && (*ptr != '\0')) { - HandleScope inner_scope; + HandleScope inner_scope(isolate_); char* filename = ptr; ptr = ReadWord(ptr); @@ -1562,7 +1569,7 @@ void ShellThread::Run() { Shell::Exit(1); } - Shell::ExecuteString(str, String::New(filename), false, false); + Shell::ExecuteString(isolate_, str, String::New(filename), false, false); } thread_context.Dispose(thread_context->GetIsolate()); @@ -1589,10 +1596,10 @@ void SourceGroup::Execute(Isolate* isolate) { const char* arg = argv_[i]; if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { // Execute argument given to -e option directly. - HandleScope handle_scope; + HandleScope handle_scope(isolate); Handle file_name = String::New("unnamed"); Handle source = String::New(argv_[i + 1]); - if (!Shell::ExecuteString(source, file_name, false, true)) { + if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { Shell::Exit(1); } ++i; @@ -1600,14 +1607,14 @@ void SourceGroup::Execute(Isolate* isolate) { // Ignore other options. They have been parsed already. } else { // Use all other arguments as names of files to load and run. - HandleScope handle_scope; + HandleScope handle_scope(isolate); Handle file_name = String::New(arg); Handle source = ReadFile(isolate, arg); if (source.IsEmpty()) { printf("Error reading '%s'\n", arg); Shell::Exit(1); } - if (!Shell::ExecuteString(source, file_name, false, true)) { + if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { Shell::Exit(1); } } @@ -1642,7 +1649,7 @@ void SourceGroup::ExecuteInThread() { { Isolate::Scope iscope(isolate); Locker lock(isolate); - HandleScope scope; + HandleScope scope(isolate); Symbols symbols(isolate); Persistent context = Shell::CreateEvaluationContext(isolate); { @@ -1840,7 +1847,7 @@ int Shell::RunMain(Isolate* isolate, int argc, char* argv[]) { #endif // V8_SHARED { // NOLINT Locker lock(isolate); - HandleScope scope; + HandleScope scope(isolate); Persistent context = CreateEvaluationContext(isolate); if (options.last_run) { // Keep using the same context in the interactive shell. @@ -1936,7 +1943,7 @@ int Shell::Main(int argc, char* argv[]) { // Run remote debugger if requested, but never on --test if (i::FLAG_remote_debugger && !options.test_shell) { InstallUtilityScript(isolate); - RunRemoteDebugger(i::FLAG_debugger_port); + RunRemoteDebugger(isolate, i::FLAG_debugger_port); return 0; } #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT diff --git a/src/d8.h b/src/d8.h index 04c5454..621ac74 100644 --- a/src/d8.h +++ b/src/d8.h @@ -265,12 +265,13 @@ class Shell : public i::AllStatic { #endif // V8_SHARED public: - static bool ExecuteString(Handle source, + static bool ExecuteString(Isolate* isolate, + Handle source, Handle name, bool print_result, bool report_exceptions); static const char* ToCString(const v8::String::Utf8Value& value); - static void ReportException(TryCatch* try_catch); + static void ReportException(Isolate* isolate, TryCatch* try_catch); static Handle ReadFile(Isolate* isolate, const char* name); static Persistent CreateEvaluationContext(Isolate* isolate); static int RunMain(Isolate* isolate, int argc, char* argv[]); @@ -279,7 +280,8 @@ class Shell : public i::AllStatic { static void OnExit(); #ifndef V8_SHARED - static Handle GetCompletions(Handle text, + static Handle GetCompletions(Isolate* isolate, + Handle text, Handle full); static int* LookupCounter(const char* name); static void* CreateHistogram(const char* name, diff --git a/src/debug.cc b/src/debug.cc index 2821578..1fd3e40 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -3567,7 +3567,8 @@ v8::Handle MessageImpl::GetEventData() const { v8::Handle MessageImpl::GetJSON() const { - v8::HandleScope scope; + v8::HandleScope scope( + reinterpret_cast(event_data_->GetIsolate())); if (IsEvent()) { // Call toJSONProtocol on the debug event object. diff --git a/src/factory.cc b/src/factory.cc index 9135d54..fece9a0 100644 --- a/src/factory.cc +++ b/src/factory.cc @@ -703,9 +703,11 @@ Handle Factory::NewReferenceError(Handle message) { } -Handle Factory::NewError(const char* maker, const char* type, - Vector< Handle > args) { - v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom. +Handle Factory::NewError(const char* maker, + const char* type, + Vector< Handle > args) { + // Instantiate a closeable HandleScope for EscapeFrom. + v8::HandleScope scope(reinterpret_cast(isolate())); Handle array = NewFixedArray(args.length()); for (int i = 0; i < args.length(); i++) { array->set(i, *args[i]); diff --git a/src/mksnapshot.cc b/src/mksnapshot.cc index a3665e9..abfe693 100644 --- a/src/mksnapshot.cc +++ b/src/mksnapshot.cc @@ -318,11 +318,12 @@ int main(int argc, char** argv) { "\nException thrown while compiling natives - see above.\n\n"); exit(1); } + Isolate* isolate = context->GetIsolate(); if (i::FLAG_extra_code != NULL) { context->Enter(); // Capture 100 frames if anything happens. V8::SetCaptureStackTraceForUncaughtExceptions(true, 100); - HandleScope scope; + HandleScope scope(isolate); const char* name = i::FLAG_extra_code; FILE* file = i::OS::FOpen(name, "rb"); if (file == NULL) { @@ -375,7 +376,7 @@ int main(int argc, char** argv) { context->Exit(); } // Make sure all builtin scripts are cached. - { HandleScope scope; + { HandleScope scope(isolate); for (int i = 0; i < i::Natives::GetBuiltinsCount(); i++) { i::Isolate::Current()->bootstrapper()->NativesSourceLookup(i); } diff --git a/test/cctest/test-accessors.cc b/test/cctest/test-accessors.cc index 0ff747c..2335d10 100644 --- a/test/cctest/test-accessors.cc +++ b/test/cctest/test-accessors.cc @@ -52,10 +52,10 @@ static v8::Handle handle_property(Local name, THREADED_TEST(PropertyHandler) { - v8::HandleScope scope; + LocalContext env; + v8::HandleScope scope(env->GetIsolate()); Local fun_templ = v8::FunctionTemplate::New(); fun_templ->InstanceTemplate()->SetAccessor(v8_str("foo"), handle_property); - LocalContext env; Local fun = fun_templ->GetFunction(); env->Global()->Set(v8_str("Fun"), fun); Local