mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
timers: use int instead of double for timeout type (#1469)
This commit is contained in:
parent
19b2d4a62a
commit
aaaa35548e
4 changed files with 24 additions and 3 deletions
13
js/timers.ts
13
js/timers.ts
|
@ -25,6 +25,9 @@ interface Timer {
|
|||
const EPOCH = Date.now();
|
||||
const APOCALYPSE = 2 ** 32 - 2;
|
||||
|
||||
// Timeout values > TIMEOUT_MAX are set to 1.
|
||||
const TIMEOUT_MAX = 2 ** 31 - 1;
|
||||
|
||||
let globalTimeoutDue: number | null = null;
|
||||
|
||||
let nextTimerId = 1;
|
||||
|
@ -50,6 +53,7 @@ function setGlobalTimeout(due: number | null, now: number) {
|
|||
timeout = due - now;
|
||||
assert(timeout >= 0);
|
||||
}
|
||||
|
||||
// Send message to the backend.
|
||||
const builder = flatbuffers.createBuilder();
|
||||
msg.SetTimeout.startSetTimeout(builder);
|
||||
|
@ -181,7 +185,16 @@ function setTimer(
|
|||
// and INT32_MAX. Any other value will cause the timer to fire immediately.
|
||||
// We emulate this behavior.
|
||||
const now = getTime();
|
||||
if (delay > TIMEOUT_MAX) {
|
||||
console.warn(
|
||||
`${delay} does not fit into` +
|
||||
" a 32-bit signed integer." +
|
||||
"\nTimeout duration was set to 1."
|
||||
);
|
||||
delay = 1;
|
||||
}
|
||||
delay = Math.max(0, delay | 0);
|
||||
|
||||
// Create a new, unscheduled timer object.
|
||||
const timer = {
|
||||
id: nextTimerId++,
|
||||
|
|
|
@ -155,3 +155,12 @@ test(async function intervalCancelInvalidSilentFail() {
|
|||
// Should silently fail (no panic)
|
||||
clearInterval(2147483647);
|
||||
});
|
||||
|
||||
test(async function fireCallbackImmediatelyWhenDelayOverMaxValue() {
|
||||
let count = 0;
|
||||
setTimeout(() => {
|
||||
count++;
|
||||
}, 2 ** 31);
|
||||
await waitForMs(1);
|
||||
assertEqual(count, 1);
|
||||
});
|
||||
|
|
|
@ -202,7 +202,7 @@ table Chdir {
|
|||
}
|
||||
|
||||
table SetTimeout {
|
||||
timeout: double;
|
||||
timeout: int;
|
||||
}
|
||||
|
||||
table Exit {
|
||||
|
|
|
@ -338,8 +338,7 @@ fn op_set_timeout(
|
|||
) -> Box<Op> {
|
||||
assert_eq!(data.len(), 0);
|
||||
let inner = base.inner_as_set_timeout().unwrap();
|
||||
// FIXME why is timeout a double if it's cast immediately to i64/u64??
|
||||
let val = inner.timeout() as i64;
|
||||
let val = inner.timeout();
|
||||
let timeout_due = if val >= 0 {
|
||||
Some(Instant::now() + Duration::from_millis(val as u64))
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue