Skip to content

Commit 66566df

Browse files
ofrobotsAli Sheikh
authored and
Ali Sheikh
committed
contextify: cache sandbox and context in locals
PR-URL: #5392 Reviewed-By: Reviewed-By: bnoordhuis - Ben Noordhuis <[email protected]>
1 parent 95ebc74 commit 66566df

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/node_contextify.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ class ContextifyContext {
5858
Persistent<Context> context_;
5959

6060
public:
61-
explicit ContextifyContext(Environment* env, Local<Object> sandbox_obj)
62-
: env_(env) {
61+
ContextifyContext(Environment* env, Local<Object> sandbox_obj) : env_(env) {
6362
Local<Context> v8_context = CreateV8Context(env, sandbox_obj);
6463
context_.Reset(env->isolate(), v8_context);
6564

@@ -122,14 +121,15 @@ class ContextifyContext {
122121
Local<Context> context = PersistentToLocal(env()->isolate(), context_);
123122
Local<Object> global =
124123
context->Global()->GetPrototype()->ToObject(env()->isolate());
124+
Local<Object> sandbox_obj = sandbox();
125125

126126
Local<Function> clone_property_method;
127127

128128
Local<Array> names = global->GetOwnPropertyNames();
129129
int length = names->Length();
130130
for (int i = 0; i < length; i++) {
131131
Local<String> key = names->Get(i)->ToString(env()->isolate());
132-
bool has = sandbox()->HasOwnProperty(context, key).FromJust();
132+
bool has = sandbox_obj->HasOwnProperty(context, key).FromJust();
133133
if (!has) {
134134
// Could also do this like so:
135135
//
@@ -161,7 +161,7 @@ class ContextifyContext {
161161
clone_property_method = Local<Function>::Cast(script->Run());
162162
CHECK(clone_property_method->IsFunction());
163163
}
164-
Local<Value> args[] = { global, key, sandbox() };
164+
Local<Value> args[] = { global, key, sandbox_obj };
165165
clone_property_method->Call(global, ARRAY_SIZE(args), args);
166166
}
167167
}
@@ -335,16 +335,18 @@ class ContextifyContext {
335335
if (ctx->context_.IsEmpty())
336336
return;
337337

338+
Local<Context> context = ctx->context();
339+
Local<Object> sandbox = ctx->sandbox();
338340
MaybeLocal<Value> maybe_rv =
339-
ctx->sandbox()->GetRealNamedProperty(ctx->context(), property);
341+
sandbox->GetRealNamedProperty(context, property);
340342
if (maybe_rv.IsEmpty()) {
341343
maybe_rv =
342-
ctx->global_proxy()->GetRealNamedProperty(ctx->context(), property);
344+
ctx->global_proxy()->GetRealNamedProperty(context, property);
343345
}
344346

345347
Local<Value> rv;
346348
if (maybe_rv.ToLocal(&rv)) {
347-
if (rv == ctx->sandbox())
349+
if (rv == sandbox)
348350
rv = ctx->global_proxy();
349351

350352
args.GetReturnValue().Set(rv);
@@ -377,14 +379,14 @@ class ContextifyContext {
377379
if (ctx->context_.IsEmpty())
378380
return;
379381

382+
Local<Context> context = ctx->context();
380383
Maybe<PropertyAttribute> maybe_prop_attr =
381-
ctx->sandbox()->GetRealNamedPropertyAttributes(ctx->context(),
382-
property);
384+
ctx->sandbox()->GetRealNamedPropertyAttributes(context, property);
383385

384386
if (maybe_prop_attr.IsNothing()) {
385387
maybe_prop_attr =
386-
ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(),
387-
property);
388+
ctx->global_proxy()->GetRealNamedPropertyAttributes(context,
389+
property);
388390
}
389391

390392
if (maybe_prop_attr.IsJust()) {

0 commit comments

Comments
 (0)