2017-11-03 11:46:57 -04:00
|
|
|
const Thread = require('./thread');
|
2018-12-04 10:35:42 -05:00
|
|
|
const Timer = require('../util/timer');
|
2017-11-03 11:46:57 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Interface provided to block primitive functions for interacting with the
|
|
|
|
* runtime, thread, target, and convenient methods.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class BlockUtility {
|
|
|
|
constructor (sequencer = null, thread = null) {
|
|
|
|
/**
|
|
|
|
* A sequencer block primitives use to branch or start procedures with
|
|
|
|
* @type {?Sequencer}
|
|
|
|
*/
|
|
|
|
this.sequencer = sequencer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The block primitives thread with the block's target, stackFrame and
|
|
|
|
* modifiable status.
|
|
|
|
* @type {?Thread}
|
|
|
|
*/
|
|
|
|
this.thread = thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The target the primitive is working on.
|
|
|
|
* @type {Target}
|
|
|
|
*/
|
|
|
|
get target () {
|
|
|
|
return this.thread.target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The runtime the block primitive is running in.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
get runtime () {
|
|
|
|
return this.sequencer.runtime;
|
|
|
|
}
|
|
|
|
|
2018-12-04 10:35:42 -05:00
|
|
|
/**
|
|
|
|
* Use the runtime's currentMSecs value as a timestamp value for now
|
|
|
|
* This is useful in some cases where we need compatibility with Scratch 2
|
|
|
|
* @type {function}
|
|
|
|
*/
|
|
|
|
get nowObj () {
|
|
|
|
if (this.runtime) {
|
|
|
|
return {
|
|
|
|
now: () => this.runtime.currentMSecs
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-03 11:46:57 -04:00
|
|
|
/**
|
|
|
|
* The stack frame used by loop and other blocks to track internal state.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
get stackFrame () {
|
2018-05-04 12:17:44 -04:00
|
|
|
const frame = this.thread.peekStackFrame();
|
|
|
|
if (frame.executionContext === null) {
|
|
|
|
frame.executionContext = {};
|
|
|
|
}
|
|
|
|
return frame.executionContext;
|
2017-11-03 11:46:57 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 10:35:42 -05:00
|
|
|
/**
|
|
|
|
* Check the stack timer and return a boolean based on whether it has finished or not.
|
|
|
|
* @return {boolean} - true if the stack timer has finished.
|
|
|
|
*/
|
|
|
|
stackTimerFinished () {
|
|
|
|
const timeElapsed = this.stackFrame.timer.timeElapsed();
|
|
|
|
if (timeElapsed < this.stackFrame.duration) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the stack timer needs initialization.
|
|
|
|
* @return {boolean} - true if the stack timer needs to be initialized.
|
|
|
|
*/
|
|
|
|
stackTimerNeedsInit () {
|
|
|
|
return !this.stackFrame.timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and start a stack timer
|
|
|
|
* @param {number} duration - a duration in milliseconds to set the timer for.
|
|
|
|
*/
|
|
|
|
startStackTimer (duration) {
|
|
|
|
if (this.nowObj) {
|
|
|
|
this.stackFrame.timer = new Timer(this.nowObj);
|
|
|
|
} else {
|
|
|
|
this.stackFrame.timer = new Timer();
|
|
|
|
}
|
|
|
|
this.stackFrame.timer.start();
|
|
|
|
this.stackFrame.duration = duration;
|
|
|
|
}
|
|
|
|
|
2017-11-03 11:46:57 -04:00
|
|
|
/**
|
|
|
|
* Set the thread to yield.
|
|
|
|
*/
|
|
|
|
yield () {
|
|
|
|
this.thread.status = Thread.STATUS_YIELD;
|
|
|
|
}
|
|
|
|
|
2018-06-06 10:13:29 -04:00
|
|
|
/**
|
|
|
|
* Set the thread to yield until the next tick of the runtime.
|
|
|
|
*/
|
|
|
|
yieldTick () {
|
|
|
|
this.thread.status = Thread.STATUS_YIELD_TICK;
|
|
|
|
}
|
|
|
|
|
2017-11-03 11:46:57 -04:00
|
|
|
/**
|
|
|
|
* Start a branch in the current block.
|
|
|
|
* @param {number} branchNum Which branch to step to (i.e., 1, 2).
|
|
|
|
* @param {boolean} isLoop Whether this block is a loop.
|
|
|
|
*/
|
|
|
|
startBranch (branchNum, isLoop) {
|
|
|
|
this.sequencer.stepToBranch(this.thread, branchNum, isLoop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop all threads.
|
|
|
|
*/
|
|
|
|
stopAll () {
|
|
|
|
this.sequencer.runtime.stopAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop threads other on this target other than the thread holding the
|
|
|
|
* executed block.
|
|
|
|
*/
|
|
|
|
stopOtherTargetThreads () {
|
|
|
|
this.sequencer.runtime.stopForTarget(this.thread.target, this.thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop this thread.
|
|
|
|
*/
|
|
|
|
stopThisScript () {
|
|
|
|
this.thread.stopThisScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a specified procedure on this thread.
|
|
|
|
* @param {string} procedureCode Procedure code for procedure to start.
|
|
|
|
*/
|
|
|
|
startProcedure (procedureCode) {
|
|
|
|
this.sequencer.stepToProcedure(this.thread, procedureCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-04 18:28:00 -04:00
|
|
|
* Get names and ids of parameters for the given procedure.
|
2017-11-03 11:46:57 -04:00
|
|
|
* @param {string} procedureCode Procedure code for procedure to query.
|
|
|
|
* @return {Array.<string>} List of param names for a procedure.
|
|
|
|
*/
|
2017-11-29 12:00:00 -05:00
|
|
|
getProcedureParamNamesAndIds (procedureCode) {
|
|
|
|
return this.thread.target.blocks.getProcedureParamNamesAndIds(procedureCode);
|
2017-11-03 11:46:57 -04:00
|
|
|
}
|
|
|
|
|
2018-10-04 18:28:00 -04:00
|
|
|
/**
|
|
|
|
* Get names, ids, and defaults of parameters for the given procedure.
|
|
|
|
* @param {string} procedureCode Procedure code for procedure to query.
|
|
|
|
* @return {Array.<string>} List of param names for a procedure.
|
|
|
|
*/
|
|
|
|
getProcedureParamNamesIdsAndDefaults (procedureCode) {
|
|
|
|
return this.thread.target.blocks.getProcedureParamNamesIdsAndDefaults(procedureCode);
|
|
|
|
}
|
|
|
|
|
2017-11-03 11:46:57 -04:00
|
|
|
/**
|
|
|
|
* Store a procedure parameter value by its name.
|
|
|
|
* @param {string} paramName The procedure's parameter name.
|
|
|
|
* @param {*} paramValue The procedure's parameter value.
|
|
|
|
*/
|
|
|
|
pushParam (paramName, paramValue) {
|
|
|
|
this.thread.pushParam(paramName, paramValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the stored parameter value for a given parameter name.
|
|
|
|
* @param {string} paramName The procedure's parameter name.
|
|
|
|
* @return {*} The parameter's current stored value.
|
|
|
|
*/
|
|
|
|
getParam (paramName) {
|
|
|
|
return this.thread.getParam(paramName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start all relevant hats.
|
|
|
|
* @param {!string} requestedHat Opcode of hats to start.
|
|
|
|
* @param {object=} optMatchFields Optionally, fields to match on the hat.
|
|
|
|
* @param {Target=} optTarget Optionally, a target to restrict to.
|
|
|
|
* @return {Array.<Thread>} List of threads started by this function.
|
|
|
|
*/
|
|
|
|
startHats (requestedHat, optMatchFields, optTarget) {
|
|
|
|
return (
|
|
|
|
this.sequencer.runtime.startHats(requestedHat, optMatchFields, optTarget)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query a named IO device.
|
|
|
|
* @param {string} device The name of like the device, like keyboard.
|
|
|
|
* @param {string} func The name of the device's function to query.
|
|
|
|
* @param {Array.<*>} args Arguments to pass to the device's function.
|
|
|
|
* @return {*} The expected output for the device's function.
|
|
|
|
*/
|
|
|
|
ioQuery (device, func, args) {
|
|
|
|
// Find the I/O device and execute the query/function call.
|
|
|
|
if (
|
|
|
|
this.sequencer.runtime.ioDevices[device] &&
|
|
|
|
this.sequencer.runtime.ioDevices[device][func]) {
|
|
|
|
const devObject = this.sequencer.runtime.ioDevices[device];
|
|
|
|
return devObject[func].apply(devObject, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BlockUtility;
|