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.
This commit is contained in:
parent
73d328de24
commit
a54f65ce75
@ -29,6 +29,7 @@ using v8::AccessType;
|
|||||||
using v8::Array;
|
using v8::Array;
|
||||||
using v8::Boolean;
|
using v8::Boolean;
|
||||||
using v8::Context;
|
using v8::Context;
|
||||||
|
using v8::External;
|
||||||
using v8::Function;
|
using v8::Function;
|
||||||
using v8::FunctionCallbackInfo;
|
using v8::FunctionCallbackInfo;
|
||||||
using v8::FunctionTemplate;
|
using v8::FunctionTemplate;
|
||||||
@ -47,18 +48,20 @@ using v8::V8;
|
|||||||
using v8::Value;
|
using v8::Value;
|
||||||
|
|
||||||
|
|
||||||
class ContextifyContext : ObjectWrap {
|
class ContextifyContext {
|
||||||
private:
|
private:
|
||||||
Persistent<Object> sandbox_;
|
Persistent<Object> sandbox_;
|
||||||
Persistent<Object> proxy_global_;
|
Persistent<Object> proxy_global_;
|
||||||
|
Persistent<Context> context_;
|
||||||
static Persistent<Function> data_wrapper_ctor;
|
static Persistent<Function> data_wrapper_ctor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Persistent<Context> context_;
|
|
||||||
static Persistent<FunctionTemplate> js_tmpl;
|
|
||||||
|
|
||||||
explicit ContextifyContext(Local<Object> sandbox) :
|
explicit ContextifyContext(Local<Object> sandbox) :
|
||||||
sandbox_(node_isolate, sandbox) {
|
sandbox_(node_isolate, sandbox) {
|
||||||
|
HandleScope scope(node_isolate);
|
||||||
|
Local<Context> 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<Object> handle) {
|
|
||||||
HandleScope scope(node_isolate);
|
|
||||||
ObjectWrap::Wrap(handle);
|
|
||||||
Local<Context> 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
|
// This is an object that just keeps an internal pointer to this
|
||||||
// ContextifyContext. It's passed to the NamedPropertyHandler. If we
|
// ContextifyContext. It's passed to the NamedPropertyHandler. If we
|
||||||
// pass the main JavaScript context object we're embedded in, then the
|
// pass the main JavaScript context object we're embedded in, then the
|
||||||
@ -125,59 +115,48 @@ class ContextifyContext : ObjectWrap {
|
|||||||
function_template->InstanceTemplate()->SetInternalFieldCount(1);
|
function_template->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
data_wrapper_ctor.Reset(node_isolate, function_template->GetFunction());
|
data_wrapper_ctor.Reset(node_isolate, function_template->GetFunction());
|
||||||
|
|
||||||
js_tmpl.Reset(node_isolate, FunctionTemplate::New(New));
|
|
||||||
Local<FunctionTemplate> ljs_tmpl = PersistentToLocal(node_isolate, js_tmpl);
|
|
||||||
ljs_tmpl->InstanceTemplate()->SetInternalFieldCount(1);
|
|
||||||
|
|
||||||
Local<String> 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);
|
NODE_SET_METHOD(target, "makeContext", MakeContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// args[0] = the sandbox object
|
static void MakeContext(const FunctionCallbackInfo<Value>& args) {
|
||||||
static void New(const FunctionCallbackInfo<Value>& args) {
|
|
||||||
HandleScope scope(node_isolate);
|
HandleScope scope(node_isolate);
|
||||||
|
|
||||||
if (!args[0]->IsObject()) {
|
if (!args[0]->IsObject()) {
|
||||||
return ThrowTypeError("sandbox argument must be an object.");
|
return ThrowTypeError("sandbox argument must be an object.");
|
||||||
}
|
}
|
||||||
ContextifyContext* ctx = new ContextifyContext(args[0].As<Object>());
|
|
||||||
ctx->Wrap(args.This());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void MakeContext(const FunctionCallbackInfo<Value>& args) {
|
|
||||||
Local<Object> sandbox = args[0].As<Object>();
|
Local<Object> sandbox = args[0].As<Object>();
|
||||||
|
|
||||||
Local<FunctionTemplate> ljs_tmpl = PersistentToLocal(node_isolate, js_tmpl);
|
Local<External> context = External::New(new ContextifyContext(sandbox));
|
||||||
Local<Value> constructor_args[] = { sandbox };
|
|
||||||
Local<Object> contextify_context_object =
|
|
||||||
ljs_tmpl->GetFunction()->NewInstance(1, constructor_args);
|
|
||||||
|
|
||||||
Local<String> hidden_name =
|
Local<String> hidden_name =
|
||||||
FIXED_ONE_BYTE_STRING(node_isolate, "_contextifyHidden");
|
FIXED_ONE_BYTE_STRING(node_isolate, "_contextifyHidden");
|
||||||
sandbox->SetHiddenValue(hidden_name, contextify_context_object);
|
sandbox->SetHiddenValue(hidden_name, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const Local<Context> ContextFromContextifiedSandbox(
|
static ContextifyContext* ContextFromContextifiedSandbox(
|
||||||
const Local<Object>& sandbox) {
|
const Local<Object>& sandbox) {
|
||||||
Local<String> hidden_name =
|
Local<String> hidden_name =
|
||||||
FIXED_ONE_BYTE_STRING(node_isolate, "_contextifyHidden");
|
FIXED_ONE_BYTE_STRING(node_isolate, "_contextifyHidden");
|
||||||
Local<Object> hidden_context =
|
Local<Value> context_external_v = sandbox->GetHiddenValue(hidden_name);
|
||||||
sandbox->GetHiddenValue(hidden_name).As<Object>();
|
if (context_external_v.IsEmpty() || !context_external_v->IsExternal()) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Local<External> context_external = context_external_v.As<External>();
|
||||||
|
|
||||||
if (hidden_context.IsEmpty()) {
|
return static_cast<ContextifyContext*>(context_external->Value());
|
||||||
|
}
|
||||||
|
|
||||||
|
static Local<Context> V8ContextFromContextifiedSandbox(
|
||||||
|
const Local<Object>& sandbox) {
|
||||||
|
ContextifyContext* contextify_context =
|
||||||
|
ContextFromContextifiedSandbox(sandbox);
|
||||||
|
if (contextify_context == NULL) {
|
||||||
ThrowTypeError("sandbox argument must have been converted to a context.");
|
ThrowTypeError("sandbox argument must have been converted to a context.");
|
||||||
return Local<Context>();
|
return Local<Context>();
|
||||||
}
|
}
|
||||||
|
|
||||||
ContextifyContext* ctx =
|
return PersistentToLocal(node_isolate, contextify_context->context_);
|
||||||
ObjectWrap::Unwrap<ContextifyContext>(hidden_context);
|
|
||||||
return PersistentToLocal(node_isolate, ctx->context_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -385,7 +364,7 @@ class ContextifyScript : ObjectWrap {
|
|||||||
|
|
||||||
// Get the context from the sandbox
|
// Get the context from the sandbox
|
||||||
Local<Context> context =
|
Local<Context> context =
|
||||||
ContextifyContext::ContextFromContextifiedSandbox(sandbox);
|
ContextifyContext::V8ContextFromContextifiedSandbox(sandbox);
|
||||||
if (try_catch.HasCaught()) {
|
if (try_catch.HasCaught()) {
|
||||||
try_catch.ReThrow();
|
try_catch.ReThrow();
|
||||||
return;
|
return;
|
||||||
@ -479,9 +458,7 @@ class ContextifyScript : ObjectWrap {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Persistent<FunctionTemplate> ContextifyContext::js_tmpl;
|
|
||||||
Persistent<Function> ContextifyContext::data_wrapper_ctor;
|
Persistent<Function> ContextifyContext::data_wrapper_ctor;
|
||||||
|
|
||||||
Persistent<FunctionTemplate> ContextifyScript::script_tmpl;
|
Persistent<FunctionTemplate> ContextifyScript::script_tmpl;
|
||||||
|
|
||||||
void InitContextify(Local<Object> target) {
|
void InitContextify(Local<Object> target) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user