Fix pen tests

This commit is contained in:
Christopher Willis-Ford 2017-10-04 15:40:25 -07:00
parent dd20e09774
commit b6727a766f
4 changed files with 47 additions and 20 deletions

View file

@ -91,6 +91,16 @@ class SharedDispatch {
} }
} }
/**
* Check if a particular service lives on another worker.
* @param {string} service - the service to check.
* @returns {boolean} - true if the service is remote (calls must cross a Worker boundary), false otherwise.
* @private
*/
_isRemoteService (service) {
return this._getServiceProvider(service).isRemote;
}
/** /**
* Like {@link call}, but force the call to be posted through a particular communication channel. * Like {@link call}, but force the call to be posted through a particular communication channel.
* @param {object} provider - send the call through this object's `postMessage` function. * @param {object} provider - send the call through this object's `postMessage` function.

View file

@ -198,7 +198,17 @@ class ExtensionManager {
blockInfo.opcode = this._sanitizeID(blockInfo.opcode); blockInfo.opcode = this._sanitizeID(blockInfo.opcode);
blockInfo.func = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode; blockInfo.func = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode;
blockInfo.text = blockInfo.text || blockInfo.opcode; blockInfo.text = blockInfo.text || blockInfo.opcode;
blockInfo.func = dispatch.call.bind(dispatch, serviceName, blockInfo.func);
/**
* This is only here because the VM performs poorly when blocks return promises.
* @TODO make it possible for the VM to resolve a promise and continue during the same frame.
*/
if (dispatch._isRemoteService(serviceName)) {
blockInfo.func = dispatch.call.bind(dispatch, serviceName, blockInfo.func);
} else {
const serviceObject = dispatch.services[serviceName];
blockInfo.func = serviceObject[blockInfo.func].bind(serviceObject);
}
return blockInfo; return blockInfo;
} }
} }

View file

@ -500,27 +500,27 @@ const specMap = {
] ]
}, },
'clearPenTrails': { 'clearPenTrails': {
opcode: 'pen_clear', opcode: 'pen.clear',
argMap: [ argMap: [
] ]
}, },
'stampCostume': { 'stampCostume': {
opcode: 'pen_stamp', opcode: 'pen.stamp',
argMap: [ argMap: [
] ]
}, },
'putPenDown': { 'putPenDown': {
opcode: 'pen_pendown', opcode: 'pen.penDown',
argMap: [ argMap: [
] ]
}, },
'putPenUp': { 'putPenUp': {
opcode: 'pen_penup', opcode: 'pen.penUp',
argMap: [ argMap: [
] ]
}, },
'penColor:': { 'penColor:': {
opcode: 'pen_setpencolortocolor', opcode: 'pen.setPenColorToColor',
argMap: [ argMap: [
{ {
type: 'input', type: 'input',
@ -530,7 +530,7 @@ const specMap = {
] ]
}, },
'changePenHueBy:': { 'changePenHueBy:': {
opcode: 'pen_changepencolorby', opcode: 'pen.changePenHueBy',
argMap: [ argMap: [
{ {
type: 'input', type: 'input',
@ -540,7 +540,7 @@ const specMap = {
] ]
}, },
'setPenHueTo:': { 'setPenHueTo:': {
opcode: 'pen_setpencolortonum', opcode: 'pen.setPenHueToNumber',
argMap: [ argMap: [
{ {
type: 'input', type: 'input',
@ -550,7 +550,7 @@ const specMap = {
] ]
}, },
'changePenShadeBy:': { 'changePenShadeBy:': {
opcode: 'pen_changepenshadeby', opcode: 'pen.changePenShadeBy',
argMap: [ argMap: [
{ {
type: 'input', type: 'input',
@ -560,7 +560,7 @@ const specMap = {
] ]
}, },
'setPenShadeTo:': { 'setPenShadeTo:': {
opcode: 'pen_setpenshadeto', opcode: 'pen.setPenShadeToNumber',
argMap: [ argMap: [
{ {
type: 'input', type: 'input',
@ -570,7 +570,7 @@ const specMap = {
] ]
}, },
'changePenSizeBy:': { 'changePenSizeBy:': {
opcode: 'pen_changepensizeby', opcode: 'pen.changePenSizeBy',
argMap: [ argMap: [
{ {
type: 'input', type: 'input',
@ -580,7 +580,7 @@ const specMap = {
] ]
}, },
'penSize:': { 'penSize:': {
opcode: 'pen_setpensizeto', opcode: 'pen.setPenSizeTo',
argMap: [ argMap: [
{ {
type: 'input', type: 'input',

View file

@ -1,8 +1,10 @@
const Worker = require('tiny-worker');
const path = require('path'); const path = require('path');
const test = require('tap').test; const test = require('tap').test;
const Scratch3PenBlocks = require('../../src/blocks/scratch3_pen'); const Scratch3PenBlocks = require('../../src/blocks/scratch3_pen');
const VirtualMachine = require('../../src/index'); const VirtualMachine = require('../../src/index');
const dispatch = require('../../src/dispatch/central-dispatch');
const makeTestStorage = require('../fixtures/make-test-storage'); const makeTestStorage = require('../fixtures/make-test-storage');
const extract = require('../fixtures/extract'); const extract = require('../fixtures/extract');
@ -10,6 +12,9 @@ const extract = require('../fixtures/extract');
const uri = path.resolve(__dirname, '../fixtures/pen.sb2'); const uri = path.resolve(__dirname, '../fixtures/pen.sb2');
const project = extract(uri); const project = extract(uri);
// By default Central Dispatch works with the Worker class built into the browser. Tell it to use TinyWorker instead.
dispatch.workerClass = Worker;
test('pen', t => { test('pen', t => {
const vm = new VirtualMachine(); const vm = new VirtualMachine();
vm.attachStorage(makeTestStorage()); vm.attachStorage(makeTestStorage());
@ -42,14 +47,16 @@ test('pen', t => {
vm.clear(); vm.clear();
vm.setCompatibilityMode(false); vm.setCompatibilityMode(false);
vm.setTurboMode(false); vm.setTurboMode(false);
vm.loadProject(project).then(() => { vm.loadProject(project)
vm.greenFlag(); .then(() => vm.extensionManager.loadExtensionURL('pen')) /** @TODO: loadProject should load extensions */
.then(() => {
vm.greenFlag();
// After two seconds, get playground data and stop // After two seconds, get playground data and stop
setTimeout(() => { setTimeout(() => {
vm.getPlaygroundData(); vm.getPlaygroundData();
vm.stopAll(); vm.stopAll();
}, 2000); }, 2000);
}); });
}); });
}); });