Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(vee-eight-4.9) contextify: update deprecated SetWeak usage #5392

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 39 additions & 73 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,53 +45,33 @@ using v8::Uint8Array;
using v8::UnboundScript;
using v8::V8;
using v8::Value;
using v8::WeakCallbackData;
using v8::WeakCallbackInfo;


class ContextifyContext {
protected:
enum Kind {
kSandbox,
kContext,
kProxyGlobal
};
// V8 reserves the first field in context objects for the debugger. We use the
// second field to hold a reference to the sandbox object.
enum { kSandboxObjectIndex = 1 };

Environment* const env_;
Persistent<Object> sandbox_;
Persistent<Context> context_;
Persistent<Object> proxy_global_;
int references_;

public:
explicit ContextifyContext(Environment* env, Local<Object> sandbox)
: env_(env),
sandbox_(env->isolate(), sandbox),
// Wait for sandbox_, proxy_global_, and context_ to die
references_(0) {
context_.Reset(env->isolate(), CreateV8Context(env));

sandbox_.SetWeak(this, WeakCallback<Object, kSandbox>);
sandbox_.MarkIndependent();
references_++;
ContextifyContext(Environment* env, Local<Object> sandbox_obj) : env_(env) {
Local<Context> v8_context = CreateV8Context(env, sandbox_obj);
context_.Reset(env->isolate(), v8_context);

// Allocation failure or maximum call stack size reached
if (context_.IsEmpty())
return;
context_.SetWeak(this, WeakCallback<Context, kContext>);
context_.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter);
context_.MarkIndependent();
references_++;

proxy_global_.Reset(env->isolate(), context()->Global());
proxy_global_.SetWeak(this, WeakCallback<Object, kProxyGlobal>);
proxy_global_.MarkIndependent();
references_++;
}


~ContextifyContext() {
context_.Reset();
proxy_global_.Reset();
sandbox_.Reset();
}


Expand All @@ -105,6 +85,15 @@ class ContextifyContext {
}


inline Local<Object> global_proxy() const {
return context()->Global();
}


inline Local<Object> sandbox() const {
return Local<Object>::Cast(context()->GetEmbedderData(kSandboxObjectIndex));
}

// XXX(isaacs): This function only exists because of a shortcoming of
// the V8 SetNamedPropertyHandler function.
//
Expand Down Expand Up @@ -132,15 +121,15 @@ class ContextifyContext {
Local<Context> context = PersistentToLocal(env()->isolate(), context_);
Local<Object> global =
context->Global()->GetPrototype()->ToObject(env()->isolate());
Local<Object> sandbox = PersistentToLocal(env()->isolate(), sandbox_);
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 @@ -172,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 All @@ -197,14 +186,13 @@ class ContextifyContext {
}


Local<Context> CreateV8Context(Environment* env) {
Local<Context> CreateV8Context(Environment* env, Local<Object> sandbox_obj) {
EscapableHandleScope scope(env->isolate());
Local<FunctionTemplate> function_template =
FunctionTemplate::New(env->isolate());
function_template->SetHiddenPrototype(true);

Local<Object> sandbox = PersistentToLocal(env->isolate(), sandbox_);
function_template->SetClassName(sandbox->GetConstructorName());
function_template->SetClassName(sandbox_obj->GetConstructorName());

Local<ObjectTemplate> object_template =
function_template->InstanceTemplate();
Expand All @@ -221,6 +209,7 @@ class ContextifyContext {

CHECK(!ctx.IsEmpty());
ctx->SetSecurityToken(env->context()->GetSecurityToken());
ctx->SetEmbedderData(kSandboxObjectIndex, sandbox_obj);

env->AssignToContext(ctx);

Expand Down Expand Up @@ -316,18 +305,9 @@ class ContextifyContext {
}


template <class T, Kind kind>
static void WeakCallback(const WeakCallbackData<T, ContextifyContext>& data) {
static void WeakCallback(const WeakCallbackInfo<ContextifyContext>& data) {
ContextifyContext* context = data.GetParameter();
if (kind == kSandbox)
context->sandbox_.ClearWeak();
else if (kind == kContext)
context->context_.ClearWeak();
else
context->proxy_global_.ClearWeak();

if (--context->references_ == 0)
delete context;
delete context;
}


Expand All @@ -349,28 +329,26 @@ class ContextifyContext {
static void GlobalPropertyGetterCallback(
Local<Name> property,
const PropertyCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();

ContextifyContext* ctx =
Unwrap<ContextifyContext>(args.Data().As<Object>());

// Stil initializing
if (ctx->context_.IsEmpty())
return;

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

Local<Value> rv;
if (maybe_rv.ToLocal(&rv)) {
if (rv == ctx->sandbox_)
rv = PersistentToLocal(isolate, ctx->proxy_global_);
if (rv == sandbox)
rv = ctx->global_proxy();

args.GetReturnValue().Set(rv);
}
Expand All @@ -381,42 +359,35 @@ class ContextifyContext {
Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();

ContextifyContext* ctx =
Unwrap<ContextifyContext>(args.Data().As<Object>());

// Stil initializing
if (ctx->context_.IsEmpty())
return;

PersistentToLocal(isolate, ctx->sandbox_)->Set(property, value);
ctx->sandbox()->Set(property, value);
}


static void GlobalPropertyQueryCallback(
Local<Name> property,
const PropertyCallbackInfo<Integer>& args) {
Isolate* isolate = args.GetIsolate();

ContextifyContext* ctx =
Unwrap<ContextifyContext>(args.Data().As<Object>());

// Stil initializing
if (ctx->context_.IsEmpty())
return;

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

if (maybe_prop_attr.IsNothing()) {
Local<Object> proxy_global = PersistentToLocal(isolate,
ctx->proxy_global_);

maybe_prop_attr =
proxy_global->GetRealNamedPropertyAttributes(ctx->context(),
property);
ctx->global_proxy()->GetRealNamedPropertyAttributes(context,
property);
}

if (maybe_prop_attr.IsJust()) {
Expand All @@ -429,18 +400,14 @@ class ContextifyContext {
static void GlobalPropertyDeleterCallback(
Local<Name> property,
const PropertyCallbackInfo<Boolean>& args) {
Isolate* isolate = args.GetIsolate();

ContextifyContext* ctx =
Unwrap<ContextifyContext>(args.Data().As<Object>());

// Stil initializing
if (ctx->context_.IsEmpty())
return;

Local<Object> sandbox = PersistentToLocal(isolate, ctx->sandbox_);

Maybe<bool> success = sandbox->Delete(ctx->context(), property);
Maybe<bool> success = ctx->sandbox()->Delete(ctx->context(), property);

if (success.IsJust())
args.GetReturnValue().Set(success.FromJust());
Expand All @@ -456,8 +423,7 @@ class ContextifyContext {
if (ctx->context_.IsEmpty())
return;

Local<Object> sandbox = PersistentToLocal(args.GetIsolate(), ctx->sandbox_);
args.GetReturnValue().Set(sandbox->GetPropertyNames());
args.GetReturnValue().Set(ctx->sandbox()->GetPropertyNames());
}
};

Expand Down