Skip to content

Commit 49f63ca

Browse files
authored
[WasmFS] Make the Node backend multi-env compatible (#26624)
Follow-up to #26608.
1 parent 557c3fb commit 49f63ca

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/lib/libwasmfs_node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
var wasmFSNodeLibrary = {
8-
$wasmfsNodeIsWindows: "!!process.platform.match(/^win/)",
8+
$wasmfsNodeIsWindows: "!!globalThis.process?.platform.match(/^win/)",
99

1010
$wasmfsNodeConvertNodeCode__deps: ['$ERRNO_CODES'],
1111
$wasmfsNodeConvertNodeCode: (e) => {

test/test_browser.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5347,6 +5347,35 @@ def test_wasmfs_opfs_errors(self):
53475347
args = ['-sWASMFS', '-pthread', '-sPROXY_TO_PTHREAD', '--post-js', postjs]
53485348
self.btest(test, cflags=args, expected='0')
53495349

5350+
def test_wasmfs_multi_environment(self):
5351+
# Test that WasmFS's Node backend can be enabled conditionally, allowing
5352+
# the same binaries to run on both web and Node.js environments.
5353+
create_file('main.c', r'''
5354+
#include <stdio.h>
5355+
#include <assert.h>
5356+
#include <unistd.h>
5357+
5358+
#include <emscripten/emscripten.h>
5359+
#include <emscripten/wasmfs.h>
5360+
5361+
EM_JS(bool, is_node, (), { return ENVIRONMENT_IS_NODE; });
5362+
5363+
// This is equivalent to building with `-sWASMFS -sNODERAWFS`, except
5364+
// that the Wasm binary can also be used on the web.
5365+
backend_t wasmfs_create_root_dir() {
5366+
return is_node() ? wasmfs_create_node_backend("")
5367+
: wasmfs_create_memory_backend();
5368+
}
5369+
5370+
int main(int argc, char** argv) {
5371+
printf("testing access to /tmp\n");
5372+
int rtn = access("/tmp", F_OK);
5373+
assert(rtn == 0);
5374+
return 0;
5375+
}
5376+
''')
5377+
self.btest_exit('main.c', cflags=['-sWASMFS', '-sENVIRONMENT=web,node'])
5378+
53505379
@no_firefox('no 4GB support yet')
53515380
def test_emmalloc_memgrowth(self):
53525381
if not self.is_4gb():

0 commit comments

Comments
 (0)