Skip to content

Commit 93a44d5

Browse files
committed
src: fix deferred events not working with -e
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: #1600 PR-URL: #1793 Reviewed-By: Trevor Norris <[email protected]>
1 parent eb1856d commit 93a44d5

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

src/node.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,13 @@
558558
'return require("vm").runInThisContext(' +
559559
JSON.stringify(body) + ', { filename: ' +
560560
JSON.stringify(name) + ' });\n';
561-
var result = module._compile(script, name + '-wrapper');
562-
if (process._print_eval) console.log(result);
561+
// Defer evaluation for a tick. This is a workaround for deferred
562+
// events not firing when evaluating scripts from the command line,
563+
// see https://github.com/nodejs/io.js/issues/1600.
564+
process.nextTick(function() {
565+
var result = module._compile(script, name + '-wrapper');
566+
if (process._print_eval) console.log(result);
567+
});
563568
}
564569

565570
function createWritableStdioStream(fd) {

test/message/eval_messages.out

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ SyntaxError: Strict mode code may not include a with statement
66
at Object.exports.runInThisContext (vm.js:*)
77
at Object.<anonymous> ([eval]-wrapper:*:*)
88
at Module._compile (module.js:*:*)
9-
at evalScript (node.js:*:*)
10-
at startup (node.js:*:*)
119
at node.js:*:*
10+
at doNTCallback0 (node.js:*:*)
11+
at process._tickCallback (node.js:*:*)
1212
42
1313
42
1414
[eval]:1
@@ -19,9 +19,9 @@ Error: hello
1919
at Object.exports.runInThisContext (vm.js:*)
2020
at Object.<anonymous> ([eval]-wrapper:*:*)
2121
at Module._compile (module.js:*:*)
22-
at evalScript (node.js:*:*)
23-
at startup (node.js:*:*)
2422
at node.js:*:*
23+
at doNTCallback0 (node.js:*:*)
24+
at process._tickCallback (node.js:*:*)
2525
[eval]:1
2626
throw new Error("hello")
2727
^
@@ -30,9 +30,9 @@ Error: hello
3030
at Object.exports.runInThisContext (vm.js:*)
3131
at Object.<anonymous> ([eval]-wrapper:*:*)
3232
at Module._compile (module.js:*:*)
33-
at evalScript (node.js:*:*)
34-
at startup (node.js:*:*)
3533
at node.js:*:*
34+
at doNTCallback0 (node.js:*:*)
35+
at process._tickCallback (node.js:*:*)
3636
100
3737
[eval]:1
3838
var x = 100; y = x;
@@ -42,9 +42,9 @@ ReferenceError: y is not defined
4242
at Object.exports.runInThisContext (vm.js:*)
4343
at Object.<anonymous> ([eval]-wrapper:*:*)
4444
at Module._compile (module.js:*:*)
45-
at evalScript (node.js:*:*)
46-
at startup (node.js:*:*)
4745
at node.js:*:*
46+
at doNTCallback0 (node.js:*:*)
47+
at process._tickCallback (node.js:*:*)
4848
[eval]:1
4949
var ______________________________________________; throw 10
5050
^

test/message/stdin_messages.out

+11-24
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ SyntaxError: Strict mode code may not include a with statement
77
at Object.exports.runInThisContext (vm.js:*)
88
at Object.<anonymous> ([stdin]-wrapper:*:*)
99
at Module._compile (module.js:*:*)
10-
at evalScript (node.js:*:*)
11-
at Socket.<anonymous> (node.js:*:*)
12-
at emitNone (events.js:*:*)
13-
at Socket.emit (events.js:*:*)
14-
at endReadableNT (_stream_readable.js:*:*)
15-
at doNTCallback2 (node.js:*:*)
10+
at node.js:*:*
11+
at doNTCallback0 (node.js:*:*)
1612
at process._tickCallback (node.js:*:*)
1713
42
1814
42
@@ -25,12 +21,9 @@ Error: hello
2521
at Object.exports.runInThisContext (vm.js:*)
2622
at Object.<anonymous> ([stdin]-wrapper:*:*)
2723
at Module._compile (module.js:*:*)
28-
at evalScript (node.js:*:*)
29-
at Socket.<anonymous> (node.js:*:*)
30-
at emitNone (events.js:*:*)
31-
at Socket.emit (events.js:*:*)
32-
at endReadableNT (_stream_readable.js:*:*)
33-
at doNTCallback2 (node.js:*:*)
24+
at node.js:*:*
25+
at doNTCallback0 (node.js:*:*)
26+
at process._tickCallback (node.js:*:*)
3427

3528
[stdin]:1
3629
throw new Error("hello")
@@ -40,12 +33,9 @@ Error: hello
4033
at Object.exports.runInThisContext (vm.js:*)
4134
at Object.<anonymous> ([stdin]-wrapper:*:*)
4235
at Module._compile (module.js:*:*)
43-
at evalScript (node.js:*:*)
44-
at Socket.<anonymous> (node.js:*:*)
45-
at emitNone (events.js:*:*)
46-
at Socket.emit (events.js:*:*)
47-
at endReadableNT (_stream_readable.js:*:*)
48-
at doNTCallback2 (node.js:*:*)
36+
at node.js:*:*
37+
at doNTCallback0 (node.js:*:*)
38+
at process._tickCallback (node.js:*:*)
4939
100
5040

5141
[stdin]:1
@@ -56,12 +46,9 @@ ReferenceError: y is not defined
5646
at Object.exports.runInThisContext (vm.js:*)
5747
at Object.<anonymous> ([stdin]-wrapper:*:*)
5848
at Module._compile (module.js:*:*)
59-
at evalScript (node.js:*:*)
60-
at Socket.<anonymous> (node.js:*:*)
61-
at emitNone (events.js:*:*)
62-
at Socket.emit (events.js:*:*)
63-
at endReadableNT (_stream_readable.js:*:*)
64-
at doNTCallback2 (node.js:*:*)
49+
at node.js:*:*
50+
at doNTCallback0 (node.js:*:*)
51+
at process._tickCallback (node.js:*:*)
6552

6653
[stdin]:1
6754
var ______________________________________________; throw 10

test/parallel/test-cli-eval-event.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const spawn = require('child_process').spawn;
6+
7+
const child = spawn(process.execPath, ['-e', `
8+
const server = require('net').createServer().listen(0);
9+
server.once('listening', server.close);
10+
`]);
11+
12+
child.once('exit', common.mustCall(function(exitCode, signalCode) {
13+
assert.equal(exitCode, 0);
14+
assert.equal(signalCode, null);
15+
}));

0 commit comments

Comments
 (0)