diff options
-rw-r--r-- | src/library_fs.js | 16 | ||||
-rw-r--r-- | tests/test_other.py | 43 |
2 files changed, 56 insertions, 3 deletions
diff --git a/src/library_fs.js b/src/library_fs.js index 16512385..5412185f 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -361,9 +361,10 @@ mergeInto(LibraryManager.library, { // SOCKFS is completed. createStream: function(stream, fd_start, fd_end) { if (!FS.FSStream) { - FS.FSStream = {}; + FS.FSStream = function(){}; + FS.FSStream.prototype = {}; // compatibility - Object.defineProperties(FS.FSStream, { + Object.defineProperties(FS.FSStream.prototype, { object: { get: function() { return this.node; }, set: function(val) { this.node = val; } @@ -379,7 +380,16 @@ mergeInto(LibraryManager.library, { } }); } - stream.prototype = FS.FSStream; + if (stream.__proto__) { + // reuse the object + stream.__proto__ = FS.FSStream.prototype; + } else { + var newStream = new FS.FSStream(); + for (var p in stream) { + newStream[p] = stream[p]; + } + stream = newStream; + } var fd = FS.nextfd(fd_start, fd_end); stream.fd = fd; FS.streams[fd] = stream; diff --git a/tests/test_other.py b/tests/test_other.py index 86e0eadf..584f6714 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -2010,6 +2010,49 @@ a(int [32], char [5]*) try_delete(path_from_root('tests', 'Module-exports', 'test.js')) try_delete(path_from_root('tests', 'Module-exports', 'test.js.map')) + def test_fs_stream_proto(self): + open('src.cpp', 'w').write(r''' +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#include <errno.h> +#include <string.h> + +int main() +{ + int file_size = 0; + int h = open("src.cpp", O_RDONLY, 0666); + if (0 != h) + { + FILE* file = fdopen(h, "rb"); + if (0 != file) + { + fseek(file, 0, SEEK_END); + file_size = ftell(file); + fseek(file, 0, SEEK_SET); + } + else + { + printf("fdopen() failed: %s\n", strerror(errno)); + return 10; + } + close(h); + printf("File size: %d\n", file_size); + } + else + { + printf("open() failed: %s\n", strerror(errno)); + return 10; + } + return 0; +} + ''') + Popen([PYTHON, EMCC, 'src.cpp', '--embed-file', 'src.cpp']).communicate() + for engine in JS_ENGINES: + out = run_js('a.out.js', engine=engine, stderr=PIPE, full_output=True) + self.assertContained('File size: 722', out) + def test_simd(self): self.clear() Popen([PYTHON, EMCC, path_from_root('tests', 'linpack.c'), '-O2', '-DSP', '--llvm-opts', '''['-O3', '-vectorize', '-vectorize-loops', '-bb-vectorize-vector-bits=128', '-force-vector-width=4']''']).communicate() |