aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bundler.zig1
-rw-r--r--src/javascript/jsc/api/bun.zig13
-rw-r--r--src/javascript/jsc/bindings/ZigGlobalObject.cpp65
3 files changed, 78 insertions, 1 deletions
diff --git a/src/bundler.zig b/src/bundler.zig
index 5b1f970bb..08f106ff0 100644
--- a/src/bundler.zig
+++ b/src/bundler.zig
@@ -1087,6 +1087,7 @@ pub const Bundler = struct {
opts.features.allow_runtime = bundler.options.allow_runtime;
opts.features.trim_unused_imports = bundler.options.trim_unused_imports orelse loader.isTypeScript();
opts.features.should_fold_numeric_constants = platform.isBun();
+ opts.features.dynamic_require = platform.isBun();
opts.can_import_from_bundle = bundler.options.node_modules_bundle != null;
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);