From a54f65ce75d56f3d0da670c127e79f048b6a78ae Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Fri, 23 Aug 2013 21:17:15 -0400 Subject: [PATCH] vm: rip out ObjectWrap from ContextifyContext This was a remnant of the original Contextify code, wherein ContextifyContext was a user-exposed object. In vm, it is not, so all of the ObjectWrap and function-template stuff for the ContextifyContext constructor is now unnecessary. --- src/node_contextify.cc | 77 +++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index d71625e2ae7..11bb9795f88 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -29,6 +29,7 @@ using v8::AccessType; using v8::Array; using v8::Boolean; using v8::Context; +using v8::External; using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; @@ -47,18 +48,20 @@ using v8::V8; using v8::Value; -class ContextifyContext : ObjectWrap { +class ContextifyContext { private: Persistent sandbox_; Persistent proxy_global_; + Persistent context_; static Persistent data_wrapper_ctor; public: - Persistent context_; - static Persistent js_tmpl; - explicit ContextifyContext(Local sandbox) : sandbox_(node_isolate, sandbox) { + HandleScope scope(node_isolate); + Local v8_context = CreateV8Context(); + context_.Reset(node_isolate, v8_context); + proxy_global_.Reset(node_isolate, v8_context->Global()); } @@ -69,19 +72,6 @@ class ContextifyContext : ObjectWrap { } - // We override ObjectWrap::Wrap so that we can create our context after - // we have a reference to our "host" JavaScript object. If we try to use - // handle_ in the ContextifyContext constructor, it will be empty since it's - // set in ObjectWrap::Wrap. - inline void Wrap(Local handle) { - HandleScope scope(node_isolate); - ObjectWrap::Wrap(handle); - Local v8_context = CreateV8Context(); - context_.Reset(node_isolate, v8_context); - proxy_global_.Reset(node_isolate, v8_context->Global()); - } - - // This is an object that just keeps an internal pointer to this // ContextifyContext. It's passed to the NamedPropertyHandler. If we // pass the main JavaScript context object we're embedded in, then the @@ -125,59 +115,48 @@ class ContextifyContext : ObjectWrap { function_template->InstanceTemplate()->SetInternalFieldCount(1); data_wrapper_ctor.Reset(node_isolate, function_template->GetFunction()); - js_tmpl.Reset(node_isolate, FunctionTemplate::New(New)); - Local ljs_tmpl = PersistentToLocal(node_isolate, js_tmpl); - ljs_tmpl->InstanceTemplate()->SetInternalFieldCount(1); - - Local class_name - = FIXED_ONE_BYTE_STRING(node_isolate, "ContextifyContext"); - ljs_tmpl->SetClassName(class_name); - target->Set(class_name, ljs_tmpl->GetFunction()); - NODE_SET_METHOD(target, "makeContext", MakeContext); } - // args[0] = the sandbox object - static void New(const FunctionCallbackInfo& args) { + static void MakeContext(const FunctionCallbackInfo& args) { HandleScope scope(node_isolate); + if (!args[0]->IsObject()) { return ThrowTypeError("sandbox argument must be an object."); } - ContextifyContext* ctx = new ContextifyContext(args[0].As()); - ctx->Wrap(args.This()); - } - - - static void MakeContext(const FunctionCallbackInfo& args) { Local sandbox = args[0].As(); - Local ljs_tmpl = PersistentToLocal(node_isolate, js_tmpl); - Local constructor_args[] = { sandbox }; - Local contextify_context_object = - ljs_tmpl->GetFunction()->NewInstance(1, constructor_args); - + Local context = External::New(new ContextifyContext(sandbox)); Local hidden_name = FIXED_ONE_BYTE_STRING(node_isolate, "_contextifyHidden"); - sandbox->SetHiddenValue(hidden_name, contextify_context_object); + sandbox->SetHiddenValue(hidden_name, context); } - static const Local ContextFromContextifiedSandbox( + static ContextifyContext* ContextFromContextifiedSandbox( const Local& sandbox) { Local hidden_name = FIXED_ONE_BYTE_STRING(node_isolate, "_contextifyHidden"); - Local hidden_context = - sandbox->GetHiddenValue(hidden_name).As(); + Local context_external_v = sandbox->GetHiddenValue(hidden_name); + if (context_external_v.IsEmpty() || !context_external_v->IsExternal()) { + return NULL; + } + Local context_external = context_external_v.As(); - if (hidden_context.IsEmpty()) { + return static_cast(context_external->Value()); + } + + static Local V8ContextFromContextifiedSandbox( + const Local& sandbox) { + ContextifyContext* contextify_context = + ContextFromContextifiedSandbox(sandbox); + if (contextify_context == NULL) { ThrowTypeError("sandbox argument must have been converted to a context."); return Local(); } - ContextifyContext* ctx = - ObjectWrap::Unwrap(hidden_context); - return PersistentToLocal(node_isolate, ctx->context_); + return PersistentToLocal(node_isolate, contextify_context->context_); } @@ -385,7 +364,7 @@ class ContextifyScript : ObjectWrap { // Get the context from the sandbox Local context = - ContextifyContext::ContextFromContextifiedSandbox(sandbox); + ContextifyContext::V8ContextFromContextifiedSandbox(sandbox); if (try_catch.HasCaught()) { try_catch.ReThrow(); return; @@ -479,9 +458,7 @@ class ContextifyScript : ObjectWrap { }; -Persistent ContextifyContext::js_tmpl; Persistent ContextifyContext::data_wrapper_ctor; - Persistent ContextifyScript::script_tmpl; void InitContextify(Local target) {