@@ -60,6 +60,11 @@ function debugModule(module, runtime) {
6060 };
6161
6262 function wrapRunDelayed(func, delayMs) {
63+ if (programPaused) {
64+ // Don't push timers and such to the callback stack while we're paused.
65+ // It causes too many callbacks to be fired during unpausing.
66+ return 0;
67+ }
6368 var cbObj = { func:func, delayMs:delayMs, timerId:0, executed:false };
6469 var timerId = setTimeout(function() {
6570 cbObj.executed = true;
@@ -103,7 +108,7 @@ function debugModule(module, runtime) {
103108 recordedEvents = events.slice();
104109 }
105110
106− function setPaused(v) {
111+ function setPausedAt(v, position) {
107112 programPaused = v;
108113 if (programPaused) {
109114 clearAsyncCallbacks();
@@ -111,7 +116,12 @@ function debugModule(module, runtime) {
111116 }
112117 else {
113118 // executeCallbacks should only be called when continuing, not when restarting.
114− //executeCallbacks(asyncCallbacks, false);
119+ if(position > 0) {
120+ // If we're unpausing not at the head, then we need to dump the
121+ // events that are ahead of where we're continuing.
122+ recordedEvents = recordedEvents.slice(0,position);
123+ executeCallbacks(asyncCallbacks, false);
124+ }
115125 tracePath.startRecording();
116126 }
117127 }
@@ -157,7 +167,7 @@ function debugModule(module, runtime) {
157167 copyRecordedEvents: copyRecordedEvents,
158168 loadRecordedEvents: loadRecordedEvents,
159169 getPaused: getPaused,
160− setPaused: setPaused,
170+ setPausedAt: setPausedAt,
161171 tracePath: tracePath,
162172 watchTracker: watchTracker,
163173 };
@@ -177,25 +187,25 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
177187 debugModule.watchTracker.clear();
178188 debugModule.tracePath.clearTraces();
179189 debugModule.clearRecordedEvents();
180− debugModule.setPaused(false);
190+ debugModule.setPausedAt(false, 0);
181191 executeCallbacks(debugModule.initialAsyncCallbacks, true);
182192 }
183193
184194 function pauseProgram() {
185− debugModule.setPaused(true);
195+ debugModule.setPausedAt(true);
186196 eventCounter = debugModule.getRecordedEventsLength();
187197 }
188198
189199 function continueProgram() {
190200 if (debugModule.getPaused())
191201 {
192− debugModule.setPaused(false);
202+ debugModule.setPausedAt(false, eventCounter);
193203 }
194204 }
195205
196206 function stepTo(index) {
197207 if (!debugModule.getPaused()) {
198− debugModule.setPaused(true);
208+ debugModule.setPausedAt(true);
199209 resetProgram();
200210 }
201211
@@ -206,8 +216,11 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
206216 if (index < eventCounter) {
207217 resetProgram();
208218 eventCounter = 0;
219+ debugModule.tracePath.clearTraces();
209220 }
210221
222+ debugModule.tracePath.startRecording();
223+
211224 assert(index >= eventCounter, "index must be bad");
212225 while (eventCounter < index) {
213226 var nextEvent = debugModule.getRecordedEventAt(eventCounter);
@@ -216,6 +229,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
216229 eventCounter += 1;
217230 }
218231 assert(eventCounter == index, "while loop didn't work");
232+ debugModule.tracePath.stopRecording();
219233 }
220234
221235 function getMaxSteps() {
@@ -258,7 +272,13 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
258272 }
259273
260274 if (hotSwapState) {
261− debugModule.setPaused(true);
275+ // The problem is that we want to previous paused state. But
276+ // by the time JS reaches here, the old code has been swapped out
277+ // and the new modules are being generated. So we can ask the
278+ // debugging console what it thinks the pause state is and go
279+ // from there.
280+ var pauseState = top.debug.paused;
281+ debugModule.setPausedAt(true);
262282 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
263283
264284 // draw new trace path
@@ -267,6 +287,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
267287 debugModule.tracePath.stopRecording();
268288
269289 stepTo(hotSwapState.eventCounter);
290+ debugModule.setPausedAt(pauseState, hotSwapState.eventCounter);
270291 }
271292
272293 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
@@ -416,6 +437,7 @@ function executeCallbacks(callbacks, reexecute) {
416437 callbacks.forEach(function(timer) {
417438 if (reexecute || !timer.executed) {
418439 var func = timer.func;
440+ timer.executed = true;
419441 func();
420442 }
421443 });