Skip to content

Commit 1b84dde

Browse files
joyeecheungRafaelGSS
authored andcommitted
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: #48186 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 06d49c1 commit 1b84dde

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/node_binding.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
V(builtins) \
3535
V(cares_wrap) \
3636
V(config) \
37+
V(constants) \
3738
V(contextify) \
3839
V(credentials) \
3940
V(encoding_binding) \
@@ -619,7 +620,6 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
619620
Realm* realm = Realm::GetCurrent(args);
620621
Isolate* isolate = realm->isolate();
621622
HandleScope scope(isolate);
622-
Local<Context> context = realm->context();
623623

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

@@ -631,10 +631,6 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
631631
if (mod != nullptr) {
632632
exports = InitInternalBinding(realm, mod);
633633
realm->internal_bindings.insert(mod);
634-
} else if (!strcmp(*module_v, "constants")) {
635-
exports = Object::New(isolate);
636-
CHECK(exports->SetPrototype(context, Null(isolate)).FromJust());
637-
DefineConstants(isolate, exports);
638634
} else {
639635
return THROW_ERR_INVALID_MODULE(isolate, "No such binding: %s", *module_v);
640636
}

src/node_constants.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@
6363

6464
namespace node {
6565

66+
using v8::Context;
67+
using v8::Isolate;
6668
using v8::Local;
69+
using v8::Null;
6770
using v8::Object;
71+
using v8::Value;
6872

69-
namespace {
73+
namespace constants {
7074

7175
void DefineErrnoConstants(Local<Object> target) {
7276
#ifdef E2BIG
@@ -1270,10 +1274,14 @@ void DefineTraceConstants(Local<Object> target) {
12701274
NODE_DEFINE_CONSTANT(target, TRACE_EVENT_PHASE_LINK_IDS);
12711275
}
12721276

1273-
} // anonymous namespace
1277+
void CreatePerContextProperties(Local<Object> target,
1278+
Local<Value> unused,
1279+
Local<Context> context,
1280+
void* priv) {
1281+
Isolate* isolate = context->GetIsolate();
1282+
Environment* env = Environment::GetCurrent(context);
12741283

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

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

1364+
} // namespace constants
13561365
} // namespace node
1366+
1367+
NODE_BINDING_CONTEXT_AWARE_INTERNAL(constants,
1368+
node::constants::CreatePerContextProperties)

src/node_constants.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@
7474
#endif // NODE_OPENSSL_DEFAULT_CIPHER_LIST
7575
#endif // HAVE_OPENSSL
7676

77-
namespace node {
78-
79-
void DefineConstants(v8::Isolate* isolate, v8::Local<v8::Object> target);
80-
} // namespace node
81-
8277
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
8378

8479
#endif // SRC_NODE_CONSTANTS_H_

0 commit comments

Comments
 (0)