Skip to content

Commit

Permalink
Update the sandbox to properly use console.
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Jul 27, 2010
1 parent 4875f76 commit 3926c86
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/wheat.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ module.exports = function setup(repo) {
}
next();
}
};
};
43 changes: 18 additions & 25 deletions lib/wheat/data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var Git = require('git'),
Path = require('path'),
Step = require('step');
Step = require('step'),
sys = require('sys'),
Script = process.binding('evals').Script;

function preProcessMarkdown(markdown) {
if (!(typeof markdown === 'string')) {
Expand Down Expand Up @@ -56,7 +58,17 @@ function preProcessMarkdown(markdown) {
function sandbox(snippet) {
snippet.result = "";
snippet.output = "";
var overrides = {
var fakeRequire = function fakeRequire(path) {
var lib = require(path);
return lib;
};
var env = {
clear: function () { snippet.output = ""; },
require: fakeRequire,
process: {
exit: function fakeExit() {},
argv: ['node', snippet.filename]
},
console: {
log: function fakePuts() {
arguments.forEach(function (data) {
Expand All @@ -68,34 +80,14 @@ function sandbox(snippet) {
snippet.output += sys.inspect(data) + "\n";
});
}
},
}
var fakeRequire = function fakeRequire(path) {
var lib = require(path);
if (overrides[path]) {
lib.mixin(overrides[path]);
}
return lib;
};
var env = {
clear: function () { snippet.output = ""; },
require: fakeRequire,
process: {
exit: function fakeExit() {},
argv: ['node', snippet.filename]
}
};
} };
env.process.__proto__ = process;

var toRun = (snippet.beforeCode ? (snippet.beforeCode + "\nclear();\n") : "") + snippet.code;

// Ignore shebang line
if (toRun.substr(0,3) == "#!/") {
toRun = toRun.substr(toRun.indexOf("\n"));
}
console.log(toRun);

try {
snippet.lastExpression = process.evalcx(toRun, env, snippet.filename);
snippet.lastExpression = Script.runInNewContext(toRun, env, snippet.filename);
} catch (err) {
snippet.error = err;
}
Expand Down Expand Up @@ -126,6 +118,7 @@ function activateSnippets(version, snippets, canExecute, callback) {
} else {
snippet.code = code.replace(/^\/\/[a-z-]+.*\n/mg, '');
}
snippet.code = snippet.code.trim();
if (canExecute && snippet.execute) {
sandbox(snippet);
}
Expand Down
11 changes: 1 addition & 10 deletions lib/wheat/prettify.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,7 @@
if (typeof window === 'undefined') {
window = GLOBAL;
if (typeof require === 'function') {
var sys = require('sys');
window.console = {
log: function (m) {
sys.puts(m.message);
sys.p(m);
},
trace: function () {
sys.puts(new Error().stack);
}
};
window.console = console;
}
}
if (typeof navigator === 'undefined') {
Expand Down

0 comments on commit 3926c86

Please sign in to comment.