From 1b84ddeec23ecbcf133f265ccb4da539d6182629 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 6 May 2023 17:01:34 +0200 Subject: [PATCH] src: implement constants binding directly Instead of adding a special case for it in the internal binding loader, just implement it as usual using a per-context property initializer. PR-URL: https://github.com/nodejs/node/pull/48186 Reviewed-By: James M Snell Reviewed-By: Chengzhong Wu --- src/node_binding.cc | 6 +----- src/node_constants.cc | 20 ++++++++++++++++---- src/node_constants.h | 5 ----- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/node_binding.cc b/src/node_binding.cc index 1d098eedbfbd66..97257d47c61738 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -34,6 +34,7 @@ V(builtins) \ V(cares_wrap) \ V(config) \ + V(constants) \ V(contextify) \ V(credentials) \ V(encoding_binding) \ @@ -619,7 +620,6 @@ void GetInternalBinding(const FunctionCallbackInfo& args) { Realm* realm = Realm::GetCurrent(args); Isolate* isolate = realm->isolate(); HandleScope scope(isolate); - Local context = realm->context(); CHECK(args[0]->IsString()); @@ -631,10 +631,6 @@ void GetInternalBinding(const FunctionCallbackInfo& args) { if (mod != nullptr) { exports = InitInternalBinding(realm, mod); realm->internal_bindings.insert(mod); - } else if (!strcmp(*module_v, "constants")) { - exports = Object::New(isolate); - CHECK(exports->SetPrototype(context, Null(isolate)).FromJust()); - DefineConstants(isolate, exports); } else { return THROW_ERR_INVALID_MODULE(isolate, "No such binding: %s", *module_v); } diff --git a/src/node_constants.cc b/src/node_constants.cc index 68b457fcd42aaa..05582b28dafd9f 100644 --- a/src/node_constants.cc +++ b/src/node_constants.cc @@ -63,10 +63,14 @@ namespace node { +using v8::Context; +using v8::Isolate; using v8::Local; +using v8::Null; using v8::Object; +using v8::Value; -namespace { +namespace constants { void DefineErrnoConstants(Local target) { #ifdef E2BIG @@ -1270,10 +1274,14 @@ void DefineTraceConstants(Local target) { NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_LINK_IDS); } -} // anonymous namespace +void CreatePerContextProperties(Local target, + Local unused, + Local context, + void* priv) { + Isolate* isolate = context->GetIsolate(); + Environment* env = Environment::GetCurrent(context); -void DefineConstants(v8::Isolate* isolate, Local target) { - Environment* env = Environment::GetCurrent(isolate); + CHECK(target->SetPrototype(env->context(), Null(env->isolate())).FromJust()); Local os_constants = Object::New(isolate); CHECK(os_constants->SetPrototype(env->context(), @@ -1353,4 +1361,8 @@ void DefineConstants(v8::Isolate* isolate, Local target) { trace_constants).Check(); } +} // namespace constants } // namespace node + +NODE_BINDING_CONTEXT_AWARE_INTERNAL(constants, + node::constants::CreatePerContextProperties) diff --git a/src/node_constants.h b/src/node_constants.h index d7de705fb8ec7e..08f94bd623aa60 100644 --- a/src/node_constants.h +++ b/src/node_constants.h @@ -74,11 +74,6 @@ #endif // NODE_OPENSSL_DEFAULT_CIPHER_LIST #endif // HAVE_OPENSSL -namespace node { - -void DefineConstants(v8::Isolate* isolate, v8::Local target); -} // namespace node - #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #endif // SRC_NODE_CONSTANTS_H_