|
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; |
@@ -571,50 +580,86 @@ inline struct node_module* FindModule(struct node_module* list, |
571 | 580 | return mp; |
572 | 581 | } |
573 | 582 |
|
574 | | -static Local<Object> InitInternalBinding(Environment* env, |
575 | | - node_module* mod, |
576 | | - Local<String> module) { |
577 | | - // Internal bindings don't have a "module" object, only exports. |
578 | | - Local<Function> ctor = env->binding_data_ctor_template() |
579 | | - ->GetFunction(env->context()) |
580 | | - .ToLocalChecked(); |
581 | | - Local<Object> exports = ctor->NewInstance(env->context()).ToLocalChecked(); |
| 583 | +void CreateInternalBindingTemplates(IsolateData* isolate_data) { |
| 584 | +#define V(modname) \ |
| 585 | + do { \ |
| 586 | + Local<FunctionTemplate> templ = \ |
| 587 | + FunctionTemplate::New(isolate_data->isolate()); \ |
| 588 | + templ->InstanceTemplate()->SetInternalFieldCount( \ |
| 589 | + BaseObject::kInternalFieldCount); \ |
| 590 | + templ->Inherit(BaseObject::GetConstructorTemplate(isolate_data)); \ |
| 591 | + _register_isolate_##modname(isolate_data, templ); \ |
| 592 | + isolate_data->set_##modname##_binding(templ); \ |
| 593 | + } while (0); |
| 594 | + NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) |
| 595 | +#undef V |
| 596 | +} |
| 597 | + |
| 598 | +static Local<Object> GetInternalBindingExportObject(IsolateData* isolate_data, |
| 599 | + const char* mod_name, |
| 600 | + Local<Context> context) { |
| 601 | + Local<FunctionTemplate> ctor; |
| 602 | +#define V(name) \ |
| 603 | + if (strcmp(mod_name, #name) == 0) { \ |
| 604 | + ctor = isolate_data->name##_binding(); \ |
| 605 | + } else // NOLINT(readability/braces) |
| 606 | + NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) |
| 607 | +#undef V |
| 608 | + { |
| 609 | + ctor = isolate_data->binding_data_ctor_template(); |
| 610 | + } |
| 611 | + |
| 612 | + Local<Object> obj = ctor->GetFunction(context) |
| 613 | + .ToLocalChecked() |
| 614 | + ->NewInstance(context) |
| 615 | + .ToLocalChecked(); |
| 616 | + return obj; |
| 617 | +} |
| 618 | + |
| 619 | +static Local<Object> InitInternalBinding(Realm* realm, node_module* mod) { |
| 620 | + EscapableHandleScope scope(realm->isolate()); |
| 621 | + Local<Context> context = realm->context(); |
| 622 | + Local<Object> exports = GetInternalBindingExportObject( |
| 623 | + realm->isolate_data(), mod->nm_modname, context); |
582 | 624 | CHECK_NULL(mod->nm_register_func); |
583 | 625 | CHECK_NOT_NULL(mod->nm_context_register_func); |
584 | | - Local<Value> unused = Undefined(env->isolate()); |
585 | | - mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv); |
586 | | - return exports; |
| 626 | + Local<Value> unused = Undefined(realm->isolate()); |
| 627 | + // Internal bindings don't have a "module" object, only exports. |
| 628 | + mod->nm_context_register_func(exports, unused, context, mod->nm_priv); |
| 629 | + return scope.Escape(exports); |
587 | 630 | } |
588 | 631 |
|
589 | 632 | void GetInternalBinding(const FunctionCallbackInfo<Value>& args) { |
590 | | - Environment* env = Environment::GetCurrent(args); |
| 633 | + Realm* realm = Realm::GetCurrent(args); |
| 634 | + Isolate* isolate = realm->isolate(); |
| 635 | + HandleScope scope(isolate); |
| 636 | + Local<Context> context = realm->context(); |
591 | 637 |
|
592 | 638 | CHECK(args[0]->IsString()); |
593 | 639 |
|
594 | 640 | Local<String> module = args[0].As<String>(); |
595 | | - node::Utf8Value module_v(env->isolate(), module); |
| 641 | + node::Utf8Value module_v(isolate, module); |
596 | 642 | Local<Object> exports; |
597 | 643 |
|
598 | 644 | node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL); |
599 | 645 | if (mod != nullptr) { |
600 | | - exports = InitInternalBinding(env, mod, module); |
601 | | - env->internal_bindings.insert(mod); |
| 646 | + exports = InitInternalBinding(realm, mod); |
| 647 | + realm->internal_bindings.insert(mod); |
602 | 648 | } else if (!strcmp(*module_v, "constants")) { |
603 | | - exports = Object::New(env->isolate()); |
604 | | - CHECK( |
605 | | - exports->SetPrototype(env->context(), Null(env->isolate())).FromJust()); |
606 | | - DefineConstants(env->isolate(), exports); |
| 649 | + exports = Object::New(isolate); |
| 650 | + CHECK(exports->SetPrototype(context, Null(isolate)).FromJust()); |
| 651 | + DefineConstants(isolate, exports); |
607 | 652 | } else if (!strcmp(*module_v, "natives")) { |
608 | | - exports = builtins::BuiltinLoader::GetSourceObject(env->context()); |
| 653 | + exports = builtins::BuiltinLoader::GetSourceObject(context); |
609 | 654 | // Legacy feature: process.binding('natives').config contains stringified |
610 | 655 | // config.gypi |
611 | 656 | CHECK(exports |
612 | | - ->Set(env->context(), |
613 | | - env->config_string(), |
614 | | - builtins::BuiltinLoader::GetConfigString(env->isolate())) |
| 657 | + ->Set(context, |
| 658 | + realm->isolate_data()->config_string(), |
| 659 | + builtins::BuiltinLoader::GetConfigString(isolate)) |
615 | 660 | .FromJust()); |
616 | 661 | } else { |
617 | | - return THROW_ERR_INVALID_MODULE(env, "No such binding: %s", *module_v); |
| 662 | + return THROW_ERR_INVALID_MODULE(isolate, "No such binding: %s", *module_v); |
618 | 663 | } |
619 | 664 |
|
620 | 665 | args.GetReturnValue().Set(exports); |
|
0 commit comments