-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Fixes #7778: Add import.meta.dirname
, import.meta.filename
for Node compatibility
#7787
Conversation
…or compatibility with Node
…rt.meta.dirname and import.meta.filename
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.
great to achieve node compatibility, but ouch now we have duplicate properties.
instead of using new LazyProperty for these properties, we can simply reuse the existing ones, even pointing to the same getter implementations.
* | ||
* Does not have trailing slash | ||
*/ | ||
readonly dirname: string; |
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 you opened this before our big @types/bun
PR. but can you remove these type declarations. they should be provided by @types/node
@@ -641,6 +683,24 @@ void ImportMetaObject::finishCreation(VM& vm) | |||
|
|||
init.set(jsString(init.vm, filename)); | |||
}); | |||
this->filenameProperty.initLater([](const JSC::LazyProperty<JSC::JSObject, JSC::JSString>::Initializer& init) { |
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.
this lazy property is identical to this->pathProperty
please remove it.
@@ -615,6 +635,28 @@ void ImportMetaObject::finishCreation(VM& vm) | |||
|
|||
init.set(jsString(init.vm, dirname)); | |||
}); | |||
this->dirnameProperty.initLater([](const JSC::LazyProperty<JSC::JSObject, JSC::JSString>::Initializer& init) { |
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.
this lazy property is identical to this->dirProperty
please remove it.
{ "file"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_file, 0 } }, | ||
{ "filename"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_filename, 0 } }, |
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.
{ "filename"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_filename, 0 } }, | |
{ "filename"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_path, 0 } }, |
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 probably need to check if it's readonly in node
@@ -454,6 +456,14 @@ JSC_DEFINE_CUSTOM_GETTER(jsImportMetaObjectGetter_dir, (JSGlobalObject * globalO | |||
|
|||
return JSValue::encode(thisObject->dirProperty.getInitializedOnMainThread(thisObject)); | |||
} | |||
JSC_DEFINE_CUSTOM_GETTER(jsImportMetaObjectGetter_dirname, (JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, PropertyName propertyName)) |
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.
remove this function
@@ -462,6 +472,14 @@ JSC_DEFINE_CUSTOM_GETTER(jsImportMetaObjectGetter_file, (JSGlobalObject * global | |||
|
|||
return JSValue::encode(thisObject->fileProperty.getInitializedOnMainThread(thisObject)); | |||
} | |||
JSC_DEFINE_CUSTOM_GETTER(jsImportMetaObjectGetter_filename, (JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, PropertyName propertyName)) |
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.
remove this function
Superseded by #8127 |
What does this PR do?
This PR fixes #7778 by adding
import.meta.dirname
andimport.meta.filename
, replicating new additions to Node v.21.2.0As suggested in the issue above,
import.meta.dirname
has the same behaviour as Bun's existingimport.meta.dir
.Bun's
import.meta.file
does not prepend the path to the filename - so forimport.meta.filename
we return a value equivalent to${import.meta.dir}/${import.meta.file}
as suggested in the original issue.There are some slight changes on the desired output listed in #7778 since none of these
import.meta
properties should currently returnundefined
:dir
undefined
/home/ubuntu
/home/ubuntu
dirname
/home/ubuntu
undefined
/home/ubuntu
file
undefined
test.mjs
test.mjs
filename
/home/ubuntu/test.mjs
undefined
/home/ubuntu/test.mjs
How did you verify your code works?
Regression tests
Updating tests in
test/js/bun/resolve/import-meta.test.js
to cover new cases.I checked the lifetime of memory allocated to verify it's (1) freed and (2) only freed when it should be
I included a test for the new code, or an existing test covers it
JSValue used outside outside of the stack is either wrapped in a JSC.Strong or is JSValueProtect'ed
I wrote TypeScript/JavaScript tests and they pass locally (
bun-debug test test/js/bun/resolve/import-meta.test.js test/regression/issue/07778.test.ts
)