Skip to content

Commit 3188415

Browse files
feat(nodefs): mmap and msync
add implementation of mmap and msync for NODEFS
1 parent 9fdc8af commit 3188415

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/library_nodefs.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,29 @@ mergeInto(LibraryManager.library, {
292292
}
293293

294294
return position;
295+
},
296+
mmap: function(stream, buffer, offset, length, position, prot, flags) {
297+
if (!FS.isFile(stream.node.mode)) {
298+
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
299+
}
300+
var ptr = _malloc(length);
301+
302+
assert(offset === 0);
303+
NODEFS.stream_ops.read(stream, buffer, ptr + offset, length, position);
304+
305+
return { ptr: ptr, allocated: true };
306+
},
307+
msync: function(stream, buffer, offset, length, mmapFlags) {
308+
if (!FS.isFile(stream.node.mode)) {
309+
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
310+
}
311+
if (mmapFlags & {{{ cDefine('MAP_PRIVATE') }}}) {
312+
// MAP_PRIVATE calls need not to be synced back to underlying fs
313+
return 0;
314+
}
315+
316+
var bytesWritten = NODEFS.stream_ops.write(stream, buffer, 0, length, offset, false);
317+
return 0;
295318
}
296319
}
297320
}

tests/fs/test_mmap.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
int main() {
2222
EM_ASM(
2323
FS.mkdir('yolo');
24+
#if NODEFS
25+
FS.mount(NODEFS, { root: '.' }, 'yolo');
26+
#endif
2427
FS.writeFile('/yolo/in.txt', 'mmap ftw!');
2528
);
2629

@@ -176,7 +179,7 @@ int main() {
176179
printf("/yolo/sharedoffset.txt content=%s %d\n", buffer + offset, offset);
177180
fclose(fd);
178181
}
179-
182+
#if !defined(NODEFS)
180183
/**
181184
* MMAP to an 'over-allocated' file
182185
*
@@ -219,6 +222,7 @@ int main() {
219222

220223
close(fd);
221224
}
225+
#endif
222226

223227
return 0;
224228
}

tests/test_core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5320,10 +5320,12 @@ def test_fs_append(self, js_engines=None):
53205320

53215321
def test_fs_mmap(self):
53225322
orig_compiler_opts = self.emcc_args[:]
5323-
for fs in ['MEMFS']:
5323+
for fs in ['MEMFS', 'NODEFS']:
53245324
src = path_from_root('tests', 'fs', 'test_mmap.c')
53255325
out = path_from_root('tests', 'fs', 'test_mmap.out')
53265326
self.emcc_args = orig_compiler_opts + ['-D' + fs]
5327+
if fs == 'NODEFS':
5328+
self.emcc_args += ['-lnodefs.js']
53275329
self.do_run_from_file(src, out)
53285330

53295331
@also_with_noderawfs

0 commit comments

Comments
 (0)