2017-04-17 15:10:04 -04:00
|
|
|
const Timer = require('../util/timer');
|
|
|
|
const Thread = require('./thread');
|
|
|
|
const execute = require('./execute.js');
|
2016-04-26 15:00:45 -04:00
|
|
|
|
2017-11-09 17:27:49 -05:00
|
|
|
/**
|
|
|
|
* Profiler frame name for stepping a single thread.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
const stepThreadProfilerFrame = 'Sequencer.stepThread';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Profiler frame name for the inner loop of stepThreads.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
const stepThreadsInnerProfilerFrame = 'Sequencer.stepThreads#inner';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Profiler frame name for execute.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
const executeProfilerFrame = 'execute';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Profiler frame ID for stepThreadProfilerFrame.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
let stepThreadProfilerId = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Profiler frame ID for stepThreadsInnerProfilerFrame.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
let stepThreadsInnerProfilerId = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Profiler frame ID for executeProfilerFrame.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
let executeProfilerId = -1;
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
class Sequencer {
|
|
|
|
constructor (runtime) {
|
|
|
|
/**
|
|
|
|
* A utility timer for timing thread sequencing.
|
|
|
|
* @type {!Timer}
|
|
|
|
*/
|
|
|
|
this.timer = new Timer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the runtime owning this sequencer.
|
|
|
|
* @type {!Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
}
|
|
|
|
|
2016-04-26 15:00:45 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Time to run a warp-mode thread, in ms.
|
|
|
|
* @type {number}
|
2016-04-26 15:00:45 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get WARP_TIME () {
|
|
|
|
return 500;
|
|
|
|
}
|
2016-04-26 16:50:49 -04:00
|
|
|
|
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Step through all threads in `this.runtime.threads`, running them in order.
|
|
|
|
* @return {Array.<!Thread>} List of inactive threads after stepping.
|
2016-04-26 16:50:49 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
stepThreads () {
|
|
|
|
// Work time is 75% of the thread stepping interval.
|
|
|
|
const WORK_TIME = 0.75 * this.runtime.currentStepTime;
|
|
|
|
// Start counting toward WORK_TIME.
|
|
|
|
this.timer.start();
|
|
|
|
// Count of active threads.
|
|
|
|
let numActiveThreads = Infinity;
|
|
|
|
// Whether `stepThreads` has run through a full single tick.
|
|
|
|
let ranFirstTick = false;
|
2017-11-03 11:52:42 -04:00
|
|
|
const doneThreads = this.runtime.threads.map(() => null);
|
2017-04-17 19:42:48 -04:00
|
|
|
// Conditions for continuing to stepping threads:
|
|
|
|
// 1. We must have threads in the list, and some must be active.
|
|
|
|
// 2. Time elapsed must be less than WORK_TIME.
|
|
|
|
// 3. Either turbo mode, or no redraw has been requested by a primitive.
|
|
|
|
while (this.runtime.threads.length > 0 &&
|
|
|
|
numActiveThreads > 0 &&
|
|
|
|
this.timer.timeElapsed() < WORK_TIME &&
|
|
|
|
(this.runtime.turboMode || !this.runtime.redrawRequested)) {
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.runtime.profiler !== null) {
|
|
|
|
if (stepThreadsInnerProfilerId === -1) {
|
|
|
|
stepThreadsInnerProfilerId = this.runtime.profiler.idByName(stepThreadsInnerProfilerFrame);
|
|
|
|
}
|
|
|
|
this.runtime.profiler.start(stepThreadsInnerProfilerId);
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
numActiveThreads = 0;
|
|
|
|
// Attempt to run each thread one time.
|
|
|
|
for (let i = 0; i < this.runtime.threads.length; i++) {
|
|
|
|
const activeThread = this.runtime.threads[i];
|
|
|
|
if (activeThread.stack.length === 0 ||
|
|
|
|
activeThread.status === Thread.STATUS_DONE) {
|
|
|
|
// Finished with this thread.
|
2017-11-03 11:52:42 -04:00
|
|
|
doneThreads[i] = activeThread;
|
2017-04-17 19:42:48 -04:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-03 11:52:42 -04:00
|
|
|
// A thread was removed, added or this thread was restarted.
|
|
|
|
if (doneThreads[i] !== null) {
|
|
|
|
doneThreads[i] = null;
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
if (activeThread.status === Thread.STATUS_YIELD_TICK &&
|
|
|
|
!ranFirstTick) {
|
|
|
|
// Clear single-tick yield from the last call of `stepThreads`.
|
|
|
|
activeThread.status = Thread.STATUS_RUNNING;
|
|
|
|
}
|
|
|
|
if (activeThread.status === Thread.STATUS_RUNNING ||
|
|
|
|
activeThread.status === Thread.STATUS_YIELD) {
|
|
|
|
// Normal-mode thread: step.
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.runtime.profiler !== null) {
|
|
|
|
if (stepThreadProfilerId === -1) {
|
|
|
|
stepThreadProfilerId = this.runtime.profiler.idByName(stepThreadProfilerFrame);
|
|
|
|
}
|
|
|
|
this.runtime.profiler.start(stepThreadProfilerId);
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
this.stepThread(activeThread);
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.runtime.profiler !== null) {
|
|
|
|
this.runtime.profiler.stop();
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
activeThread.warpTimer = null;
|
2017-05-12 02:01:37 -04:00
|
|
|
if (activeThread.isKilled) {
|
2017-08-26 13:07:47 -04:00
|
|
|
i--; // if the thread is removed from the list (killed), do not increase index
|
2017-05-12 02:01:37 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
if (activeThread.status === Thread.STATUS_RUNNING) {
|
|
|
|
numActiveThreads++;
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2016-04-26 15:00:45 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// We successfully ticked once. Prevents running STATUS_YIELD_TICK
|
|
|
|
// threads on the next tick.
|
|
|
|
ranFirstTick = true;
|
2017-11-09 17:27:49 -05:00
|
|
|
|
|
|
|
if (this.runtime.profiler !== null) {
|
|
|
|
this.runtime.profiler.stop();
|
|
|
|
}
|
2016-04-26 15:00:45 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Filter inactive threads from `this.runtime.threads`.
|
2017-11-03 16:24:12 -04:00
|
|
|
numActiveThreads = 0;
|
|
|
|
for (let i = 0; i < this.runtime.threads.length; i++) {
|
|
|
|
const thread = this.runtime.threads[i];
|
2017-11-03 11:52:42 -04:00
|
|
|
if (doneThreads[i] === null) {
|
2017-11-03 16:24:12 -04:00
|
|
|
this.runtime.threads[numActiveThreads] = thread;
|
|
|
|
numActiveThreads++;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2017-11-03 16:24:12 -04:00
|
|
|
}
|
|
|
|
this.runtime.threads.length = numActiveThreads;
|
|
|
|
|
|
|
|
// Filter undefined and null values from `doneThreads`.
|
|
|
|
let numDoneThreads = 0;
|
|
|
|
for (let i = 0; i < doneThreads.length; i++) {
|
|
|
|
const maybeThread = doneThreads[i];
|
|
|
|
if (maybeThread !== null) {
|
|
|
|
doneThreads[numDoneThreads] = maybeThread;
|
|
|
|
numDoneThreads++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doneThreads.length = numDoneThreads;
|
|
|
|
|
|
|
|
return doneThreads;
|
2016-04-26 15:00:45 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Step the requested thread for as long as necessary.
|
|
|
|
* @param {!Thread} thread Thread object to step.
|
|
|
|
*/
|
|
|
|
stepThread (thread) {
|
|
|
|
let currentBlockId = thread.peekStack();
|
|
|
|
if (!currentBlockId) {
|
|
|
|
// A "null block" - empty branch.
|
|
|
|
thread.popStack();
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2017-12-27 19:44:49 -05:00
|
|
|
// Save the current block ID to notice if we did control flow.
|
2018-03-14 19:17:29 -04:00
|
|
|
while ((currentBlockId = thread.peekStack())) {
|
2017-04-17 19:42:48 -04:00
|
|
|
let isWarpMode = thread.peekStackFrame().warpMode;
|
|
|
|
if (isWarpMode && !thread.warpTimer) {
|
|
|
|
// Initialize warp-mode timer if it hasn't been already.
|
|
|
|
// This will start counting the thread toward `Sequencer.WARP_TIME`.
|
|
|
|
thread.warpTimer = new Timer();
|
|
|
|
thread.warpTimer.start();
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Execute the current block.
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.runtime.profiler !== null) {
|
|
|
|
if (executeProfilerId === -1) {
|
|
|
|
executeProfilerId = this.runtime.profiler.idByName(executeProfilerFrame);
|
|
|
|
}
|
|
|
|
// The method commented below has its code inlined underneath to
|
|
|
|
// reduce the bias recorded for the profiler's calls in this
|
|
|
|
// time sensitive stepThread method.
|
|
|
|
//
|
|
|
|
// this.runtime.profiler.start(executeProfilerId, null);
|
|
|
|
this.runtime.profiler.records.push(
|
|
|
|
this.runtime.profiler.START, executeProfilerId, null, performance.now());
|
|
|
|
}
|
2018-04-10 16:29:51 -04:00
|
|
|
if (thread.target === null) {
|
|
|
|
this.retireThread(thread);
|
|
|
|
} else {
|
|
|
|
execute(this, thread);
|
|
|
|
}
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.runtime.profiler !== null) {
|
|
|
|
// this.runtime.profiler.stop();
|
|
|
|
this.runtime.profiler.records.push(this.runtime.profiler.STOP, performance.now());
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
thread.blockGlowInFrame = currentBlockId;
|
|
|
|
// If the thread has yielded or is waiting, yield to other threads.
|
|
|
|
if (thread.status === Thread.STATUS_YIELD) {
|
|
|
|
// Mark as running for next iteration.
|
|
|
|
thread.status = Thread.STATUS_RUNNING;
|
|
|
|
// In warp mode, yielded blocks are re-executed immediately.
|
|
|
|
if (isWarpMode &&
|
|
|
|
thread.warpTimer.timeElapsed() <= Sequencer.WARP_TIME) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (thread.status === Thread.STATUS_PROMISE_WAIT) {
|
|
|
|
// A promise was returned by the primitive. Yield the thread
|
|
|
|
// until the promise resolves. Promise resolution should reset
|
|
|
|
// thread.status to Thread.STATUS_RUNNING.
|
2016-10-17 23:23:16 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// If no control flow has happened, switch to next block.
|
|
|
|
if (thread.peekStack() === currentBlockId) {
|
|
|
|
thread.goToNextBlock();
|
|
|
|
}
|
|
|
|
// If no next block has been found at this point, look on the stack.
|
|
|
|
while (!thread.peekStack()) {
|
|
|
|
thread.popStack();
|
|
|
|
|
|
|
|
if (thread.stack.length === 0) {
|
|
|
|
// No more stack to run!
|
|
|
|
thread.status = Thread.STATUS_DONE;
|
|
|
|
return;
|
|
|
|
}
|
2017-01-28 09:11:48 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
const stackFrame = thread.peekStackFrame();
|
|
|
|
isWarpMode = stackFrame.warpMode;
|
2017-01-26 06:29:58 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
if (stackFrame.isLoop) {
|
|
|
|
// The current level of the stack is marked as a loop.
|
|
|
|
// Return to yield for the frame/tick in general.
|
|
|
|
// Unless we're in warp mode - then only return if the
|
|
|
|
// warp timer is up.
|
|
|
|
if (!isWarpMode ||
|
|
|
|
thread.warpTimer.timeElapsed() > Sequencer.WARP_TIME) {
|
|
|
|
// Don't do anything to the stack, since loops need
|
|
|
|
// to be re-executed.
|
|
|
|
return;
|
|
|
|
}
|
2017-08-26 13:07:47 -04:00
|
|
|
// Don't go to the next block for this level of the stack,
|
|
|
|
// since loops need to be re-executed.
|
2017-04-17 19:42:48 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
} else if (stackFrame.waitingReporter) {
|
|
|
|
// This level of the stack was waiting for a value.
|
|
|
|
// This means a reporter has just returned - so don't go
|
|
|
|
// to the next block for this level of the stack.
|
2016-10-17 23:23:16 -04:00
|
|
|
return;
|
2017-04-17 15:10:04 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Get next block of existing block on the stack.
|
|
|
|
thread.goToNextBlock();
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-09 14:23:34 -04:00
|
|
|
}
|
2016-05-02 15:35:29 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Step a thread into a block's branch.
|
|
|
|
* @param {!Thread} thread Thread object to step to branch.
|
|
|
|
* @param {number} branchNum Which branch to step to (i.e., 1, 2).
|
|
|
|
* @param {boolean} isLoop Whether this block is a loop.
|
|
|
|
*/
|
|
|
|
stepToBranch (thread, branchNum, isLoop) {
|
|
|
|
if (!branchNum) {
|
|
|
|
branchNum = 1;
|
|
|
|
}
|
|
|
|
const currentBlockId = thread.peekStack();
|
|
|
|
const branchId = thread.target.blocks.getBranch(
|
|
|
|
currentBlockId,
|
|
|
|
branchNum
|
|
|
|
);
|
|
|
|
thread.peekStackFrame().isLoop = isLoop;
|
|
|
|
if (branchId) {
|
|
|
|
// Push branch ID to the thread's stack.
|
|
|
|
thread.pushStack(branchId);
|
|
|
|
} else {
|
|
|
|
thread.pushStack(null);
|
|
|
|
}
|
2016-06-09 14:23:34 -04:00
|
|
|
}
|
2016-10-03 17:43:24 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Step a procedure.
|
|
|
|
* @param {!Thread} thread Thread object to step to procedure.
|
|
|
|
* @param {!string} procedureCode Procedure code of procedure to step to.
|
|
|
|
*/
|
|
|
|
stepToProcedure (thread, procedureCode) {
|
|
|
|
const definition = thread.target.blocks.getProcedureDefinition(procedureCode);
|
|
|
|
if (!definition) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check if the call is recursive.
|
|
|
|
// If so, set the thread to yield after pushing.
|
|
|
|
const isRecursive = thread.isRecursiveCall(procedureCode);
|
|
|
|
// To step to a procedure, we put its definition on the stack.
|
|
|
|
// Execution for the thread will proceed through the definition hat
|
|
|
|
// and on to the main definition of the procedure.
|
|
|
|
// When that set of blocks finishes executing, it will be popped
|
|
|
|
// from the stack by the sequencer, returning control to the caller.
|
|
|
|
thread.pushStack(definition);
|
|
|
|
// In known warp-mode threads, only yield when time is up.
|
|
|
|
if (thread.peekStackFrame().warpMode &&
|
|
|
|
thread.warpTimer.timeElapsed() > Sequencer.WARP_TIME) {
|
|
|
|
thread.status = Thread.STATUS_YIELD;
|
2016-10-24 13:01:41 -04:00
|
|
|
} else {
|
2017-04-17 19:42:48 -04:00
|
|
|
// Look for warp-mode flag on definition, and set the thread
|
|
|
|
// to warp-mode if needed.
|
|
|
|
const definitionBlock = thread.target.blocks.getBlock(definition);
|
2017-10-06 15:34:32 -04:00
|
|
|
const innerBlock = thread.target.blocks.getBlock(
|
|
|
|
definitionBlock.inputs.custom_block.block);
|
2017-12-14 14:21:57 -05:00
|
|
|
let doWarp = false;
|
|
|
|
if (innerBlock && innerBlock.mutation) {
|
|
|
|
const warp = innerBlock.mutation.warp;
|
|
|
|
if (typeof warp === 'boolean') {
|
|
|
|
doWarp = warp;
|
|
|
|
} else if (typeof warp === 'string') {
|
|
|
|
doWarp = JSON.parse(warp);
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
if (doWarp) {
|
|
|
|
thread.peekStackFrame().warpMode = true;
|
2017-08-26 13:14:26 -04:00
|
|
|
} else if (isRecursive) {
|
2017-04-17 19:42:48 -04:00
|
|
|
// In normal-mode threads, yield any time we have a recursive call.
|
2017-08-26 13:14:26 -04:00
|
|
|
thread.status = Thread.STATUS_YIELD;
|
2016-10-24 13:01:41 -04:00
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2016-06-30 17:12:16 -04:00
|
|
|
}
|
2016-04-26 15:00:45 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Retire a thread in the middle, without considering further blocks.
|
|
|
|
* @param {!Thread} thread Thread object to retire.
|
|
|
|
*/
|
|
|
|
retireThread (thread) {
|
|
|
|
thread.stack = [];
|
|
|
|
thread.stackFrame = [];
|
|
|
|
thread.requestScriptGlowInFrame = false;
|
|
|
|
thread.status = Thread.STATUS_DONE;
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 15:53:34 -04:00
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
module.exports = Sequencer;
|