forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_timers.js
595 lines (523 loc) · 16 KB
/
01_timers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict";
((window) => {
const core = window.Deno.core;
const {
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeShift,
ArrayPrototypeSplice,
DateNow,
Error,
FunctionPrototypeBind,
Map,
MapPrototypeDelete,
MapPrototypeGet,
MapPrototypeHas,
MapPrototypeSet,
MathMax,
Number,
String,
TypeError,
} = window.__bootstrap.primordials;
// Shamelessly cribbed from extensions/fetch/11_streams.js
class AssertionError extends Error {
constructor(msg) {
super(msg);
this.name = "AssertionError";
}
}
/**
* @param {unknown} cond
* @param {string=} msg
* @returns {asserts cond}
*/
function assert(cond, msg = "Assertion failed.") {
if (!cond) {
throw new AssertionError(msg);
}
}
function opStopGlobalTimer() {
core.opSync("op_global_timer_stop");
}
function opStartGlobalTimer(timeout) {
return core.opSync("op_global_timer_start", timeout);
}
async function opWaitGlobalTimer() {
await core.opAsync("op_global_timer");
}
function opNow() {
return core.opSync("op_now");
}
function sleepSync(millis = 0) {
return core.opSync("op_sleep_sync", millis);
}
// Derived from https://github.com/vadimg/js_bintrees. MIT Licensed.
class RBNode {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
this.red = true;
}
getChild(dir) {
return dir ? this.right : this.left;
}
setChild(dir, val) {
if (dir) {
this.right = val;
} else {
this.left = val;
}
}
}
class RBTree {
#comparator = null;
#root = null;
constructor(comparator) {
this.#comparator = comparator;
this.#root = null;
}
/** Returns `null` if tree is empty. */
min() {
let res = this.#root;
if (res === null) {
return null;
}
while (res.left !== null) {
res = res.left;
}
return res.data;
}
/** Returns node `data` if found, `null` otherwise. */
find(data) {
let res = this.#root;
while (res !== null) {
const c = this.#comparator(data, res.data);
if (c === 0) {
return res.data;
} else {
res = res.getChild(c > 0);
}
}
return null;
}
/** returns `true` if inserted, `false` if duplicate. */
insert(data) {
let ret = false;
if (this.#root === null) {
// empty tree
this.#root = new RBNode(data);
ret = true;
} else {
const head = new RBNode(null); // fake tree root
let dir = 0;
let last = 0;
// setup
let gp = null; // grandparent
let ggp = head; // grand-grand-parent
let p = null; // parent
let node = this.#root;
ggp.right = this.#root;
// search down
while (true) {
if (node === null) {
// insert new node at the bottom
node = new RBNode(data);
p.setChild(dir, node);
ret = true;
} else if (isRed(node.left) && isRed(node.right)) {
// color flip
node.red = true;
node.left.red = false;
node.right.red = false;
}
// fix red violation
if (isRed(node) && isRed(p)) {
const dir2 = ggp.right === gp;
assert(gp);
if (node === p.getChild(last)) {
ggp.setChild(dir2, singleRotate(gp, !last));
} else {
ggp.setChild(dir2, doubleRotate(gp, !last));
}
}
const cmp = this.#comparator(node.data, data);
// stop if found
if (cmp === 0) {
break;
}
last = dir;
dir = Number(cmp < 0); // Fix type
// update helpers
if (gp !== null) {
ggp = gp;
}
gp = p;
p = node;
node = node.getChild(dir);
}
// update root
this.#root = head.right;
}
// make root black
this.#root.red = false;
return ret;
}
/** Returns `true` if removed, `false` if not found. */
remove(data) {
if (this.#root === null) {
return false;
}
const head = new RBNode(null); // fake tree root
let node = head;
node.right = this.#root;
let p = null; // parent
let gp = null; // grand parent
let found = null; // found item
let dir = 1;
while (node.getChild(dir) !== null) {
const last = dir;
// update helpers
gp = p;
p = node;
node = node.getChild(dir);
const cmp = this.#comparator(data, node.data);
dir = cmp > 0;
// save found node
if (cmp === 0) {
found = node;
}
// push the red node down
if (!isRed(node) && !isRed(node.getChild(dir))) {
if (isRed(node.getChild(!dir))) {
const sr = singleRotate(node, dir);
p.setChild(last, sr);
p = sr;
} else if (!isRed(node.getChild(!dir))) {
const sibling = p.getChild(!last);
if (sibling !== null) {
if (
!isRed(sibling.getChild(!last)) &&
!isRed(sibling.getChild(last))
) {
// color flip
p.red = false;
sibling.red = true;
node.red = true;
} else {
assert(gp);
const dir2 = gp.right === p;
if (isRed(sibling.getChild(last))) {
gp.setChild(dir2, doubleRotate(p, last));
} else if (isRed(sibling.getChild(!last))) {
gp.setChild(dir2, singleRotate(p, last));
}
// ensure correct coloring
const gpc = gp.getChild(dir2);
assert(gpc);
gpc.red = true;
node.red = true;
assert(gpc.left);
gpc.left.red = false;
assert(gpc.right);
gpc.right.red = false;
}
}
}
}
}
// replace and remove if found
if (found !== null) {
found.data = node.data;
assert(p);
p.setChild(p.right === node, node.getChild(node.left === null));
}
// update root and make it black
this.#root = head.right;
if (this.#root !== null) {
this.#root.red = false;
}
return found !== null;
}
}
function isRed(node) {
return node !== null && node.red;
}
function singleRotate(root, dir) {
const save = root.getChild(!dir);
assert(save);
root.setChild(!dir, save.getChild(dir));
save.setChild(dir, root);
root.red = true;
save.red = false;
return save;
}
function doubleRotate(root, dir) {
root.setChild(!dir, singleRotate(root.getChild(!dir), !dir));
return singleRotate(root, dir);
}
const { console } = globalThis;
// Timeout values > TIMEOUT_MAX are set to 1.
const TIMEOUT_MAX = 2 ** 31 - 1;
let globalTimeoutDue = null;
let nextTimerId = 1;
const idMap = new Map();
const dueTree = new RBTree((a, b) => a.due - b.due);
function clearGlobalTimeout() {
globalTimeoutDue = null;
opStopGlobalTimer();
}
let pendingEvents = 0;
const pendingFireTimers = [];
/** Process and run a single ready timer macrotask.
* This function should be registered through Deno.core.setMacrotaskCallback.
* Returns true when all ready macrotasks have been processed, false if more
* ready ones are available. The Isolate future would rely on the return value
* to repeatedly invoke this function until depletion. Multiple invocations
* of this function one at a time ensures newly ready microtasks are processed
* before next macrotask timer callback is invoked. */
function handleTimerMacrotask() {
if (pendingFireTimers.length > 0) {
fire(ArrayPrototypeShift(pendingFireTimers));
return pendingFireTimers.length === 0;
}
return true;
}
async function setGlobalTimeout(due, now) {
// Since JS and Rust don't use the same clock, pass the time to rust as a
// relative time value. On the Rust side we'll turn that into an absolute
// value again.
const timeout = due - now;
assert(timeout >= 0);
// Send message to the backend.
globalTimeoutDue = due;
pendingEvents++;
// FIXME(bartlomieju): this is problematic, because `clearGlobalTimeout`
// is synchronous. That means that timer is cancelled, but this promise is still pending
// until next turn of event loop. This leads to "leaking of async ops" in tests;
// because `clearTimeout/clearInterval` might be the last statement in test function
// `opSanitizer` will immediately complain that there is pending op going on, unless
// some timeout/defer is put in place to allow promise resolution.
// Ideally `clearGlobalTimeout` doesn't return until this op is resolved, but
// I'm not if that's possible.
opStartGlobalTimer(timeout);
await opWaitGlobalTimer();
pendingEvents--;
prepareReadyTimers();
}
function prepareReadyTimers() {
const now = DateNow();
// Bail out if we're not expecting the global timer to fire.
if (globalTimeoutDue === null || pendingEvents > 0) {
return;
}
// After firing the timers that are due now, this will hold the first timer
// list that hasn't fired yet.
let nextDueNode;
while ((nextDueNode = dueTree.min()) !== null && nextDueNode.due <= now) {
dueTree.remove(nextDueNode);
// Fire all the timers in the list.
for (const timer of nextDueNode.timers) {
// With the list dropped, the timer is no longer scheduled.
timer.scheduled = false;
// Place the callback to pending timers to fire.
ArrayPrototypePush(pendingFireTimers, timer);
}
}
setOrClearGlobalTimeout(nextDueNode && nextDueNode.due, now);
}
function setOrClearGlobalTimeout(due, now) {
if (due == null) {
clearGlobalTimeout();
} else {
setGlobalTimeout(due, now);
}
}
function schedule(timer, now) {
assert(!timer.scheduled);
assert(now <= timer.due);
// Find or create the list of timers that will fire at point-in-time `due`.
const maybeNewDueNode = { due: timer.due, timers: [] };
let dueNode = dueTree.find(maybeNewDueNode);
if (dueNode === null) {
dueTree.insert(maybeNewDueNode);
dueNode = maybeNewDueNode;
}
// Append the newly scheduled timer to the list and mark it as scheduled.
ArrayPrototypePush(dueNode.timers, timer);
timer.scheduled = true;
// If the new timer is scheduled to fire before any timer that existed before,
// update the global timeout to reflect this.
if (globalTimeoutDue === null || globalTimeoutDue > timer.due) {
setOrClearGlobalTimeout(timer.due, now);
}
}
function unschedule(timer) {
// Check if our timer is pending scheduling or pending firing.
// If either is true, they are not in tree, and their idMap entry
// will be deleted soon. Remove it from queue.
let index = -1;
if ((index = ArrayPrototypeIndexOf(pendingFireTimers, timer)) >= 0) {
ArrayPrototypeSplice(pendingFireTimers, index);
return;
}
// If timer is not in the 2 pending queues and is unscheduled,
// it is not in the tree.
if (!timer.scheduled) {
return;
}
const searchKey = { due: timer.due, timers: [] };
// Find the list of timers that will fire at point-in-time `due`.
const list = dueTree.find(searchKey).timers;
if (list.length === 1) {
// Time timer is the only one in the list. Remove the entire list.
assert(list[0] === timer);
dueTree.remove(searchKey);
// If the unscheduled timer was 'next up', find when the next timer that
// still exists is due, and update the global alarm accordingly.
if (timer.due === globalTimeoutDue) {
const nextDueNode = dueTree.min();
setOrClearGlobalTimeout(
nextDueNode && nextDueNode.due,
DateNow(),
);
}
} else {
// Multiple timers that are due at the same point in time.
// Remove this timer from the list.
const index = ArrayPrototypeIndexOf(list, timer);
assert(index > -1);
ArrayPrototypeSplice(list, index, 1);
}
}
function fire(timer) {
// If the timer isn't found in the ID map, that means it has been cancelled
// between the timer firing and the promise callback (this function).
if (!MapPrototypeHas(idMap, timer.id)) {
return;
}
// Reschedule the timer if it is a repeating one, otherwise drop it.
if (!timer.repeat) {
// One-shot timer: remove the timer from this id-to-timer map.
MapPrototypeDelete(idMap, timer.id);
} else {
// Interval timer: compute when timer was supposed to fire next.
// However make sure to never schedule the next interval in the past.
const now = DateNow();
timer.due = MathMax(now, timer.due + timer.delay);
schedule(timer, now);
}
// Call the user callback. Intermediate assignment is to avoid leaking `this`
// to it, while also keeping the stack trace neat when it shows up in there.
const callback = timer.callback;
if ("function" === typeof callback) {
callback();
} else {
(0, eval)(callback);
}
}
function checkThis(thisArg) {
if (thisArg !== null && thisArg !== undefined && thisArg !== globalThis) {
throw new TypeError("Illegal invocation");
}
}
function setTimer(
cb,
delay,
args,
repeat,
) {
// If the callack is a function, bind `args` to the callback and bind `this` to globalThis(global).
// otherwise call `String` on it, and `eval` it on calls; do not pass variardic args to the string
let callback;
if ("function" === typeof cb) {
callback = FunctionPrototypeBind(cb, globalThis, ...args);
} else {
callback = String(cb);
args = []; // args are ignored
}
// In the browser, the delay value must be coercible to an integer between 0
// and INT32_MAX. Any other value will cause the timer to fire immediately.
// We emulate this behavior.
const now = DateNow();
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 = MathMax(0, delay | 0);
// Create a new, unscheduled timer object.
const timer = {
id: nextTimerId++,
callback,
args,
delay,
due: now + delay,
repeat,
scheduled: false,
};
// Register the timer's existence in the id-to-timer map.
MapPrototypeSet(idMap, timer.id, timer);
// Schedule the timer in the due table.
schedule(timer, now);
return timer.id;
}
function setTimeout(
cb,
delay = 0,
...args
) {
delay >>>= 0;
checkThis(this);
return setTimer(cb, delay, args, false);
}
function setInterval(
cb,
delay = 0,
...args
) {
delay >>>= 0;
checkThis(this);
return setTimer(cb, delay, args, true);
}
function clearTimer(id) {
id >>>= 0;
const timer = MapPrototypeGet(idMap, id);
if (timer === undefined) {
// Timer doesn't exist any more or never existed. This is not an error.
return;
}
// Unschedule the timer if it is currently scheduled, and forget about it.
unschedule(timer);
MapPrototypeDelete(idMap, timer.id);
}
function clearTimeout(id = 0) {
id >>>= 0;
if (id === 0) {
return;
}
clearTimer(id);
}
function clearInterval(id = 0) {
id >>>= 0;
if (id === 0) {
return;
}
clearTimer(id);
}
window.__bootstrap.timers = {
clearInterval,
setInterval,
clearTimeout,
setTimeout,
handleTimerMacrotask,
opStopGlobalTimer,
opStartGlobalTimer,
opNow,
sleepSync,
};
})(this);