From 1e68a1b78e68bec44796a80617f18d29e7d52255 Mon Sep 17 00:00:00 2001 From: Rick Gillaspy Date: Wed, 2 Jul 2025 15:12:02 -0400 Subject: [PATCH 1/5] add wasmfs_close_all --- src/lib/libsigs.js | 1 + src/lib/libwasmfs_opfs.js | 22 ++++++++++++++++++++- system/include/emscripten/wasmfs.h | 2 ++ system/lib/wasmfs/backends/opfs_backend.cpp | 9 +++++++++ system/lib/wasmfs/backends/opfs_backend.h | 2 ++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/lib/libsigs.js b/src/lib/libsigs.js index 4b19dec546636..b66d61e7a98bf 100644 --- a/src/lib/libsigs.js +++ b/src/lib/libsigs.js @@ -439,6 +439,7 @@ sigs = { _wasmfs_opfs_set_size_access__sig: 'vpijp', _wasmfs_opfs_set_size_file__sig: 'vpijp', _wasmfs_opfs_write_access__sig: 'iipij', + _wasmfs_opfs_close_all__sig: 'v', _wasmfs_stdin_get_char__sig: 'i', _wasmfs_thread_utils_heartbeat__sig: 'vp', alBuffer3f__sig: 'viifff', diff --git a/src/lib/libwasmfs_opfs.js b/src/lib/libwasmfs_opfs.js index 405891b05d0d6..ba65a24d27cb5 100644 --- a/src/lib/libwasmfs_opfs.js +++ b/src/lib/libwasmfs_opfs.js @@ -474,5 +474,25 @@ addToLibrary({ {{{ makeSetValue('errPtr', 0, 'err', 'i32') }}}; } wasmfsOPFSProxyFinish(ctx); - } + }, + + _wasmfs_opfs_close_all__deps: ['$wasmfsOPFSAccessHandles', '$wasmfsOPFSProxyFinish'], + _wasmfs_opfs_close_all__async: {{{ !PTHREADS }}}, + _wasmfs_opfs_close_all: async function(ctx) { + for (const i = 0; i < wasmfsOPFSAccessHandles.allocated.length; i++) + { + const handle = wasmfsOPFSAccessHandles.allocated[i]; + if (handle && handle.close) + { + try { + await handle.close(); + } catch (e) { + let err = -{{{ cDefs.EIO }}}; + {{{ makeSetValue('errPtr', 0, 'err', 'i32') }}}; + } + wasmfsOPFSAccessHandles.free(i); + } + } + wasmfsOPFSProxyFinish(ctx); + } }); diff --git a/system/include/emscripten/wasmfs.h b/system/include/emscripten/wasmfs.h index f7025dc6eb6cb..9f1c6b8f3aac4 100644 --- a/system/include/emscripten/wasmfs.h +++ b/system/include/emscripten/wasmfs.h @@ -91,6 +91,8 @@ backend_t wasmfs_create_node_backend(const char* root __attribute__((nonnull))); // thread. backend_t wasmfs_create_opfs_backend(void); +void wasmfs_close_opfs_backend(backend_t backend); + // Creates a generic JSIMPL backend backend_t wasmfs_create_jsimpl_backend(void); diff --git a/system/lib/wasmfs/backends/opfs_backend.cpp b/system/lib/wasmfs/backends/opfs_backend.cpp index 6a5be884cc38b..1183c24e3432e 100644 --- a/system/lib/wasmfs/backends/opfs_backend.cpp +++ b/system/lib/wasmfs/backends/opfs_backend.cpp @@ -393,6 +393,10 @@ class OPFSBackend : public Backend { // Symlinks not supported. return nullptr; } + + void closeAll() { + proxy([](auto ctx) { _wasmfs_opfs_close_all(ctx.ctx); }); + } }; } // anonymous namespace @@ -415,4 +419,9 @@ void EMSCRIPTEN_KEEPALIVE _wasmfs_opfs_record_entry( entries->push_back({name, File::FileKind(type), 0}); } +void wasmfs_close_opfs_backend(backend_t backend) { + auto opfsBackend = static_cast(backend); + opfsBackend->closeAll(); +} + } // extern "C" diff --git a/system/lib/wasmfs/backends/opfs_backend.h b/system/lib/wasmfs/backends/opfs_backend.h index fc484bdd067b9..683a25ce2ae75 100644 --- a/system/lib/wasmfs/backends/opfs_backend.h +++ b/system/lib/wasmfs/backends/opfs_backend.h @@ -104,4 +104,6 @@ void _wasmfs_opfs_set_size_file(em_proxying_ctx* ctx, void _wasmfs_opfs_flush_access(em_proxying_ctx* ctx, int access_id, int* err); +void _wasmfs_opfs_close_all(em_proxying_ctx* ctx); + } // extern "C" From 8c80c442a1c45cc7b6c8f239f95002754ef69752 Mon Sep 17 00:00:00 2001 From: Rick Gillaspy Date: Wed, 2 Jul 2025 16:07:08 -0400 Subject: [PATCH 2/5] fix const --- src/lib/libwasmfs_opfs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/libwasmfs_opfs.js b/src/lib/libwasmfs_opfs.js index ba65a24d27cb5..d5842d916f968 100644 --- a/src/lib/libwasmfs_opfs.js +++ b/src/lib/libwasmfs_opfs.js @@ -479,7 +479,7 @@ addToLibrary({ _wasmfs_opfs_close_all__deps: ['$wasmfsOPFSAccessHandles', '$wasmfsOPFSProxyFinish'], _wasmfs_opfs_close_all__async: {{{ !PTHREADS }}}, _wasmfs_opfs_close_all: async function(ctx) { - for (const i = 0; i < wasmfsOPFSAccessHandles.allocated.length; i++) + for (let i = 0; i < wasmfsOPFSAccessHandles.allocated.length; i++) { const handle = wasmfsOPFSAccessHandles.allocated[i]; if (handle && handle.close) From 61ab58b0f6468e5b6ec49989185783b3af7fbdc0 Mon Sep 17 00:00:00 2001 From: Rick Gillaspy Date: Thu, 3 Jul 2025 09:13:49 -0400 Subject: [PATCH 3/5] updates from code review --- src/lib/libsigs.js | 2 +- system/lib/wasmfs/backends/opfs_backend.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/libsigs.js b/src/lib/libsigs.js index b66d61e7a98bf..789cd18459ac1 100644 --- a/src/lib/libsigs.js +++ b/src/lib/libsigs.js @@ -439,7 +439,7 @@ sigs = { _wasmfs_opfs_set_size_access__sig: 'vpijp', _wasmfs_opfs_set_size_file__sig: 'vpijp', _wasmfs_opfs_write_access__sig: 'iipij', - _wasmfs_opfs_close_all__sig: 'v', + _wasmfs_opfs_close_all__sig: 'vp', _wasmfs_stdin_get_char__sig: 'i', _wasmfs_thread_utils_heartbeat__sig: 'vp', alBuffer3f__sig: 'viifff', diff --git a/system/lib/wasmfs/backends/opfs_backend.cpp b/system/lib/wasmfs/backends/opfs_backend.cpp index 1183c24e3432e..ac5d03544f6e2 100644 --- a/system/lib/wasmfs/backends/opfs_backend.cpp +++ b/system/lib/wasmfs/backends/opfs_backend.cpp @@ -421,6 +421,7 @@ void EMSCRIPTEN_KEEPALIVE _wasmfs_opfs_record_entry( void wasmfs_close_opfs_backend(backend_t backend) { auto opfsBackend = static_cast(backend); + assert(opfsBackend && "Invalid OPFS backend"); opfsBackend->closeAll(); } From 2160a01dd289e23e587f2c98a31deb815f034674 Mon Sep 17 00:00:00 2001 From: Rick Gillaspy Date: Thu, 3 Jul 2025 11:00:57 -0400 Subject: [PATCH 4/5] fix CI breaks. --- src/lib/libwasmfs_opfs.js | 5 +++-- test/other/codesize/test_codesize_hello_O0.size | 2 +- test/other/codesize/test_codesize_hello_O1.size | 2 +- test/other/codesize/test_codesize_hello_O2.size | 2 +- test/other/codesize/test_codesize_hello_O3.size | 2 +- test/other/codesize/test_codesize_hello_Os.size | 2 +- test/other/codesize/test_codesize_hello_dylink.size | 2 +- test/other/codesize/test_codesize_hello_single_file.jssize | 2 +- test/other/codesize/test_codesize_hello_wasmfs.size | 2 +- test/other/test_unoptimized_code_size_strict.wasm.size | 2 +- 10 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/lib/libwasmfs_opfs.js b/src/lib/libwasmfs_opfs.js index d5842d916f968..13cc48e782b03 100644 --- a/src/lib/libwasmfs_opfs.js +++ b/src/lib/libwasmfs_opfs.js @@ -487,8 +487,9 @@ addToLibrary({ try { await handle.close(); } catch (e) { - let err = -{{{ cDefs.EIO }}}; - {{{ makeSetValue('errPtr', 0, 'err', 'i32') }}}; +#if ASSERTIONS + err('unexpected error:', e, e.stack); +#endif } wasmfsOPFSAccessHandles.free(i); } diff --git a/test/other/codesize/test_codesize_hello_O0.size b/test/other/codesize/test_codesize_hello_O0.size index eec17de1a748f..3b8287d415f3a 100644 --- a/test/other/codesize/test_codesize_hello_O0.size +++ b/test/other/codesize/test_codesize_hello_O0.size @@ -1 +1 @@ -15133 +15135 diff --git a/test/other/codesize/test_codesize_hello_O1.size b/test/other/codesize/test_codesize_hello_O1.size index 6338b1987b648..cbb9ec4e57648 100644 --- a/test/other/codesize/test_codesize_hello_O1.size +++ b/test/other/codesize/test_codesize_hello_O1.size @@ -1 +1 @@ -2673 +2675 diff --git a/test/other/codesize/test_codesize_hello_O2.size b/test/other/codesize/test_codesize_hello_O2.size index 90e525730040d..f48626da3a84f 100644 --- a/test/other/codesize/test_codesize_hello_O2.size +++ b/test/other/codesize/test_codesize_hello_O2.size @@ -1 +1 @@ -1977 +1979 diff --git a/test/other/codesize/test_codesize_hello_O3.size b/test/other/codesize/test_codesize_hello_O3.size index b339887848ea0..b0539e90d9ac1 100644 --- a/test/other/codesize/test_codesize_hello_O3.size +++ b/test/other/codesize/test_codesize_hello_O3.size @@ -1 +1 @@ -1733 +1735 diff --git a/test/other/codesize/test_codesize_hello_Os.size b/test/other/codesize/test_codesize_hello_Os.size index a858d2d47bab2..1c38c9071a6a5 100644 --- a/test/other/codesize/test_codesize_hello_Os.size +++ b/test/other/codesize/test_codesize_hello_Os.size @@ -1 +1 @@ -1724 +1725 diff --git a/test/other/codesize/test_codesize_hello_dylink.size b/test/other/codesize/test_codesize_hello_dylink.size index afd43f8827d5b..dfa51a1359a00 100644 --- a/test/other/codesize/test_codesize_hello_dylink.size +++ b/test/other/codesize/test_codesize_hello_dylink.size @@ -1 +1 @@ -18547 +18549 diff --git a/test/other/codesize/test_codesize_hello_single_file.jssize b/test/other/codesize/test_codesize_hello_single_file.jssize index 8755c7be20a70..4cd877762ac5c 100644 --- a/test/other/codesize/test_codesize_hello_single_file.jssize +++ b/test/other/codesize/test_codesize_hello_single_file.jssize @@ -1 +1 @@ -6611 +6615 diff --git a/test/other/codesize/test_codesize_hello_wasmfs.size b/test/other/codesize/test_codesize_hello_wasmfs.size index b339887848ea0..b0539e90d9ac1 100644 --- a/test/other/codesize/test_codesize_hello_wasmfs.size +++ b/test/other/codesize/test_codesize_hello_wasmfs.size @@ -1 +1 @@ -1733 +1735 diff --git a/test/other/test_unoptimized_code_size_strict.wasm.size b/test/other/test_unoptimized_code_size_strict.wasm.size index eec17de1a748f..3b8287d415f3a 100644 --- a/test/other/test_unoptimized_code_size_strict.wasm.size +++ b/test/other/test_unoptimized_code_size_strict.wasm.size @@ -1 +1 @@ -15133 +15135 From f2e1fc2228d3dc3a878bd50d390f3c2c9d44af58 Mon Sep 17 00:00:00 2001 From: Rick Gillaspy Date: Thu, 3 Jul 2025 11:49:08 -0400 Subject: [PATCH 5/5] updates sizes --- test/code_size/audio_worklet_wasm.json | 6 +++--- test/code_size/embind_hello_wasm.json | 8 ++++---- test/code_size/hello_wasm_worker_wasm.json | 8 ++++---- test/code_size/hello_webgl2_wasm.json | 4 ++-- test/code_size/hello_webgl_wasm.json | 8 ++++---- test/code_size/hello_webgl_wasm2js.json | 8 ++++---- test/code_size/math_wasm.json | 8 ++++---- test/other/test_unoptimized_code_size.wasm.size | 2 +- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/test/code_size/audio_worklet_wasm.json b/test/code_size/audio_worklet_wasm.json index 5aad516bd2316..37e4532c6ea51 100644 --- a/test/code_size/audio_worklet_wasm.json +++ b/test/code_size/audio_worklet_wasm.json @@ -3,8 +3,8 @@ "a.html.gz": 364, "a.js": 3853, "a.js.gz": 2050, - "a.wasm": 1294, - "a.wasm.gz": 864, - "total": 5666, + "a.wasm": 1288, + "a.wasm.gz": 867, + "total": 5660, "total_gz": 3278 } diff --git a/test/code_size/embind_hello_wasm.json b/test/code_size/embind_hello_wasm.json index a9884cc49d60f..e0d0528b85c2d 100644 --- a/test/code_size/embind_hello_wasm.json +++ b/test/code_size/embind_hello_wasm.json @@ -3,8 +3,8 @@ "a.html.gz": 380, "a.js": 7266, "a.js.gz": 3321, - "a.wasm": 7300, - "a.wasm.gz": 3348, - "total": 15118, - "total_gz": 7049 + "a.wasm": 7294, + "a.wasm.gz": 3346, + "total": 15112, + "total_gz": 7047 } diff --git a/test/code_size/hello_wasm_worker_wasm.json b/test/code_size/hello_wasm_worker_wasm.json index 4b31168c56d25..b5e8b1476b786 100644 --- a/test/code_size/hello_wasm_worker_wasm.json +++ b/test/code_size/hello_wasm_worker_wasm.json @@ -3,8 +3,8 @@ "a.html.gz": 364, "a.js": 830, "a.js.gz": 530, - "a.wasm": 1891, - "a.wasm.gz": 1082, - "total": 3240, - "total_gz": 1976 + "a.wasm": 1885, + "a.wasm.gz": 1079, + "total": 3234, + "total_gz": 1973 } diff --git a/test/code_size/hello_webgl2_wasm.json b/test/code_size/hello_webgl2_wasm.json index c9afcedc357e2..f45711ef1d80d 100644 --- a/test/code_size/hello_webgl2_wasm.json +++ b/test/code_size/hello_webgl2_wasm.json @@ -3,8 +3,8 @@ "a.html.gz": 328, "a.js": 4386, "a.js.gz": 2252, - "a.wasm": 8286, - "a.wasm.gz": 5617, + "a.wasm": 8278, + "a.wasm.gz": 5627, "total": 13126, "total_gz": 8197 } diff --git a/test/code_size/hello_webgl_wasm.json b/test/code_size/hello_webgl_wasm.json index 2e1ba8e7f8d09..337555ae0e026 100644 --- a/test/code_size/hello_webgl_wasm.json +++ b/test/code_size/hello_webgl_wasm.json @@ -3,8 +3,8 @@ "a.html.gz": 328, "a.js": 3924, "a.js.gz": 2092, - "a.wasm": 8286, - "a.wasm.gz": 5617, - "total": 12664, - "total_gz": 8037 + "a.wasm": 8278, + "a.wasm.gz": 5627, + "total": 12656, + "total_gz": 8047 } diff --git a/test/code_size/hello_webgl_wasm2js.json b/test/code_size/hello_webgl_wasm2js.json index 3a2fcd28a1104..f7e13afe1e948 100644 --- a/test/code_size/hello_webgl_wasm2js.json +++ b/test/code_size/hello_webgl_wasm2js.json @@ -1,8 +1,8 @@ { "a.html": 346, "a.html.gz": 262, - "a.js": 17605, - "a.js.gz": 9614, - "total": 17951, - "total_gz": 9876 + "a.js": 17630, + "a.js.gz": 9603, + "total": 17976, + "total_gz": 9865 } diff --git a/test/code_size/math_wasm.json b/test/code_size/math_wasm.json index 9d06a35db4f41..0baeeb4cb0883 100644 --- a/test/code_size/math_wasm.json +++ b/test/code_size/math_wasm.json @@ -3,8 +3,8 @@ "a.html.gz": 380, "a.js": 110, "a.js.gz": 125, - "a.wasm": 2687, - "a.wasm.gz": 1658, - "total": 3349, - "total_gz": 2163 + "a.wasm": 2678, + "a.wasm.gz": 1655, + "total": 3340, + "total_gz": 2160 } diff --git a/test/other/test_unoptimized_code_size.wasm.size b/test/other/test_unoptimized_code_size.wasm.size index eec17de1a748f..3b8287d415f3a 100644 --- a/test/other/test_unoptimized_code_size.wasm.size +++ b/test/other/test_unoptimized_code_size.wasm.size @@ -1 +1 @@ -15133 +15135