| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 var global = this.global || {}; | 28 var global = this.global || {}; |
| 29 var setTimeout; | 29 var setTimeout; |
| 30 var clearTimeout; | 30 var clearTimeout; |
| 31 | 31 |
| 32 (function() { | 32 (function() { |
| 33 var timers = {}; | 33 var timers = {}; |
| 34 var currentId = 0; | 34 var currentId = 0; |
| 35 | 35 |
| 36 function PostMicrotask(fn) { | |
| 37 var o = {}; | |
| 38 Object.observe(o, function() { | |
| 39 fn(); | |
| 40 }); | |
| 41 // Change something to enqueue a microtask. | |
| 42 o.x = 'hello'; | |
| 43 } | |
| 44 | |
| 45 setInterval = function(fn, delay) { | 36 setInterval = function(fn, delay) { |
| 46 var i = 0; | 37 var i = 0; |
| 47 var id = currentId++; | 38 var id = currentId++; |
| 48 function loop() { | 39 function loop() { |
| 49 if (!timers[id]) { | 40 if (!timers[id]) { |
| 50 return; | 41 return; |
| 51 } | 42 } |
| 52 if (i++ >= delay) { | 43 if (i++ >= delay) { |
| 53 fn(); | 44 fn(); |
| 54 } | 45 } |
| 55 PostMicrotask(loop); | 46 %EnqueueMicrotask(loop); |
| 56 } | 47 } |
| 57 PostMicrotask(loop); | 48 %EnqueueMicrotask(loop); |
| 58 timers[id] = true; | 49 timers[id] = true; |
| 59 return id; | 50 return id; |
| 60 } | 51 } |
| 61 | 52 |
| 62 clearTimeout = function(id) { | 53 clearTimeout = function(id) { |
| 63 delete timers[id]; | 54 delete timers[id]; |
| 64 } | 55 } |
| 65 | 56 |
| 66 clearInterval = clearTimeout; | 57 clearInterval = clearTimeout; |
| 67 | 58 |
| 68 setTimeout = function(fn, delay) { | 59 setTimeout = function(fn, delay) { |
| 69 var id = setInterval(function() { | 60 var id = setInterval(function() { |
| 70 fn(); | 61 fn(); |
| 71 clearInterval(id); | 62 clearInterval(id); |
| 72 }, delay); | 63 }, delay); |
| 73 return id; | 64 return id; |
| 74 } | 65 } |
| 75 | 66 |
| 76 }()); | 67 }()); |
| OLD | NEW |