diff --git a/src/lib/libsigs.js b/src/lib/libsigs.js index 4b19dec546636..789cd18459ac1 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: 'vp', _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..13cc48e782b03 100644 --- a/src/lib/libwasmfs_opfs.js +++ b/src/lib/libwasmfs_opfs.js @@ -474,5 +474,26 @@ 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 (let i = 0; i < wasmfsOPFSAccessHandles.allocated.length; i++) + { + const handle = wasmfsOPFSAccessHandles.allocated[i]; + if (handle && handle.close) + { + try { + await handle.close(); + } catch (e) { +#if ASSERTIONS + err('unexpected error:', e, e.stack); +#endif + } + 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..ac5d03544f6e2 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,10 @@ 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); + assert(opfsBackend && "Invalid OPFS 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" 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/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.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 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