Skip to content
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

Closed
wants to merge 5 commits into from

Conversation

knightspore
Copy link
Contributor

@knightspore knightspore commented Dec 22, 2023

What does this PR do?

This PR fixes #7778 by adding import.meta.dirname and import.meta.filename, replicating new additions to Node v.21.2.0

As suggested in the issue above, import.meta.dirname has the same behaviour as Bun's existing import.meta.dir.

Bun's import.meta.file does not prepend the path to the filename - so for import.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 return undefined:

Meta Property Expected (Node) Seen Instead (Bun) New Behaviour (Bun)
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
  • Documentation or TypeScript types (it's okay to leave the rest blank in this case)
  • Code changes

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)

Copy link
Member

@paperdave paperdave left a 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;
Copy link
Member

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) {
Copy link
Member

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) {
Copy link
Member

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 } },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ "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 } },

Copy link
Collaborator

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))
Copy link
Member

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this function

@Jarred-Sumner
Copy link
Collaborator

Superseded by #8127

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Node compat: Add import.meta.dirname and import.meta.filename
3 participants