Skip to content
4 changes: 0 additions & 4 deletions src/engine/runtime-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ const RuntimeEvents = {
*/
AFTER_EXECUTE: 'AFTER_EXECUTE',

/**
* Event name for reporting asset download progress. Fired with finished, total
* @const {string}
*/
ASSET_PROGRESS: 'ASSET_PROGRESS',

/**
Expand Down
40 changes: 27 additions & 13 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ class Runtime extends EventEmitter {
*/
this.turboMode = false;

this.projectName = '';

/**
* tw: Responsible for managing the VM's many timers.
*/
Expand Down Expand Up @@ -1049,7 +1051,7 @@ class Runtime extends EventEmitter {

handleExtensionButtonPress (buttonData) {
const callback = this.extensionButtons.get(buttonData);
callback();
if (callback) callback();
}

/**
Expand Down Expand Up @@ -1085,9 +1087,6 @@ class Runtime extends EventEmitter {
return ScratchBlocksToolbox.getBlocksXML(this._blockInfo, target);
}

/**
* @returns {Array.<string>} - an array containing the scratch-blocks JSON information for each dynamic block.
*/
getBlocksJSON () {
return ScratchBlocksToolbox.getBlocksJSON(this._blockInfo);
}
Expand Down Expand Up @@ -1533,9 +1532,13 @@ class Runtime extends EventEmitter {
// Look up metadata for the relevant hat.
const hatMeta = instance._hats[requestedHatOpcode];

for (const opts in optMatchFields) {
if (!Object.prototype.hasOwnProperty.call(optMatchFields, opts)) continue;
optMatchFields[opts] = optMatchFields[opts].toUpperCase();
let matchFields = null;
if (optMatchFields) {
matchFields = {};
for (const field in optMatchFields) {
if (!Object.prototype.hasOwnProperty.call(optMatchFields, field)) continue;
matchFields[field] = String(optMatchFields[field]).toUpperCase();
}
}

// Consider all scripts, looking for hats with opcode `requestedHatOpcode`.
Expand All @@ -1550,8 +1553,8 @@ class Runtime extends EventEmitter {
// 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.
for (const matchField in optMatchFields) {
if (hatFields[matchField].value !== optMatchFields[matchField]) {
for (const matchField in matchFields) {
if (hatFields[matchField].value !== matchFields[matchField]) {
// Field mismatch.
return;
}
Expand Down Expand Up @@ -2039,6 +2042,15 @@ class Runtime extends EventEmitter {
}
}

setProjectName (name) {
this.projectName = typeof name === 'string' ? name : '';
}

setTurboMode (turboModeOn) {
this.turboMode = !!turboModeOn;
this.emit(this.turboMode ? Runtime.TURBO_MODE_ON : Runtime.TURBO_MODE_OFF);
}

/**
* Set whether we are in 30 TPS compatibility mode.
* @param {boolean} compatibilityModeOn True iff in compatibility mode.
Expand Down Expand Up @@ -2117,10 +2129,13 @@ class Runtime extends EventEmitter {
const offsetX = deltaX / 2;
const offsetY = deltaY / 2;
for (const monitor of this._monitorState.valueSeq()) {
const x = monitor.get('x');
const y = monitor.get('y');
if (typeof x !== 'number' || typeof y !== 'number') continue;
this.requestUpdateMonitor({
id: monitor.id,
x: monitor.get('x') + offsetX,
y: monitor.get('y') + offsetY
x: x + offsetX,
y: y + offsetY
});
}
this.emit(Runtime.MONITORS_UPDATE, this._monitorState.shallowClone());
Expand Down Expand Up @@ -2374,8 +2389,7 @@ class Runtime extends EventEmitter {
this.setFramerate(parsed.framerate);
}
if (parsed.turbo) {
this.turboMode = true;
this.emit(Runtime.TURBO_MODE_ON);
this.setTurboMode(true);
}
if (parsed.interpolation) {
this.setInterpolation(true);
Expand Down
9 changes: 7 additions & 2 deletions src/extension-support/scratch-blocks-toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,13 @@ const getBlocksXML = function (categories, target) {
};

const getBlocksJSON = function (categories) {
return categories.reduce(
(result, categoryInfo) => result.concat(categoryInfo.blocks.map(blockInfo => blockInfo.json)), []);
const result = [];
for (const categoryInfo of categories) {
for (const blockInfo of categoryInfo.blocks) {
if (blockInfo.json) result.push(blockInfo.json);
}
}
return result;
};

module.exports = {
Expand Down
35 changes: 17 additions & 18 deletions src/virtual-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ class VirtualMachine extends EventEmitter {
this.runtime.on(Runtime.EXTENSIONS_REORDERED, info => {
this.emit(Runtime.EXTENSIONS_REORDERED, info);
});
this.runtime.on(Runtime.EXTENSION_FIELD_ADDED, (fieldName, fieldImplementation) => {
this.emit(Runtime.EXTENSION_FIELD_ADDED, fieldName, fieldImplementation);
this.runtime.on(Runtime.EXTENSION_FIELD_ADDED, fieldInfo => {
this.emit(Runtime.EXTENSION_FIELD_ADDED, fieldInfo);
});
this.runtime.on(Runtime.BLOCKSINFO_UPDATE, categoryInfo => {
this.emit(Runtime.BLOCKSINFO_UPDATE, categoryInfo);
Expand All @@ -189,8 +189,8 @@ class VirtualMachine extends EventEmitter {
this.runtime.on(Runtime.PERIPHERAL_CONNECTED, () =>
this.emit(Runtime.PERIPHERAL_CONNECTED)
);
this.runtime.on(Runtime.PERIPHERAL_REQUEST_ERROR, () =>
this.emit(Runtime.PERIPHERAL_REQUEST_ERROR)
this.runtime.on(Runtime.PERIPHERAL_REQUEST_ERROR, data =>
this.emit(Runtime.PERIPHERAL_REQUEST_ERROR, data)
);
this.runtime.on(Runtime.PERIPHERAL_DISCONNECTED, () =>
this.emit(Runtime.PERIPHERAL_DISCONNECTED)
Expand Down Expand Up @@ -231,8 +231,8 @@ class VirtualMachine extends EventEmitter {
this.runtime.on(Runtime.COMPILE_ERROR, (target, error) => {
this.emit(Runtime.COMPILE_ERROR, target, error);
});
this.runtime.on(Runtime.ASSET_PROGRESS, (finished, total) => {
this.emit(Runtime.ASSET_PROGRESS, finished, total);
this.runtime.on(Runtime.ASSET_PROGRESS, (finished, total, detail) => {
this.emit(Runtime.ASSET_PROGRESS, finished, total, detail);
});
this.runtime.on(Runtime.TURBO_MODE_OFF, () => {
this.emit(Runtime.TURBO_MODE_OFF);
Expand Down Expand Up @@ -353,12 +353,7 @@ class VirtualMachine extends EventEmitter {
* @param {boolean} turboModeOn Whether turbo mode should be set.
*/
setTurboMode (turboModeOn) {
this.runtime.turboMode = !!turboModeOn;
if (this.runtime.turboMode) {
this.emit(Runtime.TURBO_MODE_ON);
} else {
this.emit(Runtime.TURBO_MODE_OFF);
}
this.runtime.setTurboMode(turboModeOn);
}

/**
Expand Down Expand Up @@ -394,6 +389,10 @@ class VirtualMachine extends EventEmitter {
this.runtime.setStageSize(width, height);
}

setProjectName (name) {
this.runtime.setProjectName(name);
}

setInEditor (inEditor) {
this.runtime.setInEditor(inEditor);
}
Expand Down Expand Up @@ -1695,7 +1694,7 @@ class VirtualMachine extends EventEmitter {
const allTargets = this.runtime.targets;
for (let i = 0; i < allTargets.length; i++) {
const currTarget = allTargets[i];
currTarget.blocks.updateAssetName(oldName, newName, 'sprite');
currTarget.blocks.updateAssetName(oldName, newUnusedName, 'sprite');
}

if (newUnusedName !== oldName) this.emitTargetsUpdate();
Expand Down Expand Up @@ -2070,12 +2069,11 @@ class VirtualMachine extends EventEmitter {
const originalSound = this.editingTarget.getSounds()[soundIndex];
const clone = Object.assign({}, originalSound);
const target = this.runtime.getTargetById(targetId);
if (!target) return Promise.resolve();
return loadSound(clone, this.runtime, target.sprite.soundBank).then(() => {
if (this._editCommandActive && !this._editCommandActive()) throw new Error('Editing session ended');
if (target) {
target.addSound(clone);
this.emitTargetsUpdate();
}
target.addSound(clone);
this.emitTargetsUpdate();
});
}

Expand Down Expand Up @@ -2489,6 +2487,7 @@ class VirtualMachine extends EventEmitter {
*/
postSpriteInfo (data) {
const target = this._dragTarget || this.editingTarget;
if (!target) return;
target.postSpriteInfo(data);
// Post sprite info means the gui has changed something about a sprite,
// either through the sprite info pane fields (e.g. direction, size) or
Expand All @@ -2504,7 +2503,7 @@ class VirtualMachine extends EventEmitter {
}

// Emit immediate event for collaboration sync if properties changed
if (Object.keys(changedProps).length > 0 && target) {
if (Object.keys(changedProps).length > 0) {
this.runtime.emit(Runtime.SPRITE_INFO_CHANGED, target, changedProps);
}

Expand Down
6 changes: 0 additions & 6 deletions test/fixtures/toolbox/scratch-blocks-toolbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
}
],
"json": [
null,
{
"type": "toolboxtest_command",
"inputsInline": true,
Expand Down Expand Up @@ -81,7 +80,6 @@
"previousStatement": null,
"nextStatement": null
},
null,
{
"type": "toolboxtest_reporter",
"inputsInline": true,
Expand Down Expand Up @@ -478,10 +476,6 @@
"outputShape": 3,
"previousStatement": null
},
null,
null,
null,
null,
{
"type": "plaincolors_say",
"inputsInline": true,
Expand Down
64 changes: 64 additions & 0 deletions test/unit/engine_runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,67 @@ test('Clock is reset on runtime dispose', t => {
t.ok(c.projectTimer() === 0);
t.end();
});

test('handleExtensionButtonPress ignores unknown buttons', t => {
const rt = new Runtime();
t.doesNotThrow(() => rt.handleExtensionButtonPress('test_missing'));
let pressed = 0;
rt.extensionButtons.set('test_button', () => {
pressed++;
});
rt.handleExtensionButtonPress('test_button');
t.equal(pressed, 1);
t.end();
});

const makeHatRuntime = fieldValue => {
const rt = new Runtime();
rt._hats.test_whenNamed = {restartExistingThreads: false};
const target = {id: 'target'};
rt.allScriptsByOpcodeDo = (opcode, f) => {
f({blockId: 'hat', fieldsOfInputs: {NAME: {value: fieldValue}}}, target);
};
rt._pushThread = () => ({isCompiled: true, executableHat: false});
return rt;
};

test('startHats matches without changing the caller match fields', t => {
const rt = makeHatRuntime('HELLO');
const fields = {NAME: 'hello'};
t.equal(rt.startHats('test_whenNamed', fields).length, 1);
t.same(fields, {NAME: 'hello'});
t.end();
});

test('startHats accepts match values that are not strings', t => {
const rt = makeHatRuntime('12');
t.equal(rt.startHats('test_whenNamed', {NAME: 12}).length, 1);
t.equal(rt.startHats('test_whenNamed', {NAME: 13}).length, 0);
t.end();
});

test('setStageSize keeps auto-positioned monitors auto-positioned', t => {
const rt = new Runtime();
rt.requestAddMonitor(new MonitorRecord({id: 'auto'}));
rt.requestAddMonitor(new MonitorRecord({id: 'placed', x: 10, y: 20}));
rt.setStageSize(560, 420);
t.equal(rt._monitorState.get('auto').get('x'), null);
t.equal(rt._monitorState.get('auto').get('y'), null);
t.equal(rt._monitorState.get('placed').get('x'), 50);
t.equal(rt._monitorState.get('placed').get('y'), 50);
t.end();
});

test('getBlocksJSON leaves out entries without JSON', t => {
const rt = new Runtime();
rt._blockInfo = [{
blocks: [
{json: {type: 'test_a'}},
{xml: '<sep gap="36"/>'},
{json: null, xml: '<label text="x"></label>'},
{json: {type: 'test_b'}}
]
}];
t.same(rt.getBlocksJSON(), [{type: 'test_a'}, {type: 'test_b'}]);
t.end();
});
Loading