diff --git a/src/compiler/blocks/compiler_scratch3_control.js b/src/compiler/blocks/compiler_scratch3_control.js index a82ba43f..0ef3e167 100644 --- a/src/compiler/blocks/compiler_scratch3_control.js +++ b/src/compiler/blocks/compiler_scratch3_control.js @@ -111,18 +111,23 @@ const deleteClone = /** @param {StatementUtil} util */ (util) => { util.writeLn(`if (!target.isOriginal) {`) util.writeLn(` runtime.disposeTarget(target);`); util.writeLn(` runtime.stopForTarget(target);`); - util.writeLn(` return;`); + util.retire(); util.writeLn(`}`); }; const stop = /** @param {StatementUtil} util */ (util) => { const STOP_OPTION = util.fieldValueUnsafe('STOP_OPTION'); if (STOP_OPTION === 'all') { - util.writeLn('target.runtime.stopAll();'); + util.writeLn('runtime.stopAll();'); + util.retire(); } else if (STOP_OPTION === 'other scripts in sprite' || STOP_OPTION === 'other scripts in stage') { - util.writeLn('target.runtime.stopForTarget(target, thread);'); + util.writeLn('runtime.stopForTarget(target, thread);'); } else if (STOP_OPTION === 'this script') { - util.writeLn('endCall(); return;'); + if (util.hints.isProcedure) { + util.writeLn('endCall(); return;'); + } else { + util.retire(); + } } }; diff --git a/src/compiler/compiler.js b/src/compiler/compiler.js index 3e347df8..e69bcc27 100644 --- a/src/compiler/compiler.js +++ b/src/compiler/compiler.js @@ -229,6 +229,13 @@ class StatementUtil extends BlockUtil { this.writeLn('/* no-op */'); } + /** + * Stop this thread. + */ + retire() { + this.writeLn('retire(); yield;'); + } + /** * Get a local variable. */ @@ -471,15 +478,19 @@ class Compiler { // Increase warp level if (hints.isWarp) { script += 'thread.warp++;\n'; - } else { + } else if (hints.isProcedure) { script += 'if (thread.warp) thread.warp++;\n'; } script += this.compileStack(topBlock); - // Decrease warp level - script += 'endCall();'; - script += '\n}'; + if (hints.isProcedure) { + script += 'endCall();\n'; + } else { + script += 'retire();\n'; + } + + script += '}'; const fn = execute.createScriptFactory(script); log.info(`[${this.target.getName()}] compiled script`, script);