diff --git a/src/env-inl.h b/src/env-inl.h index b233d2fddfe997..6c4120fc2e406b 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -316,16 +316,23 @@ inline Environment* Environment::GetCurrent(v8::Local context) { inline Environment* Environment::GetCurrent( const v8::FunctionCallbackInfo& info) { - CHECK(info.Data()->IsExternal()); - return static_cast(info.Data().As()->Value()); + return GetFromCallbackData(info.Data()); } template inline Environment* Environment::GetCurrent( const v8::PropertyCallbackInfo& info) { - CHECK(info.Data()->IsExternal()); - return static_cast( - info.Data().template As()->Value()); + return GetFromCallbackData(info.Data()); +} + +inline Environment* Environment::GetFromCallbackData(v8::Local val) { + DCHECK(val->IsObject()); + v8::Local obj = val.As(); + DCHECK_GE(obj->InternalFieldCount(), 1); + Environment* env = + static_cast(obj->GetAlignedPointerFromInternalField(0)); + DCHECK(env->as_callback_data_template()->HasInstance(obj)); + return env; } inline Environment* Environment::GetThreadLocalEnv() { @@ -857,7 +864,7 @@ inline v8::Local v8::Local signature, v8::ConstructorBehavior behavior, v8::SideEffectType side_effect_type) { - v8::Local external = as_external(); + v8::Local external = as_callback_data(); return v8::FunctionTemplate::New(isolate(), callback, external, signature, 0, behavior, side_effect_type); } diff --git a/src/env.cc b/src/env.cc index 72966a07422ede..bd456691799114 100644 --- a/src/env.cc +++ b/src/env.cc @@ -25,8 +25,8 @@ using v8::ArrayBuffer; using v8::Boolean; using v8::Context; using v8::EmbedderGraph; -using v8::External; using v8::Function; +using v8::FunctionTemplate; using v8::HandleScope; using v8::Integer; using v8::Isolate; @@ -195,7 +195,16 @@ Environment::Environment(IsolateData* isolate_data, // We'll be creating new objects so make sure we've entered the context. HandleScope handle_scope(isolate()); Context::Scope context_scope(context); - set_as_external(External::New(isolate(), this)); + { + Local templ = FunctionTemplate::New(isolate()); + templ->InstanceTemplate()->SetInternalFieldCount(1); + Local obj = + templ->GetFunction(context).ToLocalChecked()->NewInstance( + context).ToLocalChecked(); + obj->SetAlignedPointerInInternalField(0, this); + set_as_callback_data(obj); + set_as_callback_data_template(templ); + } // We create new copies of the per-Environment option sets, so that it is // easier to modify them after Environment creation. The defaults are diff --git a/src/env.h b/src/env.h index 8379171ac8b524..da2e83addd5f65 100644 --- a/src/env.h +++ b/src/env.h @@ -319,7 +319,8 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2; V(zero_return_string, "ZERO_RETURN") #define ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V) \ - V(as_external, v8::External) \ + V(as_callback_data, v8::Object) \ + V(as_callback_data_template, v8::FunctionTemplate) \ V(async_hooks_after_function, v8::Function) \ V(async_hooks_before_function, v8::Function) \ V(async_hooks_binding, v8::Object) \ @@ -661,6 +662,8 @@ class Environment { static inline Environment* GetCurrent( const v8::PropertyCallbackInfo& info); + static inline Environment* GetFromCallbackData(v8::Local val); + static uv_key_t thread_local_env; static inline Environment* GetThreadLocalEnv(); diff --git a/src/fs_event_wrap.cc b/src/fs_event_wrap.cc index acc83d017e0633..8009be6a10ad03 100644 --- a/src/fs_event_wrap.cc +++ b/src/fs_event_wrap.cc @@ -111,7 +111,7 @@ void FSEventWrap::Initialize(Local target, Local get_initialized_templ = FunctionTemplate::New(env->isolate(), GetInitialized, - env->as_external(), + env->as_callback_data(), Signature::New(env->isolate(), t)); t->PrototypeTemplate()->SetAccessorProperty( diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 5c491717abb5bb..0a93b77bf3aa8b 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -369,7 +369,7 @@ void SecureContext::Initialize(Environment* env, Local target) { Local ctx_getter_templ = FunctionTemplate::New(env->isolate(), CtxGetter, - env->as_external(), + env->as_callback_data(), Signature::New(env->isolate(), t)); @@ -4762,7 +4762,7 @@ void DiffieHellman::Initialize(Environment* env, Local target) { Local verify_error_getter_templ = FunctionTemplate::New(env->isolate(), DiffieHellman::VerifyErrorGetter, - env->as_external(), + env->as_callback_data(), Signature::New(env->isolate(), t), /* length */ 0, ConstructorBehavior::kThrow, diff --git a/src/node_env_var.cc b/src/node_env_var.cc index d994a2199a5202..7e4913702b86cd 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -212,7 +212,7 @@ static void EnvEnumerator(const PropertyCallbackInfo& info) { MaybeLocal CreateEnvVarProxy(Local context, Isolate* isolate, - Local data) { + Local data) { EscapableHandleScope scope(isolate); Local env_proxy_template = ObjectTemplate::New(isolate); env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration( diff --git a/src/node_process.h b/src/node_process.h index 452805ada31449..4edf10a1c546eb 100644 --- a/src/node_process.h +++ b/src/node_process.h @@ -9,7 +9,7 @@ namespace node { v8::MaybeLocal CreateEnvVarProxy(v8::Local context, v8::Isolate* isolate, - v8::Local data); + v8::Local data); // Most of the time, it's best to use `console.error` to write // to the process.stderr stream. However, in some cases, such as diff --git a/src/node_process_object.cc b/src/node_process_object.cc index a7af5e2d32b972..b424920c629e69 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -92,7 +92,7 @@ MaybeLocal CreateProcessObject( title_string, ProcessTitleGetter, env->owns_process_state() ? ProcessTitleSetter : nullptr, - env->as_external(), + env->as_callback_data(), DEFAULT, None, SideEffectType::kHasNoSideEffect) @@ -152,7 +152,7 @@ MaybeLocal CreateProcessObject( .ToLocalChecked()).FromJust(); Local env_var_proxy; - if (!CreateEnvVarProxy(context, isolate, env->as_external()) + if (!CreateEnvVarProxy(context, isolate, env->as_callback_data()) .ToLocal(&env_var_proxy)) return MaybeLocal(); @@ -322,7 +322,7 @@ MaybeLocal CreateProcessObject( debug_port_string, DebugPortGetter, env->owns_process_state() ? DebugPortSetter : nullptr, - env->as_external()) + env->as_callback_data()) .FromJust()); // process._rawDebug: may be overwritten later in JS land, but should be diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 3247a604b43616..7e1ba369220153 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -137,7 +137,7 @@ Local LibuvStreamWrap::GetConstructorTemplate( Local get_write_queue_size = FunctionTemplate::New(env->isolate(), GetWriteQueueSize, - env->as_external(), + env->as_callback_data(), Signature::New(env->isolate(), tmpl)); tmpl->PrototypeTemplate()->SetAccessorProperty( env->write_queue_size_string(), diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 3f39e7a91ce611..4e5acbfae3785a 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -927,7 +927,7 @@ void TLSWrap::Initialize(Local target, Local get_write_queue_size = FunctionTemplate::New(env->isolate(), GetWriteQueueSize, - env->as_external(), + env->as_callback_data(), Signature::New(env->isolate(), t)); t->PrototypeTemplate()->SetAccessorProperty( env->write_queue_size_string(), diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 449f17c9430e9f..82b1dfb54c69d1 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -105,7 +105,7 @@ void UDPWrap::Initialize(Local target, Local get_fd_templ = FunctionTemplate::New(env->isolate(), UDPWrap::GetFD, - env->as_external(), + env->as_callback_data(), signature); t->PrototypeTemplate()->SetAccessorProperty(env->fd_string(),