Update naming and implementation for custom procedure blocks

This commit is contained in:
Paul Kaplan 2017-11-16 14:17:08 -05:00
parent ba5b3093f6
commit 34dfbb50ba
7 changed files with 36 additions and 28 deletions

View file

@ -13,17 +13,18 @@ class Scratch3ProcedureBlocks {
*/ */
getPrimitives () { getPrimitives () {
return { return {
procedures_defnoreturn: this.defNoReturn, procedures_definition: this.definition,
procedures_callnoreturn: this.callNoReturn, procedures_call: this.call,
procedures_param: this.param argument_reporter_string_number: this.param,
argument_reporter_boolean: this.param
}; };
} }
defNoReturn () { definition () {
// No-op: execute the blocks. // No-op: execute the blocks.
} }
callNoReturn (args, util) { call (args, util) {
if (!util.stackFrame.executed) { if (!util.stackFrame.executed) {
const procedureCode = args.mutation.proccode; const procedureCode = args.mutation.proccode;
const paramNames = util.getProcedureParamNames(procedureCode); const paramNames = util.getProcedureParamNames(procedureCode);
@ -47,7 +48,7 @@ class Scratch3ProcedureBlocks {
} }
param (args, util) { param (args, util) {
const value = util.getParam(args.mutation.paramname); const value = util.getParam(args.VALUE);
return value; return value;
} }
} }

View file

@ -152,8 +152,7 @@ class Blocks {
for (const id in this._blocks) { for (const id in this._blocks) {
if (!this._blocks.hasOwnProperty(id)) continue; if (!this._blocks.hasOwnProperty(id)) continue;
const block = this._blocks[id]; const block = this._blocks[id];
if (block.opcode === 'procedures_defnoreturn' || if (block.opcode === 'procedures_definition') {
block.opcode === 'procedures_defreturn') {
const internal = this._getCustomBlockInternal(block); const internal = this._getCustomBlockInternal(block);
if (internal && internal.mutation.proccode === name) { if (internal && internal.mutation.proccode === name) {
return id; // The outer define block id return id; // The outer define block id
@ -172,7 +171,7 @@ class Blocks {
for (const id in this._blocks) { for (const id in this._blocks) {
if (!this._blocks.hasOwnProperty(id)) continue; if (!this._blocks.hasOwnProperty(id)) continue;
const block = this._blocks[id]; const block = this._blocks[id];
if (block.opcode === 'procedures_callnoreturn_internal' && if (block.opcode === 'procedures_prototype' &&
block.mutation.proccode === name) { block.mutation.proccode === name) {
return JSON.parse(block.mutation.argumentnames); return JSON.parse(block.mutation.argumentnames);
} }

View file

@ -164,7 +164,7 @@ class Thread {
let blockID = this.peekStack(); let blockID = this.peekStack();
while (blockID !== null) { while (blockID !== null) {
const block = this.target.blocks.getBlock(blockID); const block = this.target.blocks.getBlock(blockID);
if (typeof block !== 'undefined' && block.opcode === 'procedures_callnoreturn') { if (typeof block !== 'undefined' && block.opcode === 'procedures_call') {
break; break;
} }
this.popStack(); this.popStack();
@ -271,7 +271,7 @@ class Thread {
const sp = this.stack.length - 1; const sp = this.stack.length - 1;
for (let i = sp - 1; i >= 0; i--) { for (let i = sp - 1; i >= 0; i--) {
const block = this.target.blocks.getBlock(this.stack[i]); const block = this.target.blocks.getBlock(this.stack[i]);
if (block.opcode === 'procedures_callnoreturn' && if (block.opcode === 'procedures_call' &&
block.mutation.proccode === procedureCode) { block.mutation.proccode === procedureCode) {
return true; return true;
} }

View file

@ -518,7 +518,7 @@ const parseBlock = function (sb2block, getVariableId, extensions) {
}; };
activeBlock.children = [{ activeBlock.children = [{
id: inputUid, id: inputUid,
opcode: 'procedures_callnoreturn_internal', opcode: 'procedures_prototype',
inputs: {}, inputs: {},
fields: {}, fields: {},
next: null, next: null,
@ -544,13 +544,15 @@ const parseBlock = function (sb2block, getVariableId, extensions) {
argumentids: JSON.stringify(parseProcedureArgIds(sb2block[1])) argumentids: JSON.stringify(parseProcedureArgIds(sb2block[1]))
}; };
} else if (oldOpcode === 'getParam') { } else if (oldOpcode === 'getParam') {
// Mutation for procedure parameter. // Assign correct opcode based on the block shape.
activeBlock.mutation = { switch (sb2block[2]) {
tagName: 'mutation', case 'r':
children: [], activeBlock.opcode = 'argument_reporter_string_number';
paramname: sb2block[1], // Name of parameter. break;
shape: sb2block[2] // Shape - in 2.0, 'r' or 'b'. case 'b':
}; activeBlock.opcode = 'argument_reporter_boolean';
break;
}
} }
return activeBlock; return activeBlock;
}; };

View file

@ -1382,15 +1382,21 @@ const specMap = {
] ]
}, },
'procDef': { 'procDef': {
opcode: 'procedures_defnoreturn', opcode: 'procedures_definition',
argMap: [] argMap: []
}, },
'getParam': { 'getParam': {
opcode: 'procedures_param', // Doesn't map to single opcode. Import step assigns final correct opcode.
argMap: [] opcode: 'argument_reporter_string_number',
argMap: [
{
type: 'field',
fieldName: 'VALUE'
}
]
}, },
'call': { 'call': {
opcode: 'procedures_callnoreturn', opcode: 'procedures_call',
argMap: [] argMap: []
} }
}; };

View file

@ -36,7 +36,7 @@ const generateBlock = function (id) {
block: 'fakeBlock', block: 'fakeBlock',
name: 'fakeName', name: 'fakeName',
next: null, next: null,
opcode: 'procedures_defnoreturn', opcode: 'procedures_definition',
mutation: {proccode: 'fakeCode'}, mutation: {proccode: 'fakeCode'},
parent: null, parent: null,
shadow: false, shadow: false,
@ -55,7 +55,7 @@ const generateBlockInput = function (id, next, inp) {
block: 'fakeBlock', block: 'fakeBlock',
name: 'fakeName', name: 'fakeName',
next: next, next: next,
opcode: 'procedures_defnoreturn', opcode: 'procedures_definition',
mutation: {proccode: 'fakeCode'}, mutation: {proccode: 'fakeCode'},
parent: null, parent: null,
shadow: false, shadow: false,
@ -156,7 +156,7 @@ test('stepToProcedure', t => {
th.target.blocks.createBlock({ th.target.blocks.createBlock({
id: 'internalId', id: 'internalId',
opcode: 'procedures_callnoreturn_internal', opcode: 'procedures_prototype',
mutation: { mutation: {
proccode: 'othercode' proccode: 'othercode'
} }

View file

@ -194,7 +194,7 @@ test('stopThisScript', t => {
block: 'fakeBlock', block: 'fakeBlock',
name: 'STEPS', name: 'STEPS',
next: null, next: null,
opcode: 'procedures_callnoreturn', opcode: 'procedures_call',
mutation: {proccode: 'fakeCode'}, mutation: {proccode: 'fakeCode'},
parent: null, parent: null,
shadow: false, shadow: false,
@ -246,7 +246,7 @@ test('isRecursiveCall', t => {
block: 'fakeBlock', block: 'fakeBlock',
name: 'STEPS', name: 'STEPS',
next: null, next: null,
opcode: 'procedures_callnoreturn', opcode: 'procedures_call',
mutation: {proccode: 'fakeCode'}, mutation: {proccode: 'fakeCode'},
parent: null, parent: null,
shadow: false, shadow: false,