From 9690b862cb7e5888c25562a8bda65ce052934db6 Mon Sep 17 00:00:00 2001 From: Javier Gonzalez Date: Tue, 6 Mar 2018 21:31:57 +0100 Subject: [PATCH] fix: updated with the latest code review --- src/env.cc | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/env.cc b/src/env.cc index 6f4540011703ad..2122c0cbdc5701 100644 --- a/src/env.cc +++ b/src/env.cc @@ -145,12 +145,17 @@ void Environment::Start(int argc, auto process_template = FunctionTemplate::New(isolate()); process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "process")); + process_template->InstanceTemplate()->SetInternalFieldCount(1); auto process_object = process_template->GetFunction()->NewInstance(context()).ToLocalChecked(); set_process_object(process_object); SetupProcessObject(this, argc, argv, exec_argc, exec_argv); + + // Used by EnvPromiseHook to know that we are on a node context. + process_object->SetInternalField(0, v8::Int32::New(isolate(), 0x6e6f6465)); + LoadAsyncWrapperInfo(this); static uv_once_t init_once = UV_ONCE_INIT; @@ -298,12 +303,24 @@ bool Environment::EmitNapiWarning() { void Environment::EnvPromiseHook(v8::PromiseHookType type, v8::Local promise, v8::Local parent) { - auto context = promise->CreationContext(); - auto dataIndex = node::Environment::kContextEmbedderDataIndex; - // If the context is undefined (not a node context) then skip. - if (context->GetEmbedderData(dataIndex)->IsUndefined()) { + v8::Isolate *isolate = Isolate::GetCurrent(); + Local context = isolate->GetCurrentContext(); + Local global = context->Global(); + + // Make sure process is there and its first internal field is the magic value. + Local process = global->Get(OneByteString(isolate, "process")); + if (!process->IsObject()) { + return; + } + Local process_object = process.As(); + if (process_object->InternalFieldCount() < 1) { + return; + } + Local internal_field = process_object->GetInternalField(0); + if (!internal_field->IsInt32() || internal_field.As()->Value() != 0x6e6f6465) { return; } + Environment* env = Environment::GetCurrent(context); for (const PromiseHookCallback& hook : env->promise_hooks_) { hook.cb_(type, promise, parent, hook.arg_);