Skip to content

Commit 8fb1209

Browse files
committed
Refactor __wasi_fd_sync implementation
- Always call stream.stream_ops.fsync when its available. - Only use `Asyncify.handleSleep` when needed.
1 parent 1cd1c2e commit 8fb1209

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

src/lib/libwasi.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -540,34 +540,27 @@ var WasiLibrary = {
540540
return 0;
541541
},
542542

543+
fd_sync__async: true,
543544
fd_sync: (fd) => {
544545
#if SYSCALLS_REQUIRE_FILESYSTEM
545546
var stream = SYSCALLS.getStreamFromFD(fd);
547+
var rtn = stream.stream_ops.fsync?.(stream);
546548
#if ASYNCIFY
547-
return Asyncify.handleSleep((wakeUp) => {
548-
var mount = stream.node.mount;
549-
if (!mount.type.syncfs) {
550-
// We write directly to the file system, so there's nothing to do here.
551-
wakeUp(0);
552-
return;
553-
}
554-
mount.type.syncfs(mount, false, (err) => {
555-
wakeUp(err ? {{{ cDefs.EIO }}} : 0);
549+
var mount = stream.node.mount;
550+
if (mount.type.syncfs) {
551+
return Asyncify.handleSleep((wakeUp) => {
552+
mount.type.syncfs(mount, false, (err) => wakeUp(err ? {{{ cDefs.EIO }}} : 0));
556553
});
557-
});
558-
#else
559-
if (stream.stream_ops?.fsync) {
560-
return stream.stream_ops.fsync(stream);
561554
}
562-
return 0; // we can't do anything synchronously; the in-memory FS is already synced to
563555
#endif // ASYNCIFY
564-
#elif ASSERTIONS
556+
return rtn;
557+
#else // SYSCALLS_REQUIRE_FILESYSTEM
558+
#if ASSERTIONS
565559
abort('fd_sync called without SYSCALLS_REQUIRE_FILESYSTEM');
566-
#else
560+
#endif
567561
return {{{ cDefs.ENOSYS }}};
568562
#endif // SYSCALLS_REQUIRE_FILESYSTEM
569563
},
570-
fd_sync__async: true,
571564

572565
// random.h
573566

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 245482,
2+
"a.out.js": 245471,
33
"a.out.nodebug.wasm": 573657,
4-
"total": 819139,
4+
"total": 819128,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

0 commit comments

Comments
 (0)