mirror of
https://github.com/oven-sh/bun
synced 2026-02-17 06:12:08 +00:00
Fix setTimeout(() => {}, 0) and align setImmediate behavior with Node.js (#6674)
* Fix setTimeout(() => {}, 0)
* Align `setImmediate` with Node.js
* Update event_loop.zig
* Update test
* use Bun.sleep
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
@@ -629,9 +629,9 @@ pub const VirtualMachine = struct {
|
||||
}
|
||||
|
||||
pub fn isEventLoopAlive(vm: *const VirtualMachine) bool {
|
||||
return vm.active_tasks > 0 or
|
||||
vm.event_loop_handle.?.active > 0 or
|
||||
vm.event_loop.tasks.count > 0;
|
||||
return vm.active_tasks +
|
||||
@as(usize, vm.event_loop_handle.?.active) +
|
||||
vm.event_loop.tasks.count + vm.event_loop.immediate_tasks.count + vm.event_loop.next_immediate_tasks.count > 0;
|
||||
}
|
||||
|
||||
const SourceMapHandlerGetter = struct {
|
||||
@@ -1008,6 +1008,10 @@ pub const VirtualMachine = struct {
|
||||
this.eventLoop().enqueueTask(task);
|
||||
}
|
||||
|
||||
pub inline fn enqueueImmediateTask(this: *VirtualMachine, task: Task) void {
|
||||
this.eventLoop().enqueueImmediateTask(task);
|
||||
}
|
||||
|
||||
pub inline fn enqueueTaskConcurrent(this: *VirtualMachine, task: *JSC.ConcurrentTask) void {
|
||||
this.eventLoop().enqueueTaskConcurrent(task);
|
||||
}
|
||||
@@ -1046,6 +1050,8 @@ pub const VirtualMachine = struct {
|
||||
if (!this.has_enabled_macro_mode) {
|
||||
this.has_enabled_macro_mode = true;
|
||||
this.macro_event_loop.tasks = EventLoop.Queue.init(default_allocator);
|
||||
this.macro_event_loop.immediate_tasks = EventLoop.Queue.init(default_allocator);
|
||||
this.macro_event_loop.next_immediate_tasks = EventLoop.Queue.init(default_allocator);
|
||||
this.macro_event_loop.tasks.ensureTotalCapacity(16) catch unreachable;
|
||||
this.macro_event_loop.global = this.global;
|
||||
this.macro_event_loop.virtual_machine = this;
|
||||
@@ -1137,6 +1143,12 @@ pub const VirtualMachine = struct {
|
||||
vm.regular_event_loop.tasks = EventLoop.Queue.init(
|
||||
default_allocator,
|
||||
);
|
||||
vm.regular_event_loop.immediate_tasks = EventLoop.Queue.init(
|
||||
default_allocator,
|
||||
);
|
||||
vm.regular_event_loop.next_immediate_tasks = EventLoop.Queue.init(
|
||||
default_allocator,
|
||||
);
|
||||
vm.regular_event_loop.tasks.ensureUnusedCapacity(64) catch unreachable;
|
||||
vm.regular_event_loop.concurrent_tasks = .{};
|
||||
vm.event_loop = &vm.regular_event_loop;
|
||||
@@ -1240,6 +1252,12 @@ pub const VirtualMachine = struct {
|
||||
vm.regular_event_loop.tasks = EventLoop.Queue.init(
|
||||
default_allocator,
|
||||
);
|
||||
vm.regular_event_loop.immediate_tasks = EventLoop.Queue.init(
|
||||
default_allocator,
|
||||
);
|
||||
vm.regular_event_loop.next_immediate_tasks = EventLoop.Queue.init(
|
||||
default_allocator,
|
||||
);
|
||||
vm.regular_event_loop.tasks.ensureUnusedCapacity(64) catch unreachable;
|
||||
vm.regular_event_loop.concurrent_tasks = .{};
|
||||
vm.event_loop = &vm.regular_event_loop;
|
||||
@@ -1372,6 +1390,12 @@ pub const VirtualMachine = struct {
|
||||
vm.regular_event_loop.tasks = EventLoop.Queue.init(
|
||||
default_allocator,
|
||||
);
|
||||
vm.regular_event_loop.immediate_tasks = EventLoop.Queue.init(
|
||||
default_allocator,
|
||||
);
|
||||
vm.regular_event_loop.next_immediate_tasks = EventLoop.Queue.init(
|
||||
default_allocator,
|
||||
);
|
||||
vm.regular_event_loop.tasks.ensureUnusedCapacity(64) catch unreachable;
|
||||
vm.regular_event_loop.concurrent_tasks = .{};
|
||||
vm.event_loop = &vm.regular_event_loop;
|
||||
|
||||
Reference in New Issue
Block a user