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:
Jarred Sumner
2023-10-25 02:59:29 -07:00
committed by GitHub
parent c700a70872
commit 7bcf60324a
9 changed files with 169 additions and 30 deletions

View File

@@ -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;