src: fix deferred events not working with -e
authorBen Noordhuis <info@bnoordhuis.nl>
Mon, 25 May 2015 14:37:49 +0000 (16:37 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Tue, 26 May 2015 15:16:48 +0000 (17:16 +0200)
Defer evaluation of the script for a tick.  This is a workaround for
events not firing when evaluating scripts on the command line with -e.

Fixes: https://github.com/nodejs/io.js/issues/1600
PR-URL: https://github.com/nodejs/io.js/pull/1793
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
src/node.js
test/message/eval_messages.out
test/message/stdin_messages.out
test/parallel/test-cli-eval-event.js [new file with mode: 0644]

index cb50341..065c337 100644 (file)
              'return require("vm").runInThisContext(' +
              JSON.stringify(body) + ', { filename: ' +
              JSON.stringify(name) + ' });\n';
-    var result = module._compile(script, name + '-wrapper');
-    if (process._print_eval) console.log(result);
+    // Defer evaluation for a tick.  This is a workaround for deferred
+    // events not firing when evaluating scripts from the command line,
+    // see https://github.com/nodejs/io.js/issues/1600.
+    process.nextTick(function() {
+      var result = module._compile(script, name + '-wrapper');
+      if (process._print_eval) console.log(result);
+    });
   }
 
   function createWritableStdioStream(fd) {
index 809e788..be2120c 100644 (file)
@@ -6,9 +6,9 @@ SyntaxError: Strict mode code may not include a with statement
     at Object.exports.runInThisContext (vm.js:*)
     at Object.<anonymous> ([eval]-wrapper:*:*)
     at Module._compile (module.js:*:*)
-    at evalScript (node.js:*:*)
-    at startup (node.js:*:*)
     at node.js:*:*
+    at doNTCallback0 (node.js:*:*)
+    at process._tickCallback (node.js:*:*)
 42
 42
 [eval]:1
@@ -19,9 +19,9 @@ Error: hello
     at Object.exports.runInThisContext (vm.js:*)
     at Object.<anonymous> ([eval]-wrapper:*:*)
     at Module._compile (module.js:*:*)
-    at evalScript (node.js:*:*)
-    at startup (node.js:*:*)
     at node.js:*:*
+    at doNTCallback0 (node.js:*:*)
+    at process._tickCallback (node.js:*:*)
 [eval]:1
 throw new Error("hello")
       ^
@@ -30,9 +30,9 @@ Error: hello
     at Object.exports.runInThisContext (vm.js:*)
     at Object.<anonymous> ([eval]-wrapper:*:*)
     at Module._compile (module.js:*:*)
-    at evalScript (node.js:*:*)
-    at startup (node.js:*:*)
     at node.js:*:*
+    at doNTCallback0 (node.js:*:*)
+    at process._tickCallback (node.js:*:*)
 100
 [eval]:1
 var x = 100; y = x;
@@ -42,9 +42,9 @@ ReferenceError: y is not defined
     at Object.exports.runInThisContext (vm.js:*)
     at Object.<anonymous> ([eval]-wrapper:*:*)
     at Module._compile (module.js:*:*)
-    at evalScript (node.js:*:*)
-    at startup (node.js:*:*)
     at node.js:*:*
+    at doNTCallback0 (node.js:*:*)
+    at process._tickCallback (node.js:*:*)
 [eval]:1
 var ______________________________________________; throw 10
                                                     ^
index 92d09a0..9dc6451 100644 (file)
@@ -7,12 +7,8 @@ SyntaxError: Strict mode code may not include a with statement
     at Object.exports.runInThisContext (vm.js:*)
     at Object.<anonymous> ([stdin]-wrapper:*:*)
     at Module._compile (module.js:*:*)
-    at evalScript (node.js:*:*)
-    at Socket.<anonymous> (node.js:*:*)
-    at emitNone (events.js:*:*)
-    at Socket.emit (events.js:*:*)
-    at endReadableNT (_stream_readable.js:*:*)
-    at doNTCallback2 (node.js:*:*)
+    at node.js:*:*
+    at doNTCallback0 (node.js:*:*)
     at process._tickCallback (node.js:*:*)
 42
 42
@@ -25,12 +21,9 @@ Error: hello
     at Object.exports.runInThisContext (vm.js:*)
     at Object.<anonymous> ([stdin]-wrapper:*:*)
     at Module._compile (module.js:*:*)
-    at evalScript (node.js:*:*)
-    at Socket.<anonymous> (node.js:*:*)
-    at emitNone (events.js:*:*)
-    at Socket.emit (events.js:*:*)
-    at endReadableNT (_stream_readable.js:*:*)
-    at doNTCallback2 (node.js:*:*)
+    at node.js:*:*
+    at doNTCallback0 (node.js:*:*)
+    at process._tickCallback (node.js:*:*)
 
 [stdin]:1
 throw new Error("hello")
@@ -40,12 +33,9 @@ Error: hello
     at Object.exports.runInThisContext (vm.js:*)
     at Object.<anonymous> ([stdin]-wrapper:*:*)
     at Module._compile (module.js:*:*)
-    at evalScript (node.js:*:*)
-    at Socket.<anonymous> (node.js:*:*)
-    at emitNone (events.js:*:*)
-    at Socket.emit (events.js:*:*)
-    at endReadableNT (_stream_readable.js:*:*)
-    at doNTCallback2 (node.js:*:*)
+    at node.js:*:*
+    at doNTCallback0 (node.js:*:*)
+    at process._tickCallback (node.js:*:*)
 100
 
 [stdin]:1
@@ -56,12 +46,9 @@ ReferenceError: y is not defined
     at Object.exports.runInThisContext (vm.js:*)
     at Object.<anonymous> ([stdin]-wrapper:*:*)
     at Module._compile (module.js:*:*)
-    at evalScript (node.js:*:*)
-    at Socket.<anonymous> (node.js:*:*)
-    at emitNone (events.js:*:*)
-    at Socket.emit (events.js:*:*)
-    at endReadableNT (_stream_readable.js:*:*)
-    at doNTCallback2 (node.js:*:*)
+    at node.js:*:*
+    at doNTCallback0 (node.js:*:*)
+    at process._tickCallback (node.js:*:*)
 
 [stdin]:1
 var ______________________________________________; throw 10
diff --git a/test/parallel/test-cli-eval-event.js b/test/parallel/test-cli-eval-event.js
new file mode 100644 (file)
index 0000000..b19bdd3
--- /dev/null
@@ -0,0 +1,15 @@
+'use strict';
+
+const common = require('../common');
+const assert = require('assert');
+const spawn = require('child_process').spawn;
+
+const child = spawn(process.execPath, ['-e', `
+    const server = require('net').createServer().listen(0);
+    server.once('listening', server.close);
+`]);
+
+child.once('exit', common.mustCall(function(exitCode, signalCode) {
+  assert.equal(exitCode, 0);
+  assert.equal(signalCode, null);
+}));