diff --git a/index.html b/index.html index 902b4e23..d6b39d18 100644 --- a/index.html +++ b/index.html @@ -4,6 +4,7 @@
+ diff --git a/threads.js b/threads.js index d5cf0df8..7e165e4c 100644 --- a/threads.js +++ b/threads.js @@ -6,7 +6,6 @@ var Threads = function() { this.threads = []; this.empty = []; - this.ready = []; var mainThread = new Thread("main"); this.add(mainThread); this.current = mainThread; @@ -37,16 +36,3 @@ Threads.prototype.count = function() { Threads.prototype.getThread = function(pid) { return this.threads[pid]; } - -Threads.prototype.yield = function(frame) { - if (this.ready.length) { - this.current.frame = frame; - this.ready.unshift(this.current); - this.current = this.ready[this.ready.length - 1]; - } - window.postMessage(null, "*"); -} - -Threads.prototype.resume = function() { - VM.resume(this.current.frame); -} diff --git a/timer.js b/timer.js new file mode 100644 index 00000000..b24a58a0 --- /dev/null +++ b/timer.js @@ -0,0 +1,34 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ + +'use strict'; + +// Only add setZeroTimeout to the window object, and hide everything +// else in a closure. +(function() { + var timeouts = []; + var messageName = "zero-timeout-message"; + + // Like setTimeout, but only takes a function argument. There's + // no time argument (always zero) and no arguments (you have to + // use a closure). + function setZeroTimeout(fn) { + timeouts.push(fn); + window.postMessage(messageName, "*"); + } + + function handleMessage(event) { + if (event.source == window && event.data == messageName) { + event.stopPropagation(); + if (timeouts.length > 0) { + var fn = timeouts.shift(); + fn(); + } + } + } + + window.addEventListener("message", handleMessage, true); + + // Add the one thing we want added to the window object. + window.setZeroTimeout = setZeroTimeout; +})();