-
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
Changes from all commits
119c2fd
1ee92b0
5a2763a
6ca5ed4
c62797f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -417,11 +417,13 @@ JSC_DEFINE_HOST_FUNCTION(functionImportMeta__resolve, | |||||
enum class ImportMetaPropertyOffset : uint32_t { | ||||||
url, | ||||||
dir, | ||||||
dirname, | ||||||
file, | ||||||
filename, | ||||||
path, | ||||||
require, | ||||||
}; | ||||||
static constexpr uint32_t numberOfImportMetaProperties = 5; | ||||||
static constexpr uint32_t numberOfImportMetaProperties = 7; | ||||||
|
||||||
Zig::ImportMetaObject* ImportMetaObject::create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, const WTF::String& url) | ||||||
{ | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. remove this function |
||||||
{ | ||||||
ImportMetaObject* thisObject = jsDynamicCast<ImportMetaObject*>(JSValue::decode(thisValue)); | ||||||
if (UNLIKELY(!thisObject)) | ||||||
return JSValue::encode(jsUndefined()); | ||||||
|
||||||
return JSValue::encode(thisObject->dirnameProperty.getInitializedOnMainThread(thisObject)); | ||||||
} | ||||||
JSC_DEFINE_CUSTOM_GETTER(jsImportMetaObjectGetter_file, (JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, PropertyName propertyName)) | ||||||
{ | ||||||
ImportMetaObject* thisObject = jsDynamicCast<ImportMetaObject*>(JSValue::decode(thisValue)); | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. remove this function |
||||||
{ | ||||||
ImportMetaObject* thisObject = jsDynamicCast<ImportMetaObject*>(JSValue::decode(thisValue)); | ||||||
if (UNLIKELY(!thisObject)) | ||||||
return JSValue::encode(jsUndefined()); | ||||||
|
||||||
return JSValue::encode(thisObject->filenameProperty.getInitializedOnMainThread(thisObject)); | ||||||
} | ||||||
JSC_DEFINE_CUSTOM_GETTER(jsImportMetaObjectGetter_path, (JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, PropertyName propertyName)) | ||||||
{ | ||||||
ImportMetaObject* thisObject = jsDynamicCast<ImportMetaObject*>(JSValue::decode(thisValue)); | ||||||
|
@@ -489,7 +507,9 @@ static const HashTableValue ImportMetaObjectPrototypeValues[] = { | |||||
{ "resolveSync"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::NativeFunctionType, functionImportMeta__resolveSync, 0 } }, | ||||||
{ "url"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_url, 0 } }, | ||||||
{ "dir"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_dir, 0 } }, | ||||||
{ "dirname"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_dir, 0 } }, | ||||||
{ "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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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. we probably need to check if it's readonly in node |
||||||
{ "path"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_path, 0 } }, | ||||||
{ "require"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_require, 0 } }, | ||||||
{ "env"_s, static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | PropertyAttribute::DontDelete), NoIntrinsic, { HashTableValue::GetterSetterType, jsImportMetaObjectGetter_env, 0 } }, | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. this lazy property is identical to |
||||||
ImportMetaObject* meta = jsCast<ImportMetaObject*>(init.owner); | ||||||
|
||||||
WTF::URL url = isAbsolutePath(meta->url) ? WTF::URL::fileURLWithFileSystemPath(meta->url) : WTF::URL(meta->url); | ||||||
WTF::String dirname; | ||||||
|
||||||
if (url.isValid()) { | ||||||
if (url.protocolIsFile()) { | ||||||
dirname = url.fileSystemPath(); | ||||||
} else { | ||||||
dirname = url.path().toString(); | ||||||
} | ||||||
} | ||||||
|
||||||
if (dirname.endsWith(PLATFORM_SEP_s)) { | ||||||
dirname = dirname.substring(0, dirname.length() - 1); | ||||||
} else if (dirname.contains(PLATFORM_SEP)) { | ||||||
dirname = dirname.substring(0, dirname.reverseFind(PLATFORM_SEP)); | ||||||
} | ||||||
|
||||||
init.set(jsString(init.vm, dirname)); | ||||||
}); | ||||||
this->fileProperty.initLater([](const JSC::LazyProperty<JSC::JSObject, JSC::JSString>::Initializer& init) { | ||||||
ImportMetaObject* meta = jsCast<ImportMetaObject*>(init.owner); | ||||||
|
||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. this lazy property is identical to |
||||||
ImportMetaObject* meta = jsCast<ImportMetaObject*>(init.owner); | ||||||
|
||||||
WTF::URL url = isAbsolutePath(meta->url) ? WTF::URL::fileURLWithFileSystemPath(meta->url) : WTF::URL(meta->url); | ||||||
WTF::String path; | ||||||
|
||||||
if (!url.isValid()) { | ||||||
path = meta->url; | ||||||
} else { | ||||||
if (url.protocolIsFile()) { | ||||||
path = url.fileSystemPath(); | ||||||
} else { | ||||||
path = url.path().toString(); | ||||||
} | ||||||
} | ||||||
|
||||||
init.set(jsString(init.vm, path)); | ||||||
}); | ||||||
this->pathProperty.initLater([](const JSC::LazyProperty<JSC::JSObject, JSC::JSString>::Initializer& init) { | ||||||
ImportMetaObject* meta = jsCast<ImportMetaObject*>(init.owner); | ||||||
|
||||||
|
@@ -666,7 +726,9 @@ void ImportMetaObject::visitChildrenImpl(JSCell* cell, Visitor& visitor) | |||||
fn->requireProperty.visit(visitor); | ||||||
fn->urlProperty.visit(visitor); | ||||||
fn->dirProperty.visit(visitor); | ||||||
fn->dirnameProperty.visit(visitor); | ||||||
fn->fileProperty.visit(visitor); | ||||||
fn->filenameProperty.visit(visitor); | ||||||
fn->pathProperty.visit(visitor); | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { bunRunAsScript } from "harness"; | ||
import { tempDirWithFiles } from "harness"; | ||
import path from "path"; | ||
|
||
it("has import.meta behaviour that matches node.js behaviour", () => { | ||
const fileData = ` | ||
console.log(import.meta.dir); | ||
console.log(import.meta.dirname); | ||
console.log(import.meta.file); | ||
console.log(import.meta.filename); | ||
`; | ||
|
||
const testDir = tempDirWithFiles("07778", { | ||
"test.mjs": fileData, | ||
}); | ||
|
||
const { stdout, stderr } = bunRunAsScript(testDir, "test.mjs"); | ||
|
||
expect(stdout).toBeDefined(); | ||
|
||
const [dir, dirname, file, filename] = stdout.split("\n"); | ||
|
||
expect(dir).toEqual(testDir); | ||
expect(dirname).toEqual(testDir); | ||
|
||
expect(file).toEqual("test.mjs"); | ||
expect(filename).toEqual(path.join(testDir, "test.mjs")); | ||
|
||
expect(stderr).toBeDefined(); | ||
}); |
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