@@ -82,3 +82,63 @@ test('ffi callback ref and unref APIs work', () => { |
82 | 82 | lib.close(); |
83 | 83 | } |
84 | 84 | }); |
| 85 | + |
| 86 | +test('ffi callbacks drain nextTick and microtasks only from the event loop', { |
| 87 | + skip: common.isWindows, |
| 88 | +}, async () => { |
| 89 | + const { lib, functions: symbols } = getLibrary(); |
| 90 | + let ran = false; |
| 91 | + const syncCallback = lib.registerCallback( |
| 92 | + { arguments: ['i32'], return: 'i32' }, |
| 93 | + (value) => { |
| 94 | + queueMicrotask(() => { ran = true; }); |
| 95 | + return value; |
| 96 | + }, |
| 97 | + ); |
| 98 | + |
| 99 | + // The process exports libuv, so a uv timer can invoke the callback from |
| 100 | + // the event loop with no JS on the stack. |
| 101 | + const { lib: uvLib, functions: uv } = ffi.dlopen(null, { |
| 102 | + uv_handle_size: { arguments: ['i32'], return: 'u64' }, |
| 103 | + uv_timer_init: { arguments: ['pointer', 'pointer'], return: 'i32' }, |
| 104 | + uv_timer_start: { arguments: ['pointer', 'pointer', 'u64', 'u64'], return: 'i32' }, |
| 105 | + uv_close: { arguments: ['pointer', 'pointer'], return: 'void' }, |
| 106 | + }); |
| 107 | + const UV_TIMER = 13; |
| 108 | + const timerSize = Number(uv.uv_handle_size(UV_TIMER)) + 64; |
| 109 | + const timers = [new Uint8Array(timerSize), new Uint8Array(timerSize)]; |
| 110 | + const { promise, resolve } = Promise.withResolvers(); |
| 111 | + const order = []; |
| 112 | + const timerCallbacks = [ |
| 113 | + uvLib.registerCallback({ arguments: ['pointer'], return: 'void' }, () => { |
| 114 | + order.push('callback'); |
| 115 | + process.nextTick(() => order.push('nextTick')); |
| 116 | + queueMicrotask(() => order.push('microtask')); |
| 117 | + }), |
| 118 | + // The second timer runs right after the first one, with no Node.js |
| 119 | + // callback in between that could drain the queues instead. |
| 120 | + uvLib.registerCallback({ arguments: ['pointer'], return: 'void' }, () => { |
| 121 | + resolve([...order]); |
| 122 | + for (const timer of timers) uv.uv_close(timer, 0n); |
| 123 | + }), |
| 124 | + ]; |
| 125 | + |
| 126 | + try { |
| 127 | + // A callback invoked synchronously runs inside the caller's scope, so its |
| 128 | + // microtasks must not run before the native call returns. |
| 129 | + assert.strictEqual(symbols.call_int_callback(syncCallback, 1), 1); |
| 130 | + assert.strictEqual(ran, false); |
| 131 | + |
| 132 | + const loop = ffi.getCurrentEventLoop(); |
| 133 | + for (let i = 0; i < timers.length; i++) { |
| 134 | + assert.strictEqual(uv.uv_timer_init(loop, timers[i]), 0); |
| 135 | + assert.strictEqual(uv.uv_timer_start(timers[i], timerCallbacks[i], 0n, 0n), 0); |
| 136 | + } |
| 137 | + assert.deepStrictEqual(await promise, ['callback', 'nextTick', 'microtask']); |
| 138 | + // Let the uv_close() of the timer handles complete. |
| 139 | + await new Promise(setImmediate); |
| 140 | + } finally { |
| 141 | + lib.close(); |
| 142 | + uvLib.close(); |
| 143 | + } |
| 144 | +}); |