From fe5f5d0a8a0bf8354235ce660eff38da50c8d79d Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Sat, 4 Apr 2026 12:13:24 +0200 Subject: [PATCH] [WasmFS] Make the Node backend multi-env compatible --- src/lib/libwasmfs_node.js | 2 +- test/test_browser.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lib/libwasmfs_node.js b/src/lib/libwasmfs_node.js index 7f4680bc3013d..d200968433791 100644 --- a/src/lib/libwasmfs_node.js +++ b/src/lib/libwasmfs_node.js @@ -5,7 +5,7 @@ */ var wasmFSNodeLibrary = { - $wasmfsNodeIsWindows: "!!process.platform.match(/^win/)", + $wasmfsNodeIsWindows: "!!globalThis.process?.platform.match(/^win/)", $wasmfsNodeConvertNodeCode__deps: ['$ERRNO_CODES'], $wasmfsNodeConvertNodeCode: (e) => { diff --git a/test/test_browser.py b/test/test_browser.py index 7f43824793515..bf76a59441722 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -5347,6 +5347,35 @@ def test_wasmfs_opfs_errors(self): args = ['-sWASMFS', '-pthread', '-sPROXY_TO_PTHREAD', '--post-js', postjs] self.btest(test, cflags=args, expected='0') + def test_wasmfs_multi_environment(self): + # Test that WasmFS's Node backend can be enabled conditionally, allowing + # the same binaries to run on both web and Node.js environments. + create_file('main.c', r''' + #include + #include + #include + + #include + #include + + EM_JS(bool, is_node, (), { return ENVIRONMENT_IS_NODE; }); + + // This is equivalent to building with `-sWASMFS -sNODERAWFS`, except + // that the Wasm binary can also be used on the web. + backend_t wasmfs_create_root_dir() { + return is_node() ? wasmfs_create_node_backend("") + : wasmfs_create_memory_backend(); + } + + int main(int argc, char** argv) { + printf("testing access to /tmp\n"); + int rtn = access("/tmp", F_OK); + assert(rtn == 0); + return 0; + } + ''') + self.btest_exit('main.c', cflags=['-sWASMFS', '-sENVIRONMENT=web,node']) + @no_firefox('no 4GB support yet') def test_emmalloc_memgrowth(self): if not self.is_4gb():