Merge remote-tracking branch 'refs/remotes/origin/develop' into optimise/reduceGetBlockCalls

This commit is contained in:
griffpatch 2017-02-11 15:39:33 +00:00
commit 75a1542fc3
9 changed files with 311 additions and 32 deletions

8
.gitignore vendored
View file

@ -14,10 +14,4 @@ npm-*
# Build
/dist
/playground/assets
/playground/media
/playground/scratch-vm.js
/playground/scratch-vm.js.map
/playground/vendor.js
/playground/vendor.js.map
/playground/zenburn.css
/playground

View file

@ -135,8 +135,9 @@ Scratch3SoundBlocks.prototype.playNoteForBeats = function (args, util) {
var beats = Cast.toNumber(args.BEATS);
var soundState = this._getSoundState(util.target);
var inst = soundState.currentInstrument;
var vol = soundState.volume;
if (typeof this.runtime.audioEngine === 'undefined') return;
return this.runtime.audioEngine.playNoteForBeatsWithInst(note, beats, inst);
return this.runtime.audioEngine.playNoteForBeatsWithInstAndVol(note, beats, inst, vol);
};
Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) {

View file

@ -56,7 +56,7 @@ Keyboard.prototype._keyCodeToScratchKey = function (keyCode) {
case 39: return 'right arrow';
case 40: return 'down arrow';
}
return null;
return '';
};
/**

View file

@ -1,4 +1,3 @@
var loadProject = function () {
var id = location.hash.substring(1);
if (id.length < 1 || !isFinite(id)) {
@ -7,7 +6,7 @@ var loadProject = function () {
var url = 'https://projects.scratch.mit.edu/internalapi/project/' +
id + '/get/';
var r = new XMLHttpRequest();
r.onreadystatechange = function() {
r.onreadystatechange = function () {
if (this.readyState === 4) {
if (r.status === 200) {
window.vm.loadProject(this.responseText);
@ -18,7 +17,7 @@ var loadProject = function () {
r.send();
};
window.onload = function() {
window.onload = function () {
// Lots of global variables to make debugging easier
// Instantiate the VM.
var vm = new window.VirtualMachine();
@ -74,7 +73,7 @@ window.onload = function() {
// Playground data tabs.
// Block representation tab.
var blockexplorer = document.getElementById('blockexplorer');
var updateBlockExplorer = function(blocks) {
var updateBlockExplorer = function (blocks) {
blockexplorer.innerHTML = JSON.stringify(blocks, null, 2);
window.hljs.highlightBlock(blockexplorer);
};
@ -83,7 +82,7 @@ window.onload = function() {
var threadexplorer = document.getElementById('threadexplorer');
var cachedThreadJSON = '';
var updateThreadExplorer = function (newJSON) {
if (newJSON != cachedThreadJSON) {
if (newJSON !== cachedThreadJSON) {
cachedThreadJSON = newJSON;
threadexplorer.innerHTML = cachedThreadJSON;
window.hljs.highlightBlock(threadexplorer);
@ -101,7 +100,7 @@ window.onload = function() {
// VM handlers.
// Receipt of new playground data (thread, block representations).
vm.on('playgroundData', function(data) {
vm.on('playgroundData', function (data) {
updateThreadExplorer(data.threads);
updateBlockExplorer(data.blocks);
});
@ -125,7 +124,7 @@ window.onload = function() {
var targetOption = document.createElement('option');
targetOption.setAttribute('value', data.targetList[i].id);
// If target id matches editingTarget id, select it.
if (data.targetList[i].id == data.editingTarget) {
if (data.targetList[i].id === data.editingTarget) {
targetOption.setAttribute('selected', 'selected');
}
targetOption.appendChild(
@ -139,23 +138,23 @@ window.onload = function() {
};
// Feedback for stacks and blocks running.
vm.on('SCRIPT_GLOW_ON', function(data) {
vm.on('SCRIPT_GLOW_ON', function (data) {
workspace.glowStack(data.id, true);
});
vm.on('SCRIPT_GLOW_OFF', function(data) {
vm.on('SCRIPT_GLOW_OFF', function (data) {
workspace.glowStack(data.id, false);
});
vm.on('BLOCK_GLOW_ON', function(data) {
vm.on('BLOCK_GLOW_ON', function (data) {
workspace.glowBlock(data.id, true);
});
vm.on('BLOCK_GLOW_OFF', function(data) {
vm.on('BLOCK_GLOW_OFF', function (data) {
workspace.glowBlock(data.id, false);
});
vm.on('VISUAL_REPORT', function(data) {
vm.on('VISUAL_REPORT', function (data) {
workspace.reportValue(data.id, data.value);
});
vm.on('SPRITE_INFO_REPORT', function(data) {
vm.on('SPRITE_INFO_REPORT', function (data) {
if (data.id !== selectedTarget.value) return; // Not the editingTarget
document.getElementById('sinfo-x').value = data.x;
document.getElementById('sinfo-y').value = data.y;
@ -213,7 +212,7 @@ window.onload = function() {
// Feed keyboard events as VM I/O events.
document.addEventListener('keydown', function (e) {
// Don't capture keys intended for Blockly inputs.
if (e.target != document && e.target != document.body) {
if (e.target !== document && e.target !== document.body) {
return;
}
window.vm.postIOData('keyboard', {
@ -222,7 +221,7 @@ window.onload = function() {
});
e.preventDefault();
});
document.addEventListener('keyup', function(e) {
document.addEventListener('keyup', function (e) {
// Always capture up events,
// even those that have switched to other targets.
window.vm.postIOData('keyboard', {
@ -230,7 +229,7 @@ window.onload = function() {
isDown: false
});
// E.g., prevent scroll.
if (e.target != document && e.target != document.body) {
if (e.target !== document && e.target !== document.body) {
e.preventDefault();
}
});
@ -239,25 +238,25 @@ window.onload = function() {
vm.start();
// Inform VM of animation frames.
var animate = function() {
var animate = function () {
stats.update();
requestAnimationFrame(animate);
};
requestAnimationFrame(animate);
// Handlers for green flag and stop all.
document.getElementById('greenflag').addEventListener('click', function() {
document.getElementById('greenflag').addEventListener('click', function () {
vm.greenFlag();
});
document.getElementById('stopall').addEventListener('click', function() {
document.getElementById('stopall').addEventListener('click', function () {
vm.stopAll();
});
document.getElementById('turbomode').addEventListener('change', function() {
document.getElementById('turbomode').addEventListener('change', function () {
var turboOn = document.getElementById('turbomode').checked;
vm.setTurboMode(turboOn);
});
document.getElementById('compatmode').addEventListener('change',
function() {
function () {
var compatibilityMode = document.getElementById('compatmode').checked;
vm.setCompatibilityMode(compatibilityMode);
});

View file

@ -90,6 +90,15 @@ Cast.toRgbColorObject = function (value) {
return color;
};
/**
* Determine if a Scratch argument is a white space string (or null / empty).
* @param {*} val value to check.
* @return {boolean} True if the argument is all white spaces or null / empty.
*/
Cast.isWhiteSpace = function (val) {
return val === null || typeof val === 'string' && val.trim().length === 0;
};
/**
* Compare two values, using Scratch cast, case-insensitive string compare, etc.
* In Scratch 2.0, this is captured by `interp.compare.`
@ -100,6 +109,11 @@ Cast.toRgbColorObject = function (value) {
Cast.compare = function (v1, v2) {
var n1 = Number(v1);
var n2 = Number(v2);
if (n1 === 0 && Cast.isWhiteSpace(v1)) {
n1 = NaN;
} else if (n2 === 0 && Cast.isWhiteSpace(v2)) {
n2 = NaN;
}
if (isNaN(n1) || isNaN(n2)) {
// At least one argument can't be converted to a number.
// Scratch compares strings as case insensitive.

View file

@ -1,8 +1,277 @@
var test = require('tap').test;
var Thread = require('../../src/engine/thread');
var RenderedTarget = require('../../src/sprites/rendered-target');
var Sprite = require('../../src/sprites/sprite');
test('spec', function (t) {
t.type(Thread, 'function');
// @todo
var th = new Thread('arbitraryString');
t.type(th, 'object');
t.ok(th instanceof Thread);
t.type(th.pushStack, 'function');
t.type(th.reuseStackForNextBlock, 'function');
t.type(th.popStack, 'function');
t.type(th.stopThisScript, 'function');
t.type(th.peekStack, 'function');
t.type(th.peekStackFrame, 'function');
t.type(th.peekParentStackFrame, 'function');
t.type(th.pushReportedValue, 'function');
t.type(th.pushParam, 'function');
t.type(th.peekStack, 'function');
t.type(th.getParam, 'function');
t.type(th.atStackTop, 'function');
t.type(th.goToNextBlock, 'function');
t.type(th.isRecursiveCall, 'function');
t.end();
});
test('pushStack', function (t) {
var th = new Thread('arbitraryString');
th.pushStack('arbitraryString');
t.end();
});
test('popStack', function (t) {
var th = new Thread('arbitraryString');
th.pushStack('arbitraryString');
t.strictEquals(th.popStack(), 'arbitraryString');
t.strictEquals(th.popStack(), undefined);
t.end();
});
test('atStackTop', function (t) {
var th = new Thread('arbitraryString');
th.pushStack('arbitraryString');
th.pushStack('secondString');
t.strictEquals(th.atStackTop(), false);
th.popStack();
t.strictEquals(th.atStackTop(), true);
t.end();
});
test('reuseStackForNextBlock', function (t) {
var th = new Thread('arbitraryString');
th.pushStack('arbitraryString');
th.reuseStackForNextBlock('secondString');
t.strictEquals(th.popStack(), 'secondString');
t.end();
});
test('peekStackFrame', function (t) {
var th = new Thread('arbitraryString');
th.pushStack('arbitraryString');
t.strictEquals(th.peekStackFrame().warpMode, false);
th.popStack();
t.strictEquals(th.peekStackFrame(), null);
t.end();
});
test('peekParentStackFrame', function (t) {
var th = new Thread('arbitraryString');
th.pushStack('arbitraryString');
th.peekStackFrame().warpMode = true;
t.strictEquals(th.peekParentStackFrame(), null);
th.pushStack('secondString');
t.strictEquals(th.peekParentStackFrame().warpMode, true);
t.end();
});
test('pushReportedValue', function (t) {
var th = new Thread('arbitraryString');
th.pushStack('arbitraryString');
th.pushStack('secondString');
th.pushReportedValue('value');
t.strictEquals(th.peekParentStackFrame().reported.null, 'value');
t.end();
});
test('peekStack', function (t) {
var th = new Thread('arbitraryString');
th.pushStack('arbitraryString');
t.strictEquals(th.peekStack(), 'arbitraryString');
th.popStack();
t.strictEquals(th.peekStack(), null);
t.end();
});
test('PushGetParam', function (t) {
var th = new Thread('arbitraryString');
th.pushStack('arbitraryString');
th.pushParam('testParam', 'testValue');
t.strictEquals(th.peekStackFrame().params.testParam, 'testValue');
t.strictEquals(th.getParam('testParam'), 'testValue');
t.end();
});
test('goToNextBlock', function (t) {
var th = new Thread('arbitraryString');
var s = new Sprite();
var rt = new RenderedTarget(s, null);
var block1 = {fields: Object,
id: 'arbitraryString',
inputs: Object,
STEPS: Object,
block: 'fakeBlock',
name: 'STEPS',
next: 'secondString',
opcode: 'motion_movesteps',
parent: null,
shadow: false,
topLevel: true,
x: 0,
y: 0
};
var block2 = {fields: Object,
id: 'secondString',
inputs: Object,
STEPS: Object,
block: 'fakeBlock',
name: 'STEPS',
next: null,
opcode: 'procedures_callnoreturn',
mutation: {proccode: 'fakeCode'},
parent: null,
shadow: false,
topLevel: true,
x: 0,
y: 0
};
rt.blocks.createBlock(block1);
rt.blocks.createBlock(block2);
rt.blocks.createBlock(block2);
th.target = rt;
t.strictEquals(th.peekStack(), null);
th.pushStack('secondString');
t.strictEquals(th.peekStack(), 'secondString');
th.goToNextBlock();
t.strictEquals(th.peekStack(), null);
th.pushStack('secondString');
th.pushStack('arbitraryString');
t.strictEquals(th.peekStack(), 'arbitraryString');
th.goToNextBlock();
t.strictEquals(th.peekStack(), 'secondString');
th.goToNextBlock();
t.strictEquals(th.peekStack(), null);
t.end();
});
test('stopThisScript', function (t) {
var th = new Thread('arbitraryString');
var s = new Sprite();
var rt = new RenderedTarget(s, null);
var block1 = {fields: Object,
id: 'arbitraryString',
inputs: Object,
STEPS: Object,
block: 'fakeBlock',
name: 'STEPS',
next: null,
opcode: 'motion_movesteps',
parent: null,
shadow: false,
topLevel: true,
x: 0,
y: 0
};
var block2 = {fields: Object,
id: 'secondString',
inputs: Object,
STEPS: Object,
block: 'fakeBlock',
name: 'STEPS',
next: null,
opcode: 'procedures_callnoreturn',
mutation: {proccode: 'fakeCode'},
parent: null,
shadow: false,
topLevel: true,
x: 0,
y: 0
};
rt.blocks.createBlock(block1);
rt.blocks.createBlock(block2);
th.target = rt;
th.stopThisScript();
t.strictEquals(th.peekStack(), null);
th.pushStack('arbitraryString');
t.strictEquals(th.peekStack(), 'arbitraryString');
th.stopThisScript();
t.strictEquals(th.peekStack(), null);
th.pushStack('arbitraryString');
th.pushStack('secondString');
th.stopThisScript();
t.strictEquals(th.peekStack(), 'secondString');
t.end();
});
test('isRecursiveCall', function (t) {
var th = new Thread('arbitraryString');
var s = new Sprite();
var rt = new RenderedTarget(s, null);
var block1 = {fields: Object,
id: 'arbitraryString',
inputs: Object,
STEPS: Object,
block: 'fakeBlock',
name: 'STEPS',
next: null,
opcode: 'motion_movesteps',
parent: null,
shadow: false,
topLevel: true,
x: 0,
y: 0
};
var block2 = {fields: Object,
id: 'secondString',
inputs: Object,
STEPS: Object,
block: 'fakeBlock',
name: 'STEPS',
next: null,
opcode: 'procedures_callnoreturn',
mutation: {proccode: 'fakeCode'},
parent: null,
shadow: false,
topLevel: true,
x: 0,
y: 0
};
rt.blocks.createBlock(block1);
rt.blocks.createBlock(block2);
th.target = rt;
t.strictEquals(th.isRecursiveCall('fakeCode'), false);
th.pushStack('secondString');
t.strictEquals(th.isRecursiveCall('fakeCode'), false);
th.pushStack('arbitraryString');
t.strictEquals(th.isRecursiveCall('fakeCode'), true);
th.pushStack('arbitraryString');
t.strictEquals(th.isRecursiveCall('fakeCode'), true);
th.popStack();
t.strictEquals(th.isRecursiveCall('fakeCode'), true);
th.popStack();
t.strictEquals(th.isRecursiveCall('fakeCode'), false);
th.popStack();
t.strictEquals(th.isRecursiveCall('fakeCode'), false);
t.end();
});

View file

@ -5,7 +5,7 @@ var webpack = require('webpack');
var base = {
devServer: {
contentBase: path.resolve(__dirname, 'playground'),
contentBase: false,
host: '0.0.0.0',
port: process.env.PORT || 8073
},
@ -108,6 +108,8 @@ module.exports = [
to: 'media'
}, {
from: 'node_modules/highlightjs/styles/zenburn.css'
}, {
from: 'src/playground'
}])
])
})