diff options
author | 2022-05-11 00:54:42 -0700 | |
---|---|---|
committer | 2022-05-11 00:54:42 -0700 | |
commit | fd00950852c3f3bf49d9ce8f6a175753ad0167fd (patch) | |
tree | a1349f5b29a11c9acf3f01e7e6f43070776c88c4 /src/javascript | |
parent | eb2a6aee4d8450138a5b8acd547616cd651cf0e1 (diff) | |
download | bun-fd00950852c3f3bf49d9ce8f6a175753ad0167fd.tar.gz bun-fd00950852c3f3bf49d9ce8f6a175753ad0167fd.tar.zst bun-fd00950852c3f3bf49d9ce8f6a175753ad0167fd.zip |
[bun.js] Implement `import.meta.resolveSync`
Diffstat (limited to 'src/javascript')
-rw-r--r-- | src/javascript/jsc/api/bun.zig | 13 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/ZigGlobalObject.cpp | 65 |
2 files changed, 77 insertions, 1 deletions
diff --git a/src/javascript/jsc/api/bun.zig b/src/javascript/jsc/api/bun.zig index 9f0f82515..65c786598 100644 --- a/src/javascript/jsc/api/bun.zig +++ b/src/javascript/jsc/api/bun.zig @@ -921,9 +921,22 @@ export fn Bun__resolve( return JSC.JSPromise.resolvedPromiseValue(global, value); } +export fn Bun__resolveSync( + global: *JSGlobalObject, + specifier: JSValue, + source: JSValue, +) JSC.JSValue { + var exception_ = [1]JSC.JSValueRef{null}; + var exception = &exception_; + return doResolveWithArgs(global.ref(), specifier.getZigString(global), source.getZigString(global), exception, true) orelse { + return JSC.JSValue.fromRef(exception[0]); + }; +} + comptime { if (!is_bindgen) { _ = Bun__resolve; + _ = Bun__resolveSync; } } diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp index b1ce133fa..f42b2a5a2 100644 --- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp +++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp @@ -95,6 +95,7 @@ #include "JavaScriptCore/LazyClassStructureInlines.h" #include "JavaScriptCore/FunctionPrototype.h" #include "napi.h" +#include "JSZigGlobalObjectBuiltins.h" using JSGlobalObject = JSC::JSGlobalObject; using Exception = JSC::Exception; @@ -691,6 +692,7 @@ static JSC_DEFINE_HOST_FUNCTION(functionATOB, } extern "C" JSC__JSValue Bun__resolve(JSC::JSGlobalObject* global, JSC__JSValue specifier, JSC__JSValue from); +extern "C" JSC__JSValue Bun__resolveSync(JSC::JSGlobalObject* global, JSC__JSValue specifier, JSC__JSValue from); static JSC_DECLARE_HOST_FUNCTION(functionImportMeta__resolve); @@ -739,6 +741,62 @@ static JSC_DEFINE_HOST_FUNCTION(functionImportMeta__resolve, } } +static JSC_DECLARE_HOST_FUNCTION(functionImportMeta__resolveSync); + +static JSC_DEFINE_HOST_FUNCTION(functionImportMeta__resolveSync, + (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) +{ + JSC::VM& vm = globalObject->vm(); + + switch (callFrame->argumentCount()) { + case 0: { + auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); + // not "requires" because "require" could be confusing + JSC::throwTypeError(globalObject, scope, "import.meta.resolveSync needs 1 argument (a string)"_s); + scope.release(); + return JSC::JSValue::encode(JSC::JSValue {}); + } + default: { + JSC::JSValue moduleName = callFrame->argument(0); + + if (moduleName.isUndefinedOrNull()) { + auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); + JSC::throwTypeError(globalObject, scope, "import.meta.resolveSync expects a string"_s); + scope.release(); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + JSC__JSValue from; + + if (callFrame->argumentCount() > 1) { + from = JSC::JSValue::encode(callFrame->argument(1)); + } else { + JSC::JSObject* thisObject = JSC::jsDynamicCast<JSC::JSObject*>(callFrame->thisValue()); + if (UNLIKELY(!thisObject)) { + auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); + JSC::throwTypeError(globalObject, scope, "import.meta.resolveSync must be bound to an import.meta object"_s); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + auto clientData = WebCore::clientData(vm); + + from = JSC::JSValue::encode(thisObject->get(globalObject, clientData->builtinNames().pathPublicName())); + } + + auto result = Bun__resolveSync(globalObject, JSC::JSValue::encode(moduleName), from); + auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); + + if (!JSC::JSValue::decode(result).isString()) { + JSC::throwException(globalObject, scope, JSC::JSValue::decode(result)); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + scope.release(); + return result; + } + } +} + extern "C" void Bun__reportError(JSC__JSGlobalObject*, JSC__JSValue); static JSC_DECLARE_HOST_FUNCTION(functionReportError); @@ -1127,7 +1185,12 @@ JSC::JSObject* GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObje metaProperties->putDirect(vm, clientData->builtinNames().resolvePublicName(), JSC::JSFunction::create(vm, JSC::jsCast<JSC::JSGlobalObject*>(globalObject), 0, WTF::String("resolve"_s), functionImportMeta__resolve), - 0); + JSC::PropertyAttribute::Function | 0); + metaProperties->putDirect(vm, clientData->builtinNames().resolveSyncPublicName(), + JSC::JSFunction::create(vm, JSC::jsCast<JSC::JSGlobalObject*>(globalObject), 0, + WTF::String("resolveSync"_s), functionImportMeta__resolveSync), + JSC::PropertyAttribute::Function | 0); + } metaProperties->putDirect(vm, clientData->builtinNames().pathPublicName(), key); |