Skip to content

Commit

Permalink
Merge changes
Browse files Browse the repository at this point in the history
Change-Id: I7c469633613fa3a0b18281ca42ffc13fa85adcb0
  • Loading branch information
Tim Caswell committed Feb 23, 2011
1 parent a9e20b0 commit 2774aca
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions lib/wheat/data.js
Expand Up @@ -59,33 +59,53 @@ function preProcessMarkdown(markdown) {
function sandbox(snippet) {
snippet.result = "";
snippet.output = "";
var fakeRequire = function fakeRequire(path) {
function fakeRequire(path) {
var lib = require(path);
return lib;
};
}
// Create a 'pseudo-write-stream', to act as the virtual 'stdout' stream.
var stdout = new (require('stream').Stream)();
stdout.writable = true;
stdout.write = function(buf, enc) {
if (!this.writable) throw new Error("Stream is not writable");
if (!Buffer.isBuffer(buf)) {
buf = new Buffer(buf, enc);
}
this.emit('data', buf);
}
stdout.end = function(buf, enc) {
if (buf) { this.write(buf, enc); }
this.writable = false;
}
stdout.on('data', function(data) {
snippet.output += data.toString();
});

var env = {
clear: function () { snippet.output = ""; },
require: fakeRequire,
process: {
exit: function fakeExit() {},
argv: ['node', snippet.filename]
argv: [process.argv[0], snippet.filename],
stdout: stdout
},
console: {
log: function fakePuts() {
log: function fakeLog() {
arguments.forEach(function (data) {
snippet.output += data + "\n";
stdout.write(data + "\n");
});
},
dir: function fakeP(data) {
dir: function fakeDir() {
arguments.forEach(function (data) {
snippet.output += util.inspect(data) + "\n";
});
}
} };
}
};
env.process.__proto__ = process;

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

try {
snippet.lastExpression = Script.runInNewContext(toRun, env, snippet.filename);
Expand Down

0 comments on commit 2774aca

Please sign in to comment.