From a22790bfed00803826893c983360915f57cb8290 Mon Sep 17 00:00:00 2001 From: "mstarzinger@chromium.org" Date: Wed, 10 Apr 2013 09:34:37 +0000 Subject: [PATCH] Change Context::New to not create persistent handles. This moves the responsibility of putting a new context into a persistent handle to the embedder. Also it removes one API function where the copy constructor for persistent handles is needed. R=svenpanne@chromium.org Review URL: https://codereview.chromium.org/13799003 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14203 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- include/v8.h | 16 +++++++++++---- src/api.cc | 51 +++++++++++++++++++++++++++++++++++++---------- src/bootstrapper.cc | 11 ++++------ src/debug.cc | 7 ++++--- test/cctest/cctest.cc | 5 ++++- test/cctest/test-decls.cc | 2 +- 6 files changed, 65 insertions(+), 27 deletions(-) diff --git a/include/v8.h b/include/v8.h index ede5b56..a434466 100644 --- a/include/v8.h +++ b/include/v8.h @@ -3867,11 +3867,11 @@ class V8EXPORT Context { */ void ReattachGlobal(Handle global_object); - /** Creates a new context. + /** + * Creates a new context and returns a handle to the newly allocated + * context. * - * Returns a persistent handle to the newly allocated context. This - * persistent handle has to be disposed when the context is no - * longer used so the context can be garbage collected. + * \param isolate The isolate in which to create the context. * * \param extensions An optional extension configuration containing * the extensions to be installed in the newly created context. @@ -3885,6 +3885,14 @@ class V8EXPORT Context { * template. The state of the global object will be completely reset * and only object identify will remain. */ + static Local New( + Isolate* isolate, + ExtensionConfiguration* extensions = NULL, + Handle global_template = Handle(), + Handle global_object = Handle()); + + /** Deprecated. Use Isolate version instead. */ + // TODO(mstarzinger): Put this behind the V8_DEPRECATED guard. static Persistent New( ExtensionConfiguration* extensions = NULL, Handle global_template = Handle(), diff --git a/src/api.cc b/src/api.cc index 2df32ca..a7b45c4 100644 --- a/src/api.cc +++ b/src/api.cc @@ -4863,18 +4863,14 @@ static i::Handle } -Persistent v8::Context::New( +static i::Handle CreateEnvironment( + i::Isolate* isolate, v8::ExtensionConfiguration* extensions, v8::Handle global_template, v8::Handle global_object) { - i::Isolate::EnsureDefaultIsolate(); - i::Isolate* isolate = i::Isolate::Current(); - EnsureInitializedForIsolate(isolate, "v8::Context::New()"); - LOG_API(isolate, "Context::New"); - ON_BAILOUT(isolate, "v8::Context::New()", return Persistent()); + i::Handle env; // Enter V8 via an ENTER_V8 scope. - i::Handle env; { ENTER_V8(isolate); v8::Handle proxy_template = global_template; @@ -4929,10 +4925,43 @@ Persistent v8::Context::New( } // Leave V8. - if (env.is_null()) { - return Persistent(); - } - return Persistent(Utils::ToLocal(env)); + return env; +} + + +Persistent v8::Context::New( + v8::ExtensionConfiguration* extensions, + v8::Handle global_template, + v8::Handle global_object) { + i::Isolate::EnsureDefaultIsolate(); + i::Isolate* isolate = i::Isolate::Current(); + Isolate* external_isolate = reinterpret_cast(isolate); + EnsureInitializedForIsolate(isolate, "v8::Context::New()"); + LOG_API(isolate, "Context::New"); + ON_BAILOUT(isolate, "v8::Context::New()", return Persistent()); + i::HandleScope scope(isolate); + i::Handle env = + CreateEnvironment(isolate, extensions, global_template, global_object); + if (env.is_null()) return Persistent(); + return Persistent::New(external_isolate, Utils::ToLocal(env)); +} + + +Local v8::Context::New( + v8::Isolate* external_isolate, + v8::ExtensionConfiguration* extensions, + v8::Handle global_template, + v8::Handle global_object) { + i::Isolate::EnsureDefaultIsolate(); + i::Isolate* isolate = reinterpret_cast(external_isolate); + EnsureInitializedForIsolate(isolate, "v8::Context::New()"); + LOG_API(isolate, "Context::New"); + ON_BAILOUT(isolate, "v8::Context::New()", return Local()); + i::HandleScope scope(isolate); + i::Handle env = + CreateEnvironment(isolate, extensions, global_template, global_object); + if (env.is_null()) return Local(); + return Utils::ToLocal(scope.CloseAndEscape(env)); } diff --git a/src/bootstrapper.cc b/src/bootstrapper.cc index 596b480..a7c2bac 100644 --- a/src/bootstrapper.cc +++ b/src/bootstrapper.cc @@ -303,14 +303,11 @@ Handle Bootstrapper::CreateEnvironment( v8::ExtensionConfiguration* extensions) { HandleScope scope(isolate_); Genesis genesis(isolate_, global_object, global_template, extensions); - if (!genesis.result().is_null()) { - Handle ctx(isolate_->global_handles()->Create(*genesis.result())); - Handle env = Handle::cast(ctx); - if (InstallExtensions(env, extensions)) { - return env; - } + Handle env = genesis.result(); + if (env.is_null() || !InstallExtensions(env, extensions)) { + return Handle(); } - return Handle(); + return scope.CloseAndEscape(env); } diff --git a/src/debug.cc b/src/debug.cc index 91af9cc..b862c51 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -874,8 +874,9 @@ bool Debug::Load() { // Check for caught exceptions. if (caught_exception) return false; - // Debugger loaded. - debug_context_ = context; + // Debugger loaded, create debugger context global handle. + debug_context_ = Handle::cast( + isolate_->global_handles()->Create(*context)); return true; } @@ -891,7 +892,7 @@ void Debug::Unload() { DestroyScriptCache(); // Clear debugger context global handle. - Isolate::Current()->global_handles()->Destroy( + isolate_->global_handles()->Destroy( reinterpret_cast(debug_context_.location())); debug_context_ = Handle(); } diff --git a/test/cctest/cctest.cc b/test/cctest/cctest.cc index deed91a..a0091ff 100644 --- a/test/cctest/cctest.cc +++ b/test/cctest/cctest.cc @@ -68,8 +68,11 @@ void CcTest::InitializeVM(CcTestExtensionFlags extensions) { EXTENSION_LIST(CHECK_EXTENSION_FLAG) #undef CHECK_EXTENSION_FLAG if (context_.IsEmpty()) { + v8::Isolate* isolate = default_isolate(); + v8::HandleScope scope(isolate); v8::ExtensionConfiguration config(extension_count, extension_names); - context_ = v8::Context::New(&config); + v8::Local context = v8::Context::New(isolate, &config); + context_ = v8::Persistent::New(isolate, context); } context_->Enter(); } diff --git a/test/cctest/test-decls.cc b/test/cctest/test-decls.cc index ae2ec71..ed8da5c 100644 --- a/test/cctest/test-decls.cc +++ b/test/cctest/test-decls.cc @@ -695,7 +695,7 @@ TEST(ExistsInHiddenPrototype) { class SimpleContext { public: SimpleContext() { - context_ = Context::New(0); + context_ = Context::New(); context_->Enter(); } -- 2.7.4