-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
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
Merged
nodejs-github-bot
merged 12 commits into
nodejs:main
from
aduh95:define-lazy-properties
Feb 18, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0df0568
src: port `defineLazyProperties` to native code
aduh95 2f9a8b7
fixup! src: port `defineLazyProperties` to native code
aduh95 cbe1f31
fixup! src: port `defineLazyProperties` to native code
aduh95 f0cb5d4
fixup! src: port `defineLazyProperties` to native code
aduh95 b9a85f4
Apply suggestions from code review
aduh95 7792537
Apply suggestions from code review
aduh95 71fec7a
Apply suggestions from code review
aduh95 019fed5
f
aduh95 c89ce29
f
aduh95 af7bb75
declare when not building snapshot
aduh95 fde6281
Revert "declare when not building snapshot"
aduh95 55cf9bb
RegisterExternalReferences
aduh95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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