on master help sign in
Merge pull request #700 from michaelbjames/debugger-cleanup
17b2d73Evan Czaplicki
runtime/Debugger.js+31−49
@@ -1,4 +1,3 @@
1−
2(function() {
1(function() {
3'use strict';
2'use strict';
4
3
@@ -23,7 +22,7 @@ Elm.debuggerAttach = function(module, hotSwapState /* =undefined */) {
23
22
24function dispatchElmDebuggerInit() {
23function dispatchElmDebuggerInit() {
25 if (parent.window) {
24 if (parent.window) {
26− var dispatch = function(_) {
25+ var dispatch = function() {
27 parent.window.postMessage("elmDebuggerInit", window.location.origin);
26 parent.window.postMessage("elmDebuggerInit", window.location.origin);
28 };
27 };
29 if (parent.window.document.readyState === "complete") {
28 if (parent.window.document.readyState === "complete") {
@@ -42,6 +41,31 @@ function debugModule(module, runtime) {
42 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
41 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
43 var pauseTime = 0;
42 var pauseTime = 0;
44
43
44+ // runtime is the prototype of wrappedRuntime
45+ // so we can access all runtime properties too
46+ var wrappedRuntime = Object.create(runtime);
47+ wrappedRuntime.notify = wrapNotify;
48+ wrappedRuntime.runDelayed = wrapRunDelayed;
49+
50+ // make a copy of the wrappedRuntime
51+ var assignedPropTracker = Object.create(wrappedRuntime);
52+ var debuggedModule = module.make(assignedPropTracker);
53+
54+ // make sure the signal graph is actually a signal & extract the visual model
55+ if ( !('recv' in debuggedModule.main) ) {
56+ debuggedModule.main = Elm.Signal.make(runtime).constant(debuggedModule.main);
57+ }
58+
59+ // The main module stores imported modules onto the runtime.
60+ // To ensure only one instance of each module is created,
61+ // we assign them back on the original runtime object.
62+ Object.keys(assignedPropTracker).forEach(function(key) {
63+ runtime[key] = assignedPropTracker[key];
64+ });
65+
66+ var signalGraphNodes = flattenSignalGraph(wrappedRuntime.inputs);
67+ var tracePath = tracePathInit(runtime, debuggedModule.main);
68+
45 function wrapNotify(id, v) {
69 function wrapNotify(id, v) {
46 var timestep = runtime.timer.now();
70 var timestep = runtime.timer.now();
47
71
@@ -123,7 +147,7 @@ function debugModule(module, runtime) {
123 if (position > 0) {
147 if (position > 0) {
124 // If we're not unpausing at the head, then we need to dump the
148 // If we're not unpausing at the head, then we need to dump the
125 // events that are ahead of where we're continuing.
149 // events that are ahead of where we're continuing.
126− recordedEvents = recordedEvents.slice(0,position);
150+ recordedEvents = recordedEvents.slice(0, position);
127 executeCallbacks(asyncCallbacks, false);
151 executeCallbacks(asyncCallbacks, false);
128 }
152 }
129 tracePath.startRecording();
153 tracePath.startRecording();
@@ -133,30 +157,6 @@ function debugModule(module, runtime) {
133 return programPaused;
157 return programPaused;
134 }
158 }
135
159
136− // runtime is the prototype of wrappedRuntime
137− // so we can access all runtime properties too
138− var wrappedRuntime = Object.create(runtime);
139− wrappedRuntime.notify = wrapNotify;
140− wrappedRuntime.runDelayed = wrapRunDelayed;
141−
142− var assignedPropTracker = Object.create(wrappedRuntime);
143− var debuggedModule = module.make(assignedPropTracker);
144−
145− // make sure the signal graph is actually a signal & extract the visual model
146− if ( !('recv' in debuggedModule.main) ) {
147− debuggedModule.main = Elm.Signal.make(runtime).constant(debuggedModule.main);
148− }
149−
150− // The main module stores imported modules onto the runtime.
151− // To ensure only one instance of each module is created,
152− // we assign them back on the original runtime object.
153− Object.keys(assignedPropTracker).forEach(function(key) {
154− runtime[key] = assignedPropTracker[key];
155− });
156−
157− var signalGraphNodes = flattenSignalGraph(wrappedRuntime.inputs);
158− var tracePath = tracePathInit(runtime, debuggedModule.main);
159−
160 return {
160 return {
161 debuggedModule: debuggedModule,
161 debuggedModule: debuggedModule,
162 signalGraphNodes: signalGraphNodes,
162 signalGraphNodes: signalGraphNodes,
@@ -173,7 +173,7 @@ function debugModule(module, runtime) {
173 setPaused: setPaused,
173 setPaused: setPaused,
174 setContinue: setContinue,
174 setContinue: setContinue,
175 tracePath: tracePath,
175 tracePath: tracePath,
176− watchTracker: watchTracker,
176+ watchTracker: watchTracker
177 };
177 };
178}
178}
179
179
@@ -203,7 +203,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
203 function continueProgram() {
203 function continueProgram() {
204 if (debugModule.getPaused())
204 if (debugModule.getPaused())
205 {
205 {
206− if(currentEventIndex === 0) {
206+ if (currentEventIndex === 0) {
207 restartProgram();
207 restartProgram();
208 return;
208 return;
209 }
209 }
@@ -232,31 +232,18 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
232 currentEventIndex = 0;
232 currentEventIndex = 0;
233 }
233 }
234
234
235− assert(index >= currentEventIndex, "index must be bad");
236 while (currentEventIndex < index) {
235 while (currentEventIndex < index) {
237 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
236 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
238 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
237 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
239
238
240 currentEventIndex += 1;
239 currentEventIndex += 1;
241 }
240 }
242− assert(currentEventIndex == index, "while loop didn't work");
243 }
241 }
244
242
245 function getMaxSteps() {
243 function getMaxSteps() {
246 return debugModule.getRecordedEventsLength();
244 return debugModule.getRecordedEventsLength();
247 }
245 }
248
246
249− function doPlayback(eventList) {
250− var x = eventList.shift();
251− var time = x[2];
252− runtime.notify(x[0], x[1], x[2]);
253−
254− if (eventList.length > 0) {
255− var delta = eventList[0][2] - time;
256− setTimeout(function() { doPlayback(eventList); }, delta);
257− }
258− }
259−
260 function redrawGraphics() {
247 function redrawGraphics() {
261 var main = debugModule.debuggedModule.main
248 var main = debugModule.debuggedModule.main
262 for (var i = main.kids.length ; i-- ; ) {
249 for (var i = main.kids.length ; i-- ; ) {
@@ -297,9 +284,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
297 debugModule.tracePath.stopRecording();
284 debugModule.tracePath.stopRecording();
298
285
299 stepTo(hotSwapState.currentEventIndex);
286 stepTo(hotSwapState.currentEventIndex);
300− if (paused) {
287+ if (!paused) {
301− debugModule.setPaused();
302− } else {
303 debugModule.setContinue(hotSwapState.currentEventIndex);
288 debugModule.setContinue(hotSwapState.currentEventIndex);
304 }
289 }
305 }
290 }
@@ -316,8 +301,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
316 getHotSwapState: getHotSwapState,
301 getHotSwapState: getHotSwapState,
317 dispose: dispose,
302 dispose: dispose,
318 allNodes: debugModule.signalGraphNodes,
303 allNodes: debugModule.signalGraphNodes,
319− watchTracker: debugModule.watchTracker,
304+ watchTracker: debugModule.watchTracker
320− signalGraphMain: debugModule.debuggedModule.main
321 };
305 };
322
306
323 return elmDebugger;
307 return elmDebugger;
@@ -446,7 +430,6 @@ function tracePathInit(runtime, signalGraphMain) {
446 }
430 }
447}
431}
448
432
449−
450function executeCallbacks(callbacks, reexecute) {
433function executeCallbacks(callbacks, reexecute) {
451 callbacks.forEach(function(timer) {
434 callbacks.forEach(function(timer) {
452 if (reexecute || !timer.executed) {
435 if (reexecute || !timer.executed) {
@@ -484,7 +467,6 @@ function snapshotSignalGraph(signalGraphNodes) {
484 return nodeValues;
467 return nodeValues;
485};
468};
486
469
487−
488function restoreSnapshot(signalGraphNodes, snapshot) {
470function restoreSnapshot(signalGraphNodes, snapshot) {
489 assert(signalGraphNodes.length == snapshot.length, "saved program state has wrong length");
471 assert(signalGraphNodes.length == snapshot.length, "saved program state has wrong length");
490 for (var i=0; i < signalGraphNodes.length; i++) {
472 for (var i=0; i < signalGraphNodes.length; i++) {