Chromium Code Reviews| Index: sdk/lib/_internal/lib/preambles/d8.js |
| diff --git a/sdk/lib/_internal/lib/preambles/d8.js b/sdk/lib/_internal/lib/preambles/d8.js |
| index 2b20e498350596c49ba89a8227e4eef465dfeb83..73cf8c36bcd6280ce82298533db710b1f28eac84 100644 |
| --- a/sdk/lib/_internal/lib/preambles/d8.js |
| +++ b/sdk/lib/_internal/lib/preambles/d8.js |
| @@ -3,3 +3,131 @@ |
| // BSD-style license that can be found in the LICENSE file. |
| // Javascript preamble, that lets the output of dart2js run on V8's d8 shell. |
| + |
| +var setTimeout; |
| +var clearTimeout; |
| +var setInterval; |
| +var clearInterval; |
| +var dartMainRunner; |
| +var scheduleImmediate; |
| + |
| +(function() { |
| + // Event loop. |
| + |
| + // Task queue as cyclic list queue. |
| + var taskQueue = new Array(8); // Length is power of 2. |
| + var head = 0; |
| + var tail = 0; |
| + var mask = taskQueue.length - 1; |
| + function addTask(elem) { |
| + taskQueue[head] = elem; |
| + head = (head + 1) & mask; |
| + if (head == tail) _growTaskQueue(); |
| + } |
| + function hasTask() { |
| + return head != tail; |
| + } |
| + function removeTask() { |
| + if (head == tail) return; |
|
floitsch
2014/03/18 13:18:23
if (!hasTask()) return;
?
Lasse Reichstein Nielsen
2014/03/19 10:24:41
I removed hasTask, and use return of undefined to
|
| + var result = taskQueue[tail]; |
| + taskQueue[tail] = undefined; |
| + tail = (tail + 1) & mask; |
| + return result; |
| + } |
| + function _growTaskQueue() { |
| + // head == tail. |
| + var length = taskQueue.length; |
| + var split = head; |
| + taskQueue.length = length * 2; |
| + if (split * 2 < length) { // split < length / 2 |
|
floitsch
2014/03/18 13:18:23
I wouldn't do the optimization of copying the smal
Lasse Reichstein Nielsen
2014/03/19 10:24:41
But now it's there, so why remove it? :)
|
| + for (var i = 0; i < split; i++) { |
| + taskQueue[length + i] = taskQueue[i]; |
| + taskQueue[i] = undefined; |
| + } |
| + tail += length; |
| + } else { |
| + for (var i = split; i < length; i++) { |
| + taskQueue[length + i] = taskQueue[i]; |
| + taskQueue[i] = undefined; |
| + } |
| + head += length; |
| + } |
| + mask = taskQueue.length - 1; |
| + } |
| + |
| + // Timer queue as simple array queue using push/shift. |
| + var timerIds = {}; |
| + var timerQueue = []; // TODO: make this a sort of priority queue. |
| + var timerIdCounter = 1; |
| + var timerLiveCount = 0; |
| + function addTimer(f, ms) { |
| + if (ms != 0) throw "Unsupported error: non-zero timer: " + ms + " ms"; |
| + var id = timerIdCounter++; |
| + f.$timerId = id; |
| + timerIds[id] = f; |
| + timerQueue.push(f); |
| + timerLiveCount++; |
| + return id; |
| + } |
| + |
| + function cancelTimer(id) { |
| + var f = timerIds[id]; |
| + if (f == null) return; |
| + f.$timerId = undefined; |
| + delete timerIds[id]; |
| + timerLiveCount--; |
| + } |
| + |
| + function removeTimer() { |
| + do { |
| + var f = timerQueue.shift(); |
| + var id = f.$timerId; |
| + } while (id == undefined); |
| + delete timerIds[id]; |
|
floitsch
2014/03/18 13:18:23
Use cancelTimer here.
Lasse Reichstein Nielsen
2014/03/19 10:24:41
Good idea.
|
| + f.$timerId = undefined; |
| + timerLiveCount--; |
| + if (timerLiveCount == 0) { |
| + timerQueue.length = 0; // only cancelled timers left, drop them quickly. |
| + } |
| + return f; |
| + } |
| + |
| + function hasTimer() { return timerLiveCount > 0; } |
| + |
| + function eventLoop(action) { |
| + for(;;) { |
| + try { |
| + action(); |
| + } catch (e) { |
| + if (typeof onerror == "function") { |
|
floitsch
2014/03/18 13:18:23
Not sure we want to introduce this already in this
Lasse Reichstein Nielsen
2014/03/19 10:24:41
Let's keep it as tentative for now. It's not being
|
| + onerror(e, null, -1); |
| + } else { |
| + throw e; |
| + } |
| + } |
| + // Handle task queue. |
| + if (hasTask()) { |
| + action = removeTask(); |
| + continue; |
| + } |
| + if (hasTimer()) { |
| + action = removeTimer(); |
| + continue; |
| + } |
| + break; |
| + } |
| + } |
| + |
| + dartMainRunner = function(main, args) { |
| + // Initialize. |
| + var action = function() { main(args); } |
| + eventLoop(action); |
| + }; |
| + setTimeout = addTimer; |
| + clearTimeout = cancelTimer; |
| + setInterval = clearInterval = function () { |
| + throw "Unsupported error: interval timer"; |
| + } |
| + scheduleImmediate = addTask; |
| +})(); |
| + |