Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge changes

Change-Id: I7c469633613fa3a0b18281ca42ffc13fa85adcb0
  • Loading branch information...
commit 2774aca31ef7ea02cb50694f76d694f539fb3dc5 1 parent a9e20b0
Tim Caswell authored
Showing with 28 additions and 8 deletions.
  1. +28 −8 lib/wheat/data.js
View
36 lib/wheat/data.js
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.