Repository navigation
src: port initializeImportMeta to native
#57286
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
+98
−16
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a1edf9c
src: port `initializeImportMeta` to native
aduh95 a3899b0
Apply suggestions from code review
aduh95 7212254
add TODO
aduh95 537613b
only port path helpers to C++
aduh95 e7c7320
add private property to avoid calling ada twice
aduh95 24cc9a6
lint
aduh95 e2beab2
fixup! lint
aduh95 8f5285b
fixup! lint
aduh95 3fa0db0
benchmark: disambiguate `filename` and `dirname` read perf
aduh95 79ec952
fixup! benchmark: disambiguate `filename` and `dirname` read perf
aduh95 077bde3
Revert "add private property to avoid calling ada twice"
aduh95 6b10319
fixup! Revert "add private property to avoid calling ada twice"
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
Next
Next commit
src: port
initializeImportMeta to native
- Loading branch information
commit a1edf9c6821566330bb9ae5f5cd4e2bb6a242ddc
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -594,6 +594,88 @@ void GetCompileCacheEntry(const FunctionCallbackInfo<Value>& args) { | |
| isolate, v8::Null(isolate), names.data(), values.data(), names.size())); | ||
| } | ||
|
|
||
| static void InitImportMetaLazyGetter( | ||
| Local<v8::Name> name, const v8::PropertyCallbackInfo<Value>& info) { | ||
|
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. Nit: remove the |
||
| Isolate* isolate = info.GetIsolate(); | ||
| // 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 | ||
|
aduh95 marked this conversation as resolved.
Outdated
|
||
| // get the creation Realm. | ||
| Local<Value> receiver_val = info.This(); | ||
| if (!receiver_val->IsObject()) { | ||
| THROW_ERR_INVALID_INVOCATION(isolate); | ||
| return; | ||
| } | ||
| Local<Object> receiver = receiver_val.As<Object>(); | ||
| Local<Context> context; | ||
| if (!receiver->GetCreationContext().ToLocal(&context)) { | ||
| THROW_ERR_INVALID_INVOCATION(isolate); | ||
| return; | ||
| } | ||
| Environment* env = Environment::GetCurrent(context); | ||
|
|
||
| node::Utf8Value url(isolate, info.Data()); | ||
| auto file_url = ada::parse(url.ToStringView()); | ||
| CHECK(file_url); | ||
| auto file_path = url::FileURLToPath(env, *file_url); | ||
| CHECK(file_path.has_value()); | ||
| std::string_view ret_view = file_path.value(); | ||
|
|
||
| node::Utf8Value utf8name(isolate, name); | ||
| auto plain_name = utf8name.ToStringView(); | ||
| if (plain_name == "dirname") { | ||
| #ifdef _WIN32 | ||
| #define PATH_SEPARATOR '\\' | ||
| #else | ||
| #define PATH_SEPARATOR '/' | ||
| #endif | ||
| auto index = ret_view.rfind(PATH_SEPARATOR); | ||
| #undef PATH_SEPARATOR | ||
| CHECK(index != std::string_view::npos); | ||
| ret_view.remove_suffix(ret_view.size() - index); | ||
| } | ||
| Local<Value> ret; | ||
| if (ToV8Value(context, ret_view, isolate).ToLocal(&ret)) { | ||
| info.GetReturnValue().Set(ret); | ||
| } | ||
| } | ||
| void InitImportMeta(const FunctionCallbackInfo<Value>& args) { | ||
| // target, url, shouldSetDirnameAndFilename, resolve | ||
| CHECK_GE(args.Length(), 4); | ||
| CHECK(args[0]->IsObject()); | ||
| CHECK(args[1]->IsString()); | ||
| CHECK(args[2]->IsBoolean()); | ||
| CHECK(args[3]->IsFalse() || args[3]->IsFunction()); | ||
|
|
||
| Isolate* isolate = args.GetIsolate(); | ||
| Local<Context> context = isolate->GetCurrentContext(); | ||
|
|
||
| auto> | ||
|
|
||
| // N.B.: Order is important to keep keys in alphabetical order. | ||
|
|
||
| if (args[2]->IsTrue()) { | ||
| 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(); | ||
|
aduh95 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| if (args[3]->IsFunction()) { | ||
| target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "resolve"), args[3]) | ||
| .Check(); | ||
| } | ||
| target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "url"), args[1]).Check(); | ||
|
aduh95 marked this conversation as resolved.
Outdated
|
||
| } | ||
| void SaveCompileCacheEntry(const FunctionCallbackInfo<Value>& args) { | ||
| Isolate* isolate = args.GetIsolate(); | ||
| Local<Context> context = isolate->GetCurrentContext(); | ||
|
|
@@ -627,6 +709,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data, | |
| SetMethod(isolate, target, "flushCompileCache", FlushCompileCache); | ||
| SetMethod(isolate, target, "getCompileCacheEntry", GetCompileCacheEntry); | ||
| SetMethod(isolate, target, "saveCompileCacheEntry", SaveCompileCacheEntry); | ||
| SetMethod(isolate, target, "initializeImportMeta", InitImportMeta); | ||
| } | ||
|
|
||
| void BindingData::CreatePerContextProperties(Local<Object> target, | ||
|
|
@@ -682,6 +765,7 @@ void BindingData::RegisterExternalReferences( | |
| registry->Register(FlushCompileCache); | ||
| registry->Register(GetCompileCacheEntry); | ||
| registry->Register(SaveCompileCacheEntry); | ||
| registry->Register(InitImportMeta); | ||
| } | ||
|
|
||
| } // namespace modules | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.