6Elm.debuggerAttach = function(module) {
8 make: function(runtime) {
9 var wrappedModule = debugModule(module, runtime);
10 Elm.debuggerInit = function() {
11 Elm.Debugger = debuggerInit(wrappedModule, runtime);
13 return wrappedModule.moduleInstance;
18function debugModule(module, runtime) {
19 var programPaused = false;
20 var recordedEvents = [];
24 function wrapNotify(id, v) {
25 var timestep = Date.now();
28 // ignore notify because we are stepping
31 runtime.notify(id, v, timestep);
32 recordEvent(id, v, timestep);
36 function wrapRunDelayed(func, delayMs) {
37 return safeSetTimeout(func, delayMs);
40 function safeSetTimeout(func, delayMs) {
41 var timerId = setTimeout(func, delayMs);
42 asyncTimers.push({ func:func, delayMs:delayMs, timerId:timerId });
46 function recordEvent(id, v, timestep) {
47 recordedEvents.push({ id:id, value:v, timestep:timestep });
50 function clearAsyncEvents() {
51 asyncTimers.forEach(function(timer) {
52 clearTimeout(timer.timerId);
56 function clearRecordedEvents() {
60 function getRecordedEvents() {
61 return recordedEvents.slice();
64 function setPaused(v) {
68 function getPaused() {
72 var wrappedRuntime = {
74 runDelayed: wrapRunDelayed,
76 display: runtime.display,
78 addListener: runtime.addListener,
79 inputs: runtime.inputs
82 var moduleInstance = module.make(wrappedRuntime);
83 var moduleNodes = flattenNodes(wrappedRuntime.inputs);
86 moduleInstance: moduleInstance,
87 moduleNodes: moduleNodes,
88 initialNodeValues: saveNodeValues(moduleNodes),
89 initialAsyncTimers: asyncTimers.slice(),
91 clearAsyncEvents: clearAsyncEvents,
92 clearRecordedEvents: clearRecordedEvents,
93 getRecordedEvents: getRecordedEvents,
99function debuggerInit(debugModule, runtime) {
100 var Signal = Elm.Signal.make(runtime);
101 var graphicsNode = debugModule.moduleInstance.main.kids[0];
103 var tracePath = tracePathInit(runtime);
104 var tracePathNode = A2(Signal.lift, tracePath.graphicsUpdate, graphicsNode);
105 runtime.node.parentNode.appendChild(tracePath.canvas);
107 function resetProgram() {
108 debugModule.clearAsyncEvents();
109 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
113 function restartProgram() {
115 debugModule.clearRecordedEvents();
116 debugModule.initialAsyncTimers.forEach(function(timer) {
117 var func = timer.func;
122 function stepTo(index) {
123 if (!debugModule.getPaused()) {
124 debugModule.setPaused(true);
128 if (index < 0 || index >= recordedEvents.length) {
129 throw "Index out of bounds: " + index;
132 if (index < eventCounter) {
137 assert(index >= eventCounter, "index must be bad");
138 while (eventCounter < index) {
139 var nextEvent = recordedEvents[eventCounter];
140 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
144 assert(eventCounter == index, "while loop didnt' work");
147 function getMaxSteps() {
148 return recordedEvents.length - 1;
151 function doPlayback(eventList) {
152 var x = eventList.shift();
154 runtime.notify(x[0], x[1], x[2]);
156 if (eventList.length > 0) {
157 var delta = eventList[0][2] - time;
158 setTimeout(function() { doPlayback(eventList); }, delta);
162 function redrawGraphics() {
163 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
168 restart: restartProgram,
169 getMaxSteps: getMaxSteps,
176function Point(x, y) {
180 this.translate = function(x, y) {
181 return new Point(this.x + x, this.y + y);
184 this.equals = function(p) {
185 return this.x == p.x && this.y == p.y;
189function tracePathInit(runtime) {
190 var List = Elm.List.make(runtime);
191 var tracePathCanvas = createCanvas();
192 var tracePositions = {};
194 function findPositions(currentScene) {
196 function processElement(elem, offset) {
197 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
199 List.map(F2(processForm)(offset))(elem.element.model.forms);
201 else if (elem.element.ctor == "Image" && elem.props.debugTracePathId)
203 positions[elem.props.debugTracePathId] = new Point(offset.x, offset.y);
207 function processForm(offset, form) {
208 if (form.form.ctor == "FElement")
210 processElement(form.form._0, offset.translate(form.x, -form.y));
214 processElement(currentScene, new Point(0, 0));
218 function appendPositions(positions) {
219 for (var id in positions) {
220 var pos = positions[id];
221 if (tracePositions.hasOwnProperty(id)) {
222 var points = tracePositions[id];
223 if (!pos.equals(points[points.length-1])) {
228 tracePositions[id] = [pos];
233 function graphicsUpdate(currentScene) {
234 var ctx = tracePathCanvas.getContext('2d');
235 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
236 ctx.fillText("Debugger trace path", 20, 20);
239 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
240 appendPositions(findPositions(currentScene));
241 for (var id in tracePositions)
244 var points = tracePositions[id];
245 for (var i=0; i < points.length; i++)
249 ctx.moveTo(p.x, p.y);
252 ctx.lineTo(p.x, p.y);
257 ctx.strokeStyle = "black";
263 function clearTraces() {
268 graphicsUpdate: graphicsUpdate,
269 canvas: tracePathCanvas,
270 clearTraces: clearTraces
275function createCanvas() {
276 var c = document.createElement('canvas');
277 c.width = window.innerWidth;
278 c.height = window.innerHeight;
279 c.style.position = "absolute";
285function assert(bool, msg) {
287 throw "Assertion error: " + msg;
291function saveNodeValues(allNodes) {
294 allNodes.forEach(function(node) {
295 nodeValues.push({ value: node.value, id: node.id });
302function restoreNodeValues(allNodes, nodeStates) {
303 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
304 for (var i=0; i < allNodes.length; i++) {
305 var node = allNodes[i];
306 var state = nodeStates[i];
307 assert(node.id == state.id, "the nodes moved position");
309 node.value = state.value;
313function flattenNodes(nodes) {
314 var treeReduce = function(prev, cur, i, arr) {
315 var flatKids = cur.kids.reduce(treeReduce, []);
317 return prev.concat(flatKids);
320 return nodes.reduce(treeReduce, []);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list