From 0bf9ab59a18e12bf150f7a95d06c1d3553b7cd2f Mon Sep 17 00:00:00 2001 From: Tim Mickel Date: Fri, 10 Jun 2016 10:40:15 -0400 Subject: [PATCH] Add implementation for `ifElse` --- src/blocks/scratch3_control.js | 14 ++++++++++++++ src/engine/execute.js | 4 ++-- src/engine/sequencer.js | 13 +++++++++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/blocks/scratch3_control.js b/src/blocks/scratch3_control.js index 9c09e3684..1d05a8c7a 100644 --- a/src/blocks/scratch3_control.js +++ b/src/blocks/scratch3_control.js @@ -16,6 +16,7 @@ Scratch3ControlBlocks.prototype.getPrimitives = function() { 'control_forever': this.forever, 'control_wait': this.wait, 'control_if': this.if, + 'control_if_else': this.ifElse, 'control_stop': this.stop }; }; @@ -55,6 +56,19 @@ Scratch3ControlBlocks.prototype.if = function(args, util) { } }; +Scratch3ControlBlocks.prototype.ifElse = function(args, util) { + // Only execute one time. `ifElse` will be returned to + // when the substack finishes, but it shouldn't execute again. + if (util.stackFrame.executed === undefined) { + util.stackFrame.executed = true; + if (args.CONDITION) { + util.startSubstack(1); + } else { + util.startSubstack(2); + } + } +}; + Scratch3ControlBlocks.prototype.stop = function() { // @todo - don't use this.runtime this.runtime.stopAll(); diff --git a/src/engine/execute.js b/src/engine/execute.js index e8ca1417e..5ed51cb62 100644 --- a/src/engine/execute.js +++ b/src/engine/execute.js @@ -69,8 +69,8 @@ var execute = function (sequencer, thread) { }, timeout: YieldTimers.timeout, stackFrame: currentStackFrame, - startSubstack: function () { - sequencer.stepToSubstack(thread); + startSubstack: function (substackNum) { + sequencer.stepToSubstack(thread, substackNum); } }); } diff --git a/src/engine/sequencer.js b/src/engine/sequencer.js index 1593c1904..6659f12d8 100644 --- a/src/engine/sequencer.js +++ b/src/engine/sequencer.js @@ -104,11 +104,17 @@ Sequencer.prototype.startThread = function (thread) { /** * Step a thread into a block's substack. * @param {!Thread} thread Thread object to step to substack. - * @param {string} currentBlockId Block which owns a substack to step to. + * @param {Number} substackNum Which substack to step to (i.e., 1, 2). */ -Sequencer.prototype.stepToSubstack = function (thread) { +Sequencer.prototype.stepToSubstack = function (thread, substackNum) { + if (!substackNum) { + substackNum = 1; + } var currentBlockId = thread.peekStack(); - var substackId = this.runtime.blocks.getSubstack(currentBlockId); + var substackId = this.runtime.blocks.getSubstack( + currentBlockId, + substackNum + ); if (substackId) { // Push substack ID to the thread's stack. thread.pushStack(substackId); @@ -121,7 +127,6 @@ Sequencer.prototype.stepToSubstack = function (thread) { /** * Finish stepping a thread and proceed it to the next block. * @param {!Thread} thread Thread object to proceed. - * @param {string} currentBlockId Block we are finished with. */ Sequencer.prototype.proceedThread = function (thread) { var currentBlockId = thread.peekStack();