on master help sign in
Merge pull request #700 from michaelbjames/debugger-cleanup

Debugger cleanup

commit17b2d733e53eeb31d42c12e7da453f120165beed authorEvan Czaplicki <info@elm-lang.org> dateWed 23 Jul 2014, 17:25 (12 years ago) parent0b319a5a79e903
1 file changed+31−49
runtime/Debugger.js+31−49
runtime/Debugger.js+31−49
@@ -1,4 +1,3 @@
1−
21(function() {
32'use strict';
43
@@ -23,7 +22,7 @@ Elm.debuggerAttach = function(module, hotSwapState /* =undefined */) {
2322
2423function dispatchElmDebuggerInit() {
2524 if (parent.window) {
26− var dispatch = function(_) {
25+ var dispatch = function() {
2726 parent.window.postMessage("elmDebuggerInit", window.location.origin);
2827 };
2928 if (parent.window.document.readyState === "complete") {
@@ -42,6 +41,31 @@ function debugModule(module, runtime) {
4241 var watchTracker = Elm.Native.Debug.make(runtime).watchTracker;
4342 var pauseTime = 0;
4443
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+
4569 function wrapNotify(id, v) {
4670 var timestep = runtime.timer.now();
4771
@@ -123,7 +147,7 @@ function debugModule(module, runtime) {
123147 if (position > 0) {
124148 // If we're not unpausing at the head, then we need to dump the
125149 // events that are ahead of where we're continuing.
126− recordedEvents = recordedEvents.slice(0,position);
150+ recordedEvents = recordedEvents.slice(0, position);
127151 executeCallbacks(asyncCallbacks, false);
128152 }
129153 tracePath.startRecording();
@@ -133,30 +157,6 @@ function debugModule(module, runtime) {
133157 return programPaused;
134158 }
135159
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−
160160 return {
161161 debuggedModule: debuggedModule,
162162 signalGraphNodes: signalGraphNodes,
@@ -173,7 +173,7 @@ function debugModule(module, runtime) {
173173 setPaused: setPaused,
174174 setContinue: setContinue,
175175 tracePath: tracePath,
176− watchTracker: watchTracker,
176+ watchTracker: watchTracker
177177 };
178178}
179179
@@ -203,7 +203,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
203203 function continueProgram() {
204204 if (debugModule.getPaused())
205205 {
206− if(currentEventIndex === 0) {
206+ if (currentEventIndex === 0) {
207207 restartProgram();
208208 return;
209209 }
@@ -232,31 +232,18 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
232232 currentEventIndex = 0;
233233 }
234234
235− assert(index >= currentEventIndex, "index must be bad");
236235 while (currentEventIndex < index) {
237236 var nextEvent = debugModule.getRecordedEventAt(currentEventIndex);
238237 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
239238
240239 currentEventIndex += 1;
241240 }
242− assert(currentEventIndex == index, "while loop didn't work");
243241 }
244242
245243 function getMaxSteps() {
246244 return debugModule.getRecordedEventsLength();
247245 }
248246
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−
260247 function redrawGraphics() {
261248 var main = debugModule.debuggedModule.main
262249 for (var i = main.kids.length ; i-- ; ) {
@@ -297,9 +284,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
297284 debugModule.tracePath.stopRecording();
298285
299286 stepTo(hotSwapState.currentEventIndex);
300− if (paused) {
301− debugModule.setPaused();
302− } else {
287+ if (!paused) {
303288 debugModule.setContinue(hotSwapState.currentEventIndex);
304289 }
305290 }
@@ -316,8 +301,7 @@ function debuggerInit(debugModule, runtime, hotSwapState /* =undefined */) {
316301 getHotSwapState: getHotSwapState,
317302 dispose: dispose,
318303 allNodes: debugModule.signalGraphNodes,
319− watchTracker: debugModule.watchTracker,
320− signalGraphMain: debugModule.debuggedModule.main
304+ watchTracker: debugModule.watchTracker
321305 };
322306
323307 return elmDebugger;
@@ -446,7 +430,6 @@ function tracePathInit(runtime, signalGraphMain) {
446430 }
447431}
448432
449−
450433function executeCallbacks(callbacks, reexecute) {
451434 callbacks.forEach(function(timer) {
452435 if (reexecute || !timer.executed) {
@@ -484,7 +467,6 @@ function snapshotSignalGraph(signalGraphNodes) {
484467 return nodeValues;
485468};
486469
487−
488470function restoreSnapshot(signalGraphNodes, snapshot) {
489471 assert(signalGraphNodes.length == snapshot.length, "saved program state has wrong length");
490472 for (var i=0; i < signalGraphNodes.length; i++) {