Skip to content

PackageToJS: Import only the UWASI features Swift needs - #33

Closed
krodak wants to merge 1 commit into
mainfrom
kr/package-to-js-uwasi-minimal
Closed

krodak wants to merge 1 commit into
mainfrom
kr/package-to-js-uwasi-minimal

Conversation

@krodak

@krodak krodak commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

With --wasi-runtime uwasi, the generated platform wrappers import useAll and MemoryFileSystem, so every bundle ships UWASI's in-memory file system. That makes it larger than the @bjorn3/browser_wasi_shim default, and most apps never open a file.

Bundled with esbuild (minified, gzip):

Setup Size
@bjorn3/browser_wasi_shim (current default) 4.9 KB
UWASI with useAll (before) 8.8 KB
UWASI without the file system (after) 4.8 KB

1. Default feature set

The wrappers now import useStdio, useArgs, useEnviron, useClock, usePoll, useProc and useRandom. A Swift executable needs args, environ, random, clock and proc to start, and wasi-libc needs fd_prestat_get to return EBADF while it scans for preopens. Stdio also keeps runtime errors such as Fatal error: Index out of range visible. File syscalls now return ENOSYS.

2. Code coverage

With --enable-code-coverage, the Node wrapper adds useMemoryFS, because the test runner reads the coverage profile through extractFile. The browser wrapper never imports a file system, and nothing imports useAll.

3. Custom setups

An app that needs a file system or a different feature set can pass its own wasi object to the generated instantiate.js. The README and the package output docs describe this. If that turns out to be too much work for users, we can add a config flag later.

Test Plan

  • swift test --package-path ./Plugins/PackageToJS
  • node --test Plugins/PackageToJS/Tests/Inputs/UWASIAdapterTests.mjs
  • Examples/NodeJS with --wasi-runtime uwasi: node main.mjs prints the greeting, and the generated wrappers import no file system
  • A Swift executable that calls print and fopen: output works, and fopen fails without the file system

The UWASI platform wrappers imported `useAll` and `MemoryFileSystem`,
so bundles always carried UWASI's in-memory file system and came out
larger than the browser_wasi_shim default. Import `useStdio`, `useArgs`,
`useEnviron`, `useClock`, `usePoll`, `useProc` and `useRandom` instead.
File syscalls now return ENOSYS.

With `--enable-code-coverage`, the Node wrapper adds `useMemoryFS`,
because the test runner reads the coverage profile through
`extractFile`. The browser wrapper never imports a file system.
@krodak
krodak force-pushed the kr/package-to-js-uwasi-minimal branch from e89d7ae to 817304f Compare November 3, 2026 15:14
@krodak krodak changed the title PackageToJS: Import only the UWASI features Swift needs by default PackageToJS: Import only the UWASI features Swift needs Nov 3, 2026
@krodak krodak closed this Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant