@@ -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,17 +108,21 @@ function debugModule(module, runtime) {
103108 recordedEvents = events.slice();
104109 }
105110
106− function setPaused(v) {
107− programPaused = v;
108− if (programPaused) {
109− clearAsyncCallbacks();
110− tracePath.stopRecording();
111− }
112− else {
113− // executeCallbacks should only be called when continuing, not when restarting.
114− //executeCallbacks(asyncCallbacks, false);
115− tracePath.startRecording();
111+ function setPaused() {
112+ programPaused = true;
113+ clearAsyncCallbacks();
114+ tracePath.stopRecording();
115+ }
116+
117+ function setContinue(position) {
118+ programPaused = false;
119+ if (position > 0) {
120+ // If we're not unpausing 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);
116124 }
125+ tracePath.startRecording();
117126 }
118127
119128 function getPaused() {
@@ -158,6 +167,7 @@ function debugModule(module, runtime) {
158167 loadRecordedEvents: loadRecordedEvents,
159168 getPaused: getPaused,
160169 setPaused: setPaused,
170+ setContinue: setContinue,
161171 tracePath: tracePath,
162172 watchTracker: watchTracker,
163173 };
@@ -177,25 +187,35 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
177187 debugModule.watchTracker.clear();
178188 debugModule.tracePath.clearTraces();
179189 debugModule.clearRecordedEvents();
180− debugModule.setPaused(false);
190+ debugModule.setContinue(0);
181191 executeCallbacks(debugModule.initialAsyncCallbacks, true);
182192 }
183193
184194 function pauseProgram() {
185− debugModule.setPaused(true);
195+ debugModule.setPaused();
186196 eventCounter = debugModule.getRecordedEventsLength();
187197 }
188198
189199 function continueProgram() {
190200 if (debugModule.getPaused())
191201 {
192− debugModule.setPaused(false);
202+ if(eventCounter === 0) {
203+ restartProgram();
204+ return;
205+ }
206+ resetProgram();
207+ var index = eventCounter;
208+ eventCounter = 0;
209+ debugModule.tracePath.clearTraces();
210+ debugModule.tracePath.startRecording();
211+ stepTo(index);
212+ debugModule.setContinue(eventCounter);
193213 }
194214 }
195215
196216 function stepTo(index) {
197217 if (!debugModule.getPaused()) {
198− debugModule.setPaused(true);
218+ debugModule.setPaused();
199219 resetProgram();
200220 }
201221
@@ -258,7 +278,13 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
258278 }
259279
260280 if (hotSwapState) {
261− debugModule.setPaused(true);
281+ // The problem is that we want to previous paused state. But
282+ // by the time JS reaches here, the old code has been swapped out
283+ // and the new modules are being generated. So we can ask the
284+ // debugging console what it thinks the pause state is and go
285+ // from there.
286+ var paused = top.debug.paused;
287+ debugModule.setPaused();
262288 debugModule.loadRecordedEvents(hotSwapState.recordedEvents);
263289
264290 // draw new trace path
@@ -267,6 +293,11 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
267293 debugModule.tracePath.stopRecording();
268294
269295 stepTo(hotSwapState.eventCounter);
296+ if (paused) {
297+ debugModule.setPaused();
298+ } else {
299+ debugModule.setContinue(hotSwapState.eventCounter);
300+ }
270301 }
271302
272303 runtime.node.parentNode.appendChild(debugModule.tracePath.canvas);
@@ -416,6 +447,7 @@ function executeCallbacks(callbacks, reexecute) {
416447 callbacks.forEach(function(timer) {
417448 if (reexecute || !timer.executed) {
418449 var func = timer.func;
450+ timer.executed = true;
419451 func();
420452 }
421453 });