-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
src: port initializeImportMeta
to native
#57286
base: main
Are you sure you want to change the base?
Conversation
Review requested:
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #57286 +/- ##
==========================================
- Coverage 90.24% 90.24% -0.01%
==========================================
Files 630 630
Lines 184908 184970 +62
Branches 36181 36203 +22
==========================================
+ Hits 166874 166927 +53
- Misses 11061 11073 +12
+ Partials 6973 6970 -3
|
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'm not great with cpp, but it looks fine AFAICT, and conceptually checks out 🙂
|
||
meta.url = url; | ||
_initializeImportMeta( | ||
meta, url, StringPrototypeStartsWith(url, 'file:'), |
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.
meta, url, StringPrototypeStartsWith(url, 'file:'), | |
meta, | |
url, | |
StringPrototypeStartsWith(url, 'file:'), |
// This getter has no JavaScript function representation and is not | ||
// invoked in the creation context. | ||
// When this getter is invoked in a vm context, the `Realm::GetCurrent(info)` | ||
// returns a nullptr and. Retrieve the creation context via `this` object and |
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 there's a typo here: and. Retrieve
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.
target | ||
->SetLazyDataProperty(context, | ||
FIXED_ONE_BYTE_STRING(isolate, "dirname"), | ||
InitImportMetaLazyGetter, | ||
args[1]) | ||
.Check(); | ||
target | ||
->SetLazyDataProperty(context, | ||
FIXED_ONE_BYTE_STRING(isolate, "filename"), | ||
InitImportMetaLazyGetter, | ||
args[1]) | ||
.Check(); |
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.
We could use env properties.
target | |
->SetLazyDataProperty(context, | |
FIXED_ONE_BYTE_STRING(isolate, "dirname"), | |
InitImportMetaLazyGetter, | |
args[1]) | |
.Check(); | |
target | |
->SetLazyDataProperty(context, | |
FIXED_ONE_BYTE_STRING(isolate, "filename"), | |
InitImportMetaLazyGetter, | |
args[1]) | |
.Check(); | |
target | |
->SetLazyDataProperty(context, | |
env->dirname_string(), | |
InitImportMetaLazyGetter, | |
args[1]) | |
.Check(); | |
target | |
->SetLazyDataProperty(context, | |
env->filename_string(), | |
InitImportMetaLazyGetter, | |
args[1]) | |
.Check(); |
Would need to retrieve env
. Maybe with Environment* env = Environment::GetCurrent(args);
.
And would need to add missing entries to https://github.com/nodejs/node/blob/main/src/env_properties.h
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.
What would be the reasoning for doing that?
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'm too new to the codebase to have a deep explanation.
Here are my newbie reasons: it makes the code cleaner and it has been used in the C++ land (at least in my recent C++ PRs, more experienced people suggested I do this)
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.
Well, env->filename_string()
already exists so it would make sense to use it rather than creating a new copy. If we're using that then we may as well as one for dirname... keeping in mind, however, that there is already one for __dirname
.
target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "resolve"), args[3]) | ||
.Check(); | ||
} | ||
target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "url"), args[1]).Check(); |
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.
Like the previous comment
target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "resolve"), args[3]) | |
.Check(); | |
} | |
target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "url"), args[1]).Check(); | |
target->Set(context, env->resolve_string(), args[3]) | |
.Check(); | |
} | |
target->Set(context, env->url_string(), args[1]).Check(); |
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 the original implementation is much easier to understand and maintain.
Do you have any suggestions on how to improve it? |
Supersedes #57003.
We should probably avoid translating the URL to a path twice when the user needs both
dirname
andfilename
, but I'm not sure if there's an elegant way to do it without crossing the C++/JS bundary.