2017-04-17 15:10:04 -04:00
|
|
|
const EventEmitter = require('events');
|
2017-08-04 14:25:17 -04:00
|
|
|
const {OrderedMap} = require('immutable');
|
2017-10-04 15:48:08 -04:00
|
|
|
const escapeHtml = require('escape-html');
|
2017-08-04 14:25:17 -04:00
|
|
|
|
|
|
|
const ArgumentType = require('../extension-support/argument-type');
|
2017-04-17 15:10:04 -04:00
|
|
|
const Blocks = require('./blocks');
|
2017-08-04 14:25:17 -04:00
|
|
|
const BlockType = require('../extension-support/block-type');
|
2018-04-09 18:34:46 -04:00
|
|
|
const Profiler = require('./profiler');
|
2017-08-04 14:25:17 -04:00
|
|
|
const Sequencer = require('./sequencer');
|
2018-04-09 18:34:46 -04:00
|
|
|
const ScratchBlocksConstants = require('./scratch-blocks-constants');
|
2018-03-27 15:30:23 -04:00
|
|
|
const TargetType = require('../extension-support/target-type');
|
2017-04-17 15:10:04 -04:00
|
|
|
const Thread = require('./thread');
|
2018-03-27 17:35:04 -04:00
|
|
|
const log = require('../util/log');
|
2018-03-27 15:30:23 -04:00
|
|
|
const maybeFormatMessage = require('../util/maybe-format-message');
|
2018-05-15 22:22:44 -04:00
|
|
|
const StageLayering = require('./stage-layering');
|
2016-04-18 18:03:18 -04:00
|
|
|
|
2016-08-15 21:37:36 -04:00
|
|
|
// Virtual I/O devices.
|
2017-04-17 15:10:04 -04:00
|
|
|
const Clock = require('../io/clock');
|
2017-04-28 16:40:13 -04:00
|
|
|
const DeviceManager = require('../io/deviceManager');
|
2017-04-17 15:10:04 -04:00
|
|
|
const Keyboard = require('../io/keyboard');
|
|
|
|
const Mouse = require('../io/mouse');
|
2018-01-26 16:13:01 -05:00
|
|
|
const MouseWheel = require('../io/mouseWheel');
|
2018-04-03 11:44:44 -04:00
|
|
|
const Video = require('../io/video');
|
2016-08-15 21:37:36 -04:00
|
|
|
|
2017-04-17 15:10:04 -04:00
|
|
|
const defaultBlockPackages = {
|
2016-10-24 11:02:19 -04:00
|
|
|
scratch3_control: require('../blocks/scratch3_control'),
|
|
|
|
scratch3_event: require('../blocks/scratch3_event'),
|
|
|
|
scratch3_looks: require('../blocks/scratch3_looks'),
|
|
|
|
scratch3_motion: require('../blocks/scratch3_motion'),
|
|
|
|
scratch3_operators: require('../blocks/scratch3_operators'),
|
2016-12-21 15:18:38 -05:00
|
|
|
scratch3_sound: require('../blocks/scratch3_sound'),
|
2016-10-24 11:02:19 -04:00
|
|
|
scratch3_sensing: require('../blocks/scratch3_sensing'),
|
|
|
|
scratch3_data: require('../blocks/scratch3_data'),
|
2017-10-06 12:36:27 -04:00
|
|
|
scratch3_procedures: require('../blocks/scratch3_procedures')
|
2016-05-02 12:20:27 -04:00
|
|
|
};
|
|
|
|
|
2017-08-04 14:25:17 -04:00
|
|
|
/**
|
|
|
|
* Information used for converting Scratch argument types into scratch-blocks data.
|
|
|
|
* @type {object.<ArgumentType, {shadowType: string, fieldType: string}>}}
|
|
|
|
*/
|
|
|
|
const ArgumentTypeMap = (() => {
|
|
|
|
const map = {};
|
2017-10-06 16:37:59 -04:00
|
|
|
map[ArgumentType.ANGLE] = {
|
|
|
|
shadowType: 'math_angle',
|
|
|
|
fieldType: 'NUM'
|
|
|
|
};
|
2017-09-27 00:02:53 -04:00
|
|
|
map[ArgumentType.COLOR] = {
|
|
|
|
shadowType: 'colour_picker'
|
|
|
|
};
|
2017-08-04 14:25:17 -04:00
|
|
|
map[ArgumentType.NUMBER] = {
|
|
|
|
shadowType: 'math_number',
|
|
|
|
fieldType: 'NUM'
|
|
|
|
};
|
|
|
|
map[ArgumentType.STRING] = {
|
|
|
|
shadowType: 'text',
|
|
|
|
fieldType: 'TEXT'
|
|
|
|
};
|
|
|
|
map[ArgumentType.BOOLEAN] = {
|
2017-10-07 02:24:41 -04:00
|
|
|
check: 'Boolean'
|
2017-08-04 14:25:17 -04:00
|
|
|
};
|
|
|
|
return map;
|
|
|
|
})();
|
|
|
|
|
2018-03-27 17:35:04 -04:00
|
|
|
/**
|
|
|
|
* Predefined "Converted block info" for a separator between blocks in a block category
|
|
|
|
* @type {ConvertedBlockInfo}
|
|
|
|
*/
|
|
|
|
const ConvertedSeparator = {
|
|
|
|
info: {},
|
|
|
|
json: null,
|
|
|
|
xml: '<sep gap="36"/>'
|
|
|
|
};
|
|
|
|
|
2017-11-09 17:27:49 -05:00
|
|
|
/**
|
|
|
|
* Numeric ID for Runtime._step in Profiler instances.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
let stepProfilerId = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Numeric ID for Sequencer.stepThreads in Profiler instances.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
let stepThreadsProfilerId = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Numeric ID for RenderWebGL.draw in Profiler instances.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
let rendererDrawProfilerId = -1;
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
/**
|
2016-08-11 11:11:27 -04:00
|
|
|
* Manages targets, scripts, and the sequencer.
|
2016-12-23 10:39:19 -05:00
|
|
|
* @constructor
|
2016-04-18 17:20:30 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
class Runtime extends EventEmitter {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Target management and storage.
|
|
|
|
* @type {Array.<!Target>}
|
|
|
|
*/
|
|
|
|
this.targets = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage container for flyout blocks.
|
|
|
|
* These will execute on `_editingTarget.`
|
|
|
|
* @type {!Blocks}
|
|
|
|
*/
|
|
|
|
this.flyoutBlocks = new Blocks();
|
|
|
|
|
2017-05-08 09:53:16 -04:00
|
|
|
/**
|
|
|
|
* Storage container for monitor blocks.
|
|
|
|
* These will execute on a target maybe
|
|
|
|
* @type {!Blocks}
|
|
|
|
*/
|
|
|
|
this.monitorBlocks = new Blocks();
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Currently known editing target for the VM.
|
|
|
|
* @type {?Target}
|
|
|
|
*/
|
|
|
|
this._editingTarget = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map to look up a block primitive's implementation function by its opcode.
|
|
|
|
* This is a two-step lookup: package name first, then primitive name.
|
|
|
|
* @type {Object.<string, Function>}
|
|
|
|
*/
|
|
|
|
this._primitives = {};
|
|
|
|
|
2017-08-04 14:25:17 -04:00
|
|
|
/**
|
|
|
|
* Map to look up all block information by extended opcode.
|
2017-08-25 16:53:57 -04:00
|
|
|
* @type {Array.<CategoryInfo>}
|
2017-08-04 14:25:17 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2017-08-25 16:53:57 -04:00
|
|
|
this._blockInfo = [];
|
2017-08-04 14:25:17 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Map to look up hat blocks' metadata.
|
|
|
|
* Keys are opcode for hat, values are metadata objects.
|
|
|
|
* @type {Object.<string, Object>}
|
|
|
|
*/
|
|
|
|
this._hats = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently known values for edge-activated hats.
|
|
|
|
* Keys are block ID for the hat; values are the currently known values.
|
|
|
|
* @type {Object.<string, *>}
|
|
|
|
*/
|
|
|
|
this._edgeActivatedHatValues = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of script block IDs that were glowing during the previous frame.
|
|
|
|
* @type {!Array.<!string>}
|
|
|
|
*/
|
|
|
|
this._scriptGlowsPreviousFrame = [];
|
|
|
|
|
|
|
|
/**
|
2017-05-10 14:06:02 -04:00
|
|
|
* Number of non-monitor threads running during the previous frame.
|
2017-04-17 19:42:48 -04:00
|
|
|
* @type {number}
|
|
|
|
*/
|
2017-05-10 14:06:02 -04:00
|
|
|
this._nonMonitorThreadCount = 0;
|
2017-04-17 19:42:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently known number of clones, used to enforce clone limit.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this._cloneCounter = 0;
|
|
|
|
|
2017-05-15 16:25:34 -04:00
|
|
|
/**
|
|
|
|
* Flag to emit a targets update at the end of a step. When target data
|
|
|
|
* changes, this flag is set to true.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this._refreshTargets = false;
|
|
|
|
|
2017-11-15 17:53:43 -05:00
|
|
|
/**
|
|
|
|
* Map to look up all monitor block information by opcode.
|
|
|
|
* @type {object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.monitorBlockInfo = {};
|
|
|
|
|
2017-05-11 15:23:11 -04:00
|
|
|
/**
|
2017-05-24 15:42:29 -04:00
|
|
|
* Ordered map of all monitors, which are MonitorReporter objects.
|
2017-05-11 15:23:11 -04:00
|
|
|
*/
|
2017-05-24 15:42:29 -04:00
|
|
|
this._monitorState = OrderedMap({});
|
2017-05-19 12:28:05 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Monitor state from last tick
|
|
|
|
*/
|
2017-05-24 15:42:29 -04:00
|
|
|
this._prevMonitorState = OrderedMap({});
|
2017-05-15 10:18:48 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Whether the project is in "turbo mode."
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
|
|
|
this.turboMode = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the project is in "compatibility mode" (30 TPS).
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
|
|
|
this.compatibilityMode = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the current runtime stepping interval, set
|
|
|
|
* by a `setInterval`.
|
|
|
|
* @type {!number}
|
|
|
|
*/
|
|
|
|
this._steppingInterval = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current length of a step.
|
|
|
|
* Changes as mode switches, and used by the sequencer to calculate
|
|
|
|
* WORK_TIME.
|
|
|
|
* @type {!number}
|
|
|
|
*/
|
|
|
|
this.currentStepTime = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether any primitive has requested a redraw.
|
|
|
|
* Affects whether `Sequencer.stepThreads` will yield
|
|
|
|
* after stepping each thread.
|
|
|
|
* Reset on every frame.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.redrawRequested = false;
|
|
|
|
|
|
|
|
// Register all given block packages.
|
|
|
|
this._registerBlockPackages();
|
|
|
|
|
|
|
|
// Register and initialize "IO devices", containers for processing
|
|
|
|
// I/O related data.
|
|
|
|
/** @type {Object.<string, Object>} */
|
|
|
|
this.ioDevices = {
|
|
|
|
clock: new Clock(),
|
2017-04-28 16:40:13 -04:00
|
|
|
deviceManager: new DeviceManager(),
|
2017-04-17 19:42:48 -04:00
|
|
|
keyboard: new Keyboard(this),
|
2018-01-26 16:13:01 -05:00
|
|
|
mouse: new Mouse(this),
|
2018-04-03 11:44:44 -04:00
|
|
|
mouseWheel: new MouseWheel(this),
|
|
|
|
video: new Video(this)
|
2017-04-17 19:42:48 -04:00
|
|
|
};
|
2017-11-09 17:27:49 -05:00
|
|
|
|
2018-06-19 17:59:03 -04:00
|
|
|
this.extensionDevices = {};
|
|
|
|
|
2017-11-09 17:27:49 -05:00
|
|
|
/**
|
|
|
|
* A runtime profiler that records timed events for later playback to
|
|
|
|
* diagnose Scratch performance.
|
|
|
|
* @type {Profiler}
|
|
|
|
*/
|
|
|
|
this.profiler = null;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2016-04-26 15:51:14 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Width of the stage, in pixels.
|
|
|
|
* @const {number}
|
2016-04-26 15:51:14 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get STAGE_WIDTH () {
|
|
|
|
return 480;
|
|
|
|
}
|
2016-04-26 14:20:44 -04:00
|
|
|
|
2016-04-26 15:51:14 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Height of the stage, in pixels.
|
|
|
|
* @const {number}
|
2016-04-26 15:51:14 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get STAGE_HEIGHT () {
|
|
|
|
return 360;
|
|
|
|
}
|
2016-05-02 12:20:27 -04:00
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Event name for glowing a script.
|
|
|
|
* @const {string}
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get SCRIPT_GLOW_ON () {
|
|
|
|
return 'SCRIPT_GLOW_ON';
|
|
|
|
}
|
2016-10-13 17:15:49 -04:00
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Event name for unglowing a script.
|
|
|
|
* @const {string}
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get SCRIPT_GLOW_OFF () {
|
|
|
|
return 'SCRIPT_GLOW_OFF';
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
2016-05-02 12:20:27 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Event name for glowing a block.
|
|
|
|
* @const {string}
|
2016-05-02 12:20:27 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get BLOCK_GLOW_ON () {
|
|
|
|
return 'BLOCK_GLOW_ON';
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Event name for unglowing a block.
|
|
|
|
* @const {string}
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get BLOCK_GLOW_OFF () {
|
|
|
|
return 'BLOCK_GLOW_OFF';
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
2017-12-01 20:06:55 -05:00
|
|
|
/**
|
|
|
|
* Event name when the project is started (threads may not necessarily be
|
|
|
|
* running).
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
static get PROJECT_START () {
|
|
|
|
return 'PROJECT_START';
|
|
|
|
}
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
2017-10-06 13:43:07 -04:00
|
|
|
* Event name when threads start running.
|
|
|
|
* Used by the UI to indicate running status.
|
2017-04-17 19:42:48 -04:00
|
|
|
* @const {string}
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get PROJECT_RUN_START () {
|
|
|
|
return 'PROJECT_RUN_START';
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
|
|
|
/**
|
2017-10-06 13:43:07 -04:00
|
|
|
* Event name when threads stop running
|
|
|
|
* Used by the UI to indicate not-running status.
|
2017-04-17 19:42:48 -04:00
|
|
|
* @const {string}
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get PROJECT_RUN_STOP () {
|
|
|
|
return 'PROJECT_RUN_STOP';
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
2017-10-06 13:43:07 -04:00
|
|
|
/**
|
|
|
|
* Event name for project being stopped or restarted by the user.
|
|
|
|
* Used by blocks that need to reset state.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
static get PROJECT_STOP_ALL () {
|
|
|
|
return 'PROJECT_STOP_ALL';
|
|
|
|
}
|
|
|
|
|
2016-11-23 15:47:49 -05:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Event name for visual value report.
|
|
|
|
* @const {string}
|
2016-11-23 15:47:49 -05:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get VISUAL_REPORT () {
|
|
|
|
return 'VISUAL_REPORT';
|
|
|
|
}
|
2016-11-23 15:47:49 -05:00
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
2017-05-12 11:42:22 -04:00
|
|
|
* Event name for targets update report.
|
2017-04-17 19:42:48 -04:00
|
|
|
* @const {string}
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-05-12 11:42:22 -04:00
|
|
|
static get TARGETS_UPDATE () {
|
|
|
|
return 'TARGETS_UPDATE';
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
2017-05-09 12:10:11 -04:00
|
|
|
/**
|
|
|
|
* Event name for monitors update.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
static get MONITORS_UPDATE () {
|
|
|
|
return 'MONITORS_UPDATE';
|
|
|
|
}
|
|
|
|
|
2018-02-12 10:25:42 -05:00
|
|
|
/**
|
|
|
|
* Event name for block drag update.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
static get BLOCK_DRAG_UPDATE () {
|
|
|
|
return 'BLOCK_DRAG_UPDATE';
|
|
|
|
}
|
|
|
|
|
2018-02-23 11:57:19 -05:00
|
|
|
/**
|
|
|
|
* Event name for block drag end.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
static get BLOCK_DRAG_END () {
|
|
|
|
return 'BLOCK_DRAG_END';
|
|
|
|
}
|
2018-02-12 10:25:42 -05:00
|
|
|
|
2017-08-29 14:43:09 -04:00
|
|
|
/**
|
|
|
|
* Event name for reporting that an extension was added.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
2017-09-04 14:06:31 -04:00
|
|
|
static get EXTENSION_ADDED () {
|
|
|
|
return 'EXTENSION_ADDED';
|
2017-08-29 14:43:09 -04:00
|
|
|
}
|
|
|
|
|
2018-06-19 17:59:03 -04:00
|
|
|
/**
|
|
|
|
* Event name for updating the available set of peripheral devices.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
static get PERIPHERAL_LIST_UPDATE () {
|
|
|
|
return 'PERIPHERAL_LIST_UPDATE';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event name for reporting that a peripheral has connected.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
static get PERIPHERAL_CONNECTED () {
|
|
|
|
return 'PERIPHERAL_CONNECTED';
|
|
|
|
}
|
|
|
|
|
2018-06-21 15:18:56 -04:00
|
|
|
/**
|
|
|
|
* Event name for reporting that a peripheral has encountered an error.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
static get PERIPHERAL_ERROR () {
|
|
|
|
return 'PERIPHERAL_ERROR';
|
|
|
|
}
|
|
|
|
|
2017-12-11 15:41:45 -05:00
|
|
|
/**
|
|
|
|
* Event name for reporting that blocksInfo was updated.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
static get BLOCKSINFO_UPDATE () {
|
|
|
|
return 'BLOCKSINFO_UPDATE';
|
|
|
|
}
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* How rapidly we try to step threads by default, in ms.
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get THREAD_STEP_INTERVAL () {
|
|
|
|
return 1000 / 60;
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* In compatibility mode, how rapidly we try to step threads, in ms.
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get THREAD_STEP_INTERVAL_COMPATIBILITY () {
|
|
|
|
return 1000 / 30;
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* How many clones can be created at a time.
|
|
|
|
* @const {number}
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get MAX_CLONES () {
|
|
|
|
return 300;
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
2016-10-17 23:23:16 -04:00
|
|
|
|
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* Register default block packages with this runtime.
|
|
|
|
* @todo Prefix opcodes with package name.
|
|
|
|
* @private
|
2016-10-17 23:23:16 -04:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
_registerBlockPackages () {
|
|
|
|
for (const packageName in defaultBlockPackages) {
|
|
|
|
if (defaultBlockPackages.hasOwnProperty(packageName)) {
|
|
|
|
// @todo pass a different runtime depending on package privilege?
|
|
|
|
const packageObject = new (defaultBlockPackages[packageName])(this);
|
|
|
|
// Collect primitives from package.
|
|
|
|
if (packageObject.getPrimitives) {
|
|
|
|
const packagePrimitives = packageObject.getPrimitives();
|
|
|
|
for (const op in packagePrimitives) {
|
|
|
|
if (packagePrimitives.hasOwnProperty(op)) {
|
|
|
|
this._primitives[op] =
|
|
|
|
packagePrimitives[op].bind(packageObject);
|
|
|
|
}
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Collect hat metadata from package.
|
|
|
|
if (packageObject.getHats) {
|
|
|
|
const packageHats = packageObject.getHats();
|
|
|
|
for (const hatName in packageHats) {
|
|
|
|
if (packageHats.hasOwnProperty(hatName)) {
|
|
|
|
this._hats[hatName] = packageHats[hatName];
|
|
|
|
}
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
2016-05-02 12:20:27 -04:00
|
|
|
}
|
2017-11-20 17:23:59 -05:00
|
|
|
// Collect monitored from package.
|
|
|
|
if (packageObject.getMonitored) {
|
|
|
|
this.monitorBlockInfo = Object.assign({}, this.monitorBlockInfo, packageObject.getMonitored());
|
|
|
|
}
|
2016-05-02 12:20:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 16:37:59 -04:00
|
|
|
/**
|
|
|
|
* Generate an extension-specific menu ID.
|
|
|
|
* @param {string} menuName - the name of the menu.
|
|
|
|
* @param {string} extensionId - the ID of the extension hosting the menu.
|
|
|
|
* @returns {string} - the constructed ID.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_makeExtensionMenuId (menuName, extensionId) {
|
2018-04-20 07:54:02 -04:00
|
|
|
return `${extensionId}_menu_${escapeHtml(menuName)}`;
|
2017-10-06 16:37:59 -04:00
|
|
|
}
|
|
|
|
|
2018-03-27 15:30:23 -04:00
|
|
|
/**
|
|
|
|
* Create a context ("args") object for use with `formatMessage` on messages which might be target-specific.
|
|
|
|
* @param {Target} [target] - the target to use as context. If a target is not provided, default to the current
|
|
|
|
* editing target or the stage.
|
|
|
|
*/
|
|
|
|
makeMessageContextForTarget (target) {
|
|
|
|
const context = {};
|
|
|
|
target = target || this.getEditingTarget() || this.getTargetForStage();
|
|
|
|
if (target) {
|
|
|
|
context.targetType = (target.isStage ? TargetType.STAGE : TargetType.SPRITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:25:17 -04:00
|
|
|
/**
|
|
|
|
* Register the primitives provided by an extension.
|
2018-03-27 17:35:04 -04:00
|
|
|
* @param {ExtensionMetadata} extensionInfo - information about the extension (id, blocks, etc.)
|
2017-08-04 14:25:17 -04:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_registerExtensionPrimitives (extensionInfo) {
|
|
|
|
const categoryInfo = {
|
|
|
|
id: extensionInfo.id,
|
2018-03-27 15:30:23 -04:00
|
|
|
name: maybeFormatMessage(extensionInfo.name),
|
2018-06-27 15:53:18 -04:00
|
|
|
showStatusButton: extensionInfo.showStatusButton,
|
2017-12-19 14:47:11 -05:00
|
|
|
blockIconURI: extensionInfo.blockIconURI,
|
|
|
|
menuIconURI: extensionInfo.menuIconURI,
|
2018-06-27 18:10:51 -04:00
|
|
|
color1: extensionInfo.colour || '#0FBD8C',
|
|
|
|
color2: extensionInfo.colourSecondary || '#0DA57A',
|
|
|
|
color3: extensionInfo.colourTertiary || '#0B8E69',
|
2017-10-06 16:37:59 -04:00
|
|
|
blocks: [],
|
|
|
|
menus: []
|
2017-08-04 14:25:17 -04:00
|
|
|
};
|
|
|
|
|
2017-08-25 16:53:57 -04:00
|
|
|
this._blockInfo.push(categoryInfo);
|
|
|
|
|
2018-03-27 17:35:04 -04:00
|
|
|
this._fillExtensionCategory(categoryInfo, extensionInfo);
|
2017-08-29 14:43:09 -04:00
|
|
|
|
2017-10-06 16:37:59 -04:00
|
|
|
this.emit(Runtime.EXTENSION_ADDED, categoryInfo.blocks.concat(categoryInfo.menus));
|
|
|
|
}
|
|
|
|
|
2017-12-11 15:41:45 -05:00
|
|
|
/**
|
|
|
|
* Reregister the primitives for an extension
|
2018-03-27 17:35:04 -04:00
|
|
|
* @param {ExtensionMetadata} extensionInfo - new info (results of running getInfo) for an extension
|
2017-12-11 15:41:45 -05:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_refreshExtensionPrimitives (extensionInfo) {
|
|
|
|
let extensionBlocks = [];
|
|
|
|
for (const categoryInfo of this._blockInfo) {
|
|
|
|
if (extensionInfo.id === categoryInfo.id) {
|
|
|
|
categoryInfo.blocks = [];
|
|
|
|
categoryInfo.menus = [];
|
2018-03-27 17:35:04 -04:00
|
|
|
this._fillExtensionCategory(categoryInfo, extensionInfo);
|
2017-12-11 15:41:45 -05:00
|
|
|
extensionBlocks = extensionBlocks.concat(categoryInfo.blocks, categoryInfo.menus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit(Runtime.BLOCKSINFO_UPDATE, extensionBlocks);
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:35:04 -04:00
|
|
|
/**
|
|
|
|
* Read extension information, convert menus and blocks, and store the results in the provided category object.
|
|
|
|
* @param {CategoryInfo} categoryInfo - the category to be filled
|
|
|
|
* @param {ExtensionMetadata} extensionInfo - the extension metadata to read
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_fillExtensionCategory (categoryInfo, extensionInfo) {
|
|
|
|
for (const menuName in extensionInfo.menus) {
|
|
|
|
if (extensionInfo.menus.hasOwnProperty(menuName)) {
|
|
|
|
const menuItems = extensionInfo.menus[menuName];
|
|
|
|
const convertedMenu = this._buildMenuForScratchBlocks(menuName, menuItems, categoryInfo);
|
|
|
|
categoryInfo.menus.push(convertedMenu);
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 15:52:51 -04:00
|
|
|
|
2018-03-27 17:35:04 -04:00
|
|
|
for (const blockInfo of extensionInfo.blocks) {
|
|
|
|
if (blockInfo === '---') {
|
|
|
|
categoryInfo.blocks.push(ConvertedSeparator);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const convertedBlock = this._convertForScratchBlocks(blockInfo, categoryInfo);
|
|
|
|
const opcode = convertedBlock.json.type;
|
|
|
|
categoryInfo.blocks.push(convertedBlock);
|
2018-03-27 17:35:04 -04:00
|
|
|
if (blockInfo.blockType !== BlockType.EVENT) {
|
|
|
|
this._primitives[opcode] = convertedBlock.info.func;
|
|
|
|
}
|
|
|
|
if (blockInfo.blockType === BlockType.EVENT || blockInfo.blockType === BlockType.HAT) {
|
|
|
|
this._hats[opcode] = {
|
|
|
|
edgeActivated: blockInfo.isEdgeActivated,
|
|
|
|
restartExistingThreads: blockInfo.shouldRestartExistingThreads
|
|
|
|
};
|
2018-03-27 17:35:04 -04:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log.error('Error parsing block: ', {block: blockInfo, error: e});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 16:37:59 -04:00
|
|
|
/**
|
|
|
|
* Build the scratch-blocks JSON for a menu. Note that scratch-blocks treats menus as a special kind of block.
|
|
|
|
* @param {string} menuName - the name of the menu
|
2017-10-11 14:41:03 -04:00
|
|
|
* @param {array} menuItems - the list of items for this menu
|
2017-10-06 16:37:59 -04:00
|
|
|
* @param {CategoryInfo} categoryInfo - the category for this block
|
|
|
|
* @returns {object} - a JSON-esque object ready for scratch-blocks' consumption
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_buildMenuForScratchBlocks (menuName, menuItems, categoryInfo) {
|
|
|
|
const menuId = this._makeExtensionMenuId(menuName, categoryInfo.id);
|
2018-02-16 13:49:26 -05:00
|
|
|
let options = null;
|
2018-01-26 17:36:36 -05:00
|
|
|
if (typeof menuItems === 'function') {
|
2018-03-02 13:58:43 -05:00
|
|
|
options = menuItems;
|
2018-01-26 17:36:36 -05:00
|
|
|
} else {
|
2018-03-27 15:30:23 -04:00
|
|
|
const extensionMessageContext = this.makeMessageContextForTarget();
|
2018-01-26 17:36:36 -05:00
|
|
|
options = menuItems.map(item => {
|
2018-03-27 15:30:23 -04:00
|
|
|
const formattedItem = maybeFormatMessage(item, extensionMessageContext);
|
|
|
|
switch (typeof formattedItem) {
|
2018-01-26 17:36:36 -05:00
|
|
|
case 'string':
|
2018-03-27 15:30:23 -04:00
|
|
|
return [formattedItem, formattedItem];
|
2018-01-26 17:36:36 -05:00
|
|
|
case 'object':
|
2018-03-27 15:30:23 -04:00
|
|
|
return [maybeFormatMessage(item.text, extensionMessageContext), item.value];
|
2018-01-26 17:36:36 -05:00
|
|
|
default:
|
2018-03-27 15:30:23 -04:00
|
|
|
throw new Error(`Can't interpret menu item: ${JSON.stringify(item)}`);
|
2018-01-26 17:36:36 -05:00
|
|
|
}
|
|
|
|
});
|
2017-10-11 14:41:03 -04:00
|
|
|
}
|
2017-10-06 16:37:59 -04:00
|
|
|
return {
|
|
|
|
json: {
|
|
|
|
message0: '%1',
|
|
|
|
type: menuId,
|
|
|
|
inputsInline: true,
|
|
|
|
output: 'String',
|
|
|
|
colour: categoryInfo.color1,
|
|
|
|
colourSecondary: categoryInfo.color2,
|
|
|
|
colourTertiary: categoryInfo.color3,
|
|
|
|
outputShape: ScratchBlocksConstants.OUTPUT_SHAPE_ROUND,
|
|
|
|
args0: [
|
|
|
|
{
|
|
|
|
type: 'field_dropdown',
|
|
|
|
name: menuName,
|
|
|
|
options: options
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
2017-08-04 14:25:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-27 17:35:04 -04:00
|
|
|
* Convert ExtensionBlockMetadata into scratch-blocks JSON & XML, and generate a proxy function.
|
|
|
|
* @param {ExtensionBlockMetadata} blockInfo - the block to convert
|
2017-08-04 14:25:17 -04:00
|
|
|
* @param {CategoryInfo} categoryInfo - the category for this block
|
2018-03-27 17:35:04 -04:00
|
|
|
* @returns {ConvertedBlockInfo} - the converted & original block information
|
2017-08-04 14:25:17 -04:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_convertForScratchBlocks (blockInfo, categoryInfo) {
|
2018-04-20 07:54:02 -04:00
|
|
|
const extendedOpcode = `${categoryInfo.id}_${blockInfo.opcode}`;
|
2018-03-27 17:35:04 -04:00
|
|
|
|
2017-08-04 14:25:17 -04:00
|
|
|
const blockJSON = {
|
2017-08-29 14:43:09 -04:00
|
|
|
type: extendedOpcode,
|
2017-08-04 14:25:17 -04:00
|
|
|
inputsInline: true,
|
|
|
|
category: categoryInfo.name,
|
|
|
|
colour: categoryInfo.color1,
|
|
|
|
colourSecondary: categoryInfo.color2,
|
2017-12-11 17:01:23 -05:00
|
|
|
colourTertiary: categoryInfo.color3,
|
2017-12-14 14:37:08 -05:00
|
|
|
extensions: ['scratch_extension']
|
2017-08-04 14:25:17 -04:00
|
|
|
};
|
2018-03-27 17:35:04 -04:00
|
|
|
const context = {
|
|
|
|
// TODO: store this somewhere so that we can map args appropriately after translation.
|
|
|
|
// This maps an arg name to its relative position in the original (usually English) block text.
|
|
|
|
// When displaying a block in another language we'll need to run a `replace` action similar to the one
|
|
|
|
// below, but each `[ARG]` will need to be replaced with the number in this map.
|
|
|
|
argsMap: {},
|
|
|
|
blockJSON,
|
|
|
|
categoryInfo,
|
|
|
|
blockInfo,
|
|
|
|
inputList: []
|
|
|
|
};
|
2017-08-04 14:25:17 -04:00
|
|
|
|
2017-12-15 14:09:29 -05:00
|
|
|
// If an icon for the extension exists, prepend it to each block, with a vertical separator.
|
2018-05-17 09:27:24 -04:00
|
|
|
// We can overspecify an icon for each block, but if no icon exists on a block, fall back to
|
|
|
|
// the category block icon.
|
|
|
|
const iconURI = blockInfo.blockIconURI || categoryInfo.blockIconURI;
|
|
|
|
|
|
|
|
if (iconURI) {
|
2017-12-15 14:09:29 -05:00
|
|
|
blockJSON.message0 = '%1 %2';
|
2017-11-03 11:35:42 -04:00
|
|
|
const iconJSON = {
|
|
|
|
type: 'field_image',
|
2018-05-17 09:27:24 -04:00
|
|
|
src: iconURI,
|
2017-11-03 11:35:42 -04:00
|
|
|
width: 40,
|
|
|
|
height: 40
|
|
|
|
};
|
2017-12-15 14:09:29 -05:00
|
|
|
const separatorJSON = {
|
|
|
|
type: 'field_vertical_separator'
|
|
|
|
};
|
2018-03-27 17:35:04 -04:00
|
|
|
blockJSON.args0 = [
|
|
|
|
iconJSON,
|
|
|
|
separatorJSON
|
|
|
|
];
|
2017-11-03 11:35:42 -04:00
|
|
|
}
|
|
|
|
|
2017-08-04 14:25:17 -04:00
|
|
|
switch (blockInfo.blockType) {
|
|
|
|
case BlockType.COMMAND:
|
2017-09-06 03:24:24 -04:00
|
|
|
blockJSON.outputShape = ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE;
|
2017-08-29 14:43:09 -04:00
|
|
|
blockJSON.previousStatement = null; // null = available connection; undefined = hat
|
|
|
|
if (!blockInfo.isTerminal) {
|
|
|
|
blockJSON.nextStatement = null; // null = available connection; undefined = terminal
|
|
|
|
}
|
2017-08-04 14:25:17 -04:00
|
|
|
break;
|
|
|
|
case BlockType.REPORTER:
|
|
|
|
blockJSON.output = 'String'; // TODO: distinguish number & string here?
|
2017-09-06 03:24:24 -04:00
|
|
|
blockJSON.outputShape = ScratchBlocksConstants.OUTPUT_SHAPE_ROUND;
|
2017-08-04 14:25:17 -04:00
|
|
|
break;
|
|
|
|
case BlockType.BOOLEAN:
|
|
|
|
blockJSON.output = 'Boolean';
|
2017-09-06 03:24:24 -04:00
|
|
|
blockJSON.outputShape = ScratchBlocksConstants.OUTPUT_SHAPE_HEXAGONAL;
|
2017-08-04 14:25:17 -04:00
|
|
|
break;
|
|
|
|
case BlockType.HAT:
|
2018-03-27 17:35:04 -04:00
|
|
|
case BlockType.EVENT:
|
|
|
|
if (!blockInfo.hasOwnProperty('isEdgeActivated')) {
|
|
|
|
// if absent, this property defaults to true
|
|
|
|
blockInfo.isEdgeActivated = true;
|
|
|
|
}
|
2017-09-06 03:24:24 -04:00
|
|
|
blockJSON.outputShape = ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE;
|
2017-08-29 14:43:09 -04:00
|
|
|
blockJSON.nextStatement = null; // null = available connection; undefined = terminal
|
2017-08-04 14:25:17 -04:00
|
|
|
break;
|
|
|
|
case BlockType.CONDITIONAL:
|
2018-03-27 17:35:04 -04:00
|
|
|
case BlockType.LOOP:
|
|
|
|
blockInfo.branchCount = blockInfo.branchCount || 1;
|
2017-09-06 03:24:24 -04:00
|
|
|
blockJSON.outputShape = ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE;
|
2017-08-29 14:43:09 -04:00
|
|
|
blockJSON.previousStatement = null; // null = available connection; undefined = hat
|
2018-03-27 17:35:04 -04:00
|
|
|
if (!blockInfo.isTerminal) {
|
|
|
|
blockJSON.nextStatement = null; // null = available connection; undefined = terminal
|
|
|
|
}
|
2017-08-04 14:25:17 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:35:04 -04:00
|
|
|
const blockText = Array.isArray(blockInfo.text) ? blockInfo.text : [blockInfo.text];
|
|
|
|
let inTextNum = 0; // text for the next block "arm" is blockText[inTextNum]
|
|
|
|
let inBranchNum = 0; // how many branches have we placed into the JSON so far?
|
|
|
|
let outLineNum = 0; // used for scratch-blocks `message${outLineNum}` and `args${outLineNum}`
|
|
|
|
const convertPlaceholders = this._convertPlaceholders.bind(this, context);
|
2018-03-27 15:30:23 -04:00
|
|
|
const extensionMessageContext = this.makeMessageContextForTarget();
|
2018-03-27 17:35:04 -04:00
|
|
|
|
|
|
|
// alternate between a block "arm" with text on it and an open slot for a substack
|
|
|
|
while (inTextNum < blockText.length || inBranchNum < blockInfo.branchCount) {
|
|
|
|
if (inTextNum < blockText.length) {
|
|
|
|
context.outLineNum = outLineNum;
|
2018-03-27 15:30:23 -04:00
|
|
|
const lineText = maybeFormatMessage(blockText[inTextNum], extensionMessageContext);
|
|
|
|
const convertedText = lineText.replace(/\[(.+?)]/g, convertPlaceholders);
|
2018-03-27 17:35:04 -04:00
|
|
|
if (blockJSON[`message${outLineNum}`]) {
|
|
|
|
blockJSON[`message${outLineNum}`] += convertedText;
|
|
|
|
} else {
|
|
|
|
blockJSON[`message${outLineNum}`] = convertedText;
|
|
|
|
}
|
|
|
|
++inTextNum;
|
|
|
|
++outLineNum;
|
|
|
|
}
|
|
|
|
if (inBranchNum < blockInfo.branchCount) {
|
|
|
|
blockJSON[`message${outLineNum}`] = '%1';
|
|
|
|
blockJSON[`args${outLineNum}`] = [{
|
|
|
|
type: 'input_statement',
|
|
|
|
name: `SUBSTACK${inBranchNum > 0 ? inBranchNum + 1 : ''}`
|
|
|
|
}];
|
|
|
|
++inBranchNum;
|
|
|
|
++outLineNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add icon to the bottom right of a loop block
|
|
|
|
if (blockInfo.blockType === BlockType.LOOP) {
|
|
|
|
blockJSON[`lastDummyAlign${outLineNum}`] = 'RIGHT';
|
|
|
|
blockJSON[`message${outLineNum}`] = '%1';
|
|
|
|
blockJSON[`args${outLineNum}`] = [{
|
|
|
|
type: 'field_image',
|
|
|
|
src: './static/blocks-media/repeat.svg', // TODO: use a constant or make this configurable?
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
alt: '*',
|
|
|
|
flip_rtl: true
|
|
|
|
}];
|
|
|
|
++outLineNum;
|
2017-08-04 14:25:17 -04:00
|
|
|
}
|
|
|
|
|
2018-03-27 17:35:04 -04:00
|
|
|
const blockXML = `<block type="${extendedOpcode}">${context.inputList.join('')}</block>`;
|
2017-08-04 14:25:17 -04:00
|
|
|
|
|
|
|
return {
|
2018-03-27 17:35:04 -04:00
|
|
|
info: context.blockInfo,
|
|
|
|
json: context.blockJSON,
|
2017-08-04 14:25:17 -04:00
|
|
|
xml: blockXML
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:35:04 -04:00
|
|
|
/**
|
|
|
|
* Helper for _convertForScratchBlocks which handles linearization of argument placeholders. Called as a callback
|
|
|
|
* from string#replace. In addition to the return value the JSON and XML items in the context will be filled.
|
|
|
|
* @param {object} context - information shared with _convertForScratchBlocks about the block, etc.
|
|
|
|
* @param {string} match - the overall string matched by the placeholder regex, including brackets: '[FOO]'.
|
|
|
|
* @param {string} placeholder - the name of the placeholder being matched: 'FOO'.
|
|
|
|
* @return {string} scratch-blocks placeholder for the argument: '%1'.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_convertPlaceholders (context, match, placeholder) {
|
|
|
|
// Sanitize the placeholder to ensure valid XML
|
|
|
|
placeholder = placeholder.replace(/[<"&]/, '_');
|
|
|
|
|
|
|
|
const argJSON = {
|
|
|
|
type: 'input_value',
|
|
|
|
name: placeholder
|
|
|
|
};
|
|
|
|
|
|
|
|
const argInfo = context.blockInfo.arguments[placeholder] || {};
|
|
|
|
const argTypeInfo = ArgumentTypeMap[argInfo.type] || {};
|
|
|
|
const defaultValue = (typeof argInfo.defaultValue === 'undefined' ?
|
|
|
|
'' :
|
2018-03-27 15:30:23 -04:00
|
|
|
escapeHtml(maybeFormatMessage(argInfo.defaultValue, this.makeMessageContextForTarget()).toString()));
|
2018-03-27 17:35:04 -04:00
|
|
|
|
|
|
|
if (argTypeInfo.check) {
|
|
|
|
argJSON.check = argTypeInfo.check;
|
|
|
|
}
|
|
|
|
|
|
|
|
const shadowType = (argInfo.menu ?
|
|
|
|
this._makeExtensionMenuId(argInfo.menu, context.categoryInfo.id) :
|
|
|
|
argTypeInfo.shadowType);
|
|
|
|
const fieldType = argInfo.menu || argTypeInfo.fieldType;
|
|
|
|
|
|
|
|
// <value> is the ScratchBlocks name for a block input.
|
|
|
|
context.inputList.push(`<value name="${placeholder}">`);
|
|
|
|
|
|
|
|
// The <shadow> is a placeholder for a reporter and is visible when there's no reporter in this input.
|
|
|
|
// Boolean inputs don't need to specify a shadow in the XML.
|
|
|
|
if (shadowType) {
|
|
|
|
context.inputList.push(`<shadow type="${shadowType}">`);
|
|
|
|
|
|
|
|
// <field> is a text field that the user can type into. Some shadows, like the color picker, don't allow
|
|
|
|
// text input and therefore don't need a field element.
|
|
|
|
if (fieldType) {
|
|
|
|
context.inputList.push(`<field name="${fieldType}">${defaultValue}</field>`);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.inputList.push('</shadow>');
|
|
|
|
}
|
|
|
|
|
|
|
|
context.inputList.push('</value>');
|
|
|
|
|
|
|
|
const argsName = `args${context.outLineNum}`;
|
|
|
|
const blockArgs = (context.blockJSON[argsName] = context.blockJSON[argsName] || []);
|
|
|
|
blockArgs.push(argJSON);
|
|
|
|
const argNum = blockArgs.length;
|
|
|
|
context.argsMap[placeholder] = argNum;
|
|
|
|
return `%${argNum}`;
|
|
|
|
}
|
|
|
|
|
2017-08-25 16:53:57 -04:00
|
|
|
/**
|
|
|
|
* @returns {string} scratch-blocks XML description for all dynamic blocks, wrapped in <category> elements.
|
|
|
|
*/
|
|
|
|
getBlocksXML () {
|
|
|
|
const xmlParts = [];
|
|
|
|
for (const categoryInfo of this._blockInfo) {
|
|
|
|
const {name, color1, color2} = categoryInfo;
|
2017-11-01 11:07:26 -04:00
|
|
|
const paletteBlocks = categoryInfo.blocks.filter(block => !block.info.hideFromPalette);
|
2017-12-19 14:47:11 -05:00
|
|
|
const colorXML = `colour="${color1}" secondaryColour="${color2}"`;
|
|
|
|
|
|
|
|
// Use a menu icon if there is one. Otherwise, use the block icon. If there's no icon,
|
|
|
|
// the category menu will show its default colored circle.
|
|
|
|
let menuIconURI = '';
|
|
|
|
if (categoryInfo.menuIconURI) {
|
|
|
|
menuIconURI = categoryInfo.menuIconURI;
|
|
|
|
} else if (categoryInfo.blockIconURI) {
|
|
|
|
menuIconURI = categoryInfo.blockIconURI;
|
|
|
|
}
|
|
|
|
const menuIconXML = menuIconURI ?
|
|
|
|
`iconURI="${menuIconURI}"` : '';
|
|
|
|
|
2018-06-27 15:53:18 -04:00
|
|
|
let statusButtonXML = '';
|
|
|
|
if (categoryInfo.showStatusButton) {
|
|
|
|
statusButtonXML = 'showStatusButton="true"';
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlParts.push(`<category name="${name}" id="${categoryInfo.id}"
|
|
|
|
${statusButtonXML} ${colorXML} ${menuIconXML}>`);
|
2017-11-01 11:07:26 -04:00
|
|
|
xmlParts.push.apply(xmlParts, paletteBlocks.map(block => block.xml));
|
2017-08-25 16:53:57 -04:00
|
|
|
xmlParts.push('</category>');
|
|
|
|
}
|
|
|
|
return xmlParts.join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Array.<string>} - an array containing the scratch-blocks JSON information for each dynamic block.
|
|
|
|
*/
|
|
|
|
getBlocksJSON () {
|
|
|
|
return this._blockInfo.reduce(
|
|
|
|
(result, categoryInfo) => result.concat(categoryInfo.blocks.map(blockInfo => blockInfo.json)), []);
|
|
|
|
}
|
|
|
|
|
2018-06-19 17:59:03 -04:00
|
|
|
registerExtensionDevice (extensionId, device) {
|
|
|
|
this.extensionDevices[extensionId] = device;
|
|
|
|
}
|
|
|
|
|
|
|
|
startDeviceScan (extensionId) {
|
|
|
|
if (this.extensionDevices[extensionId]) {
|
2018-06-24 22:00:23 -04:00
|
|
|
this.extensionDevices[extensionId].startDeviceScan();
|
2018-06-19 17:59:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connectToPeripheral (extensionId, peripheralId) {
|
|
|
|
if (this.extensionDevices[extensionId]) {
|
2018-06-20 13:59:23 -04:00
|
|
|
this.extensionDevices[extensionId].connectDevice(peripheralId);
|
2018-06-19 17:59:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Retrieve the function associated with the given opcode.
|
|
|
|
* @param {!string} opcode The opcode to look up.
|
|
|
|
* @return {Function} The function which implements the opcode.
|
|
|
|
*/
|
|
|
|
getOpcodeFunction (opcode) {
|
|
|
|
return this._primitives[opcode];
|
|
|
|
}
|
2016-05-02 12:20:27 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether an opcode represents a hat block.
|
|
|
|
* @param {!string} opcode The opcode to look up.
|
|
|
|
* @return {boolean} True if the op is known to be a hat.
|
|
|
|
*/
|
|
|
|
getIsHat (opcode) {
|
|
|
|
return this._hats.hasOwnProperty(opcode);
|
|
|
|
}
|
2016-08-23 18:12:32 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether an opcode represents an edge-activated hat block.
|
|
|
|
* @param {!string} opcode The opcode to look up.
|
|
|
|
* @return {boolean} True if the op is known to be a edge-activated hat.
|
|
|
|
*/
|
|
|
|
getIsEdgeActivatedHat (opcode) {
|
|
|
|
return this._hats.hasOwnProperty(opcode) &&
|
|
|
|
this._hats[opcode].edgeActivated;
|
|
|
|
}
|
2016-08-23 18:12:32 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Update an edge-activated hat block value.
|
|
|
|
* @param {!string} blockId ID of hat to store value for.
|
|
|
|
* @param {*} newValue Value to store for edge-activated hat.
|
|
|
|
* @return {*} The old value for the edge-activated hat.
|
|
|
|
*/
|
|
|
|
updateEdgeActivatedValue (blockId, newValue) {
|
|
|
|
const oldValue = this._edgeActivatedHatValues[blockId];
|
|
|
|
this._edgeActivatedHatValues[blockId] = newValue;
|
|
|
|
return oldValue;
|
|
|
|
}
|
2016-08-23 18:12:32 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Clear all edge-activaed hat values.
|
|
|
|
*/
|
|
|
|
clearEdgeActivatedValues () {
|
|
|
|
this._edgeActivatedHatValues = {};
|
|
|
|
}
|
2016-08-23 18:12:32 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Attach the audio engine
|
|
|
|
* @param {!AudioEngine} audioEngine The audio engine to attach
|
|
|
|
*/
|
|
|
|
attachAudioEngine (audioEngine) {
|
|
|
|
this.audioEngine = audioEngine;
|
|
|
|
}
|
2017-01-27 13:44:48 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Attach the renderer
|
|
|
|
* @param {!RenderWebGL} renderer The renderer to attach
|
|
|
|
*/
|
|
|
|
attachRenderer (renderer) {
|
|
|
|
this.renderer = renderer;
|
2018-05-15 22:22:44 -04:00
|
|
|
this.renderer.setLayerGroupOrdering(StageLayering.LAYER_GROUPS);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-09-20 15:07:05 -04:00
|
|
|
|
2018-05-10 11:00:51 -04:00
|
|
|
/**
|
|
|
|
* Set the svg adapter, which converts scratch 2 svgs to scratch 3 svgs
|
|
|
|
* @param {!SvgRenderer} svgAdapter The adapter to attach
|
|
|
|
*/
|
|
|
|
attachV2SVGAdapter (svgAdapter) {
|
|
|
|
this.v2SvgAdapter = svgAdapter;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Attach the storage module
|
|
|
|
* @param {!ScratchStorage} storage The storage module to attach
|
|
|
|
*/
|
|
|
|
attachStorage (storage) {
|
|
|
|
this.storage = storage;
|
|
|
|
}
|
2017-01-04 18:37:55 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
2016-08-23 18:12:32 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Create a thread and push it to the list of threads.
|
|
|
|
* @param {!string} id ID of block that starts the stack.
|
|
|
|
* @param {!Target} target Target to run thread on.
|
2017-05-09 17:34:13 -04:00
|
|
|
* @param {?object} opts optional arguments
|
2017-07-12 15:43:13 -04:00
|
|
|
* @param {?boolean} opts.stackClick true if the script was activated by clicking on the stack
|
2017-05-15 10:12:25 -04:00
|
|
|
* @param {?boolean} opts.updateMonitor true if the script should update a monitor value
|
2017-04-17 19:42:48 -04:00
|
|
|
* @return {!Thread} The newly created thread.
|
|
|
|
*/
|
2017-05-09 17:34:13 -04:00
|
|
|
_pushThread (id, target, opts) {
|
|
|
|
opts = Object.assign({
|
2017-07-12 15:43:13 -04:00
|
|
|
stackClick: false,
|
2017-05-09 17:34:13 -04:00
|
|
|
updateMonitor: false
|
|
|
|
}, opts);
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
const thread = new Thread(id);
|
|
|
|
thread.target = target;
|
2017-07-12 15:43:13 -04:00
|
|
|
thread.stackClick = opts.stackClick;
|
2017-05-09 17:34:13 -04:00
|
|
|
thread.updateMonitor = opts.updateMonitor;
|
2018-04-10 16:29:51 -04:00
|
|
|
thread.blockContainer = opts.updateMonitor ?
|
|
|
|
this.monitorBlocks :
|
|
|
|
target.blocks;
|
2017-05-09 17:34:13 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
thread.pushStack(id);
|
|
|
|
this.threads.push(thread);
|
|
|
|
return thread;
|
|
|
|
}
|
2016-04-26 16:50:49 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
2017-11-17 15:12:07 -05:00
|
|
|
* Stop a thread: stop running it immediately, and remove it from the thread list later.
|
|
|
|
* @param {!Thread} thread Thread object to remove from actives
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2017-11-17 15:12:07 -05:00
|
|
|
_stopThread (thread) {
|
|
|
|
// Mark the thread for later removal
|
|
|
|
thread.isKilled = true;
|
2017-04-17 19:42:48 -04:00
|
|
|
// Inform sequencer to stop executing that thread.
|
|
|
|
this.sequencer.retireThread(thread);
|
2016-05-02 13:09:38 -04:00
|
|
|
}
|
2016-04-26 16:50:49 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Restart a thread in place, maintaining its position in the list of threads.
|
|
|
|
* This is used by `startHats` to and is necessary to ensure 2.0-like execution order.
|
|
|
|
* Test project: https://scratch.mit.edu/projects/130183108/
|
|
|
|
* @param {!Thread} thread Thread object to restart.
|
2017-11-07 14:22:20 -05:00
|
|
|
* @return {Thread} The restarted thread.
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
|
|
|
_restartThread (thread) {
|
|
|
|
const newThread = new Thread(thread.topBlock);
|
|
|
|
newThread.target = thread.target;
|
2017-07-12 15:43:13 -04:00
|
|
|
newThread.stackClick = thread.stackClick;
|
2017-05-11 17:11:06 -04:00
|
|
|
newThread.updateMonitor = thread.updateMonitor;
|
2018-04-10 16:29:51 -04:00
|
|
|
newThread.blockContainer = thread.blockContainer;
|
2017-04-17 19:42:48 -04:00
|
|
|
newThread.pushStack(thread.topBlock);
|
|
|
|
const i = this.threads.indexOf(thread);
|
|
|
|
if (i > -1) {
|
|
|
|
this.threads[i] = newThread;
|
2017-11-07 14:22:20 -05:00
|
|
|
return newThread;
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2017-11-07 14:22:20 -05:00
|
|
|
this.threads.push(thread);
|
|
|
|
return thread;
|
2016-11-10 15:05:49 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether a thread is currently active/running.
|
|
|
|
* @param {?Thread} thread Thread object to check.
|
|
|
|
* @return {boolean} True if the thread is active/running.
|
|
|
|
*/
|
|
|
|
isActiveThread (thread) {
|
2017-11-07 14:15:53 -05:00
|
|
|
return (
|
|
|
|
(
|
|
|
|
thread.stack.length > 0 &&
|
|
|
|
thread.status !== Thread.STATUS_DONE) &&
|
|
|
|
this.threads.indexOf(thread) > -1);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-08-29 10:01:31 -04:00
|
|
|
|
2018-06-06 09:57:53 -04:00
|
|
|
/**
|
|
|
|
* Return whether a thread is waiting for more information or done.
|
|
|
|
* @param {?Thread} thread Thread object to check.
|
|
|
|
* @return {boolean} True if the thread is waiting
|
|
|
|
*/
|
|
|
|
isWaitingThread (thread) {
|
|
|
|
return (
|
|
|
|
thread.status === Thread.STATUS_PROMISE_WAIT ||
|
|
|
|
thread.status === Thread.STATUS_YIELD_TICK ||
|
|
|
|
!this.isActiveThread(thread)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Toggle a script.
|
|
|
|
* @param {!string} topBlockId ID of block that starts the script.
|
2017-05-08 16:51:45 -04:00
|
|
|
* @param {?object} opts optional arguments to toggle script
|
|
|
|
* @param {?string} opts.target target ID for target to run script on. If not supplied, uses editing target.
|
2017-07-31 17:57:17 -04:00
|
|
|
* @param {?boolean} opts.stackClick true if the user activated the stack by clicking, false if not. This
|
|
|
|
* determines whether we show a visual report when turning on the script.
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2017-05-08 16:51:45 -04:00
|
|
|
toggleScript (topBlockId, opts) {
|
2017-05-09 17:34:13 -04:00
|
|
|
opts = Object.assign({
|
|
|
|
target: this._editingTarget,
|
2017-07-31 17:57:17 -04:00
|
|
|
stackClick: false
|
2017-05-09 17:34:13 -04:00
|
|
|
}, opts);
|
2017-04-17 19:42:48 -04:00
|
|
|
// Remove any existing thread.
|
|
|
|
for (let i = 0; i < this.threads.length; i++) {
|
2017-07-28 14:38:26 -04:00
|
|
|
// Toggling a script that's already running turns it off
|
|
|
|
if (this.threads[i].topBlock === topBlockId && this.threads[i].status !== Thread.STATUS_DONE) {
|
2017-07-12 15:43:13 -04:00
|
|
|
const blockContainer = opts.target.blocks;
|
|
|
|
const opcode = blockContainer.getOpcode(blockContainer.getBlock(topBlockId));
|
2017-08-29 14:43:09 -04:00
|
|
|
|
2017-07-12 15:43:13 -04:00
|
|
|
if (this.getIsEdgeActivatedHat(opcode) && this.threads[i].stackClick !== opts.stackClick) {
|
|
|
|
// Allow edge activated hat thread stack click to coexist with
|
|
|
|
// edge activated hat thread that runs every frame
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-17 15:12:07 -05:00
|
|
|
this._stopThread(this.threads[i]);
|
2017-04-17 19:42:48 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-04-29 17:31:04 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Otherwise add it.
|
2017-05-09 17:34:13 -04:00
|
|
|
this._pushThread(topBlockId, opts.target, opts);
|
2016-04-29 17:31:04 -04:00
|
|
|
}
|
|
|
|
|
2017-07-31 17:57:17 -04:00
|
|
|
/**
|
|
|
|
* Enqueue a script that when finished will update the monitor for the block.
|
|
|
|
* @param {!string} topBlockId ID of block that starts the script.
|
2017-11-20 17:23:59 -05:00
|
|
|
* @param {?Target} optTarget target Target to run script on. If not supplied, uses editing target.
|
2017-07-31 17:57:17 -04:00
|
|
|
*/
|
|
|
|
addMonitorScript (topBlockId, optTarget) {
|
|
|
|
if (!optTarget) optTarget = this._editingTarget;
|
|
|
|
for (let i = 0; i < this.threads.length; i++) {
|
|
|
|
// Don't re-add the script if it's already running
|
|
|
|
if (this.threads[i].topBlock === topBlockId && this.threads[i].status !== Thread.STATUS_DONE &&
|
|
|
|
this.threads[i].updateMonitor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise add it.
|
|
|
|
this._pushThread(topBlockId, optTarget, {updateMonitor: true});
|
|
|
|
}
|
2017-07-12 15:43:13 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Run a function `f` for all scripts in a workspace.
|
|
|
|
* `f` will be called with two parameters:
|
|
|
|
* - the top block ID of the script.
|
|
|
|
* - the target that owns the script.
|
|
|
|
* @param {!Function} f Function to call for each script.
|
|
|
|
* @param {Target=} optTarget Optionally, a target to restrict to.
|
|
|
|
*/
|
|
|
|
allScriptsDo (f, optTarget) {
|
|
|
|
let targets = this.targets;
|
|
|
|
if (optTarget) {
|
|
|
|
targets = [optTarget];
|
|
|
|
}
|
|
|
|
for (let t = targets.length - 1; t >= 0; t--) {
|
|
|
|
const target = targets[t];
|
|
|
|
const scripts = target.blocks.getScripts();
|
|
|
|
for (let j = 0; j < scripts.length; j++) {
|
|
|
|
const topBlockId = scripts[j];
|
|
|
|
f(topBlockId, target);
|
|
|
|
}
|
2016-05-03 17:39:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Start all relevant hats.
|
|
|
|
* @param {!string} requestedHatOpcode 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 (requestedHatOpcode,
|
|
|
|
optMatchFields, optTarget) {
|
|
|
|
if (!this._hats.hasOwnProperty(requestedHatOpcode)) {
|
|
|
|
// No known hat with this opcode.
|
2016-08-23 18:12:32 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
const instance = this;
|
|
|
|
const newThreads = [];
|
2016-12-23 09:38:18 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
for (const opts in optMatchFields) {
|
|
|
|
if (!optMatchFields.hasOwnProperty(opts)) continue;
|
|
|
|
optMatchFields[opts] = optMatchFields[opts].toUpperCase();
|
2016-12-23 09:38:18 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Consider all scripts, looking for hats with opcode `requestedHatOpcode`.
|
|
|
|
this.allScriptsDo((topBlockId, target) => {
|
|
|
|
const blocks = target.blocks;
|
|
|
|
const block = blocks.getBlock(topBlockId);
|
|
|
|
const potentialHatOpcode = block.opcode;
|
|
|
|
if (potentialHatOpcode !== requestedHatOpcode) {
|
|
|
|
// Not the right hat.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match any requested fields.
|
|
|
|
// For example: ensures that broadcasts match.
|
|
|
|
// This needs to happen before the block is evaluated
|
|
|
|
// (i.e., before the predicate can be run) because "broadcast and wait"
|
|
|
|
// needs to have a precise collection of started threads.
|
|
|
|
let hatFields = blocks.getFields(block);
|
|
|
|
|
|
|
|
// If no fields are present, check inputs (horizontal blocks)
|
|
|
|
if (Object.keys(hatFields).length === 0) {
|
2017-10-13 04:46:35 -04:00
|
|
|
hatFields = {}; // don't overwrite the block's actual fields list
|
2017-04-17 19:42:48 -04:00
|
|
|
const hatInputs = blocks.getInputs(block);
|
|
|
|
for (const input in hatInputs) {
|
|
|
|
if (!hatInputs.hasOwnProperty(input)) continue;
|
|
|
|
const id = hatInputs[input].block;
|
|
|
|
const inpBlock = blocks.getBlock(id);
|
|
|
|
const fields = blocks.getFields(inpBlock);
|
2017-10-13 04:46:35 -04:00
|
|
|
Object.assign(hatFields, fields);
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-23 09:38:18 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
if (optMatchFields) {
|
|
|
|
for (const matchField in optMatchFields) {
|
|
|
|
if (hatFields[matchField].value.toUpperCase() !==
|
|
|
|
optMatchFields[matchField]) {
|
|
|
|
// Field mismatch.
|
|
|
|
return;
|
|
|
|
}
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
2016-08-29 10:18:49 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
|
|
|
// Look up metadata for the relevant hat.
|
|
|
|
const hatMeta = instance._hats[requestedHatOpcode];
|
|
|
|
if (hatMeta.restartExistingThreads) {
|
|
|
|
// If `restartExistingThreads` is true, we should stop
|
|
|
|
// any existing threads starting with the top block.
|
|
|
|
for (let i = 0; i < instance.threads.length; i++) {
|
|
|
|
if (instance.threads[i].topBlock === topBlockId &&
|
2017-07-31 17:57:17 -04:00
|
|
|
!instance.threads[i].stackClick && // stack click threads and hat threads can coexist
|
2017-04-17 19:42:48 -04:00
|
|
|
instance.threads[i].target === target) {
|
2017-11-07 14:22:20 -05:00
|
|
|
newThreads.push(instance._restartThread(instance.threads[i]));
|
2017-04-17 19:42:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If `restartExistingThreads` is false, we should
|
|
|
|
// give up if any threads with the top block are running.
|
|
|
|
for (let j = 0; j < instance.threads.length; j++) {
|
|
|
|
if (instance.threads[j].topBlock === topBlockId &&
|
2017-07-12 15:43:13 -04:00
|
|
|
instance.threads[j].target === target &&
|
2017-07-31 17:57:17 -04:00
|
|
|
!instance.threads[j].stackClick && // stack click threads and hat threads can coexist
|
|
|
|
instance.threads[j].status !== Thread.STATUS_DONE) {
|
2017-04-17 19:42:48 -04:00
|
|
|
// Some thread is already running.
|
|
|
|
return;
|
|
|
|
}
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Start the thread with this top block.
|
|
|
|
newThreads.push(instance._pushThread(topBlockId, target));
|
|
|
|
}, optTarget);
|
|
|
|
return newThreads;
|
|
|
|
}
|
2016-08-23 18:12:32 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Dispose all targets. Return to clean state.
|
|
|
|
*/
|
|
|
|
dispose () {
|
|
|
|
this.stopAll();
|
|
|
|
this.targets.map(this.disposeTarget, this);
|
2018-05-09 11:34:01 -04:00
|
|
|
this._monitorState = OrderedMap({});
|
|
|
|
// @todo clear out extensions? turboMode? etc.
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-10-17 13:43:38 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Dispose of a target.
|
|
|
|
* @param {!Target} disposingTarget Target to dispose of.
|
|
|
|
*/
|
|
|
|
disposeTarget (disposingTarget) {
|
|
|
|
this.targets = this.targets.filter(target => {
|
|
|
|
if (disposingTarget !== target) return true;
|
|
|
|
// Allow target to do dispose actions.
|
|
|
|
target.dispose();
|
|
|
|
// Remove from list of targets.
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Stop any threads acting on the target.
|
|
|
|
* @param {!Target} target Target to stop threads for.
|
|
|
|
* @param {Thread=} optThreadException Optional thread to skip.
|
|
|
|
*/
|
|
|
|
stopForTarget (target, optThreadException) {
|
|
|
|
// Stop any threads on the target.
|
|
|
|
for (let i = 0; i < this.threads.length; i++) {
|
|
|
|
if (this.threads[i] === optThreadException) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (this.threads[i].target === target) {
|
2017-11-17 15:12:07 -05:00
|
|
|
this._stopThread(this.threads[i]);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Start all threads that start with the green flag.
|
|
|
|
*/
|
|
|
|
greenFlag () {
|
|
|
|
this.stopAll();
|
2017-12-01 20:06:55 -05:00
|
|
|
this.emit(Runtime.PROJECT_START);
|
2017-04-17 19:42:48 -04:00
|
|
|
this.ioDevices.clock.resetProjectTimer();
|
|
|
|
this.clearEdgeActivatedValues();
|
|
|
|
// Inform all targets of the green flag.
|
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
this.targets[i].onGreenFlag();
|
2016-09-15 19:37:12 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
this.startHats('event_whenflagclicked');
|
2016-09-15 19:37:12 -04:00
|
|
|
}
|
2016-04-29 17:58:31 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Stop "everything."
|
|
|
|
*/
|
|
|
|
stopAll () {
|
2017-10-06 13:43:07 -04:00
|
|
|
// Emit stop event to allow blocks to clean up any state.
|
|
|
|
this.emit(Runtime.PROJECT_STOP_ALL);
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Dispose all clones.
|
|
|
|
const newTargets = [];
|
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
this.targets[i].onStopAll();
|
|
|
|
if (this.targets[i].hasOwnProperty('isOriginal') &&
|
|
|
|
!this.targets[i].isOriginal) {
|
|
|
|
this.targets[i].dispose();
|
|
|
|
} else {
|
|
|
|
newTargets.push(this.targets[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.targets = newTargets;
|
|
|
|
// Dispose all threads.
|
2017-11-17 15:12:07 -05:00
|
|
|
this.threads.forEach(thread => this._stopThread(thread));
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Repeatedly run `sequencer.stepThreads` and filter out
|
|
|
|
* inactive threads after each iteration.
|
|
|
|
*/
|
|
|
|
_step () {
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.profiler !== null) {
|
|
|
|
if (stepProfilerId === -1) {
|
|
|
|
stepProfilerId = this.profiler.idByName('Runtime._step');
|
|
|
|
}
|
|
|
|
this.profiler.start(stepProfilerId);
|
|
|
|
}
|
2017-11-17 15:12:07 -05:00
|
|
|
|
|
|
|
// Clean up threads that were told to stop during or since the last step
|
|
|
|
this.threads = this.threads.filter(thread => !thread.isKilled);
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
// Find all edge-activated hats, and add them to threads to be evaluated.
|
|
|
|
for (const hatType in this._hats) {
|
|
|
|
if (!this._hats.hasOwnProperty(hatType)) continue;
|
|
|
|
const hat = this._hats[hatType];
|
|
|
|
if (hat.edgeActivated) {
|
|
|
|
this.startHats(hatType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.redrawRequested = false;
|
2017-05-08 09:53:16 -04:00
|
|
|
this._pushMonitors();
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.profiler !== null) {
|
|
|
|
if (stepThreadsProfilerId === -1) {
|
|
|
|
stepThreadsProfilerId = this.profiler.idByName('Sequencer.stepThreads');
|
|
|
|
}
|
|
|
|
this.profiler.start(stepThreadsProfilerId);
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
const doneThreads = this.sequencer.stepThreads();
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.profiler !== null) {
|
|
|
|
this.profiler.stop();
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
this._updateGlows(doneThreads);
|
2017-05-10 14:06:02 -04:00
|
|
|
// Add done threads so that even if a thread finishes within 1 frame, the green
|
|
|
|
// flag will still indicate that a script ran.
|
2017-05-11 10:20:33 -04:00
|
|
|
this._emitProjectRunStatus(
|
|
|
|
this.threads.length + doneThreads.length -
|
|
|
|
this._getMonitorThreadCount([...this.threads, ...doneThreads]));
|
2017-04-17 19:42:48 -04:00
|
|
|
if (this.renderer) {
|
|
|
|
// @todo: Only render when this.redrawRequested or clones rendered.
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.profiler !== null) {
|
|
|
|
if (rendererDrawProfilerId === -1) {
|
|
|
|
rendererDrawProfilerId = this.profiler.idByName('RenderWebGL.draw');
|
|
|
|
}
|
|
|
|
this.profiler.start(rendererDrawProfilerId);
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
this.renderer.draw();
|
2017-11-09 17:27:49 -05:00
|
|
|
if (this.profiler !== null) {
|
|
|
|
this.profiler.stop();
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2017-05-18 16:50:34 -04:00
|
|
|
|
2017-05-16 09:20:52 -04:00
|
|
|
if (this._refreshTargets) {
|
|
|
|
this.emit(Runtime.TARGETS_UPDATE);
|
|
|
|
this._refreshTargets = false;
|
|
|
|
}
|
2017-05-15 16:25:34 -04:00
|
|
|
|
2017-05-19 12:28:05 -04:00
|
|
|
if (!this._prevMonitorState.equals(this._monitorState)) {
|
2017-05-22 17:31:53 -04:00
|
|
|
this.emit(Runtime.MONITORS_UPDATE, this._monitorState);
|
|
|
|
this._prevMonitorState = this._monitorState;
|
2017-05-19 12:28:05 -04:00
|
|
|
}
|
2017-11-09 17:27:49 -05:00
|
|
|
|
|
|
|
if (this.profiler !== null) {
|
|
|
|
this.profiler.stop();
|
|
|
|
this.profiler.reportFrames();
|
|
|
|
}
|
2016-10-26 11:32:15 -04:00
|
|
|
}
|
2017-05-15 10:18:48 -04:00
|
|
|
|
2017-05-10 14:06:02 -04:00
|
|
|
/**
|
|
|
|
* Get the number of threads in the given array that are monitor threads (threads
|
|
|
|
* that update monitor values, and don't count as running a script).
|
|
|
|
* @param {!Array.<Thread>} threads The set of threads to look through.
|
|
|
|
* @return {number} The number of monitor threads in threads.
|
|
|
|
*/
|
|
|
|
_getMonitorThreadCount (threads) {
|
|
|
|
let count = 0;
|
|
|
|
threads.forEach(thread => {
|
|
|
|
if (thread.updateMonitor) count++;
|
|
|
|
});
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-05-08 09:53:16 -04:00
|
|
|
/**
|
|
|
|
* Queue monitor blocks to sequencer to be run.
|
|
|
|
*/
|
|
|
|
_pushMonitors () {
|
|
|
|
this.monitorBlocks.runAllMonitored(this);
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Set the current editing target known by the runtime.
|
|
|
|
* @param {!Target} editingTarget New editing target.
|
|
|
|
*/
|
|
|
|
setEditingTarget (editingTarget) {
|
|
|
|
this._editingTarget = editingTarget;
|
|
|
|
// Script glows must be cleared.
|
|
|
|
this._scriptGlowsPreviousFrame = [];
|
|
|
|
this._updateGlows();
|
2017-05-12 11:42:22 -04:00
|
|
|
this.requestTargetsUpdate(editingTarget);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-09-08 09:40:53 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Set whether we are in 30 TPS compatibility mode.
|
|
|
|
* @param {boolean} compatibilityModeOn True iff in compatibility mode.
|
|
|
|
*/
|
|
|
|
setCompatibilityMode (compatibilityModeOn) {
|
|
|
|
this.compatibilityMode = compatibilityModeOn;
|
|
|
|
if (this._steppingInterval) {
|
|
|
|
clearInterval(this._steppingInterval);
|
|
|
|
this.start();
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Emit glows/glow clears for scripts after a single tick.
|
|
|
|
* Looks at `this.threads` and notices which have turned on/off new glows.
|
|
|
|
* @param {Array.<Thread>=} optExtraThreads Optional list of inactive threads.
|
|
|
|
*/
|
|
|
|
_updateGlows (optExtraThreads) {
|
|
|
|
const searchThreads = [];
|
|
|
|
searchThreads.push.apply(searchThreads, this.threads);
|
|
|
|
if (optExtraThreads) {
|
|
|
|
searchThreads.push.apply(searchThreads, optExtraThreads);
|
|
|
|
}
|
|
|
|
// Set of scripts that request a glow this frame.
|
|
|
|
const requestedGlowsThisFrame = [];
|
|
|
|
// Final set of scripts glowing during this frame.
|
|
|
|
const finalScriptGlows = [];
|
|
|
|
// Find all scripts that should be glowing.
|
|
|
|
for (let i = 0; i < searchThreads.length; i++) {
|
|
|
|
const thread = searchThreads[i];
|
|
|
|
const target = thread.target;
|
|
|
|
if (target === this._editingTarget) {
|
|
|
|
const blockForThread = thread.blockGlowInFrame;
|
|
|
|
if (thread.requestScriptGlowInFrame) {
|
|
|
|
let script = target.blocks.getTopLevelScript(blockForThread);
|
|
|
|
if (!script) {
|
|
|
|
// Attempt to find in flyout blocks.
|
|
|
|
script = this.flyoutBlocks.getTopLevelScript(
|
|
|
|
blockForThread
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (script) {
|
|
|
|
requestedGlowsThisFrame.push(script);
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2016-10-13 17:15:49 -04:00
|
|
|
}
|
2016-09-08 09:40:53 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
// Compare to previous frame.
|
|
|
|
for (let j = 0; j < this._scriptGlowsPreviousFrame.length; j++) {
|
|
|
|
const previousFrameGlow = this._scriptGlowsPreviousFrame[j];
|
|
|
|
if (requestedGlowsThisFrame.indexOf(previousFrameGlow) < 0) {
|
|
|
|
// Glow turned off.
|
|
|
|
this.glowScript(previousFrameGlow, false);
|
|
|
|
} else {
|
|
|
|
// Still glowing.
|
|
|
|
finalScriptGlows.push(previousFrameGlow);
|
|
|
|
}
|
2016-09-08 09:40:53 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
for (let k = 0; k < requestedGlowsThisFrame.length; k++) {
|
|
|
|
const currentFrameGlow = requestedGlowsThisFrame[k];
|
|
|
|
if (this._scriptGlowsPreviousFrame.indexOf(currentFrameGlow) < 0) {
|
|
|
|
// Glow turned on.
|
|
|
|
this.glowScript(currentFrameGlow, true);
|
|
|
|
finalScriptGlows.push(currentFrameGlow);
|
|
|
|
}
|
2016-09-08 09:40:53 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
this._scriptGlowsPreviousFrame = finalScriptGlows;
|
2016-09-08 09:40:53 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Emit run start/stop after each tick. Emits when `this.threads.length` goes
|
|
|
|
* between non-zero and zero
|
|
|
|
*
|
2017-05-10 14:06:02 -04:00
|
|
|
* @param {number} nonMonitorThreadCount The new nonMonitorThreadCount
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2017-05-11 10:20:33 -04:00
|
|
|
_emitProjectRunStatus (nonMonitorThreadCount) {
|
2017-05-10 14:06:02 -04:00
|
|
|
if (this._nonMonitorThreadCount === 0 && nonMonitorThreadCount > 0) {
|
2017-04-17 19:42:48 -04:00
|
|
|
this.emit(Runtime.PROJECT_RUN_START);
|
|
|
|
}
|
2017-05-10 14:06:02 -04:00
|
|
|
if (this._nonMonitorThreadCount > 0 && nonMonitorThreadCount === 0) {
|
2017-04-17 19:42:48 -04:00
|
|
|
this.emit(Runtime.PROJECT_RUN_STOP);
|
|
|
|
}
|
2017-05-10 14:06:02 -04:00
|
|
|
this._nonMonitorThreadCount = nonMonitorThreadCount;
|
2016-11-23 15:47:49 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* "Quiet" a script's glow: stop the VM from generating glow/unglow events
|
|
|
|
* about that script. Use when a script has just been deleted, but we may
|
|
|
|
* still be tracking glow data about it.
|
|
|
|
* @param {!string} scriptBlockId Id of top-level block in script to quiet.
|
|
|
|
*/
|
|
|
|
quietGlow (scriptBlockId) {
|
|
|
|
const index = this._scriptGlowsPreviousFrame.indexOf(scriptBlockId);
|
|
|
|
if (index > -1) {
|
|
|
|
this._scriptGlowsPreviousFrame.splice(index, 1);
|
|
|
|
}
|
2016-09-15 13:51:40 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Emit feedback for block glowing (used in the sequencer).
|
|
|
|
* @param {?string} blockId ID for the block to update glow
|
|
|
|
* @param {boolean} isGlowing True to turn on glow; false to turn off.
|
|
|
|
*/
|
|
|
|
glowBlock (blockId, isGlowing) {
|
|
|
|
if (isGlowing) {
|
|
|
|
this.emit(Runtime.BLOCK_GLOW_ON, {id: blockId});
|
|
|
|
} else {
|
|
|
|
this.emit(Runtime.BLOCK_GLOW_OFF, {id: blockId});
|
|
|
|
}
|
2016-05-02 18:09:02 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Emit feedback for script glowing.
|
|
|
|
* @param {?string} topBlockId ID for the top block to update glow
|
|
|
|
* @param {boolean} isGlowing True to turn on glow; false to turn off.
|
|
|
|
*/
|
|
|
|
glowScript (topBlockId, isGlowing) {
|
|
|
|
if (isGlowing) {
|
|
|
|
this.emit(Runtime.SCRIPT_GLOW_ON, {id: topBlockId});
|
|
|
|
} else {
|
|
|
|
this.emit(Runtime.SCRIPT_GLOW_OFF, {id: topBlockId});
|
|
|
|
}
|
2016-08-23 15:47:21 -04:00
|
|
|
}
|
|
|
|
|
2018-02-12 10:25:42 -05:00
|
|
|
/**
|
|
|
|
* Emit whether blocks are being dragged over gui
|
|
|
|
* @param {boolean} areBlocksOverGui True if blocks are dragged out of blocks workspace, false otherwise
|
|
|
|
*/
|
|
|
|
emitBlockDragUpdate (areBlocksOverGui) {
|
|
|
|
this.emit(Runtime.BLOCK_DRAG_UPDATE, areBlocksOverGui);
|
|
|
|
}
|
|
|
|
|
2018-02-23 11:57:19 -05:00
|
|
|
/**
|
|
|
|
* Emit event to indicate that the block drag has ended with the blocks outside the blocks workspace
|
|
|
|
* @param {Array.<object>} blocks The set of blocks dragged to the GUI
|
|
|
|
*/
|
|
|
|
emitBlockEndDrag (blocks) {
|
|
|
|
this.emit(Runtime.BLOCK_DRAG_END, blocks);
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Emit value for reporter to show in the blocks.
|
|
|
|
* @param {string} blockId ID for the block.
|
|
|
|
* @param {string} value Value to show associated with the block.
|
|
|
|
*/
|
|
|
|
visualReport (blockId, value) {
|
|
|
|
this.emit(Runtime.VISUAL_REPORT, {id: blockId, value: String(value)});
|
|
|
|
}
|
2016-07-07 19:42:38 -04:00
|
|
|
|
2017-05-09 12:10:11 -04:00
|
|
|
/**
|
2017-05-11 15:23:11 -04:00
|
|
|
* Add a monitor to the state. If the monitor already exists in the state,
|
2018-06-11 08:37:25 -04:00
|
|
|
* updates those properties that are defined in the given monitor record.
|
2017-05-24 15:42:29 -04:00
|
|
|
* @param {!MonitorRecord} monitor Monitor to add.
|
2017-05-09 12:10:11 -04:00
|
|
|
*/
|
2017-05-15 10:12:25 -04:00
|
|
|
requestAddMonitor (monitor) {
|
2018-06-06 15:54:12 -04:00
|
|
|
const id = monitor.get('id');
|
2018-06-07 15:18:45 -04:00
|
|
|
if (!this.requestUpdateMonitor(monitor)) { // update monitor if it exists in the state
|
|
|
|
// if the monitor did not exist in the state, add it
|
2018-06-06 15:54:12 -04:00
|
|
|
this._monitorState = this._monitorState.set(id, monitor);
|
|
|
|
}
|
2017-05-09 12:10:11 -04:00
|
|
|
}
|
|
|
|
|
2017-05-10 15:47:06 -04:00
|
|
|
/**
|
2018-06-11 08:37:25 -04:00
|
|
|
* Update a monitor in the state and report success/failure of update.
|
2017-05-24 15:42:29 -04:00
|
|
|
* @param {!Map} monitor Monitor values to update. Values on the monitor with overwrite
|
|
|
|
* values on the old monitor with the same ID. If a value isn't defined on the new monitor,
|
|
|
|
* the old monitor will keep its old value.
|
2018-06-08 15:59:35 -04:00
|
|
|
* @return {boolean} true if monitor exists in the state and was updated, false if it did not exist.
|
2017-05-10 15:47:06 -04:00
|
|
|
*/
|
2017-05-15 10:12:25 -04:00
|
|
|
requestUpdateMonitor (monitor) {
|
2017-11-13 16:28:50 -05:00
|
|
|
const id = monitor.get('id');
|
|
|
|
if (this._monitorState.has(id)) {
|
2017-05-19 12:28:05 -04:00
|
|
|
this._monitorState =
|
2018-06-07 11:24:55 -04:00
|
|
|
// Use mergeWith here to prevent undefined values from overwriting existing ones
|
2018-06-06 15:54:12 -04:00
|
|
|
this._monitorState.set(id, this._monitorState.get(id).mergeWith((prev, next) => {
|
2018-06-07 11:24:55 -04:00
|
|
|
if (typeof next === 'undefined' || next === null) {
|
2018-06-06 15:54:12 -04:00
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
return next;
|
|
|
|
}, monitor));
|
2018-06-07 15:18:45 -04:00
|
|
|
return true;
|
2017-05-19 12:28:05 -04:00
|
|
|
}
|
2018-06-07 15:18:45 -04:00
|
|
|
return false;
|
2017-05-19 12:28:05 -04:00
|
|
|
}
|
2018-06-11 08:37:25 -04:00
|
|
|
|
2017-05-10 17:00:08 -04:00
|
|
|
/**
|
2017-05-11 15:23:11 -04:00
|
|
|
* Removes a monitor from the state. Does nothing if the monitor already does
|
|
|
|
* not exist in the state.
|
2017-05-19 17:28:00 -04:00
|
|
|
* @param {!string} monitorId ID of the monitor to remove.
|
2017-05-10 17:00:08 -04:00
|
|
|
*/
|
2017-05-15 10:12:25 -04:00
|
|
|
requestRemoveMonitor (monitorId) {
|
2018-06-08 15:59:35 -04:00
|
|
|
this._monitorState = this._monitorState.delete(monitorId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-11 08:37:25 -04:00
|
|
|
* Hides a monitor and returns success/failure of action.
|
|
|
|
* @param {!string} monitorId ID of the monitor to hide.
|
2018-06-08 15:59:35 -04:00
|
|
|
* @return {boolean} true if monitor exists and was updated, false otherwise
|
|
|
|
*/
|
|
|
|
requestHideMonitor (monitorId) {
|
|
|
|
return this.requestUpdateMonitor(new Map([
|
|
|
|
['id', monitorId],
|
|
|
|
['visible', false]
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-11 08:37:25 -04:00
|
|
|
* Shows a monitor and returns success/failure of action.
|
2018-06-08 15:59:35 -04:00
|
|
|
* not exist in the state.
|
2018-06-11 08:37:25 -04:00
|
|
|
* @param {!string} monitorId ID of the monitor to show.
|
2018-06-08 15:59:35 -04:00
|
|
|
* @return {boolean} true if monitor exists and was updated, false otherwise
|
|
|
|
*/
|
|
|
|
requestShowMonitor (monitorId) {
|
|
|
|
return this.requestUpdateMonitor(new Map([
|
|
|
|
['id', monitorId],
|
|
|
|
['visible', true]
|
|
|
|
]));
|
2017-05-10 17:00:08 -04:00
|
|
|
}
|
|
|
|
|
2017-11-14 18:25:54 -05:00
|
|
|
/**
|
|
|
|
* Removes all monitors with the given target ID from the state. Does nothing if
|
|
|
|
* the monitor already does not exist in the state.
|
|
|
|
* @param {!string} targetId Remove all monitors with given target ID.
|
|
|
|
*/
|
|
|
|
requestRemoveMonitorByTargetId (targetId) {
|
|
|
|
this._monitorState = this._monitorState.filterNot(value => value.targetId === targetId);
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get a target by its id.
|
|
|
|
* @param {string} targetId Id of target to find.
|
|
|
|
* @return {?Target} The target, if found.
|
|
|
|
*/
|
|
|
|
getTargetById (targetId) {
|
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
const target = this.targets[i];
|
|
|
|
if (target.id === targetId) {
|
|
|
|
return target;
|
|
|
|
}
|
2016-06-29 13:48:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get the first original (non-clone-block-created) sprite given a name.
|
|
|
|
* @param {string} spriteName Name of sprite to look for.
|
|
|
|
* @return {?Target} Target representing a sprite of the given name.
|
|
|
|
*/
|
|
|
|
getSpriteTargetByName (spriteName) {
|
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
const target = this.targets[i];
|
2018-02-12 15:33:27 -05:00
|
|
|
if (target.isStage) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
if (target.sprite && target.sprite.name === spriteName) {
|
|
|
|
return target;
|
|
|
|
}
|
2016-08-31 12:18:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get a target by its drawable id.
|
|
|
|
* @param {number} drawableID drawable id of target to find
|
|
|
|
* @return {?Target} The target, if found
|
|
|
|
*/
|
|
|
|
getTargetByDrawableId (drawableID) {
|
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
const target = this.targets[i];
|
|
|
|
if (target.drawableID === drawableID) return target;
|
|
|
|
}
|
2017-03-03 09:35:57 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Update the clone counter to track how many clones are created.
|
|
|
|
* @param {number} changeAmount How many clones have been created/destroyed.
|
|
|
|
*/
|
|
|
|
changeCloneCounter (changeAmount) {
|
|
|
|
this._cloneCounter += changeAmount;
|
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Return whether there are clones available.
|
|
|
|
* @return {boolean} True until the number of clones hits Runtime.MAX_CLONES.
|
|
|
|
*/
|
|
|
|
clonesAvailable () {
|
|
|
|
return this._cloneCounter < Runtime.MAX_CLONES;
|
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
|
2017-06-16 17:22:51 -04:00
|
|
|
/**
|
|
|
|
* Report that a new target has been created, possibly by cloning an existing target.
|
|
|
|
* @param {Target} newTarget - the newly created target.
|
|
|
|
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
|
|
|
|
* @fires Runtime#targetWasCreated
|
|
|
|
*/
|
|
|
|
fireTargetWasCreated (newTarget, sourceTarget) {
|
|
|
|
this.emit('targetWasCreated', newTarget, sourceTarget);
|
|
|
|
}
|
|
|
|
|
2017-10-06 13:43:07 -04:00
|
|
|
/**
|
2017-10-11 14:19:15 -04:00
|
|
|
* Report that a clone target is being removed.
|
|
|
|
* @param {Target} target - the target being removed
|
|
|
|
* @fires Runtime#targetWasRemoved
|
2017-10-06 13:43:07 -04:00
|
|
|
*/
|
2017-10-11 14:19:15 -04:00
|
|
|
fireTargetWasRemoved (target) {
|
|
|
|
this.emit('targetWasRemoved', target);
|
2017-10-06 13:43:07 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Get a target representing the Scratch stage, if one exists.
|
|
|
|
* @return {?Target} The target, if found.
|
|
|
|
*/
|
|
|
|
getTargetForStage () {
|
|
|
|
for (let i = 0; i < this.targets.length; i++) {
|
|
|
|
const target = this.targets[i];
|
|
|
|
if (target.isStage) {
|
|
|
|
return target;
|
|
|
|
}
|
2016-09-08 09:40:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 19:15:07 -04:00
|
|
|
/**
|
|
|
|
* Get the editing target.
|
|
|
|
* @return {?Target} The editing target.
|
|
|
|
*/
|
|
|
|
getEditingTarget () {
|
|
|
|
return this._editingTarget;
|
|
|
|
}
|
|
|
|
|
2018-07-03 23:20:49 -04:00
|
|
|
getAllVarNamesOfType (varType) {
|
|
|
|
let varNames = [];
|
|
|
|
for (const target of this.targets) {
|
|
|
|
const targetVarNames = target.getAllVariableNamesInScopeByType(varType, true);
|
|
|
|
varNames = varNames.concat(targetVarNames);
|
|
|
|
}
|
|
|
|
return varNames;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Tell the runtime to request a redraw.
|
|
|
|
* Use after a clone/sprite has completed some visible operation on the stage.
|
|
|
|
*/
|
|
|
|
requestRedraw () {
|
|
|
|
this.redrawRequested = true;
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
|
2017-05-12 11:42:22 -04:00
|
|
|
/**
|
|
|
|
* Emit a targets update at the end of the step if the provided target is
|
|
|
|
* the original sprite
|
|
|
|
* @param {!Target} target Target requesting the targets update
|
|
|
|
*/
|
|
|
|
requestTargetsUpdate (target) {
|
|
|
|
if (!target.isOriginal) return;
|
|
|
|
this._refreshTargets = true;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Set up timers to repeatedly step in a browser.
|
|
|
|
*/
|
|
|
|
start () {
|
|
|
|
let interval = Runtime.THREAD_STEP_INTERVAL;
|
|
|
|
if (this.compatibilityMode) {
|
|
|
|
interval = Runtime.THREAD_STEP_INTERVAL_COMPATIBILITY;
|
|
|
|
}
|
|
|
|
this.currentStepTime = interval;
|
|
|
|
this._steppingInterval = setInterval(() => {
|
|
|
|
this._step();
|
|
|
|
}, interval);
|
|
|
|
}
|
2017-11-09 17:28:40 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn on profiling.
|
|
|
|
* @param {Profiler/FrameCallback} onFrame A callback handle passed a
|
|
|
|
* profiling frame when the profiler reports its collected data.
|
|
|
|
*/
|
|
|
|
enableProfiling (onFrame) {
|
|
|
|
if (Profiler.available()) {
|
|
|
|
this.profiler = new Profiler(onFrame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn off profiling.
|
|
|
|
*/
|
|
|
|
disableProfiling () {
|
|
|
|
this.profiler = null;
|
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2016-04-26 16:50:49 -04:00
|
|
|
|
2017-06-16 17:22:51 -04:00
|
|
|
/**
|
|
|
|
* Event fired after a new target has been created, possibly by cloning an existing target.
|
|
|
|
*
|
|
|
|
* @event Runtime#targetWasCreated
|
|
|
|
* @param {Target} newTarget - the newly created target.
|
|
|
|
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
|
|
|
|
*/
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
module.exports = Runtime;
|