Skip to content

Commit 9ddb44b

Browse files
ofrobotsMyles Borins
authored and
Myles Borins
committed
contextify: cache sandbox and context in locals
PR-URL: #5392 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 8ebdcd6 commit 9ddb44b

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/node_contextify.cc

+13-11
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class ContextifyContext {
5656
Persistent<Context> context_;
5757

5858
public:
59-
explicit ContextifyContext(Environment* env, Local<Object> sandbox_obj)
60-
: env_(env) {
59+
ContextifyContext(Environment* env, Local<Object> sandbox_obj) : env_(env) {
6160
Local<Context> v8_context = CreateV8Context(env, sandbox_obj);
6261
context_.Reset(env->isolate(), v8_context);
6362

@@ -120,14 +119,15 @@ class ContextifyContext {
120119
Local<Context> context = PersistentToLocal(env()->isolate(), context_);
121120
Local<Object> global =
122121
context->Global()->GetPrototype()->ToObject(env()->isolate());
122+
Local<Object> sandbox_obj = sandbox();
123123

124124
Local<Function> clone_property_method;
125125

126126
Local<Array> names = global->GetOwnPropertyNames();
127127
int length = names->Length();
128128
for (int i = 0; i < length; i++) {
129129
Local<String> key = names->Get(i)->ToString(env()->isolate());
130-
bool has = sandbox()->HasOwnProperty(context, key).FromJust();
130+
bool has = sandbox_obj->HasOwnProperty(context, key).FromJust();
131131
if (!has) {
132132
// Could also do this like so:
133133
//
@@ -160,7 +160,7 @@ class ContextifyContext {
160160
clone_property_method = Local<Function>::Cast(script->Run());
161161
CHECK(clone_property_method->IsFunction());
162162
}
163-
Local<Value> args[] = { global, key, sandbox() };
163+
Local<Value> args[] = { global, key, sandbox_obj };
164164
clone_property_method->Call(global, ARRAY_SIZE(args), args);
165165
}
166166
}
@@ -333,16 +333,18 @@ class ContextifyContext {
333333
if (ctx->context_.IsEmpty())
334334
return;
335335

336+
Local<Context> context = ctx->context();
337+
Local<Object> sandbox = ctx->sandbox();
336338
MaybeLocal<Value> maybe_rv =
337-
ctx->sandbox()->GetRealNamedProperty(ctx->context(), property);
339+
sandbox->GetRealNamedProperty(context, property);
338340
if (maybe_rv.IsEmpty()) {
339341
maybe_rv =
340-
ctx->global_proxy()->GetRealNamedProperty(ctx->context(), property);
342+
ctx->global_proxy()->GetRealNamedProperty(context, property);
341343
}
342344

343345
Local<Value> rv;
344346
if (maybe_rv.ToLocal(&rv)) {
345-
if (rv == ctx->sandbox())
347+
if (rv == sandbox)
346348
rv = ctx->global_proxy();
347349

348350
args.GetReturnValue().Set(rv);
@@ -375,14 +377,14 @@ class ContextifyContext {
375377
if (ctx->context_.IsEmpty())
376378
return;
377379

380+
Local<Context> context = ctx->context();
378381
Maybe<PropertyAttribute> maybe_prop_attr =
379-
ctx->sandbox()->GetRealNamedPropertyAttributes(ctx->context(),
380-
property);
382+
ctx->sandbox()->GetRealNamedPropertyAttributes(context, property);
381383

382384
if (maybe_prop_attr.IsNothing()) {
383385
maybe_prop_attr =
384-
ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(),
385-
property);
386+
ctx->global_proxy()->GetRealNamedPropertyAttributes(context,
387+
property);
386388
}
387389

388390
if (maybe_prop_attr.IsJust()) {

0 commit comments

Comments
 (0)