diff options
author | 2023-09-07 21:07:00 -0800 | |
---|---|---|
committer | 2023-09-07 21:07:00 -0800 | |
commit | 822a00c4d508b54f650933a73ca5f4a3af9a7983 (patch) | |
tree | 8c0d1e7ce7e9c230f97bcc17c3169647267b1eec /src/bun.js/bindings/ImportMetaObject.cpp | |
parent | f6a621f36a87d54d14e2b2ea81c1bcef4906ae63 (diff) | |
download | bun-822a00c4d508b54f650933a73ca5f4a3af9a7983.tar.gz bun-822a00c4d508b54f650933a73ca5f4a3af9a7983.tar.zst bun-822a00c4d508b54f650933a73ca5f4a3af9a7983.zip |
Fix a couple important bugs (#4560)bun-v1.0.0
Diffstat (limited to 'src/bun.js/bindings/ImportMetaObject.cpp')
-rw-r--r-- | src/bun.js/bindings/ImportMetaObject.cpp | 58 |
1 files changed, 35 insertions, 23 deletions
diff --git a/src/bun.js/bindings/ImportMetaObject.cpp b/src/bun.js/bindings/ImportMetaObject.cpp index c53235824..4160102a5 100644 --- a/src/bun.js/bindings/ImportMetaObject.cpp +++ b/src/bun.js/bindings/ImportMetaObject.cpp @@ -457,14 +457,20 @@ void ImportMetaObject::finishCreation(VM& vm) ImportMetaObject* meta = jsCast<ImportMetaObject*>(init.owner); WTF::URL url = meta->url.startsWith('/') ? WTF::URL::fileURLWithFileSystemPath(meta->url) : WTF::URL(meta->url); - WTF::StringView path; - if (url.protocolIs("file"_s)) { - path = url.fileSystemPath(); + WTF::String path; + + if (url.isValid()) { + + if (url.protocolIs("file"_s)) { + path = url.fileSystemPath(); + } else { + path = url.path().toString(); + } } else { - path = url.path(); + path = meta->url; } - JSFunction* value = jsCast<JSFunction*>(Bun::JSCommonJSModule::createBoundRequireFunction(init.vm, meta->globalObject(), path.toString())); + JSFunction* value = jsCast<JSFunction*>(Bun::JSCommonJSModule::createBoundRequireFunction(init.vm, meta->globalObject(), path)); init.set(value); }); this->urlProperty.initLater([](const JSC::LazyProperty<JSC::JSObject, JSC::JSString>::Initializer& init) { @@ -477,12 +483,14 @@ void ImportMetaObject::finishCreation(VM& vm) ImportMetaObject* meta = jsCast<ImportMetaObject*>(init.owner); WTF::URL url = meta->url.startsWith('/') ? WTF::URL::fileURLWithFileSystemPath(meta->url) : WTF::URL(meta->url); - WTF::StringView dirname; + WTF::String dirname; - if (url.protocolIs("file"_s)) { - dirname = url.fileSystemPath(); - } else { - dirname = url.path(); + if (url.isValid()) { + if (url.protocolIs("file"_s)) { + dirname = url.fileSystemPath(); + } else { + dirname = url.path().toString(); + } } if (dirname.endsWith("/"_s)) { @@ -491,20 +499,25 @@ void ImportMetaObject::finishCreation(VM& vm) dirname = dirname.substring(0, dirname.reverseFind('/')); } - init.set(jsString(init.vm, dirname.toString())); + init.set(jsString(init.vm, dirname)); }); this->fileProperty.initLater([](const JSC::LazyProperty<JSC::JSObject, JSC::JSString>::Initializer& init) { ImportMetaObject* meta = jsCast<ImportMetaObject*>(init.owner); WTF::URL url = meta->url.startsWith('/') ? WTF::URL::fileURLWithFileSystemPath(meta->url) : WTF::URL(meta->url); - WTF::StringView path; - if (url.protocolIs("file"_s)) { - path = url.fileSystemPath(); + WTF::String path; + + if (!url.isValid()) { + path = meta->url; } else { - path = url.path(); + if (url.protocolIs("file"_s)) { + path = url.fileSystemPath(); + } else { + path = url.path().toString(); + } } - WTF::StringView filename; + WTF::String filename; if (path.endsWith("/"_s)) { filename = path.substring(path.reverseFind('/', path.length() - 2) + 1); @@ -512,21 +525,20 @@ void ImportMetaObject::finishCreation(VM& vm) filename = path.substring(path.reverseFind('/') + 1); } - init.set(jsString(init.vm, filename.toString())); + init.set(jsString(init.vm, filename)); }); this->pathProperty.initLater([](const JSC::LazyProperty<JSC::JSObject, JSC::JSString>::Initializer& init) { ImportMetaObject* meta = jsCast<ImportMetaObject*>(init.owner); WTF::URL url = meta->url.startsWith('/') ? WTF::URL::fileURLWithFileSystemPath(meta->url) : WTF::URL(meta->url); - WTF::StringView path; - if (url.protocolIs("file"_s)) { - path = url.fileSystemPath(); + if (!url.isValid()) { + init.set(jsString(init.vm, meta->url)); + } else if (url.protocolIs("file"_s)) { + init.set(jsString(init.vm, url.fileSystemPath())); } else { - path = url.path(); + init.set(jsString(init.vm, url.path())); } - - init.set(jsString(init.vm, path.toString())); }); } |