-
-
Couldn't load subscription status.
- Fork 33.6k
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
Changes from all commits
0df0568
2f9a8b7
cbe1f31
f0cb5d4
b9a85f4
7792537
71fec7a
019fed5
c89ce29
af7bb75
fde6281
55cf9bb
File filter
Filter by extension
Conversations
Jump to
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() | ||
|
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