diff --git a/src/library_nodefs.js b/src/library_nodefs.js index 168edee8a52c..94c3b2c99caf 100644 --- a/src/library_nodefs.js +++ b/src/library_nodefs.js @@ -292,6 +292,29 @@ mergeInto(LibraryManager.library, { } return position; + }, + mmap: function(stream, buffer, offset, length, position, prot, flags) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError({{{ cDefine('ENODEV') }}}); + } + var ptr = _malloc(length); + + assert(offset === 0); + NODEFS.stream_ops.read(stream, buffer, ptr + offset, length, position); + + return { ptr: ptr, allocated: true }; + }, + msync: function(stream, buffer, offset, length, mmapFlags) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError({{{ cDefine('ENODEV') }}}); + } + if (mmapFlags & {{{ cDefine('MAP_PRIVATE') }}}) { + // MAP_PRIVATE calls need not to be synced back to underlying fs + return 0; + } + + var bytesWritten = NODEFS.stream_ops.write(stream, buffer, 0, length, offset, false); + return 0; } } } diff --git a/tests/fs/test_mmap.c b/tests/fs/test_mmap.c index b61856f2a86f..263ccca8e193 100644 --- a/tests/fs/test_mmap.c +++ b/tests/fs/test_mmap.c @@ -21,6 +21,9 @@ int main() { EM_ASM( FS.mkdir('yolo'); + #if NODEFS + FS.mount(NODEFS, { root: '.' }, 'yolo'); + #endif FS.writeFile('/yolo/in.txt', 'mmap ftw!'); ); @@ -176,7 +179,7 @@ int main() { printf("/yolo/sharedoffset.txt content=%s %d\n", buffer + offset, offset); fclose(fd); } - +#if !defined(NODEFS) /** * MMAP to an 'over-allocated' file * @@ -219,6 +222,7 @@ int main() { close(fd); } +#endif return 0; } diff --git a/tests/test_core.py b/tests/test_core.py index 015b8e14f2d6..2cb8666d4221 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -5320,10 +5320,12 @@ def test_fs_append(self, js_engines=None): def test_fs_mmap(self): orig_compiler_opts = self.emcc_args[:] - for fs in ['MEMFS']: + for fs in ['MEMFS', 'NODEFS']: src = path_from_root('tests', 'fs', 'test_mmap.c') out = path_from_root('tests', 'fs', 'test_mmap.out') self.emcc_args = orig_compiler_opts + ['-D' + fs] + if fs == 'NODEFS': + self.emcc_args += ['-lnodefs.js'] self.do_run_from_file(src, out) @also_with_noderawfs