Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/jsc/VirtualMachine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,11 @@
!self.bun_watcher.is_null()
}

/// After the first `beforeExit`, `uncaught_exception` ends the process if no listener takes the error.
pub fn exits_on_uncaught_exception(&self) -> bool {
self.exit_on_uncaught_exception
}

/// Thin setter so callers don't need `.with` plumbing on the thread-local.
#[inline]
pub fn set_is_main_thread_vm(value: bool) {
Expand Down Expand Up @@ -3393,6 +3398,26 @@
self.main = bun_ptr::RawSlice::new(path);
}

/// Whether `key` names the module that `bun:main` imports.
pub fn is_entry_point(&mut self, key: &bun_core::String) -> bool {
let main = self.main;
if key.eql_utf8(MAIN_FILE_NAME) || key.eql_utf8(main.slice()) {
return true;
}
// Run as `node`, `main` is the path as typed: the import adds an extension and follows a symlink.
let mut resolved = ResolveFunctionResult::default();
self._resolve(
&mut resolved,
main.slice(),
MAIN_FILE_NAME,
true,
false,
bun_resolver::GlobalCache::disable,
)
.is_ok()
&& key.eql_utf8(resolved.path)
Comment thread
robobun marked this conversation as resolved.
Resolved
}

/// `eventLoop().waitForPromise(promise)` — spin tick/auto_tick until
/// `promise` settles. Thin forwarder; body lives in
/// [`crate::event_loop::EventLoop::wait_for_promise`].
Expand Down Expand Up @@ -4385,6 +4410,20 @@
self.unhandled_rejection_owned(global_object, reason, promise, owner);
}

/// Like `uncaught_exception(.., true)`, but the current `Bun.ModuleGraph` takes it first.
pub fn uncaught_rejection(&mut self, global_object: &JSGlobalObject, reason: JSValue) -> bool {
if self.is_shutting_down() || !self.script_allowed() || reason.is_termination_exception() {
return true;
}
let owner = Bun__ModuleGraph__rejecting(global_object);
if owner.is_cell()
&& Bun__ModuleGraph__handleUnhandledRejection(global_object, reason, owner)
{
return true;
}
self.uncaught_exception(global_object, reason, true)
}

/// `owner`: the `Bun.ModuleGraph` in whose context the promise was rejected (its `onError`
/// takes it), or null. The tracker queue reports after the fact, so it decided when it happened.
pub fn unhandled_rejection_owned(
Expand Down Expand Up @@ -5110,7 +5149,7 @@

/// Note: `is_a_file_path` is a runtime
/// arg to avoid duplicating the body for both monomorphizations.
pub(crate) fn _resolve(

Check warning on line 5152 in src/jsc/VirtualMachine.rs

View workflow job for this annotation

GitHub Actions / mordant

`_resolve` takes bools `is_esm` and `is_a_file_path`, and 1 of its 2 calls passes bare `true`/`false` for both. At `_resolve(.., true, false, ..)` nothing says which is which
&mut self,
ret: &mut ResolveFunctionResult,
specifier: &[u8],
Expand Down
12 changes: 10 additions & 2 deletions src/jsc/modules/NodeModuleModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,9 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionLoad, (JSGlobalObject * globalObject, JSC::Ca
return JSC::JSValue::encode(JSC::jsUndefined());
}

extern "C" void Bun__VirtualMachine__setOverrideModuleRunMainPromise(void* bunVM, JSPromise* promise);
extern "C" bool Bun__VirtualMachine__entryPointTakesRunMain(void* bunVM, const BunString* key, JSPromise* promise);
BUN_DECLARE_HOST_FUNCTION(Bun__onRunMainRejected);

JSC_DEFINE_HOST_FUNCTION(jsFunctionRunMain, (JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
auto& vm = JSC::getVM(globalObject);
Expand All @@ -814,7 +816,13 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionRunMain, (JSGlobalObject * globalObject, JSC:

auto* promise = JSC::loadAndEvaluateModule(globalObject, key.string(), nullptr, nullptr);
RETURN_IF_EXCEPTION(scope, {});
Bun__VirtualMachine__setOverrideModuleRunMainPromise(defaultGlobalObject(globalObject)->bunVM(), promise);
BunString keyString = Bun::toString(key.string());
if (Bun__VirtualMachine__entryPointTakesRunMain(defaultGlobalObject(globalObject)->bunVM(), &keyString, promise))
return JSC::JSValue::encode(JSC::jsUndefined());

// JSC marks this promise as handled. Node raises an uncaught exception: https://github.com/nodejs/node/blob/v26.12.0/lib/internal/modules/run_main.js#L111
auto* onRejected = JSC::JSFunction::create(vm, globalObject, 1, String(), Bun__onRunMainRejected, ImplementationVisibility::Private);
promise->performPromiseThenExported(vm, globalObject, jsUndefined(), onRejected, JSPromise::create(vm, globalObject->promiseStructure()));

return JSC::JSValue::encode(JSC::jsUndefined());
}
Expand Down
31 changes: 27 additions & 4 deletions src/runtime/hw_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,38 @@ pub(crate) fn set_override_module_run_main(vm: &mut VirtualMachine, is_patched:
}
}

/// `export fn Bun__VirtualMachine__setOverrideModuleRunMainPromise(vm, promise)`
// HOST_EXPORT(Bun__VirtualMachine__setOverrideModuleRunMainPromise, c)
pub(crate) fn set_override_module_run_main_promise(
/// Whether the entry point reports the load that `Module.runMain(key)` started. If not, the call does.
// HOST_EXPORT(Bun__VirtualMachine__entryPointTakesRunMain, c)
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) fn entry_point_takes_run_main(
vm: &mut VirtualMachine,
key: &bun_core::String,
promise: *mut JSInternalPromise,
) {
) -> bool {
if !vm.has_patched_run_main {
// `reload_entry_point` loads the entry point itself.
return vm.is_entry_point(key);
}
// `reload_entry_point` takes the first load of the `Module.runMain` that it calls.
if vm.pending_internal_promise().is_none() {
vm.set_pending_internal_promise(Some(promise));
return true;
}
false
}

// HOST_EXPORT(Bun__onRunMainRejected)
pub(crate) fn on_run_main_rejected(
global: &JSGlobalObject,
callframe: &CallFrame,
) -> bun_jsc::JsResult<JSValue> {
let reason = callframe.argument(0);
let vm = global.bun_vm().as_mut();
// Rethrown, it goes to the rejection tracker, as a reloaded entry point's does: a watcher has to stay.
if vm.is_watcher_enabled() && vm.exits_on_uncaught_exception() {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return Err(global.throw_value(reason));
Comment thread
robobun marked this conversation as resolved.
Resolved
}
let _ = vm.uncaught_rejection(global, reason);
Ok(JSValue::UNDEFINED)
}

/// Exported as `Bun__VM__setEntryPointEvalResultESM`.
Expand Down
Loading
Loading