|
const Timer = require('../util/timer'); |
|
const Thread = require('./thread'); |
|
const execute = require('./execute.js'); |
|
const compilerExecute = require('../compiler/jsexecute'); |
|
|
|
|
|
|
|
|
|
|
|
const stepThreadProfilerFrame = 'Sequencer.stepThread'; |
|
|
|
|
|
|
|
|
|
|
|
const stepThreadsInnerProfilerFrame = 'Sequencer.stepThreads#inner'; |
|
|
|
|
|
|
|
|
|
|
|
const executeProfilerFrame = 'execute'; |
|
|
|
|
|
|
|
|
|
|
|
let stepThreadProfilerId = -1; |
|
|
|
|
|
|
|
|
|
|
|
let stepThreadsInnerProfilerId = -1; |
|
|
|
|
|
|
|
|
|
|
|
let executeProfilerId = -1; |
|
|
|
class Sequencer { |
|
constructor (runtime) { |
|
|
|
|
|
|
|
|
|
this.timer = new Timer(); |
|
|
|
|
|
|
|
|
|
|
|
this.runtime = runtime; |
|
|
|
this.activeThread = null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static get WARP_TIME () { |
|
return 500; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
stepThreads () { |
|
|
|
const WORK_TIME = 0.75 * this.runtime.currentStepTime; |
|
|
|
|
|
|
|
this.runtime.updateCurrentMSecs(); |
|
|
|
this.timer.start(); |
|
|
|
let numActiveThreads = Infinity; |
|
|
|
let ranFirstTick = false; |
|
const doneThreads = []; |
|
|
|
|
|
|
|
|
|
while (this.runtime.threads.length > 0 && |
|
numActiveThreads > 0 && |
|
this.timer.timeElapsed() < WORK_TIME && |
|
(this.runtime.turboMode || !this.runtime.redrawRequested)) { |
|
if (this.runtime.profiler !== null) { |
|
if (stepThreadsInnerProfilerId === -1) { |
|
stepThreadsInnerProfilerId = this.runtime.profiler.idByName(stepThreadsInnerProfilerFrame); |
|
} |
|
this.runtime.profiler.start(stepThreadsInnerProfilerId); |
|
} |
|
|
|
numActiveThreads = 0; |
|
let stoppedThread = false; |
|
|
|
const threads = this.runtime.threads; |
|
for (let i = 0; i < threads.length; i++) { |
|
const activeThread = this.activeThread = threads[i]; |
|
|
|
if (activeThread.stack.length === 0 || |
|
activeThread.status === Thread.STATUS_DONE) { |
|
|
|
stoppedThread = true; |
|
continue; |
|
} |
|
if (activeThread.status === Thread.STATUS_PAUSED) { |
|
if (activeThread.timer && !activeThread.timer._pausedTime) { |
|
activeThread.timer.pause(); |
|
} |
|
continue; |
|
} |
|
if (activeThread.status === Thread.STATUS_YIELD_TICK && |
|
!ranFirstTick) { |
|
|
|
activeThread.status = Thread.STATUS_RUNNING; |
|
} |
|
if (activeThread.status === Thread.STATUS_RUNNING || |
|
activeThread.status === Thread.STATUS_YIELD) { |
|
|
|
if (this.runtime.profiler !== null) { |
|
if (stepThreadProfilerId === -1) { |
|
stepThreadProfilerId = this.runtime.profiler.idByName(stepThreadProfilerFrame); |
|
} |
|
|
|
|
|
this.runtime.profiler.increment(stepThreadProfilerId); |
|
} |
|
this.stepThread(activeThread); |
|
activeThread.warpTimer = null; |
|
} |
|
if (activeThread.status === Thread.STATUS_RUNNING) { |
|
numActiveThreads++; |
|
} |
|
|
|
|
|
if (activeThread.stack.length === 0 || |
|
activeThread.status === Thread.STATUS_DONE) { |
|
|
|
stoppedThread = true; |
|
} |
|
} |
|
|
|
|
|
ranFirstTick = true; |
|
|
|
if (this.runtime.profiler !== null) { |
|
this.runtime.profiler.stop(); |
|
} |
|
|
|
|
|
if (stoppedThread) { |
|
let nextActiveThread = 0; |
|
for (let i = 0; i < this.runtime.threads.length; i++) { |
|
const thread = this.runtime.threads[i]; |
|
if (thread.stack.length !== 0 && |
|
thread.status !== Thread.STATUS_DONE) { |
|
this.runtime.threads[nextActiveThread] = thread; |
|
nextActiveThread++; |
|
} else { |
|
this.runtime.threadMap.delete(thread.getId()); |
|
doneThreads.push(thread); |
|
} |
|
} |
|
this.runtime.threads.length = nextActiveThread; |
|
} |
|
} |
|
|
|
this.activeThread = null; |
|
|
|
return doneThreads; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
stepThread (thread) { |
|
if (thread.isCompiled) { |
|
compilerExecute(thread); |
|
return; |
|
} |
|
|
|
let currentBlockId = thread.peekStack(); |
|
if (!currentBlockId) { |
|
|
|
thread.popStack(); |
|
|
|
|
|
if (thread.stack.length === 0) { |
|
thread.status = Thread.STATUS_DONE; |
|
this.runtime.emit('THREAD_FINISHED', thread); |
|
return; |
|
} |
|
} |
|
|
|
while ((currentBlockId = thread.peekStack())) { |
|
let isWarpMode = thread.peekStackFrame().warpMode; |
|
if (isWarpMode && !thread.warpTimer) { |
|
|
|
|
|
thread.warpTimer = new Timer(); |
|
thread.warpTimer.start(); |
|
} |
|
|
|
if (this.runtime.profiler !== null) { |
|
if (executeProfilerId === -1) { |
|
executeProfilerId = this.runtime.profiler.idByName(executeProfilerFrame); |
|
} |
|
|
|
|
|
this.runtime.profiler.increment(executeProfilerId); |
|
} |
|
if (thread.target === null) { |
|
this.retireThread(thread); |
|
} else { |
|
execute(this, thread); |
|
} |
|
thread.blockGlowInFrame = currentBlockId; |
|
|
|
if (thread.status === Thread.STATUS_YIELD) { |
|
|
|
thread.status = Thread.STATUS_RUNNING; |
|
|
|
if (isWarpMode && |
|
thread.warpTimer.timeElapsed() <= Sequencer.WARP_TIME) { |
|
continue; |
|
} |
|
return; |
|
} else if (thread.status === Thread.STATUS_PROMISE_WAIT) { |
|
|
|
|
|
|
|
return; |
|
} else if (thread.status === Thread.STATUS_YIELD_TICK) { |
|
|
|
return; |
|
} else if (thread.status === Thread.STATUS_DONE) { |
|
|
|
return; |
|
} |
|
|
|
if (thread.peekStack() === currentBlockId) { |
|
thread.goToNextBlock(); |
|
} |
|
|
|
while (!thread.peekStack()) { |
|
thread.popStack(); |
|
|
|
if (thread.stack.length === 0) { |
|
|
|
thread.status = Thread.STATUS_DONE; |
|
this.runtime.emit('THREAD_FINISHED', thread); |
|
return; |
|
} |
|
|
|
const stackFrame = thread.peekStackFrame(); |
|
isWarpMode = stackFrame.warpMode; |
|
|
|
if (stackFrame.isLoop) { |
|
|
|
|
|
|
|
|
|
if (!isWarpMode || |
|
thread.warpTimer.timeElapsed() > Sequencer.WARP_TIME) { |
|
|
|
|
|
return; |
|
} |
|
|
|
|
|
continue; |
|
|
|
} else if (stackFrame.waitingReporter) { |
|
|
|
|
|
|
|
return; |
|
} |
|
|
|
thread.goToNextBlock(); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) { |
|
|
|
thread.pushStack(branchId); |
|
} else { |
|
thread.pushStack(null); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
stepToProcedure (thread, procedureCode) { |
|
const definition = thread.target.blocks.getProcedureDefinition(procedureCode); |
|
if (!definition) { |
|
return; |
|
} |
|
|
|
|
|
const isRecursive = thread.isRecursiveCall(procedureCode); |
|
|
|
|
|
|
|
|
|
|
|
thread.pushStack(definition); |
|
|
|
if (thread.peekStackFrame().warpMode && |
|
thread.warpTimer.timeElapsed() > Sequencer.WARP_TIME) { |
|
thread.status = Thread.STATUS_YIELD; |
|
} else { |
|
|
|
|
|
const definitionBlock = thread.target.blocks.getBlock(definition); |
|
const innerBlock = thread.target.blocks.getBlock( |
|
definitionBlock.inputs.custom_block.block); |
|
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); |
|
} |
|
} |
|
if (doWarp) { |
|
thread.peekStackFrame().warpMode = true; |
|
} else if (isRecursive) { |
|
|
|
thread.status = Thread.STATUS_YIELD; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
retireThread (thread) { |
|
thread.stack = []; |
|
thread.stackFrame = []; |
|
thread.requestScriptGlowInFrame = false; |
|
thread.status = Thread.STATUS_DONE; |
|
this.runtime.emit('THREAD_FINISHED', thread); |
|
if (thread.isCompiled) { |
|
thread.procedures = null; |
|
thread.generator = null; |
|
} |
|
} |
|
} |
|
|
|
module.exports = Sequencer; |
|
|