Skip to content
This repository
Browse code

Cleaner exit handling.

  • Loading branch information...
commit 0768043dd2d61df2137707a046a7aa3c89aeb294 1 parent 218e310
Gianni Chiappetta authored January 19, 2010
10  example/example.js
@@ -5,25 +5,25 @@ var s = new Sandbox();
5 5
 
6 6
 // Example 1 - Standard JS
7 7
 s.run("1 + 1", function(output) {
8  
-  sys.puts("Example 1: " + output);
  8
+  sys.puts("Example 1: " + output + "\n");
9 9
 });
10 10
 
11 11
 // Example 2 - Something slightly more complex
12 12
 s.run("(function(name) { return 'Hi there, ' + name + '!'; })('Fabio')", function(output) {
13  
-  sys.puts("Example 2: " + output);
  13
+  sys.puts("Example 2: " + output + "\n");
14 14
 });
15 15
 
16 16
 // Example 3 - Syntax error
17 17
 s.run("lol)hai", function(output) {
18  
-  sys.puts("Example 3: " + output);
  18
+  sys.puts("Example 3: " + output + "\n");
19 19
 });
20 20
 
21 21
 // Example 4 - Restricted code
22 22
 s.run("process.platform", function(output) {
23  
-  sys.puts("Example 4: " + output);
  23
+  sys.puts("Example 4: " + output + "\n");
24 24
 });
25 25
 
26 26
 // Example 5 - Infinite loop
27 27
 s.run("while (true) {}", function(output) {
28  
-  sys.puts("Example 5: " + output);
  28
+  sys.puts("Example 5: " + output + "\n");
29 29
 });
13  lib/sandbox.js
@@ -15,31 +15,30 @@ function Sandbox(options) {
15 15
         output = function(data) {
16 16
           if (!!data) stdout += data;
17 17
         },
18  
-        child = process.createChildProcess("node", [this.options.shovel]);
  18
+        child = process.createChildProcess(this.options.node, [this.options.shovel]);
19 19
     
20 20
     // Listen
21 21
     child.addListener("output", output);
22 22
     child.addListener("exit", function(code) {
23  
-      if (code != 15) {
24  
-        clearTimeout(timer);
25  
-        hollaback.call(this, stdout);
26  
-      }
  23
+      clearTimeout(timer);
  24
+      hollaback.call(this, stdout);
27 25
     });
28 26
     
29 27
     // Go
30 28
     child.write(code);
  29
+    child.close();
31 30
     timer = setTimeout(function() {
32 31
       child.removeListener("output", output);
  32
+      stdout = "TimeoutError";
33 33
       child.kill();
34  
-      hollaback.call(this, "TimeoutError");
35 34
     }, this.options.timeout);
36  
-    child.close();
37 35
   };
38 36
 }
39 37
 
40 38
 // Options
41 39
 Sandbox.options = {
42 40
   timeout: 500,
  41
+  node: "node",
43 42
   shovel: (function() {
44 43
     var p = __filename.split("/").slice(0, -1);
45 44
     p.push("shovel.js");
1  lib/shovel.js
@@ -31,6 +31,7 @@ function run() {
31 31
       return e.name + ': ' + e.message;
32 32
     }
33 33
   })();
  34
+  
34 35
   process.stdio.write(output);
35 36
   process.exit(0);
36 37
 }

0 notes on commit 0768043

Please sign in to comment.
Something went wrong with that request. Please try again.