Skip to content

Commit

Permalink
enable running functions as sideEffects
Browse files Browse the repository at this point in the history
 - allows box2d animation display
  • Loading branch information
iffsid committed May 5, 2014
1 parent 97544fe commit 4b25d55
Showing 1 changed file with 31 additions and 34 deletions.
65 changes: 31 additions & 34 deletions editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function makewebchurchrunner(engineOptions){
return function(editorModel) {
var cm = editorModel.get('codeMirror');
var code = editorModel.get('code');

var $results = cm.$results;

$results.show();
Expand All @@ -54,9 +54,9 @@ function makewebchurchrunner(engineOptions){
}

editorModel.trigger('run:start');
try {
try {
var runResult = evaluate(code, engineOptions);

var underlyingData;

// render all side effects
Expand All @@ -68,58 +68,55 @@ function makewebchurchrunner(engineOptions){
$results.append( $("<div></div>").append(e.data));
}
if (e.type == "table") {

var tableString = wrap("table", e.data.map(function(row) {
var cols = row.map(function(x) { return wrap('td', x); }).join("");
return wrap("tr", cols);
return wrap("tr", cols);
}).join("\n"));

$results.append( $(tableString) );

}

if (e.type == "function") {
e.data($results);
}
});

underlyingData = runResult;
runResult = format_result(runResult);
if (!(runResult == "undefined")) {
$results.append($("<pre>"+runResult+'</pre>'));
}

editorModel.trigger('run:finish');

} catch (e) {

editorModel.trigger('run:finish');

var error = e.message;
$results
.append( $("<p></p>")
.addClass('error')
.text(error) );

if (e.stackarray != undefined) {
var churchStack = $("<pre></pre>");
churchStack.text(e.stackarray
.map(function(x) { return _.template("{{text}}: {{start}}-{{end}}", x) })
.join("\n"))
$results.append( '<div><u>Church stack array:</u></div>', churchStack);

$results.append( '<div><u>Church stack array:</u></div>', churchStack);

var start=e.start.split(":"), end=e.end.split(":");
cm.errormark = cm.markText({line: Number(start[0])-1, ch: Number(start[1])-1},
{line: Number(end[0])-1, ch: Number(end[1])},
{className: "CodeMirrorError", clearOnEnter: true});

}

var jsStack = $("<pre></pre>");
jsStack.text(e.jsStack.join('\n'));

$results.append('<div><u>JS stack:</u></div>', jsStack );


}
}
};
};

Expand All @@ -142,11 +139,11 @@ var inject = function(domEl, options) {
options = _(options).defaults({
code: $(domEl).text(),
engine: "webchurch",
exerciseName: ""
exerciseName: ""
});

var editorModel = new EditorModel(options);

// editor
var cm = CodeMirror(
// TODO: defer this - we might not want to display immediately...
Expand All @@ -167,17 +164,17 @@ var inject = function(domEl, options) {

// when text in codemirror changes, update editormodel
cm.on('change', function(cmInstance) {
editorModel.set('code', cmInstance.getValue())
editorModel.set('code', cmInstance.getValue())
});

//fold ";;;fold:" parts:
var lastLine = cm.lastLine();
for(var i=0;i<=lastLine;i++) {
var txt = cm.getLine(i),
pos = txt.indexOf(";;;fold:");
if (pos==0) {cm.foldCode(CodeMirror.Pos(i,pos),folding.tripleCommentRangeFinder);}
}

// results div
var $results = $("<div class='results'>");
$results.hide();
Expand All @@ -193,8 +190,8 @@ var inject = function(domEl, options) {
selectedString: engine == cm.engine ? "selected" : ""
});

return str;
}
return str;
}
).join("\n") + "\n</select>",
$engineSelector = $(engineSelectorString);

Expand All @@ -207,8 +204,8 @@ var inject = function(domEl, options) {
var $resetButton = $("<button class='reset'>").html("Reset");
$resetButton.click(function() {
cm.setValue( editorModel.get('initialOptions').code );
cm.$engineSelector.val( editorModel.get('initialOptions').engine );
$results.hide.html('');
cm.$engineSelector.val( editorModel.get('initialOptions').engine );
$results.hide.html('');
});

// run button
Expand All @@ -219,11 +216,11 @@ var inject = function(domEl, options) {

setTimeout(function() {
editorModel.run();
}, 15);
}, 15);

editorModel.on('run:finish', function() {
$runButton.removeAttr('disabled');
})
})
});

var $codeControls = $("<div class='code-controls'>");
Expand All @@ -243,23 +240,23 @@ var inject = function(domEl, options) {
);

$(cm.display.wrapper).prepend(
$cogMenu
$cogMenu
);

// add code controls and results divs after codemirror
$(cm.display.wrapper).prepend($codeControls);

$(cm.display.wrapper).after($results);

$(cm.display.wrapper).attr("id", "ex-" + options.exerciseName);

cm.$runButton = $runButton;
cm.$engineSelector = $engineSelector;
cm.$resetButton = $resetButton;
cm.$results = $results;

return editorModel

};

module.exports = {
Expand Down

0 comments on commit 4b25d55

Please sign in to comment.