Skip to content
Browse files

Node sandbox should use strict mode.

  • Loading branch information...
1 parent 1451206 commit 2a1cd19c76d517dd139a22571fdb88d8fd739582 @dsamarin dsamarin committed Jan 22, 2013
Showing with 75 additions and 67 deletions.
  1. +75 −67 lib/sandbox/v8/shovel.js
View
142 lib/sandbox/v8/shovel.js
@@ -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.
Something went wrong with that request. Please try again.