aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2011-06-19 12:03:10 -0700
committerAlon Zakai <alonzakai@gmail.com>2011-06-19 12:03:10 -0700
commitf83a7e74298a283425248adf1dc8a0b9c07beaee (patch)
tree7c6dd1efcb18345522c537d14a58364f233b6826
parent1eab898b003282bdd4b4839090ec9a6093c0e331 (diff)
support for customizing interactive input by setting Module.stdin
-rw-r--r--src/library.js49
-rw-r--r--tests/files.cpp1
-rw-r--r--tests/runner.py7
3 files changed, 42 insertions, 15 deletions
diff --git a/src/library.js b/src/library.js
index cfe8e7dd..97f7f61e 100644
--- a/src/library.js
+++ b/src/library.js
@@ -243,7 +243,12 @@ var Library = {
// stdio.h - file functions
+ stdin: 0,
+ stdout: 0,
+ stderr: 0,
+
$STDIO__postset: 'STDIO.init()',
+ $STDIO__deps: ['stdin', 'stdout', 'stderr'],
$STDIO: {
streams: {},
filenames: {},
@@ -252,18 +257,27 @@ var Library = {
SEEK_CUR: 1, /* Current position. */
SEEK_END: 2, /* End of file. */
init: function() {
- try {
- _stdin = Pointer_make([0], null, ALLOC_STATIC, 'void*');
- {{{ makeSetValue('_stdin', '0', "STDIO.prepare('<<stdin>>', null, null, true)", 'i32') }}};
- } catch(e){} // stdin/out/err may not exist if not needed
- try {
- _stdout = Pointer_make([0], null, ALLOC_STATIC, 'void*');
- {{{ makeSetValue('_stdout', '0', "STDIO.prepare('<<stdin>>', null, true)", 'i32') }}};
- } catch(e){}
- try {
- _stderr = Pointer_make([0], null, ALLOC_STATIC, 'void*');
- {{{ makeSetValue('_stderr', '0', "STDIO.prepare('<<stdin>>', null, true)", 'i32') }}};
- } catch(e){}
+ _stdin = Pointer_make([0], null, ALLOC_STATIC, 'void*');
+ {{{ makeSetValue('_stdin', '0', "STDIO.prepare('<<stdin>>', null, null, true)", 'i32') }}};
+ if (Module.stdin) {
+ // Make sure stdin returns a newline
+ var orig = Module.stdin;
+ Module.stdin = function stdinFixed(prompt) {
+ var ret = orig(prompt);
+ if (ret[ret.length-1] !== '\n') ret = ret + '\n';
+ return ret;
+ }
+ } else {
+ Module.stdin = function stdin(prompt) {
+ return window.prompt(prompt);
+ };
+ }
+
+ _stdout = Pointer_make([0], null, ALLOC_STATIC, 'void*');
+ {{{ makeSetValue('_stdout', '0', "STDIO.prepare('<<stdin>>', null, true)", 'i32') }}};
+
+ _stderr = Pointer_make([0], null, ALLOC_STATIC, 'void*');
+ {{{ makeSetValue('_stderr', '0', "STDIO.prepare('<<stdin>>', null, true)", 'i32') }}};
},
cleanFilename: function(filename) {
return filename.replace('./', '');
@@ -304,7 +318,7 @@ var Library = {
if (info.interactiveInput) {
for (var i = 0; i < size; i++) {
if (info.data.length === 0) {
- info.data = intArrayFromString(window.prompt(PRINTBUFFER.length > 0 ? PRINTBUFFER : '?')).map(function(x) { return x === 0 ? 10 : x }); // change 0 to newline
+ info.data = intArrayFromString(Module.stdin(PRINTBUFFER.length > 0 ? PRINTBUFFER : '?')).map(function(x) { return x === 0 ? 10 : x }); // change 0 to newline
PRINTBUFFER = '';
if (info.data.length === 0) return i;
}
@@ -465,6 +479,15 @@ var Library = {
return chr;
},
+ gets: function(ptr) {
+ var num = 0;
+ while (STDIO.read({{{ makeGetValue('_stdin', '0', 'void*') }}}, ptr+num, 1) &&
+ {{{ makeGetValue('ptr', 'num', 'i8') }}} !== 10) { num++; }
+ if (num === 0) return 0;
+ {{{ makeSetValue('ptr', 'num', 0, 'i8') }}}
+ return ptr;
+ },
+
// unix file IO, see http://rabbit.eng.miami.edu/info/functions/unixio.html
open: function(filename, flags, mode) {
diff --git a/tests/files.cpp b/tests/files.cpp
index 99eddec5..202c03bb 100644
--- a/tests/files.cpp
+++ b/tests/files.cpp
@@ -30,6 +30,7 @@ int main()
// Standard streams
+ printf("input:%s\n", gets((char*)malloc(1024)));
fwrite("texto\n", 1, 6, stdout);
fwrite("texte\n", 1, 6, stderr);
diff --git a/tests/runner.py b/tests/runner.py
index 34595497..80503de6 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -1769,7 +1769,10 @@ if 'benchmark' not in sys.argv:
def post(filename):
src = open(filename, 'r').read().replace(
'// {{PRE_RUN_ADDITIONS}}',
- '''STDIO.prepare('somefile.binary', [100, 200, 50, 25, 10, 77, 123]);''' # 200 becomes -56, since signed chars are used in memory
+ '''
+ STDIO.prepare('somefile.binary', [100, 200, 50, 25, 10, 77, 123]); // 200 becomes -56, since signed chars are used in memory
+ Module.stdin = function(prompt) { return 'hi there!' };
+ '''
)
open(filename, 'w').write(src)
@@ -1778,7 +1781,7 @@ if 'benchmark' not in sys.argv:
other.close()
src = open(path_from_root('tests', 'files.cpp'), 'r').read()
- self.do_test(src, 'size: 7\ndata: 100,-56,50,25,10,77,123\ntexto\ntexte\n5 : 10,30,20,11,88\nother=some data.\n', post_build=post)
+ self.do_test(src, 'size: 7\ndata: 100,-56,50,25,10,77,123\ninput:hi there!\ntexto\ntexte\n5 : 10,30,20,11,88\nother=some data.\n', post_build=post)
### 'Big' tests