From 6ec231db9d419a760a92dbdd347778237f583a58 Mon Sep 17 00:00:00 2001 From: Wang Yu Date: Fri, 12 May 2017 14:01:37 +0800 Subject: [PATCH] Fix the next thread is skipped after clone deleted Problem: When a clone is deleted, its thread is removed from the thread list; but the index "i" in the execution for-loop will still +1, making the next thread skipped. Changes: Added a flag "isKilled" on a thread; Set the flag when a thread is removed from the thread list; reduce the "i" counter if the thread of the current loop is removed. --- src/engine/runtime.js | 1 + src/engine/sequencer.js | 3 +++ src/engine/thread.js | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 34c008a1..7f75f952 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -590,6 +590,7 @@ class Runtime extends EventEmitter { continue; } if (this.threads[i].target === target) { + this.threads[i].isKilled = true; this._removeThread(this.threads[i]); } } diff --git a/src/engine/sequencer.js b/src/engine/sequencer.js index c8051c87..d5c79eae 100644 --- a/src/engine/sequencer.js +++ b/src/engine/sequencer.js @@ -69,6 +69,9 @@ class Sequencer { // Normal-mode thread: step. this.stepThread(activeThread); activeThread.warpTimer = null; + if (activeThread.isKilled) { + i--; // if the thread is removed from the list (killed), do not increase index + } } if (activeThread.status === Thread.STATUS_RUNNING) { numActiveThreads++; diff --git a/src/engine/thread.js b/src/engine/thread.js index 208ff1e9..3b8ce32b 100644 --- a/src/engine/thread.js +++ b/src/engine/thread.js @@ -30,6 +30,12 @@ class Thread { */ this.status = 0; /* Thread.STATUS_RUNNING */ + /** + * Whether the thread is killed in the middle of execution. + * @type {boolean} + */ + this.isKilled = false; + /** * Target of this thread. * @type {?Target}