mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-22 14:02:37 -05:00
Move threads list to the Runtime
This commit is contained in:
parent
b6186a44f3
commit
dbfb3356c6
2 changed files with 26 additions and 15 deletions
|
@ -10,11 +10,27 @@ function Runtime () {
|
|||
EventEmitter.call(this);
|
||||
|
||||
// State for the runtime
|
||||
/** @type {Object.<string, Object>} */
|
||||
/**
|
||||
* All blocks in the workspace.
|
||||
* Keys are block IDs, values are metadata about the block.
|
||||
* @type {Object.<string, Object>}
|
||||
*/
|
||||
this.blocks = {};
|
||||
/** @type {Array.<String>} */
|
||||
|
||||
/**
|
||||
* All stacks in the workspace.
|
||||
* A list of block IDs that represent stacks (first block in stack).
|
||||
* @type {Array.<String>}
|
||||
*/
|
||||
this.stacks = [];
|
||||
|
||||
/**
|
||||
* A list of threads that are currently running in the VM.
|
||||
* Threads are added when execution starts and pruned when execution ends.
|
||||
* @type {Array.<Thread>}
|
||||
*/
|
||||
this.threads = [];
|
||||
|
||||
/** @type {!Sequencer} */
|
||||
this.sequencer = new Sequencer();
|
||||
}
|
||||
|
|
|
@ -1,13 +1,6 @@
|
|||
var Timer = require('../util/Timer');
|
||||
|
||||
function Sequencer () {
|
||||
/**
|
||||
* A list of threads that are currently running in the VM.
|
||||
* Threads are added when execution starts and pruned when execution ends.
|
||||
* @type {Array.<Thread>}
|
||||
*/
|
||||
this.threads = [];
|
||||
|
||||
/**
|
||||
* A utility timer for timing thread sequencing.
|
||||
* @type {!Timer}
|
||||
|
@ -25,34 +18,36 @@ Sequencer.WORK_TIME = 1000 / 60;
|
|||
|
||||
/**
|
||||
* Step through all threads in `this.threads`, running them in order.
|
||||
* @return {Array.<Thread>} New threads after this round of execution.
|
||||
*/
|
||||
Sequencer.prototype.stepThreads = function () {
|
||||
Sequencer.prototype.stepThreads = function (threads) {
|
||||
// Start counting toward WORK_TIME
|
||||
this.timer.start();
|
||||
// While there are still threads to run and we are within WORK_TIME,
|
||||
// continue executing threads.
|
||||
while (this.threads.length > 0 &&
|
||||
while (threads.length > 0 &&
|
||||
this.timer.timeElapsed() < Sequencer.WORK_TIME) {
|
||||
// New threads at the end of the iteration.
|
||||
var newThreads = [];
|
||||
// Attempt to run each thread one time
|
||||
for (var i = 0; i < this.threads.length; i++) {
|
||||
var activeThread = this.threads[i];
|
||||
for (var i = 0; i < threads.length; i++) {
|
||||
var activeThread = threads[i];
|
||||
this.stepThread(activeThread);
|
||||
if (activeThread.nextBlock !== null) {
|
||||
newThreads.push(activeThread);
|
||||
}
|
||||
}
|
||||
// Effectively filters out threads that have stopped.
|
||||
this.threads = newThreads;
|
||||
threads = newThreads;
|
||||
}
|
||||
return threads;
|
||||
};
|
||||
|
||||
/**
|
||||
* Step the requested thread
|
||||
* @param {!Thread} thread Thread object to step
|
||||
*/
|
||||
Sequencer.protoype.stepThread = function (thread) {
|
||||
Sequencer.prototype.stepThread = function (thread) {
|
||||
// @todo
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue