Skip to content

Commit

Permalink
Node sandbox should use strict mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsamarin committed Jan 22, 2013
1 parent 1451206 commit 2a1cd19
Showing 1 changed file with 75 additions and 67 deletions.
142 changes: 75 additions & 67 deletions lib/sandbox/v8/shovel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,89 @@ var util = require("util");
var vm = require("vm");
var file = require('fs');

function Sandbox(utils_file) {
this.utils_file = utils_file;
this.utils_global = {
exports: {},
global: {},
version: "V8 " + process.versions.v8
var Sandbox = (function() {
"use strict";

function Sandbox(utils_file) {
this.utils_file = utils_file;
this.utils_global = {
exports: {},
global: {},
version: "V8 " + process.versions.v8
};
this.input_code = null;
}

Sandbox.prototype.load_input = function(callback) {
var input = [];
var stdin = process.openStdin();
var self = this;

stdin.on("data", function(data) {
input.push(data);
});

stdin.on("end", function() {
try {
self.input_code = input.join("");
callback.call(self);
} catch (e) {
Sandbox.report_error(e);
}
});
};
this.input_code = null;
}

Sandbox.prototype.load_input = function(callback) {
var input = [];
var stdin = process.openStdin();
var self = this;
Sandbox.prototype.run = function() {
"use strict";

stdin.on("data", function(data) {
input.push(data);
});
this.load_input(function() {
var output;

// Run utilities file
vm.runInNewContext(file.readFileSync(this.utils_file), this.utils_global);

// Execute utilities' run function
if (typeof this.utils_global.exports.run !== "function")
throw new Error("The exports.run function is not callable.");

output = this.utils_global.exports.run(this.execute.bind(this));

process.stdout.write(output+"\n");
process.exit();
});
};

Sandbox.prototype.execute = function() {
if (typeof this.utils_global.global !== "object")
throw new Error("Expected global to be an object.");

return vm.runInNewContext(this.input_code, this.utils_global.global, "irc");
};

Sandbox.report_error = function(error) {
process.stdout.write(
JSON.stringify(
{"data": {},
"error": "Internal Error: V8 sandbox, "+error,
"result": "undefined"}
)+"\n");
process.exit();
}

stdin.on("end", function() {
Sandbox.main = function(argv) {
try {
self.input_code = input.join("");
callback.call(self);
if (argv[2]) {
var sandbox = new Sandbox(argv[2]);
sandbox.run();
} else {
throw new Error("Required argument for utilities file.");
}
} catch (e) {
Sandbox.report_error(e);
}
});
};

Sandbox.prototype.run = function() {
this.load_input(function() {
var output;

// Run utilities file
vm.runInNewContext(file.readFileSync(this.utils_file), this.utils_global);

// Execute utilities' run function
if (typeof this.utils_global.exports.run !== "function")
throw new Error("The exports.run function is not callable.");

output = this.utils_global.exports.run(this.execute.bind(this));

process.stdout.write(output+"\n");
process.exit();
});
};

Sandbox.prototype.execute = function() {
if (typeof this.utils_global.global !== "object")
throw new Error("Expected global to be an object.");

return vm.runInNewContext(this.input_code, this.utils_global.global, "irc");
};

Sandbox.report_error = function(error) {
process.stdout.write(
JSON.stringify(
{"data": {},
"error": "Internal Error: V8 sandbox, "+error,
"result": "undefined"}
)+"\n");
process.exit();
}
};

Sandbox.main = function(argv) {
try {
if (argv[2]) {
var sandbox = new Sandbox(argv[2]);
sandbox.run();
} else {
throw new Error("Required argument for utilities file.");
}
} catch (e) {
Sandbox.report_error(e);
}
};
return Sandbox;
}());

Sandbox.main(process.argv);

0 comments on commit 2a1cd19

Please sign in to comment.