Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
src: port defineLazyProperties to native code#57081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
0df05682f9a8b7cbe1f31f0cb5d4b9a85f4779253771fec7a019fed5c89ce29af7bb75fde628155cf9bbFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -348,6 +348,69 @@ static void IsInsideNodeModules(const FunctionCallbackInfo<Value>& args) { | ||
| args.GetReturnValue().Set(result); | ||
| } | ||
| static void DefineLazyPropertiesGetter( | ||
| Local<v8::Name> name, const v8::PropertyCallbackInfo<Value>& info) { | ||
| Realm* realm = Realm::GetCurrent(info); | ||
| Isolate* isolate = realm->isolate(); | ||
| auto context = isolate->GetCurrentContext(); | ||
| Local<Value> arg = info.Data(); | ||
| Local<Value> require_result; | ||
| if (!realm->builtin_module_require() | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth to cache the modules in C++ or just pass the | ||
| ->Call(context, Null(isolate), 1, &arg) | ||
| .ToLocal(&require_result)) { | ||
| // V8 will have scheduled an error to be thrown. | ||
| return; | ||
| } | ||
| Local<Value> ret; | ||
| if (!require_result.As<v8::Object>()->Get(context, name).ToLocal(&ret)) { | ||
| // V8 will have scheduled an error to be thrown. | ||
| return; | ||
| } | ||
| info.GetReturnValue().Set(ret); | ||
| } | ||
| static void DefineLazyProperties(const FunctionCallbackInfo<Value>& args) { | ||
| // target: object, id: string, keys: string[][, enumerable = true] | ||
| CHECK_GE(args.Length(), 3); | ||
| // target: Object where to define the lazy properties. | ||
| CHECK(args[0]->IsObject()); | ||
| // id: Internal module to lazy-load where the API to expose are implemented. | ||
| CHECK(args[1]->IsString()); | ||
| // keys: Keys to map from `require(id)` and `target`. | ||
| CHECK(args[2]->IsArray()); | ||
| // enumerable: Whether the property should be enumerable. | ||
| CHECK(args.Length() == 3 || args[3]->IsBoolean()); | ||
| Environment* env = Environment::GetCurrent(args); | ||
| Isolate* isolate = env->isolate(); | ||
| auto context = isolate->GetCurrentContext(); | ||
| auto target = args[0].As<Object>(); | ||
| Local<Value> id = args[1]; | ||
| v8::PropertyAttribute attribute = | ||
| args.Length() == 3 || args[3]->IsTrue() ? v8::None : v8::DontEnum; | ||
| const Local<Array> keys = args[2].As<Array>(); | ||
| size_t length = keys->Length(); | ||
| for (size_t i = 0; i < length; i++) { | ||
| Local<Value> key; | ||
| if (!keys->Get(context, i).ToLocal(&key)) { | ||
| // V8 will have scheduled an error to be thrown. | ||
| return; | ||
| } | ||
| CHECK(key->IsString()); | ||
| if (target | ||
| ->SetLazyDataProperty(context, | ||
| key.As<String>(), | ||
| DefineLazyPropertiesGetter, | ||
| id, | ||
| attribute) | ||
| .IsNothing()) { | ||
| // V8 will have scheduled an error to be thrown. | ||
| return; | ||
| }; | ||
| } | ||
| } | ||
| void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | ||
| registry->Register(GetPromiseDetails); | ||
| registry->Register(GetProxyDetails); | ||
| @@ -364,6 +427,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | ||
| registry->Register(fast_guess_handle_type_.GetTypeInfo()); | ||
| registry->Register(ParseEnv); | ||
| registry->Register(IsInsideNodeModules); | ||
| registry->Register(DefineLazyProperties); | ||
| registry->Register(DefineLazyPropertiesGetter); | ||
| } | ||
| void Initialize(Local<Object> target, | ||
| @@ -448,6 +513,7 @@ void Initialize(Local<Object> target, | ||
| } | ||
| SetMethod(context, target, "isInsideNodeModules", IsInsideNodeModules); | ||
| SetMethod(context, target, "defineLazyProperties", DefineLazyProperties); | ||
| SetMethodNoSideEffect( | ||
| context, target, "getPromiseDetails", GetPromiseDetails); | ||
| SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think instead of doing it in JS -> C++, we might as well just do it in
BootstrapRealm()implementations directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to do it in a follow up if no one beats me to it