kripken/emscripten — pull request #1408 by rfk (2013-07-18 11:51:28+00:00)
Title: Make stdin/stdout/stderr be 3/4/5, and alias them to 0/1/2.

I've been working on compiling pypy with emscripten. Unfortunately it generates code with hard-coded references to the assumed stdio file descriptor numbers, like write(1, buf) to write to stdout. Obviously this doesn't work well with emscripten, but fixing it to look up the defined value of STDOUT_FILENO is not straightforward.

Would you be open to something like the attached to support code that assumes stdin=0, stdout=1, etc?
The idea is:

* Start fds at 3, so stdin=3, stdout=4 and stdin=5. This lets us unambiguously identify non-compliant code that is passing in a hard-coded fd of 0, 1 or 2.
* Alias 0,1,2 to 3,4,5 in the FS.streams array. This makes low-level calls like write(1, buf) work transparently, but avoids passing out the aliased fds to calling code where a zero value might cause problems.