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 List = Elm.List.make(runtime);
102 var graphicsNode = debugModule.moduleInstance.main.kids[0];
104 var tracePathCanvas = document.createElement('canvas');
105 tracePathCanvas.width = window.innerWidth;
106 tracePathCanvas.height = window.innerHeight;
107 tracePathCanvas.style.position = "absolute";
108 tracePathCanvas.style.top = "0";
109 tracePathCanvas.style.left = "0";
110 runtime.node.parentNode.appendChild(tracePathCanvas);
112 function graphicsUpdate(currentScene) {
113 var ctx = tracePathCanvas.getContext('2d');
114 ctx.clearRect(0, 0, tracePathCanvas.width, tracePathCanvas.height);
115 ctx.fillText("Debugger trace path", 20, 20);
117 function drawElement(elem) {
118 if (elem.element.ctor == "Custom" && elem.element.type == "Collage")
120 List.map(drawForm)(elem.element.model.forms);
122 else if (elem.element.ctor == "Image")
127 ctx.arc(x, y, 2, 0, 2*Math.PI);
128 ctx.fillStyle = "grey";
131 ctx.strokeStype = "black";
136 function drawForm(form) {
138 ctx.scale(form.scale, form.scale);
139 ctx.rotate(form.theta);
140 ctx.translate(form.x, -form.y);
141 if (form.form.ctor == "FElement")
143 drawElement(form.form._0);
150 ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2);
151 drawElement(currentScene);
155 var tracePathNode = A2(Signal.lift, graphicsUpdate, graphicsNode);
157 function resetProgram() {
158 debugModule.clearAsyncEvents();
159 restoreNodeValues(debugModule.moduleNodes, debugModule.initialNodeValues);
163 function restartProgram() {
165 debugModule.clearRecordedEvents();
166 debugModule.initialAsyncTimers.forEach(function(timer) {
167 var func = timer.func;
172 function stepTo(index) {
173 if (!debugModule.getPaused()) {
174 debugModule.setPaused(true);
178 if (index < 0 || index >= recordedEvents.length) {
179 throw "Index out of bounds: " + index;
182 if (index < eventCounter) {
187 assert(index >= eventCounter, "index must be bad");
188 while (eventCounter < index) {
189 var nextEvent = recordedEvents[eventCounter];
190 runtime.notify(nextEvent.id, nextEvent.value, nextEvent.timestep);
194 assert(eventCounter == index, "while loop didnt' work");
197 function getMaxSteps() {
198 return recordedEvents.length - 1;
201 function doPlayback(eventList) {
202 var x = eventList.shift();
204 runtime.notify(x[0], x[1], x[2]);
206 if (eventList.length > 0) {
207 var delta = eventList[0][2] - time;
208 setTimeout(function() { doPlayback(eventList); }, delta);
212 function redrawGraphics() {
213 graphicsNode.recv(Date.now(), true, debugModule.moduleInstance.main.id);
218 restart: restartProgram,
219 getMaxSteps: getMaxSteps,
228function assert(bool, msg) {
230 throw "Assertion error: " + msg;
234function saveNodeValues(allNodes) {
237 allNodes.forEach(function(node) {
238 nodeValues.push({ value: node.value, id: node.id });
245function restoreNodeValues(allNodes, nodeStates) {
246 assert(allNodes.length == nodeStates.length, "saved program state has wrong length");
247 for (var i=0; i < allNodes.length; i++) {
248 var node = allNodes[i];
249 var state = nodeStates[i];
250 assert(node.id == state.id, "the nodes moved position");
252 node.value = state.value;
256function flattenNodes(nodes) {
257 var treeReduce = function(prev, cur, i, arr) {
258 var flatKids = cur.kids.reduce(treeReduce, []);
260 return prev.concat(flatKids);
263 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