workaround Node.js bugs (#1775)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 3 Apr 2017 10:56:11 +0000 (18:56 +0800)
committerGitHub <noreply@github.com>
Mon, 3 Apr 2017 10:56:11 +0000 (18:56 +0800)
Wrap test code in IIFE before passing to `vm`

fixes #1768
fixes #1771

test/sandbox.js

index 396a6e2..ea3a60a 100644 (file)
@@ -12,7 +12,6 @@ var FUNC_TOSTRING = [
     '        return "[Function: __func_" + i + "__]";',
     "    }",
     "}();",
-    ""
 ].join("\n");
 exports.run_code = function(code) {
     var stdout = "";
@@ -21,7 +20,12 @@ exports.run_code = function(code) {
         stdout += chunk;
     };
     try {
-        new vm.Script(FUNC_TOSTRING + code).runInNewContext({
+        vm.runInNewContext([
+            "!function() {",
+            FUNC_TOSTRING,
+            code,
+            "}();",
+        ].join("\n"), {
             console: {
                 log: function() {
                     return console.log.apply(console, [].map.call(arguments, function(arg) {