Skip to content

Commit

Permalink
Complete implementation of stop block (#271)
Browse files Browse the repository at this point in the history
* "Other scripts in stage" sb2

* Complete implementation of "stop" block
  • Loading branch information
tmickel committed Oct 14, 2016
1 parent c45b420 commit 3bfd755
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/blocks/scratch3_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,16 @@ Scratch3ControlBlocks.prototype.ifElse = function(args, util) {
}
};

Scratch3ControlBlocks.prototype.stop = function() {
// @todo - don't use this.runtime
this.runtime.stopAll();
Scratch3ControlBlocks.prototype.stop = function(args, util) {
var option = args.STOP_OPTION;
if (option == 'all') {
util.stopAll();
} else if (option == 'other scripts in sprite' ||
option == 'other scripts in stage') {
util.stopOtherTargetThreads();
} else if (option == 'this script') {
util.stopThread();
}
};

// @todo (GH-146): remove.
Expand Down
9 changes: 9 additions & 0 deletions src/engine/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ var execute = function (sequencer, thread) {
startBranch: function (branchNum) {
sequencer.stepToBranch(thread, branchNum);
},
stopAll: function () {
runtime.stopAll();
},
stopOtherTargetThreads: function() {
runtime.stopForTarget(target, thread);
},
stopThread: function() {
sequencer.retireThread(thread);
},
startProcedure: function (procedureName) {
sequencer.stepToProcedure(thread, procedureName);
},
Expand Down
9 changes: 8 additions & 1 deletion src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ Runtime.prototype._pushThread = function (id, target) {
* @param {?Thread} thread Thread object to remove from actives
*/
Runtime.prototype._removeThread = function (thread) {
// Inform sequencer to stop executing that thread.
this.sequencer.retireThread(thread);
// Remove from the list.
var i = this.threads.indexOf(thread);
if (i > -1) {
this.threads.splice(i, 1);
Expand Down Expand Up @@ -382,10 +385,14 @@ Runtime.prototype.disposeTarget = function (target) {
/**
* Stop any threads acting on the target.
* @param {!Target} target Target to stop threads for.
* @param {Thread=} opt_threadException Optional thread to skip.
*/
Runtime.prototype.stopForTarget = function (target) {
Runtime.prototype.stopForTarget = function (target, opt_threadException) {
// Stop any threads on the target.
for (var i = 0; i < this.threads.length; i++) {
if (this.threads[i] === opt_threadException) {
continue;
}
if (this.threads[i].target == target) {
this._removeThread(this.threads[i]);
}
Expand Down
3 changes: 2 additions & 1 deletion src/import/sb2import.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ function parseBlock (sb2block) {
if (oldOpcode == 'stopScripts') {
// Mutation for stop block: if the argument is 'other scripts',
// the block needs a next connection.
if (sb2block[1] == 'other scripts in sprite') {
if (sb2block[1] == 'other scripts in sprite' ||
sb2block[1] == 'other scripts in stage') {
activeBlock.mutation = {
tagName: 'mutation',
hasnext: 'true',
Expand Down

0 comments on commit 3bfd755

Please sign in to comment.