diff --git a/src/library.js b/src/library.js index 1d7a50c9e..cfe8e7ddc 100644 --- a/src/library.js +++ b/src/library.js @@ -205,16 +205,6 @@ var Library = { }, _ZNSo3putEc: 'putchar', - getc: function(file) { - return -1; // EOF - }, - getc_unlocked: 'getc', - _IO_getc: 'getc', - - ungetc: function(chr, stream) { - return chr; - }, - _ZNSo5flushEv: function() { __print__('\n'); }, @@ -312,9 +302,13 @@ var Library = { var info = STDIO.streams[stream]; if (!info) return -1; if (info.interactiveInput) { - var text = intArrayFromString(window.prompt('?')); - for (var i = 0; i < text.length && i < size; i++) { - {{{ makeSetValue('ptr', '0', 'text[i]', 'i8') }}} + 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 + PRINTBUFFER = ''; + if (info.data.length === 0) return i; + } + {{{ makeSetValue('ptr', '0', 'info.data.shift()', 'i8') }}} ptr++; } return size; @@ -390,6 +384,7 @@ var Library = { fread__deps: ['$STDIO'], fread: function(ptr, size, count, stream) { var info = STDIO.streams[stream]; + if (info.interactiveInput) return STDIO.read(stream, ptr, size*count); for (var i = 0; i < count; i++) { if (info.position + size > info.data.length) { info.eof = 1; @@ -452,9 +447,22 @@ var Library = { fputc__deps: ['$STDIO'], fputc: function(chr, stream) { - if (!_fputc.ptr) _fputc.ptr = _malloc(1); - {{{ makeSetValue('_fputc.ptr', '0', 'chr', 'i8') }}} - STDIO.write(stream, _fputc.ptr, 1); + if (!Module._fputc_ptr) Module._fputc_ptr = _malloc(1); + {{{ makeSetValue('Module._fputc_ptr', '0', 'chr', 'i8') }}} + STDIO.write(stream, Module._fputc_ptr, 1); + }, + + getc: function(file) { + if (!Module._getc_ptr) Module._getc_ptr = _malloc(1); + var ret = STDIO.read(file, Module._getc_ptr, 1); + if (ret === 0) return -1; // EOF + return {{{ makeGetValue('Module._getc_ptr', '0', 'i8') }}} + }, + getc_unlocked: 'getc', + _IO_getc: 'getc', + + ungetc: function(chr, stream) { + return chr; }, // unix file IO, see http://rabbit.eng.miami.edu/info/functions/unixio.html diff --git a/tests/runner.py b/tests/runner.py index 76fab3698..34595497f 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -239,9 +239,12 @@ class RunnerCore(unittest.TestCase): def do_emscripten(self, filename, output_processor=None): # Run Emscripten exported_settings = {} - for setting in ['QUANTUM_SIZE', 'RELOOP', 'OPTIMIZE', 'ASSERTIONS', 'USE_TYPED_ARRAYS', 'SAFE_HEAP', 'CHECK_OVERFLOWS', 'CORRECT_OVERFLOWS', 'CORRECT_SIGNS', 'CHECK_SIGNS', 'CORRECT_OVERFLOWS_LINES', 'CORRECT_SIGNS_LINES', 'CORRECT_ROUNDINGS', 'CORRECT_ROUNDINGS_LINES', 'INVOKE_RUN', 'SAFE_HEAP_LINES', 'INIT_STACK', 'AUTO_OPTIMIZE']: - value = eval(setting) - exported_settings[setting] = value + for setting in ['QUANTUM_SIZE', 'RELOOP', 'OPTIMIZE', 'ASSERTIONS', 'USE_TYPED_ARRAYS', 'SAFE_HEAP', 'CHECK_OVERFLOWS', 'CORRECT_OVERFLOWS', 'CORRECT_SIGNS', 'CHECK_SIGNS', 'CORRECT_OVERFLOWS_LINES', 'CORRECT_SIGNS_LINES', 'CORRECT_ROUNDINGS', 'CORRECT_ROUNDINGS_LINES', 'INVOKE_RUN', 'SAFE_HEAP_LINES', 'INIT_STACK', 'AUTO_OPTIMIZE', 'EXPORTED_FUNCTIONS']: + try: + value = eval(setting) + exported_settings[setting] = value + except: + pass compiler_output = timeout_run(Popen([EMSCRIPTEN, filename + '.o.ll', str(exported_settings).replace("'", '"'), filename + '.o.js'], stdout=PIPE, stderr=STDOUT), TIMEOUT, 'Compiling') # Detect compilation crashes and errors @@ -2158,6 +2161,8 @@ if 'benchmark' not in sys.argv: global RELOOP; RELOOP = 0 # Too slow; we do care about typed arrays and OPTIMIZE though global SAFE_HEAP; SAFE_HEAP = 0 # Has bitfields which are false positives. Also the PyFloat_Init tries to detect endianness. global CORRECT_SIGNS; CORRECT_SIGNS = 1 # Not sure why, but needed + global EXPORTED_FUNCTIONS; EXPORTED_FUNCTIONS = ['_main', '_PyRun_SimpleStringFlags'] # for the demo + self.do_ll_test(path_from_root('tests', 'python', 'python.ll'), 'hello python world!\n\n[0, 2, 4, 6]\n\n5\n\n22\n\n5.470', args=['-S', '-c' '''print "hello python world!"; print [x*2 for x in range(4)]; t=2; print 10-3-t; print (lambda x: x*2)(11); print '%f' % 5.47'''])