diff --git a/src/engine/blocks.js b/src/engine/blocks.js index 5b74cf55b..f25827b39 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -152,10 +152,12 @@ class Blocks { for (const id in this._blocks) { if (!this._blocks.hasOwnProperty(id)) continue; const block = this._blocks[id]; - if ((block.opcode === 'procedures_defnoreturn' || - block.opcode === 'procedures_defreturn') && - this._blocks[block.inputs.custom_block.block].mutation.proccode === name) { - return id; + if (block.opcode === 'procedures_defnoreturn' || + block.opcode === 'procedures_defreturn') { + const internal = this._getCustomBlockInternal(block); + if (internal && internal.mutation.proccode === name) { + return id; // The outer define block id + } } } return null; @@ -546,6 +548,17 @@ class Blocks { return params; } + /** + * Helper to get the corresponding internal procedure definition block + * @param {!object} defineBlock Outer define block. + * @return {!object} internal definition block which has the mutation. + */ + _getCustomBlockInternal (defineBlock) { + if (defineBlock.inputs && defineBlock.inputs.custom_block) { + return this._blocks[defineBlock.inputs.custom_block.block]; + } + } + /** * Helper to add a stack to `this._scripts`. * @param {?string} topBlockId ID of block that starts the script. diff --git a/test/unit/engine_sequencer.js b/test/unit/engine_sequencer.js index e5dcd0be7..76323e67c 100644 --- a/test/unit/engine_sequencer.js +++ b/test/unit/engine_sequencer.js @@ -153,9 +153,19 @@ test('stepToProcedure', t => { t.strictEquals(th.peekStack(), expectedBlock); s.stepToProcedure(th, 'faceCode'); t.strictEquals(th.peekStack(), expectedBlock); - s.stepToProcedure(th, 'faceCode'); - th.target.blocks.getBlock(th.stack[th.stack.length - 4]).mutation.proccode = 'othercode'; + + th.target.blocks.createBlock({ + id: 'internalId', + opcode: 'procedures_defnoreturn_internal', + mutation: { + proccode: 'othercode' + } + }); expectedBlock = th.stack[th.stack.length - 4]; + th.target.blocks.getBlock(expectedBlock).inputs.custom_block = { + type: 'custom_block', + block: 'internalId' + }; s.stepToProcedure(th, 'othercode'); t.strictEquals(th.peekStack(), expectedBlock);