OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Javascript preamble, that lets the output of dart2js run on V8's d8 shell. | 5 // Javascript preamble, that lets the output of dart2js run on V8's d8 shell. |
6 | 6 |
7 // Node wraps files and provides them with a different `this`. The global | 7 // Node wraps files and provides them with a different `this`. The global |
8 // `this` can be accessed through `global`. | 8 // `this` can be accessed through `global`. |
9 | 9 |
10 var self = this; | 10 var self = this; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 var timerIds = {}; | 78 var timerIds = {}; |
79 var timerIdCounter = 1; // Counter used to assign ids. | 79 var timerIdCounter = 1; // Counter used to assign ids. |
80 | 80 |
81 // Zero-timer queue as simple array queue using push/shift. | 81 // Zero-timer queue as simple array queue using push/shift. |
82 var zeroTimerQueue = []; | 82 var zeroTimerQueue = []; |
83 | 83 |
84 function addTimer(f, ms) { | 84 function addTimer(f, ms) { |
85 var id = timerIdCounter++; | 85 var id = timerIdCounter++; |
86 f.$timerId = id; | 86 f.$timerId = id; |
87 timerIds[id] = f; | 87 timerIds[id] = f; |
88 if (ms == 0 && !isNextTimerDue()) { | 88 if (ms == 0) { |
89 zeroTimerQueue.push(f); | 89 zeroTimerQueue.push(f); |
90 } else { | 90 } else { |
91 addDelayedTimer(f, ms); | 91 addDelayedTimer(f, ms); |
92 } | 92 } |
93 return id; | 93 return id; |
94 } | 94 } |
95 | 95 |
96 function nextZeroTimer() { | 96 function nextZeroTimer() { |
97 while (zeroTimerQueue.length > 0) { | 97 while (zeroTimerQueue.length > 0) { |
98 var action = zeroTimerQueue.shift(); | 98 var action = zeroTimerQueue.shift(); |
(...skipping 28 matching lines...) Expand all Loading... |
127 // Install the mock Date object only once. | 127 // Install the mock Date object only once. |
128 // Following calls to "now" will just use the new (mocked) Date.now | 128 // Following calls to "now" will just use the new (mocked) Date.now |
129 // method directly. | 129 // method directly. |
130 installMockDate(); | 130 installMockDate(); |
131 now = Date.now; | 131 now = Date.now; |
132 return Date.now(); | 132 return Date.now(); |
133 }; | 133 }; |
134 var originalDate = Date; | 134 var originalDate = Date; |
135 var originalNow = originalDate.now; | 135 var originalNow = originalDate.now; |
136 function advanceTimeTo(time) { | 136 function advanceTimeTo(time) { |
137 var now = originalNow(); | 137 timeOffset = time - originalNow(); |
138 if (timeOffset < time - now) { | |
139 timeOffset = time - now; | |
140 } | |
141 } | 138 } |
142 function installMockDate() { | 139 function installMockDate() { |
143 var NewDate = function Date(Y, M, D, h, m, s, ms) { | 140 var NewDate = function Date(Y, M, D, h, m, s, ms) { |
144 if (this instanceof Date) { | 141 if (this instanceof Date) { |
145 // Assume a construct call. | 142 // Assume a construct call. |
146 switch (arguments.length) { | 143 switch (arguments.length) { |
147 case 0: return new originalDate(originalNow() + timeOffset); | 144 case 0: return new originalDate(originalNow() + timeOffset); |
148 case 1: return new originalDate(Y); | 145 case 1: return new originalDate(Y); |
149 case 2: return new originalDate(Y, M); | 146 case 2: return new originalDate(Y, M); |
150 case 3: return new originalDate(Y, M, D); | 147 case 3: return new originalDate(Y, M, D); |
(...skipping 24 matching lines...) Expand all Loading... |
175 timerList = [timeout, f]; | 172 timerList = [timeout, f]; |
176 timerIndex[timeout] = timerList; | 173 timerIndex[timeout] = timerList; |
177 var index = timerHeap.length; | 174 var index = timerHeap.length; |
178 timerHeap.length += 1; | 175 timerHeap.length += 1; |
179 bubbleUp(index, timeout, timerList); | 176 bubbleUp(index, timeout, timerList); |
180 } else { | 177 } else { |
181 timerList.push(f); | 178 timerList.push(f); |
182 } | 179 } |
183 } | 180 } |
184 | 181 |
185 function isNextTimerDue() { | |
186 if (timerHeap.length == 0) return false; | |
187 var head = timerHeap[0]; | |
188 return head[0] < originalNow() + timeOffset; | |
189 } | |
190 | |
191 function nextDelayedTimerQueue() { | 182 function nextDelayedTimerQueue() { |
192 if (timerHeap.length == 0) return null; | 183 if (timerHeap.length == 0) return null; |
193 var result = timerHeap[0]; | 184 var result = timerHeap[0]; |
194 var last = timerHeap.pop(); | 185 var last = timerHeap.pop(); |
195 if (timerHeap.length > 0) { | 186 if (timerHeap.length > 0) { |
196 bubbleDown(0, last[0], last); | 187 bubbleDown(0, last[0], last); |
197 } | 188 } |
198 return result; | 189 return result; |
199 } | 190 } |
200 | 191 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 var action = function() { main(args); } | 273 var action = function() { main(args); } |
283 eventLoop(action); | 274 eventLoop(action); |
284 }; | 275 }; |
285 self.setTimeout = addTimer; | 276 self.setTimeout = addTimer; |
286 self.clearTimeout = cancelTimer; | 277 self.clearTimeout = cancelTimer; |
287 self.setInterval = addInterval; | 278 self.setInterval = addInterval; |
288 self.clearInterval = cancelTimer; | 279 self.clearInterval = cancelTimer; |
289 self.scheduleImmediate = addTask; | 280 self.scheduleImmediate = addTask; |
290 self.self = self; | 281 self.self = self; |
291 })(self); | 282 })(self); |
OLD | NEW |