Skip to content

Commit

Permalink
src: access ContextifyContext* more directly in property cbs
Browse files Browse the repository at this point in the history
PR-URL: #20455
Fixes: #18897
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
  • Loading branch information
addaleax authored and MylesBorins committed May 8, 2018
1 parent c0f1535 commit c21a52f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Local<Value> ContextifyContext::CreateDataWrapper(Environment* env) {
if (wrapper.IsEmpty())
return scope.Escape(Local<Value>::New(env->isolate(), Local<Value>()));

Wrap(wrapper, this);
wrapper->SetAlignedPointerInInternalField(0, this);
return scope.Escape(wrapper);
}

Expand Down Expand Up @@ -290,12 +290,19 @@ ContextifyContext* ContextifyContext::ContextFromContextifiedSandbox(
return nullptr;
}

// static
template <typename T>
ContextifyContext* ContextifyContext::Get(const PropertyCallbackInfo<T>& args) {
Local<Value> data = args.Data();
return static_cast<ContextifyContext*>(
data.As<Object>()->GetAlignedPointerFromInternalField(0));
}

// static
void ContextifyContext::PropertyGetterCallback(
Local<Name> property,
const PropertyCallbackInfo<Value>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand Down Expand Up @@ -324,8 +331,7 @@ void ContextifyContext::PropertySetterCallback(
Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<Value>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand Down Expand Up @@ -385,8 +391,7 @@ void ContextifyContext::PropertySetterCallback(
void ContextifyContext::PropertyDescriptorCallback(
Local<Name> property,
const PropertyCallbackInfo<Value>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand All @@ -408,8 +413,7 @@ void ContextifyContext::PropertyDefinerCallback(
Local<Name> property,
const PropertyDescriptor& desc,
const PropertyCallbackInfo<Value>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand Down Expand Up @@ -471,8 +475,7 @@ void ContextifyContext::PropertyDefinerCallback(
void ContextifyContext::PropertyDeleterCallback(
Local<Name> property,
const PropertyCallbackInfo<Boolean>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand All @@ -491,8 +494,7 @@ void ContextifyContext::PropertyDeleterCallback(
// static
void ContextifyContext::PropertyEnumeratorCallback(
const PropertyCallbackInfo<Array>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand All @@ -505,8 +507,7 @@ void ContextifyContext::PropertyEnumeratorCallback(
void ContextifyContext::IndexedPropertyGetterCallback(
uint32_t index,
const PropertyCallbackInfo<Value>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand All @@ -521,8 +522,7 @@ void ContextifyContext::IndexedPropertySetterCallback(
uint32_t index,
Local<Value> value,
const PropertyCallbackInfo<Value>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand All @@ -536,8 +536,7 @@ void ContextifyContext::IndexedPropertySetterCallback(
void ContextifyContext::IndexedPropertyDescriptorCallback(
uint32_t index,
const PropertyCallbackInfo<Value>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand All @@ -552,8 +551,7 @@ void ContextifyContext::IndexedPropertyDefinerCallback(
uint32_t index,
const PropertyDescriptor& desc,
const PropertyCallbackInfo<Value>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand All @@ -567,8 +565,7 @@ void ContextifyContext::IndexedPropertyDefinerCallback(
void ContextifyContext::IndexedPropertyDeleterCallback(
uint32_t index,
const PropertyCallbackInfo<Boolean>& args) {
ContextifyContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
ContextifyContext* ctx = ContextifyContext::Get(args);

// Still initializing
if (ctx->context_.IsEmpty())
Expand Down
3 changes: 3 additions & 0 deletions src/node_contextify.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class ContextifyContext {
context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject));
}

template <typename T>
static ContextifyContext* Get(const v8::PropertyCallbackInfo<T>& args);

private:
static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args);
static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down

0 comments on commit c21a52f

Please sign in to comment.