mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-05-17 00:41:24 -04:00
Add thread management and stepping to runtime
This commit is contained in:
parent
dbfb3356c6
commit
92dab97b84
5 changed files with 610 additions and 376 deletions
|
@ -1,5 +1,6 @@
|
||||||
var EventEmitter = require('events');
|
var EventEmitter = require('events');
|
||||||
var Sequencer = require('./sequencer');
|
var Sequencer = require('./sequencer');
|
||||||
|
var Thread = require('./thread');
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,7 +33,7 @@ function Runtime () {
|
||||||
this.threads = [];
|
this.threads = [];
|
||||||
|
|
||||||
/** @type {!Sequencer} */
|
/** @type {!Sequencer} */
|
||||||
this.sequencer = new Sequencer();
|
this.sequencer = new Sequencer(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,6 +41,11 @@ function Runtime () {
|
||||||
*/
|
*/
|
||||||
util.inherits(Runtime, EventEmitter);
|
util.inherits(Runtime, EventEmitter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* How rapidly we try to step threads, in ms.
|
||||||
|
*/
|
||||||
|
Runtime.THREAD_STEP_INTERVAL = 1000 / 60;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Block management: create blocks and stacks from a `create` event
|
* Block management: create blocks and stacks from a `create` event
|
||||||
* @param {!Object} block Blockly create event to be processed
|
* @param {!Object} block Blockly create event to be processed
|
||||||
|
@ -152,6 +158,49 @@ Runtime.prototype.deleteBlock = function (e) {
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a thread and push it to the list of threads.
|
||||||
|
* @param {!string} id ID of block that starts the stack
|
||||||
|
*/
|
||||||
|
Runtime.prototype._pushThread = function (id) {
|
||||||
|
if (this.stacks.indexOf(id) < -1) return;
|
||||||
|
var thread = new Thread(id);
|
||||||
|
this.threads.push(thread);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a thread from the list of threads.
|
||||||
|
* @param {!string} id ID of block that starts the stack
|
||||||
|
*/
|
||||||
|
Runtime.prototype._removeThread = function (id) {
|
||||||
|
var i = this.threads.indexOf(id);
|
||||||
|
if (i > -1) this.threads.splice(i, 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repeatedly run `sequencer.stepThreads` and filter out
|
||||||
|
* inactive threads after each iteration.
|
||||||
|
*/
|
||||||
|
Runtime.prototype._step = function () {
|
||||||
|
var inactiveThreads = this.sequencer.stepThreads(this.threads);
|
||||||
|
for (var i = 0; i < inactiveThreads.length; i++) {
|
||||||
|
this._removeThread(inactiveThreads[i]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up timers to repeatedly step in a browser
|
||||||
|
*/
|
||||||
|
Runtime.prototype.start = function () {
|
||||||
|
if (!window.setInterval) return;
|
||||||
|
window.setInterval(function() {
|
||||||
|
this._step();
|
||||||
|
}.bind(this), Runtime.THREAD_STEP_INTERVAL);
|
||||||
|
};
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper to remove a stack from `this.stacks`
|
* Helper to remove a stack from `this.stacks`
|
||||||
* @param {?string} id ID of block that starts the stack
|
* @param {?string} id ID of block that starts the stack
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
var Timer = require('../util/Timer');
|
var Timer = require('../util/Timer');
|
||||||
|
|
||||||
function Sequencer () {
|
function Sequencer (runtime) {
|
||||||
/**
|
/**
|
||||||
* A utility timer for timing thread sequencing.
|
* A utility timer for timing thread sequencing.
|
||||||
* @type {!Timer}
|
* @type {!Timer}
|
||||||
*/
|
*/
|
||||||
this.timer = new Timer();
|
this.timer = new Timer();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reference to the runtime owning this sequencer.
|
||||||
|
* @type {!Runtime}
|
||||||
|
*/
|
||||||
|
this.runtime = runtime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,11 +24,13 @@ Sequencer.WORK_TIME = 1000 / 60;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Step through all threads in `this.threads`, running them in order.
|
* Step through all threads in `this.threads`, running them in order.
|
||||||
* @return {Array.<Thread>} New threads after this round of execution.
|
* @return {Array.<Thread>} All threads which have finished in this iteration.
|
||||||
*/
|
*/
|
||||||
Sequencer.prototype.stepThreads = function (threads) {
|
Sequencer.prototype.stepThreads = function (threads) {
|
||||||
// Start counting toward WORK_TIME
|
// Start counting toward WORK_TIME
|
||||||
this.timer.start();
|
this.timer.start();
|
||||||
|
// List of threads which have been killed by this step.
|
||||||
|
var inactiveThreads = [];
|
||||||
// While there are still threads to run and we are within WORK_TIME,
|
// While there are still threads to run and we are within WORK_TIME,
|
||||||
// continue executing threads.
|
// continue executing threads.
|
||||||
while (threads.length > 0 &&
|
while (threads.length > 0 &&
|
||||||
|
@ -35,12 +43,14 @@ Sequencer.prototype.stepThreads = function (threads) {
|
||||||
this.stepThread(activeThread);
|
this.stepThread(activeThread);
|
||||||
if (activeThread.nextBlock !== null) {
|
if (activeThread.nextBlock !== null) {
|
||||||
newThreads.push(activeThread);
|
newThreads.push(activeThread);
|
||||||
|
} else {
|
||||||
|
inactiveThreads.push(activeThread);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Effectively filters out threads that have stopped.
|
// Effectively filters out threads that have stopped.
|
||||||
threads = newThreads;
|
threads = newThreads;
|
||||||
}
|
}
|
||||||
return threads;
|
return inactiveThreads;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,7 +58,10 @@ Sequencer.prototype.stepThreads = function (threads) {
|
||||||
* @param {!Thread} thread Thread object to step
|
* @param {!Thread} thread Thread object to step
|
||||||
*/
|
*/
|
||||||
Sequencer.prototype.stepThread = function (thread) {
|
Sequencer.prototype.stepThread = function (thread) {
|
||||||
// @todo
|
// @todo Actually run the blocks
|
||||||
|
// Currently goes to the next block in the sequence.
|
||||||
|
var nextBlock = this.runtime._getNextBlock(thread.nextBlock);
|
||||||
|
thread.nextBlock = nextBlock;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Sequencer;
|
module.exports = Sequencer;
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
function Thread () {
|
/**
|
||||||
|
* A thread is a running stack context and all the metadata needed.
|
||||||
|
* @param {?string} firstBlock First block to execute in the thread.
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
function Thread (firstBlock) {
|
||||||
/**
|
/**
|
||||||
* Next block that the thread will execute.
|
* Next block that the thread will execute.
|
||||||
* @type {string}
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
this.nextBlock = null;
|
this.nextBlock = firstBlock;
|
||||||
/**
|
/**
|
||||||
* Stack for the thread. When the sequencer enters a control structure,
|
* Stack for the thread. When the sequencer enters a control structure,
|
||||||
* the block is pushed onto the stack so we know where to exit.
|
* the block is pushed onto the stack so we know where to exit.
|
||||||
|
|
11
vm.min.js
vendored
11
vm.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue