Skip to content

Commit 9690b86

Browse files
authored
fix: updated with the latest code review
1 parent 5e9128f commit 9690b86

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/env.cc

+21-4
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,17 @@ void Environment::Start(int argc,
145145

146146
auto process_template = FunctionTemplate::New(isolate());
147147
process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "process"));
148+
process_template->InstanceTemplate()->SetInternalFieldCount(1);
148149

149150
auto process_object =
150151
process_template->GetFunction()->NewInstance(context()).ToLocalChecked();
151152
set_process_object(process_object);
152153

153154
SetupProcessObject(this, argc, argv, exec_argc, exec_argv);
155+
156+
// Used by EnvPromiseHook to know that we are on a node context.
157+
process_object->SetInternalField(0, v8::Int32::New(isolate(), 0x6e6f6465));
158+
154159
LoadAsyncWrapperInfo(this);
155160

156161
static uv_once_t init_once = UV_ONCE_INIT;
@@ -298,12 +303,24 @@ bool Environment::EmitNapiWarning() {
298303
void Environment::EnvPromiseHook(v8::PromiseHookType type,
299304
v8::Local<v8::Promise> promise,
300305
v8::Local<v8::Value> parent) {
301-
auto context = promise->CreationContext();
302-
auto dataIndex = node::Environment::kContextEmbedderDataIndex;
303-
// If the context is undefined (not a node context) then skip.
304-
if (context->GetEmbedderData(dataIndex)->IsUndefined()) {
306+
v8::Isolate *isolate = Isolate::GetCurrent();
307+
Local<v8::Context> context = isolate->GetCurrentContext();
308+
Local<v8::Object> global = context->Global();
309+
310+
// Make sure process is there and its first internal field is the magic value.
311+
Local<v8::Value> process = global->Get(OneByteString(isolate, "process"));
312+
if (!process->IsObject()) {
313+
return;
314+
}
315+
Local<v8::Object> process_object = process.As<v8::Object>();
316+
if (process_object->InternalFieldCount() < 1) {
317+
return;
318+
}
319+
Local<v8::Value> internal_field = process_object->GetInternalField(0);
320+
if (!internal_field->IsInt32() || internal_field.As<v8::Int32>()->Value() != 0x6e6f6465) {
305321
return;
306322
}
323+
307324
Environment* env = Environment::GetCurrent(context);
308325
for (const PromiseHookCallback& hook : env->promise_hooks_) {
309326
hook.cb_(type, promise, parent, hook.arg_);

0 commit comments

Comments
 (0)