|
110 | 110 | NODE_BUILTIN_BINDINGS(V) |
111 | 111 | #undef V |
112 | 112 |
|
| 113 | +#define V(modname) \ |
| 114 | + void _register_isolate_##modname(node::IsolateData* isolate_data, \ |
| 115 | + v8::Local<v8::FunctionTemplate> target); |
| 116 | +NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) |
| 117 | +#undef V |
| 118 | + |
113 | 119 | #ifdef _AIX |
114 | 120 | // On AIX, dlopen() behaves differently from other operating systems, in that |
115 | 121 | // it returns unique values from each call, rather than identical values, when |
@@ -237,9 +243,12 @@ static bool libc_may_be_musl() { return false; } |
237 | 243 | namespace node { |
238 | 244 |
|
239 | 245 | using v8::Context; |
| 246 | +using v8::EscapableHandleScope; |
240 | 247 | using v8::Exception; |
241 | | -using v8::Function; |
242 | 248 | using v8::FunctionCallbackInfo; |
| 249 | +using v8::FunctionTemplate; |
| 250 | +using v8::HandleScope; |
| 251 | +using v8::Isolate; |
243 | 252 | using v8::Local; |
244 | 253 | using v8::Object; |
245 | 254 | using v8::String; |
@@ -584,50 +593,86 @@ inline struct node_module* FindModule(struct node_module* list, |
584 | 593 | return mp; |
585 | 594 | } |
586 | 595 |
|
587 | | -static Local<Object> InitInternalBinding(Environment* env, |
588 | | - node_module* mod, |
589 | | - Local<String> module) { |
590 | | - // Internal bindings don't have a "module" object, only exports. |
591 | | - Local<Function> ctor = env->binding_data_ctor_template() |
592 | | - ->GetFunction(env->context()) |
593 | | - .ToLocalChecked(); |
594 | | - Local<Object> exports = ctor->NewInstance(env->context()).ToLocalChecked(); |
| 596 | +void CreateInternalBindingTemplates(IsolateData* isolate_data) { |
| 597 | +#define V(modname) \ |
| 598 | + do { \ |
| 599 | + Local<FunctionTemplate> templ = \ |
| 600 | + FunctionTemplate::New(isolate_data->isolate()); \ |
| 601 | + templ->InstanceTemplate()->SetInternalFieldCount( \ |
| 602 | + BaseObject::kInternalFieldCount); \ |
| 603 | + templ->Inherit(BaseObject::GetConstructorTemplate(isolate_data)); \ |
| 604 | + _register_isolate_##modname(isolate_data, templ); \ |
| 605 | + isolate_data->set_##modname##_binding(templ); \ |
| 606 | + } while (0); |
| 607 | + NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) |
| 608 | +#undef V |
| 609 | +} |
| 610 | + |
| 611 | +static Local<Object> GetInternalBindingExportObject(IsolateData* isolate_data, |
| 612 | + const char* mod_name, |
| 613 | + Local<Context> context) { |
| 614 | + Local<FunctionTemplate> ctor; |
| 615 | +#define V(name) \ |
| 616 | + if (strcmp(mod_name, #name) == 0) { \ |
| 617 | + ctor = isolate_data->name##_binding(); \ |
| 618 | + } else // NOLINT(readability/braces) |
| 619 | + NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) |
| 620 | +#undef V |
| 621 | + { |
| 622 | + ctor = isolate_data->binding_data_ctor_template(); |
| 623 | + } |
| 624 | + |
| 625 | + Local<Object> obj = ctor->GetFunction(context) |
| 626 | + .ToLocalChecked() |
| 627 | + ->NewInstance(context) |
| 628 | + .ToLocalChecked(); |
| 629 | + return obj; |
| 630 | +} |
| 631 | + |
| 632 | +static Local<Object> InitInternalBinding(Realm* realm, node_module* mod) { |
| 633 | + EscapableHandleScope scope(realm->isolate()); |
| 634 | + Local<Context> context = realm->context(); |
| 635 | + Local<Object> exports = GetInternalBindingExportObject( |
| 636 | + realm->isolate_data(), mod->nm_modname, context); |
595 | 637 | CHECK_NULL(mod->nm_register_func); |
596 | 638 | CHECK_NOT_NULL(mod->nm_context_register_func); |
597 | | - Local<Value> unused = Undefined(env->isolate()); |
598 | | - mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv); |
599 | | - return exports; |
| 639 | + Local<Value> unused = Undefined(realm->isolate()); |
| 640 | + // Internal bindings don't have a "module" object, only exports. |
| 641 | + mod->nm_context_register_func(exports, unused, context, mod->nm_priv); |
| 642 | + return scope.Escape(exports); |
600 | 643 | } |
601 | 644 |
|
602 | 645 | void GetInternalBinding(const FunctionCallbackInfo<Value>& args) { |
603 | | - Environment* env = Environment::GetCurrent(args); |
| 646 | + Realm* realm = Realm::GetCurrent(args); |
| 647 | + Isolate* isolate = realm->isolate(); |
| 648 | + HandleScope scope(isolate); |
| 649 | + Local<Context> context = realm->context(); |
604 | 650 |
|
605 | 651 | CHECK(args[0]->IsString()); |
606 | 652 |
|
607 | 653 | Local<String> module = args[0].As<String>(); |
608 | | - node::Utf8Value module_v(env->isolate(), module); |
| 654 | + node::Utf8Value module_v(isolate, module); |
609 | 655 | Local<Object> exports; |
610 | 656 |
|
611 | 657 | node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL); |
612 | 658 | if (mod != nullptr) { |
613 | | - exports = InitInternalBinding(env, mod, module); |
614 | | - env->internal_bindings.insert(mod); |
| 659 | + exports = InitInternalBinding(realm, mod); |
| 660 | + realm->internal_bindings.insert(mod); |
615 | 661 | } else if (!strcmp(*module_v, "constants")) { |
616 | | - exports = Object::New(env->isolate()); |
617 | | - CHECK( |
618 | | - exports->SetPrototype(env->context(), Null(env->isolate())).FromJust()); |
619 | | - DefineConstants(env->isolate(), exports); |
| 662 | + exports = Object::New(isolate); |
| 663 | + CHECK(exports->SetPrototype(context, Null(isolate)).FromJust()); |
| 664 | + DefineConstants(isolate, exports); |
620 | 665 | } else if (!strcmp(*module_v, "natives")) { |
621 | | - exports = builtins::BuiltinLoader::GetSourceObject(env->context()); |
| 666 | + exports = builtins::BuiltinLoader::GetSourceObject(context); |
622 | 667 | // Legacy feature: process.binding('natives').config contains stringified |
623 | 668 | // config.gypi |
624 | 669 | CHECK(exports |
625 | | - ->Set(env->context(), |
626 | | - env->config_string(), |
627 | | - builtins::BuiltinLoader::GetConfigString(env->isolate())) |
| 670 | + ->Set(context, |
| 671 | + realm->isolate_data()->config_string(), |
| 672 | + builtins::BuiltinLoader::GetConfigString(isolate)) |
628 | 673 | .FromJust()); |
629 | 674 | } else { |
630 | | - return THROW_ERR_INVALID_MODULE(env, "No such binding: %s", *module_v); |
| 675 | + return THROW_ERR_INVALID_MODULE(isolate, "No such binding: %s", *module_v); |
631 | 676 | } |
632 | 677 |
|
633 | 678 | args.GetReturnValue().Set(exports); |
|
0 commit comments