|
102 | 102 | NODE_BUILTIN_BINDINGS(V)
|
103 | 103 | #undef V
|
104 | 104 |
|
| 105 | +#define V(modname) \ |
| 106 | + void _register_isolate_##modname(node::IsolateData* isolate_data, \ |
| 107 | + v8::Local<v8::FunctionTemplate> target); |
| 108 | +NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) |
| 109 | +#undef V |
| 110 | + |
105 | 111 | #ifdef _AIX
|
106 | 112 | // On AIX, dlopen() behaves differently from other operating systems, in that
|
107 | 113 | // it returns unique values from each call, rather than identical values, when
|
@@ -229,9 +235,12 @@ static bool libc_may_be_musl() { return false; }
|
229 | 235 | namespace node {
|
230 | 236 |
|
231 | 237 | using v8::Context;
|
| 238 | +using v8::EscapableHandleScope; |
232 | 239 | using v8::Exception;
|
233 |
| -using v8::Function; |
234 | 240 | using v8::FunctionCallbackInfo;
|
| 241 | +using v8::FunctionTemplate; |
| 242 | +using v8::HandleScope; |
| 243 | +using v8::Isolate; |
235 | 244 | using v8::Local;
|
236 | 245 | using v8::Object;
|
237 | 246 | using v8::String;
|
@@ -552,50 +561,86 @@ inline struct node_module* FindModule(struct node_module* list,
|
552 | 561 | return mp;
|
553 | 562 | }
|
554 | 563 |
|
555 |
| -static Local<Object> InitInternalBinding(Environment* env, |
556 |
| - node_module* mod, |
557 |
| - Local<String> module) { |
558 |
| - // Internal bindings don't have a "module" object, only exports. |
559 |
| - Local<Function> ctor = env->binding_data_ctor_template() |
560 |
| - ->GetFunction(env->context()) |
561 |
| - .ToLocalChecked(); |
562 |
| - Local<Object> exports = ctor->NewInstance(env->context()).ToLocalChecked(); |
| 564 | +void CreateInternalBindingTemplates(IsolateData* isolate_data) { |
| 565 | +#define V(modname) \ |
| 566 | + do { \ |
| 567 | + Local<FunctionTemplate> templ = \ |
| 568 | + FunctionTemplate::New(isolate_data->isolate()); \ |
| 569 | + templ->InstanceTemplate()->SetInternalFieldCount( \ |
| 570 | + BaseObject::kInternalFieldCount); \ |
| 571 | + templ->Inherit(BaseObject::GetConstructorTemplate(isolate_data)); \ |
| 572 | + _register_isolate_##modname(isolate_data, templ); \ |
| 573 | + isolate_data->set_##modname##_binding(templ); \ |
| 574 | + } while (0); |
| 575 | + NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) |
| 576 | +#undef V |
| 577 | +} |
| 578 | + |
| 579 | +static Local<Object> GetInternalBindingExportObject(IsolateData* isolate_data, |
| 580 | + const char* mod_name, |
| 581 | + Local<Context> context) { |
| 582 | + Local<FunctionTemplate> ctor; |
| 583 | +#define V(name) \ |
| 584 | + if (strcmp(mod_name, #name) == 0) { \ |
| 585 | + ctor = isolate_data->name##_binding(); \ |
| 586 | + } else // NOLINT(readability/braces) |
| 587 | + NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) |
| 588 | +#undef V |
| 589 | + { |
| 590 | + ctor = isolate_data->binding_data_ctor_template(); |
| 591 | + } |
| 592 | + |
| 593 | + Local<Object> obj = ctor->GetFunction(context) |
| 594 | + .ToLocalChecked() |
| 595 | + ->NewInstance(context) |
| 596 | + .ToLocalChecked(); |
| 597 | + return obj; |
| 598 | +} |
| 599 | + |
| 600 | +static Local<Object> InitInternalBinding(Realm* realm, node_module* mod) { |
| 601 | + EscapableHandleScope scope(realm->isolate()); |
| 602 | + Local<Context> context = realm->context(); |
| 603 | + Local<Object> exports = GetInternalBindingExportObject( |
| 604 | + realm->isolate_data(), mod->nm_modname, context); |
563 | 605 | CHECK_NULL(mod->nm_register_func);
|
564 | 606 | CHECK_NOT_NULL(mod->nm_context_register_func);
|
565 |
| - Local<Value> unused = Undefined(env->isolate()); |
566 |
| - mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv); |
567 |
| - return exports; |
| 607 | + Local<Value> unused = Undefined(realm->isolate()); |
| 608 | + // Internal bindings don't have a "module" object, only exports. |
| 609 | + mod->nm_context_register_func(exports, unused, context, mod->nm_priv); |
| 610 | + return scope.Escape(exports); |
568 | 611 | }
|
569 | 612 |
|
570 | 613 | void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
|
571 |
| - Environment* env = Environment::GetCurrent(args); |
| 614 | + Realm* realm = Realm::GetCurrent(args); |
| 615 | + Isolate* isolate = realm->isolate(); |
| 616 | + HandleScope scope(isolate); |
| 617 | + Local<Context> context = realm->context(); |
572 | 618 |
|
573 | 619 | CHECK(args[0]->IsString());
|
574 | 620 |
|
575 | 621 | Local<String> module = args[0].As<String>();
|
576 |
| - node::Utf8Value module_v(env->isolate(), module); |
| 622 | + node::Utf8Value module_v(isolate, module); |
577 | 623 | Local<Object> exports;
|
578 | 624 |
|
579 | 625 | node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL);
|
580 | 626 | if (mod != nullptr) {
|
581 |
| - exports = InitInternalBinding(env, mod, module); |
582 |
| - env->internal_bindings.insert(mod); |
| 627 | + exports = InitInternalBinding(realm, mod); |
| 628 | + realm->internal_bindings.insert(mod); |
583 | 629 | } else if (!strcmp(*module_v, "constants")) {
|
584 |
| - exports = Object::New(env->isolate()); |
585 |
| - CHECK( |
586 |
| - exports->SetPrototype(env->context(), Null(env->isolate())).FromJust()); |
587 |
| - DefineConstants(env->isolate(), exports); |
| 630 | + exports = Object::New(isolate); |
| 631 | + CHECK(exports->SetPrototype(context, Null(isolate)).FromJust()); |
| 632 | + DefineConstants(isolate, exports); |
588 | 633 | } else if (!strcmp(*module_v, "natives")) {
|
589 |
| - exports = builtins::BuiltinLoader::GetSourceObject(env->context()); |
| 634 | + exports = builtins::BuiltinLoader::GetSourceObject(context); |
590 | 635 | // Legacy feature: process.binding('natives').config contains stringified
|
591 | 636 | // config.gypi
|
592 | 637 | CHECK(exports
|
593 |
| - ->Set(env->context(), |
594 |
| - env->config_string(), |
595 |
| - builtins::BuiltinLoader::GetConfigString(env->isolate())) |
| 638 | + ->Set(context, |
| 639 | + realm->isolate_data()->config_string(), |
| 640 | + builtins::BuiltinLoader::GetConfigString(isolate)) |
596 | 641 | .FromJust());
|
597 | 642 | } else {
|
598 |
| - return THROW_ERR_INVALID_MODULE(env, "No such binding: %s", *module_v); |
| 643 | + return THROW_ERR_INVALID_MODULE(isolate, "No such binding: %s", *module_v); |
599 | 644 | }
|
600 | 645 |
|
601 | 646 | args.GetReturnValue().Set(exports);
|
|
0 commit comments