Skip to content

Commit

Permalink
contextify: cache sandbox and context in locals
Browse files Browse the repository at this point in the history
PR-URL: #5392
Reviewed-By: Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
ofrobots committed Feb 26, 2016
1 parent 87a0ac7 commit 3921770
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ class ContextifyContext {
Persistent<Context> context_;

public:
explicit ContextifyContext(Environment* env, Local<Object> sandbox_obj)
: env_(env) {
ContextifyContext(Environment* env, Local<Object> sandbox_obj) : env_(env) {
Local<Context> v8_context = CreateV8Context(env, sandbox_obj);
context_.Reset(env->isolate(), v8_context);

Expand Down Expand Up @@ -122,14 +121,15 @@ class ContextifyContext {
Local<Context> context = PersistentToLocal(env()->isolate(), context_);
Local<Object> global =
context->Global()->GetPrototype()->ToObject(env()->isolate());
Local<Object> sandbox_obj = sandbox();

Local<Function> clone_property_method;

Local<Array> names = global->GetOwnPropertyNames();
int length = names->Length();
for (int i = 0; i < length; i++) {
Local<String> key = names->Get(i)->ToString(env()->isolate());
bool has = sandbox()->HasOwnProperty(context, key).FromJust();
bool has = sandbox_obj->HasOwnProperty(context, key).FromJust();
if (!has) {
// Could also do this like so:
//
Expand Down Expand Up @@ -161,7 +161,7 @@ class ContextifyContext {
clone_property_method = Local<Function>::Cast(script->Run());
CHECK(clone_property_method->IsFunction());
}
Local<Value> args[] = { global, key, sandbox() };
Local<Value> args[] = { global, key, sandbox_obj };
clone_property_method->Call(global, ARRAY_SIZE(args), args);
}
}
Expand Down Expand Up @@ -336,16 +336,18 @@ class ContextifyContext {
if (ctx->context_.IsEmpty())
return;

Local<Context> context = ctx->context();
Local<Object> sandbox = ctx->sandbox();
MaybeLocal<Value> maybe_rv =
ctx->sandbox()->GetRealNamedProperty(ctx->context(), property);
sandbox->GetRealNamedProperty(context, property);
if (maybe_rv.IsEmpty()) {
maybe_rv =
ctx->global_proxy()->GetRealNamedProperty(ctx->context(), property);
ctx->global_proxy()->GetRealNamedProperty(context, property);
}

Local<Value> rv;
if (maybe_rv.ToLocal(&rv)) {
if (rv == ctx->sandbox())
if (rv == sandbox)
rv = ctx->global_proxy();

args.GetReturnValue().Set(rv);
Expand Down Expand Up @@ -378,14 +380,14 @@ class ContextifyContext {
if (ctx->context_.IsEmpty())
return;

Local<Context> context = ctx->context();
Maybe<PropertyAttribute> maybe_prop_attr =
ctx->sandbox()->GetRealNamedPropertyAttributes(ctx->context(),
property);
ctx->sandbox()->GetRealNamedPropertyAttributes(context, property);

if (maybe_prop_attr.IsNothing()) {
maybe_prop_attr =
ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(),
property);
ctx->global_proxy()->GetRealNamedPropertyAttributes(context,
property);
}

if (maybe_prop_attr.IsJust()) {
Expand Down

0 comments on commit 3921770

Please sign in to comment.