diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp index 795a0b0465..1f83c0eecd 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.cpp +++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp @@ -93,7 +93,7 @@ static bool canPerformFastEnumeration(Structure* s) } extern "C" bool Bun__VM__specifierIsEvalEntryPoint(void*, EncodedJSValue); -extern "C" void Bun__VM__setEntryPointEvalResult(void*, EncodedJSValue); +extern "C" void Bun__VM__setEntryPointEvalResultCJS(void*, EncodedJSValue); static bool evaluateCommonJSModuleOnce(JSC::VM& vm, Zig::GlobalObject* globalObject, JSCommonJSModule* moduleObject, JSString* dirname, JSValue filename, WTF::NakedPtr& exception) { @@ -139,7 +139,7 @@ static bool evaluateCommonJSModuleOnce(JSC::VM& vm, Zig::GlobalObject* globalObj return false; } - Bun__VM__setEntryPointEvalResult(globalObject->bunVM(), JSValue::encode(result)); + Bun__VM__setEntryPointEvalResultCJS(globalObject->bunVM(), JSValue::encode(result)); moduleObject->sourceCode.clear(); diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index a8792fb47a..18e76e569d 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -4508,8 +4508,6 @@ JSC::JSObject* GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObje return Zig::ImportMetaObject::create(globalObject, keyString); } -extern "C" void Bun__VM__setEvalResultIfEntryPoint(void*, EncodedJSValue, EncodedJSValue); - JSC::JSValue GlobalObject::moduleLoaderEvaluate(JSGlobalObject* lexicalGlobalObject, JSModuleLoader* moduleLoader, JSValue key, JSValue moduleRecordValue, JSValue scriptFetcher, @@ -4527,6 +4525,9 @@ JSC::JSValue GlobalObject::moduleLoaderEvaluate(JSGlobalObject* lexicalGlobalObj return result; } +extern "C" bool Bun__VM__specifierIsEvalEntryPoint(void*, EncodedJSValue); +extern "C" void Bun__VM__setEntryPointEvalResultESM(void*, EncodedJSValue); + JSC::JSValue EvalGlobalObject::moduleLoaderEvaluate(JSGlobalObject* lexicalGlobalObject, JSModuleLoader* moduleLoader, JSValue key, JSValue moduleRecordValue, JSValue scriptFetcher, @@ -4535,16 +4536,18 @@ JSC::JSValue EvalGlobalObject::moduleLoaderEvaluate(JSGlobalObject* lexicalGloba Zig::GlobalObject* globalObject = jsCast(lexicalGlobalObject); if (UNLIKELY(scriptFetcher && scriptFetcher.isObject())) { - Bun__VM__setEvalResultIfEntryPoint(globalObject->bunVM(), JSValue::encode(key), JSValue::encode(scriptFetcher)); + if (Bun__VM__specifierIsEvalEntryPoint(globalObject->bunVM(), JSValue::encode(key))) { + Bun__VM__setEntryPointEvalResultESM(globalObject->bunVM(), JSValue::encode(scriptFetcher)); + } return scriptFetcher; } JSC::JSValue result = moduleLoader->evaluateNonVirtual(lexicalGlobalObject, key, moduleRecordValue, scriptFetcher, sentValue, resumeMode); - // need to check each module evaluated to cover cases like these (23 should be the result): - // `import "./foo"; 23; import "./bar"` - Bun__VM__setEvalResultIfEntryPoint(globalObject->bunVM(), JSValue::encode(key), JSValue::encode(result)); + if (Bun__VM__specifierIsEvalEntryPoint(globalObject->bunVM(), JSValue::encode(key))) { + Bun__VM__setEntryPointEvalResultESM(globalObject->bunVM(), JSValue::encode(result)); + } return result; } diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 1493b22149..dc445c041f 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -576,7 +576,10 @@ pub const VirtualMachine = struct { rare_data: ?*JSC.RareData = null, is_us_loop_entered: bool = false, pending_internal_promise: *JSC.JSInternalPromise = undefined, - entry_point_result: JSC.Strong = .{}, + entry_point_result: struct { + value: JSC.Strong = .{}, + cjs_set_value: bool = false, + } = .{}, auto_install_dependencies: bool = false, @@ -913,22 +916,24 @@ pub const VirtualMachine = struct { return false; } - pub fn setEvalResultIfEntryPoint(this: *VirtualMachine, specifier: JSValue, result: JSValue) callconv(.C) void { - if (this.specifierIsEvalEntryPoint(specifier)) { - this.entry_point_result.set(this.global, result); + pub fn setEntryPointEvalResultESM(this: *VirtualMachine, result: JSValue) callconv(.C) void { + // allow esm evaluate to set value multiple times + if (!this.entry_point_result.cjs_set_value) { + this.entry_point_result.value.set(this.global, result); } } - pub fn setEntryPointEvalResult(this: *VirtualMachine, value: JSValue) callconv(.C) void { - if (!this.entry_point_result.has()) { - this.entry_point_result.set(this.global, value); + pub fn setEntryPointEvalResultCJS(this: *VirtualMachine, value: JSValue) callconv(.C) void { + if (!this.entry_point_result.value.has()) { + this.entry_point_result.value.set(this.global, value); + this.entry_point_result.cjs_set_value = true; } } comptime { @export(scriptExecutionStatus, .{ .name = "Bun__VM__scriptExecutionStatus" }); - @export(setEvalResultIfEntryPoint, .{ .name = "Bun__VM__setEvalResultIfEntryPoint" }); - @export(setEntryPointEvalResult, .{ .name = "Bun__VM__setEntryPointEvalResult" }); + @export(setEntryPointEvalResultESM, .{ .name = "Bun__VM__setEntryPointEvalResultESM" }); + @export(setEntryPointEvalResultCJS, .{ .name = "Bun__VM__setEntryPointEvalResultCJS" }); @export(specifierIsEvalEntryPoint, .{ .name = "Bun__VM__specifierIsEvalEntryPoint" }); } diff --git a/src/bun_js.zig b/src/bun_js.zig index 271c255e66..5dbb101a12 100644 --- a/src/bun_js.zig +++ b/src/bun_js.zig @@ -394,7 +394,7 @@ pub const Run = struct { if (this.ctx.runtime_options.eval.eval_and_print) { const to_print = brk: { - const result = vm.entry_point_result.trySwap() orelse .undefined; + const result = vm.entry_point_result.value.trySwap() orelse .undefined; if (result.asAnyPromise()) |promise| { if (promise.status(vm.jsc) != .Pending) { break :brk promise.result(vm.jsc);