Skip to content

Commit d650e53

Browse files
authored
Merge pull request #439 from griffpatch/bug/StopThisScript
Implement "Stop this script" function
2 parents 45f39c2 + 9cb5953 commit d650e53

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/blocks/scratch3_control.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Scratch3ControlBlocks.prototype.stop = function (args, util) {
110110
option === 'other scripts in stage') {
111111
util.stopOtherTargetThreads();
112112
} else if (option === 'this script') {
113-
util.stopThread();
113+
util.stopThisScript();
114114
}
115115
};
116116

src/engine/execute.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ var execute = function (sequencer, thread) {
183183
stopOtherTargetThreads: function () {
184184
runtime.stopForTarget(target, thread);
185185
},
186-
stopThread: function () {
187-
sequencer.retireThread(thread);
186+
stopThisScript: function () {
187+
thread.stopThisScript();
188188
},
189189
startProcedure: function (procedureCode) {
190190
sequencer.stepToProcedure(thread, procedureCode);

src/engine/thread.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ Thread.prototype.popStack = function () {
140140
return this.stack.pop();
141141
};
142142

143+
/**
144+
* Pop back down the stack frame until we hit a procedure call or the stack frame is emptied
145+
*/
146+
Thread.prototype.stopThisScript = function () {
147+
var blockID = this.peekStack();
148+
while (blockID !== null) {
149+
var block = this.target.blocks.getBlock(blockID);
150+
if (typeof block !== 'undefined' && block.opcode === 'procedures_callnoreturn') {
151+
break;
152+
}
153+
this.popStack();
154+
blockID = this.peekStack();
155+
}
156+
157+
if (this.stack.length === 0) {
158+
// Clean up!
159+
this.requestScriptGlowInFrame = false;
160+
this.status = Thread.STATUS_DONE;
161+
}
162+
};
163+
143164
/**
144165
* Get top stack item.
145166
* @return {?string} Block ID on top of stack.

test/unit/blocks_control.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ test('stop', function (t) {
104104
var state = {
105105
stopAll: 0,
106106
stopOtherTargetThreads: 0,
107-
stopThread: 0
107+
stopThisScript: 0
108108
};
109109
var util = {
110110
stopAll: function () {
@@ -113,8 +113,8 @@ test('stop', function (t) {
113113
stopOtherTargetThreads: function () {
114114
state.stopOtherTargetThreads++;
115115
},
116-
stopThread: function () {
117-
state.stopThread++;
116+
stopThisScript: function () {
117+
state.stopThisScript++;
118118
}
119119
};
120120

@@ -125,6 +125,6 @@ test('stop', function (t) {
125125
c.stop({STOP_OPTION: 'this script'}, util);
126126
t.strictEqual(state.stopAll, 1);
127127
t.strictEqual(state.stopOtherTargetThreads, 2);
128-
t.strictEqual(state.stopThread, 1);
128+
t.strictEqual(state.stopThisScript, 1);
129129
t.end();
130130
});

0 commit comments

Comments
 (0)