Files
scriptc/tests/corpus/6236-builtin-function-values.cjs
T
Chris Tate 3a09a000db Leave the stored timer functions out of library builds
scr_dyn_invoke.c links into libraries that use dynamic calls, and its stored
timer functions call scr_set_timeout_handle, scr_set_interval,
scr_clear_interval, scr_set_immediate, scr_clear_immediate, scr_next_tick
and scr_queue_microtask_dyn, which live in scr_async.c. Libraries never link
scr_async.c (a library that schedules timers is refused at SC4005), so their
host links failed with undefined symbols. Under SCR_LIB the calls that
schedule are left out, and a call through a stored timer function throws the
SC4005 event loop error. Executables compile the same code as before.

- 6236-builtin-function-values.cjs counts the interval's runs apart from the
  order of the other callbacks and reports once the interval has stopped, so
  a slow machine prints the same line as Node.
- node24-util-types: Function now reads as a value, so the fixture's first
  refusal is the SC1090 for extending Promise instead of the SC2009 for the
  FunctionConstructor value.
2026-10-18 21:37:02 -05:00

56 lines
2.3 KiB
JavaScript

// Builtin timer functions stored as values (bcryptjs's `nextTick` feature
// probe, async libraries' scheduler selection): conditional, logical and
// assigned values are real functions — typeof answers "function", identity
// holds across spellings, and calls schedule with Node's argument contract
// and ordering.
var nextTick =
typeof setImmediate === "function"
? setImmediate
: typeof queueMicrotask === "function"
? queueMicrotask
: setTimeout;
const pick = process.argv.length > 100;
const later = pick ? setImmediate : process.nextTick;
const timeout = pick ? setImmediate : setTimeout;
const micro = pick || queueMicrotask;
let assigned = setTimeout;
assigned = pick ? assigned : setImmediate;
const { setInterval: every, clearInterval: stop } = globalThis;
console.log(typeof nextTick, typeof later, typeof timeout, typeof micro, typeof assigned, typeof every);
console.log(nextTick === setImmediate, later === process.nextTick, timeout === setTimeout, micro === queueMicrotask, assigned === globalThis.setImmediate, every === setInterval, stop === clearInterval);
console.log(nextTick.name, later.name, timeout.name, setTimeout.length, queueMicrotask.length);
const order = [];
nextTick(function () { order.push("immediate:" + arguments.length); });
later((a, b) => order.push("tick:" + a + b), 1, 2);
// Microtask and nextTick queues order differently in CommonJS and ESM
// entry evaluation, so the microtask reports through its own flag.
let microRan = false;
micro(() => { microRan = true; });
timeout((x) => order.push("timeout:" + x), 60, "x");
assigned(() => order.push("assigned"));
const cancelled = timeout(() => order.push("cancelled"), 1);
(pick ? clearImmediate : clearTimeout)(cancelled);
// The interval counts its runs apart from `order`, and the report waits until
// it has stopped, so slow machines print the same line.
let count = 0;
const handle = every(() => {
count++;
if (count === 2) {
stop(handle);
timeout(() => console.log(order.join(" "), "interval:" + count, "micro:" + microRan), 120);
}
}, 20);
for (const bad of [undefined, 1, "fn"]) {
try {
later(bad);
} catch (e) {
console.log("rejected", typeof bad, e.code, e instanceof TypeError);
}
}
const log = pick ? console.error : console.log;
log("log", typeof log);
const mx = pick ? Math.min : Math.max;
console.log(mx(1, 3, 2), typeof mx);