@@ -145,12 +145,17 @@ void Environment::Start(int argc,
145
145
146
146
auto process_template = FunctionTemplate::New (isolate ());
147
147
process_template->SetClassName (FIXED_ONE_BYTE_STRING (isolate (), " process" ));
148
+ process_template->InstanceTemplate ()->SetInternalFieldCount (1 );
148
149
149
150
auto process_object =
150
151
process_template->GetFunction ()->NewInstance (context ()).ToLocalChecked ();
151
152
set_process_object (process_object);
152
153
153
154
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
+
154
159
LoadAsyncWrapperInfo (this );
155
160
156
161
static uv_once_t init_once = UV_ONCE_INIT;
@@ -298,12 +303,24 @@ bool Environment::EmitNapiWarning() {
298
303
void Environment::EnvPromiseHook (v8::PromiseHookType type,
299
304
v8::Local<v8::Promise> promise,
300
305
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 ) {
305
321
return ;
306
322
}
323
+
307
324
Environment* env = Environment::GetCurrent (context);
308
325
for (const PromiseHookCallback& hook : env->promise_hooks_ ) {
309
326
hook.cb_ (type, promise, parent, hook.arg_ );
0 commit comments