Skip to content

Commit

Permalink
support closing the standard streams on node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
luite committed May 22, 2015
1 parent fbddb25 commit 4f2af22
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions pkg/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,8 @@ function h$unlockFile(fd) {


// engine-dependent setup
var h$base_readStdin, h$base_writeStderr, h$base_writeStdout;
var h$base_readStdin , h$base_writeStderr, h$base_writeStdout;
var h$base_closeStdin = null, h$base_closeStderr = null, h$base_closeStdout = null;
var h$base_readFile, h$base_writeFile, h$base_closeFile;
#ifndef GHCJS_BROWSER
var h$base_stdin_waiting = new h$Queue();
Expand Down Expand Up @@ -515,6 +516,12 @@ if(h$isNode) {
h$base_process_stdin();
}

h$base_closeStdin = function(fd, fdo, c) {
TRACE_IO("close stdin");
process.stdin.close();
c();
}

h$base_writeFile = function(fd, fdo, buf, buf_offset, n, c) {
var pos = typeof fdo.pos === 'number' ? fdo.pos : null;
TRACE_IO("base_writeFile: " + fd + " " + pos + " " + buf_offset + " " + n);
Expand All @@ -541,11 +548,23 @@ if(h$isNode) {
h$base_writeFile(1, fdo, buf, buf_offset, n, c);
}

h$base_closeStdout = function(fd, fdo, c) {
TRACE_IO("close stdout");
process.stdout.close();
c();
}

h$base_writeStderr = function(fd, fdo, buf, buf_offset, n, c) {
TRACE_IO("write stderr");
h$base_writeFile(2, fdo, buf, buf_offset, n, c);
}

h$base_closeStderr = function(fd, fdo, c) {
TRACE_IO("close stderr");
process.stderr.close();
c();
}

process.stdin.on('readable', h$base_process_stdin);
process.stdin.on('end', function() { h$base_stdin_eof = true; h$base_process_stdin(); });

Expand Down Expand Up @@ -606,9 +625,9 @@ if(h$isNode) {
}
#endif

var h$base_stdin_fd = { read: h$base_readStdin };
var h$base_stdout_fd = { write: h$base_writeStdout };
var h$base_stderr_fd = { write: h$base_writeStderr };
var h$base_stdin_fd = { read: h$base_readStdin, close: h$base_closeStdin };
var h$base_stdout_fd = { write: h$base_writeStdout, close: h$base_closeStdout };
var h$base_stderr_fd = { write: h$base_writeStderr, close: h$base_closeStderr };

var h$base_fdN = -1; // negative file descriptors are 'virtual'
var h$base_fds = [h$base_stdin_fd, h$base_stdout_fd, h$base_stderr_fd];
Expand Down

0 comments on commit 4f2af22

Please sign in to comment.