From 85167870d01687cc0c6fd2cb7d1bfb33f3c3cf4e Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Thu, 1 May 2025 13:57:41 -0700 Subject: [PATCH] simplify --- src/bun.js/api/Timer.zig | 18 ++++++++++-------- src/bun.js/bindings/NodeTimerObject.cpp | 6 +++--- src/bun.js/event_loop.zig | 5 ++--- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/bun.js/api/Timer.zig b/src/bun.js/api/Timer.zig index bc1dc4bc9f..10e8674e1b 100644 --- a/src/bun.js/api/Timer.zig +++ b/src/bun.js/api/Timer.zig @@ -750,8 +750,9 @@ pub const ImmediateObject = struct { return globalObject.throw("Immediate is not constructible", .{}); } - pub fn runImmediateTask(this: *ImmediateObject, vm: *VirtualMachine, exception_thrown: bool) bool { - return this.internals.runImmediateTask(vm, exception_thrown); + // returns true if an exception was thrown + pub fn runImmediateTask(this: *ImmediateObject, vm: *VirtualMachine) bool { + return this.internals.runImmediateTask(vm); } pub fn toPrimitive(this: *ImmediateObject, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) bun.JSError!JSValue { @@ -853,21 +854,22 @@ pub const TimerObjectInternals = struct { extern "c" fn Bun__JSTimeout__call(globalObject: *JSC.JSGlobalObject, timer: JSValue, callback: JSValue, arguments: JSValue) bool; - pub fn runImmediateTask(this: *TimerObjectInternals, vm: *VirtualMachine, exception_thrown: bool) bool { + // returns true if an exception was thrown + pub fn runImmediateTask(this: *TimerObjectInternals, vm: *VirtualMachine) bool { if (this.flags.has_cleared_timer or // unref'd setImmediate callbacks should only run if there are things keeping the event // loop alive other than setImmediates (!this.flags.is_keeping_event_loop_alive and !vm.isEventLoopAliveExcludingImmediates())) { this.deref(); - return exception_thrown; + return false; } const timer = this.strong_this.get() orelse { if (Environment.isDebug) { @panic("TimerObjectInternals.runImmediateTask: this_object is null"); } - return exception_thrown; + return false; }; const globalThis = vm.global; this.strong_this.deinit(); @@ -878,16 +880,16 @@ pub const TimerObjectInternals = struct { const callback = ImmediateObject.js.callbackGetCached(timer).?; const arguments = ImmediateObject.js.argumentsGetCached(timer).?; this.ref(); - const threw_exception = this.run(globalThis, timer, callback, arguments, this.asyncID(), vm); + const exception_thrown = this.run(globalThis, timer, callback, arguments, this.asyncID(), vm); this.deref(); if (this.eventLoopTimer().state == .FIRED) { this.deref(); } - vm.eventLoop().exitMaybeDrainMicrotask(!exception_thrown and !threw_exception); + vm.eventLoop().exitMaybeDrainMicrotask(!exception_thrown); - return exception_thrown or threw_exception; + return exception_thrown; } pub fn asyncID(this: *const TimerObjectInternals) u64 { diff --git a/src/bun.js/bindings/NodeTimerObject.cpp b/src/bun.js/bindings/NodeTimerObject.cpp index 10fd17945f..0840f62528 100644 --- a/src/bun.js/bindings/NodeTimerObject.cpp +++ b/src/bun.js/bindings/NodeTimerObject.cpp @@ -38,7 +38,7 @@ static bool call(JSGlobalObject* globalObject, JSValue timerObject, JSValue call auto callData = JSC::getCallData(callbackValue); if (callData.type == CallData::Type::None) { Bun__reportUnhandledError(globalObject, JSValue::encode(createNotAFunctionError(globalObject, callbackValue))); - return false; + return true; } MarkedArgumentBuffer args; @@ -73,12 +73,12 @@ static bool call(JSGlobalObject* globalObject, JSValue timerObject, JSValue call return hadException; } -// Returns false if an exception was thrown. +// Returns true if an exception was thrown. extern "C" bool Bun__JSTimeout__call(JSGlobalObject* globalObject, EncodedJSValue timerObject, EncodedJSValue callbackValue, EncodedJSValue argumentsValue) { auto& vm = globalObject->vm(); if (UNLIKELY(vm.hasPendingTerminationException())) { - return false; + return true; } return call(globalObject, JSValue::decode(timerObject), JSValue::decode(callbackValue), JSValue::decode(argumentsValue)); diff --git a/src/bun.js/event_loop.zig b/src/bun.js/event_loop.zig index 39c77359e0..901be07b48 100644 --- a/src/bun.js/event_loop.zig +++ b/src/bun.js/event_loop.zig @@ -1423,11 +1423,10 @@ pub const EventLoop = struct { var exception_thrown = false; for (to_run_now.items) |task| { - exception_thrown = task.runImmediateTask(virtual_machine, exception_thrown); + exception_thrown = task.runImmediateTask(virtual_machine); } - // if an exception was thrown, drain the pending microtasks from the remaining - // immediate tasks. + // make sure microtasks are drained if the last task had an exception if (exception_thrown) { this.maybeDrainMicrotasks(); }