Skip to content

Commit

Permalink
src: implement constants binding directly
Browse files Browse the repository at this point in the history
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: nodejs#48186
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
  • Loading branch information
joyeecheung authored and Ceres6 committed Aug 14, 2023
1 parent eb0dd74 commit bd3fd2a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
6 changes: 1 addition & 5 deletions src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
V(builtins) \
V(cares_wrap) \
V(config) \
V(constants) \
V(contextify) \
V(credentials) \
V(encoding_binding) \
Expand Down Expand Up @@ -619,7 +620,6 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);
Isolate* isolate = realm->isolate();
HandleScope scope(isolate);
Local<Context> context = realm->context();

CHECK(args[0]->IsString());

Expand All @@ -631,10 +631,6 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& 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);
}
Expand Down
20 changes: 16 additions & 4 deletions src/node_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> target) {
#ifdef E2BIG
Expand Down Expand Up @@ -1270,10 +1274,14 @@ void DefineTraceConstants(Local<Object> target) {
NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_LINK_IDS);
}

} // anonymous namespace
void CreatePerContextProperties(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Isolate* isolate = context->GetIsolate();
Environment* env = Environment::GetCurrent(context);

void DefineConstants(v8::Isolate* isolate, Local<Object> target) {
Environment* env = Environment::GetCurrent(isolate);
CHECK(target->SetPrototype(env->context(), Null(env->isolate())).FromJust());

Local<Object> os_constants = Object::New(isolate);
CHECK(os_constants->SetPrototype(env->context(),
Expand Down Expand Up @@ -1353,4 +1361,8 @@ void DefineConstants(v8::Isolate* isolate, Local<Object> target) {
trace_constants).Check();
}

} // namespace constants
} // namespace node

NODE_BINDING_CONTEXT_AWARE_INTERNAL(constants,
node::constants::CreatePerContextProperties)
5 changes: 0 additions & 5 deletions src/node_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@
#endif // NODE_OPENSSL_DEFAULT_CIPHER_LIST
#endif // HAVE_OPENSSL

namespace node {

void DefineConstants(v8::Isolate* isolate, v8::Local<v8::Object> target);
} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif // SRC_NODE_CONSTANTS_H_

0 comments on commit bd3fd2a

Please sign in to comment.