Reviewers: Sven Panne,

Message:
The cctests are not yet adapted, this is a huge chunk of work and I wanted to
sync that with you to not cause any merge conflicts for you.

Description:
Change Context::New 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.

[email protected]


Please review this at https://codereview.chromium.org/13799003/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M include/v8.h
  M src/api.cc
  M src/bootstrapper.cc
  M src/debug.cc
  M test/cctest/test-decls.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index 8dabef0d5885448bfdd590c37f88ab4dfe8218a0..ef88564e194a5ccf76a958debf01b52dfd18dc9d 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -3864,11 +3864,11 @@ class V8EXPORT Context {
    */
   void ReattachGlobal(Handle<Object> 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.
@@ -3882,6 +3882,13 @@ class V8EXPORT Context {
    * template. The state of the global object will be completely reset
    * and only object identify will remain.
    */
+  static Local<Context> New(
+      Isolate* isolate,
+      ExtensionConfiguration* extensions = NULL,
+      Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
+      Handle<Value> global_object = Handle<Value>());
+
+  /** Deprecated. Use Isolate version instead. */
   static Persistent<Context> New(
       ExtensionConfiguration* extensions = NULL,
       Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 3fab8de82fe3a50b7b089810a734b6f4e7369bf0..6ce7de0557340db07e126ca9e344fac35b63bba1 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -4863,18 +4863,14 @@ static i::Handle<i::FunctionTemplateInfo>
 }


-Persistent<Context> v8::Context::New(
+static i::Handle<i::Context> CreateEnvironment(
+    i::Isolate* isolate,
     v8::ExtensionConfiguration* extensions,
     v8::Handle<ObjectTemplate> global_template,
     v8::Handle<Value> 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<Context>());
+  i::Handle<i::Context> env;

   // Enter V8 via an ENTER_V8 scope.
-  i::Handle<i::Context> env;
   {
     ENTER_V8(isolate);
     v8::Handle<ObjectTemplate> proxy_template = global_template;
@@ -4929,10 +4925,41 @@ Persistent<Context> v8::Context::New(
   }
   // Leave V8.

-  if (env.is_null()) {
-    return Persistent<Context>();
-  }
-  return Persistent<Context>(Utils::ToLocal(env));
+  return env;
+}
+
+
+Persistent<Context> v8::Context::New(
+    v8::ExtensionConfiguration* extensions,
+    v8::Handle<ObjectTemplate> global_template,
+    v8::Handle<Value> global_object) {
+  i::Isolate::EnsureDefaultIsolate();
+  i::Isolate* isolate = i::Isolate::Current();
+  Isolate* external_isolate = reinterpret_cast<Isolate*>(isolate);
+  EnsureInitializedForIsolate(isolate, "v8::Context::New()");
+  LOG_API(isolate, "Context::New");
+  ON_BAILOUT(isolate, "v8::Context::New()", return Persistent<Context>());
+  i::HandleScope scope(isolate);
+  i::Handle<i::Context> env =
+ CreateEnvironment(isolate, extensions, global_template, global_object);
+  return Persistent<Context>::New(external_isolate, Utils::ToLocal(env));
+}
+
+
+Local<Context> v8::Context::New(
+    v8::Isolate* external_isolate,
+    v8::ExtensionConfiguration* extensions,
+    v8::Handle<ObjectTemplate> global_template,
+    v8::Handle<Value> global_object) {
+  i::Isolate::EnsureDefaultIsolate();
+  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
+  EnsureInitializedForIsolate(isolate, "v8::Context::New()");
+  LOG_API(isolate, "Context::New");
+  ON_BAILOUT(isolate, "v8::Context::New()", return Local<Context>());
+  i::HandleScope scope(isolate);
+  i::Handle<i::Context> env =
+ CreateEnvironment(isolate, extensions, global_template, global_object);
+  return Utils::ToLocal(scope.CloseAndEscape(env));
 }


Index: src/bootstrapper.cc
diff --git a/src/bootstrapper.cc b/src/bootstrapper.cc
index 596b480b70f8526956264cc2d10b5f6511f0a351..f40aa456ef9e858a16398caf3cb9fd7690fe681f 100644
--- a/src/bootstrapper.cc
+++ b/src/bootstrapper.cc
@@ -303,11 +303,10 @@ Handle<Context> Bootstrapper::CreateEnvironment(
     v8::ExtensionConfiguration* extensions) {
   HandleScope scope(isolate_);
   Genesis genesis(isolate_, global_object, global_template, extensions);
-  if (!genesis.result().is_null()) {
- Handle<Object> ctx(isolate_->global_handles()->Create(*genesis.result()));
-    Handle<Context> env = Handle<Context>::cast(ctx);
+  Handle<Context> env = genesis.result();
+  if (!env.is_null()) {
     if (InstallExtensions(env, extensions)) {
-      return env;
+      return scope.CloseAndEscape(env);
     }
   }
   return Handle<Context>();
Index: src/debug.cc
diff --git a/src/debug.cc b/src/debug.cc
index 91af9ccc1bab32cb351006da1d51de108c6a006e..b862c51b0f26a5308f5bbe1266b0b21f6cf31c33 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<Context>::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<Object**>(debug_context_.location()));
   debug_context_ = Handle<Context>();
 }
Index: test/cctest/test-decls.cc
diff --git a/test/cctest/test-decls.cc b/test/cctest/test-decls.cc
index ae2ec712ab752206e66dba243423c9fbfd555373..ed8da5c1b08678ede8336751b7f59b4c7243dec7 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();
   }



--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to