Skip to content

Commit d693ed0

Browse files
committed
compiler: a WebAssembly build for platforms without a binary
@reactivescript/compiler-wasm32-wasi is the Node-API build for wasm32-wasip1-threads, which index.js already falls back to: it compiles .rx in WebContainers (StackBlitz), on musl and on Windows on ARM, as @solidjs/compiler-wasm32-wasi does for Solid. - build.rs sets up emnapi 1.x for wasm32 as @solidjs/compiler's build.rs does; napi, napi-derive and napi-build are held at the versions it is written for. - src/wasm.rs (the language server's wasm32-unknown-unknown module, whose host reads files) is left out of the Node-API build, which has std::fs. - CI builds and tests it (NAPI_RS_FORCE_WASI=1 npm test) beside the native targets and uploads its loader files with the module, so napi artifacts fills npm/wasm32-wasi at release. Locally: the WASI build passes test/node.mjs through Node's WASI, loaded from the installed platform package; the native build still passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0117BoWPLMLsJjr7WiwaUgvd
1 parent 76a7b93 commit d693ed0

11 files changed

Lines changed: 368 additions & 106 deletions

File tree

‎.github/workflows/compiler.yml‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
- { os: macos-latest, target: aarch64-apple-darwin, test: true }
2727
- { os: macos-latest, target: x86_64-apple-darwin, test: false } # cross-built on Apple silicon
2828
- { os: windows-latest, target: x86_64-pc-windows-msvc, test: true }
29+
- { os: ubuntu-latest, target: wasm32-wasip1-threads, test: true } # @reactivescript/compiler-wasm32-wasi: WebContainers, musl, Windows on ARM
2930
runs-on: ${{ matrix.os }}
3031
defaults:
3132
run:
@@ -45,14 +46,25 @@ jobs:
4546
node-version: 22
4647
- run: npm ci
4748
- name: Node-API build
48-
run: npx napi build --platform --release --strip --features node --target ${{ matrix.target }} --manifest-path ./Cargo.toml -- --lib
49+
# RUSTC: napi-build finds crt1-reactor.o from it for WASI; the rustup proxy's name gives a module that
50+
# deadlocks before its threads start (as @solidjs/compiler's workflow notes)
51+
run: RUSTC="$(rustup which rustc)" npx napi build --platform --release --strip --features node --target ${{ matrix.target }} --manifest-path ./Cargo.toml -- --lib
4952
- name: Test
5053
if: matrix.test
5154
run: npm test
55+
env:
56+
NAPI_RS_FORCE_WASI: ${{ matrix.target == 'wasm32-wasip1-threads' && '1' || '' }}
5257
- uses: actions/upload-artifact@v4
5358
with:
5459
name: binding-${{ matrix.target }}
55-
path: compiler/*.node
60+
path: |
61+
compiler/*.node
62+
compiler/compiler.wasm32-wasi.wasm
63+
compiler/compiler.wasi.cjs
64+
compiler/compiler.wasi.d.cts
65+
compiler/compiler.wasi-browser.js
66+
compiler/wasi-worker.mjs
67+
compiler/wasi-worker-browser.mjs
5668
if-no-files-found: error
5769

5870
# which version is new: on main only, after every platform built

‎.gitignore‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ compiler/target/
33
compiler/*.node
44
compiler/test/.out/
55
compiler/artifacts/
6+
compiler/*.wasm
7+
compiler/browser.js
8+
compiler/compiler.wasi.cjs
9+
compiler/compiler.wasi.d.cts
10+
compiler/compiler.wasi-browser.js
11+
compiler/wasi-worker.mjs
12+
compiler/wasi-worker-browser.mjs

‎compiler/Cargo.lock‎

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎compiler/Cargo.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ oxc_ast = "0.144"
3232
oxc_span = "0.144"
3333
oxc_sourcemap = "8.1"
3434
oxc_syntax = "0.144"
35-
napi = { version = "3.12", default-features = false, features = ["napi4"], optional = true }
36-
napi-derive = { version = "3.6", optional = true }
35+
napi = { version = "=3.12.1", default-features = false, features = ["napi4"], optional = true }
36+
napi-derive = { version = "=3.6.3", optional = true }
3737

3838
[build-dependencies]
39-
napi-build = "2.4"
39+
napi-build = "=2.4.1" # held with napi and napi-derive: the WASI build's emnapi setup (build.rs) is written for this set
4040

4141
[profile.release]
4242
debug = false

‎compiler/README.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ RX_CRATE=1 npm --prefix ../ref run ssr # … served and hydrated from
4848
The crate's Node-API build (`--features node`, `src/node.rs`) is the npm
4949
package `@reactivescript/compiler`, with one binary per platform
5050
(`@reactivescript/compiler-<platform>`, in `npm/`) as `@solidjs/compiler`
51-
ships. Its `/vite` export (`vite.mjs`) is ReactiveScript's front end for
51+
ships, plus `@reactivescript/compiler-wasm32-wasi`, the same build for
52+
`wasm32-wasip1-threads`, which loads wherever no binary fits (WebContainers
53+
such as StackBlitz, musl, Windows on ARM). Build it with
54+
`RUSTC="$(rustup which rustc)" npx napi build --platform --release --strip
55+
--features node --target wasm32-wasip1-threads -- --lib`, and test it with
56+
`NAPI_RS_FORCE_WASI=1 npm test`. `build.rs` sets up emnapi for it as
57+
`@solidjs/compiler`'s does, and the napi crates are held at the versions that
58+
setup works with. Its `/vite` export (`vite.mjs`) is ReactiveScript's front end for
5259
Solid's Vite plugin, which loads it on its own: an app installs the package
5360
and keeps `solid()` as it is (spec/11 CP-7, decision 7).
5461

@@ -82,8 +89,7 @@ The app mounts a program with `createApp` from `@reactivescript/runtime`
8289
(`../runtime`, CP-4a), as the examples do.
8390

8491
Not yet: hot updates of `.rx` modules are untested; server rendering in Vite
85-
needs a request-scoped app for server functions to run in; no WebAssembly
86-
fallback for platforms without a binary.
92+
needs a request-scoped app for server functions to run in.
8793

8894
### Releasing
8995

@@ -99,11 +105,11 @@ tags it `compiler-v<version>`.
99105
`runtime` workflow, tagged `runtime-v<version>`.
100106

101107
Once, by hand: npm sets up trusted publishing only on a package that
102-
exists, so each of the seven packages needs a placeholder first,
108+
exists, so each of the eight packages needs a placeholder first,
103109

104110
```sh
105111
npm login
106-
for p in runtime compiler compiler-darwin-x64 compiler-darwin-arm64 compiler-linux-x64-gnu compiler-linux-arm64-gnu compiler-win32-x64-msvc; do
112+
for p in runtime compiler compiler-darwin-x64 compiler-darwin-arm64 compiler-linux-x64-gnu compiler-linux-arm64-gnu compiler-win32-x64-msvc compiler-wasm32-wasi; do
107113
npx setup-npm-trusted-publish "@reactivescript/$p"
108114
done
109115
```

‎compiler/build.rs‎

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,119 @@
1+
// The Node-API build links against the host's symbols (src/node.rs). For wasm32 (the WASI package,
2+
// @reactivescript/compiler-wasm32-wasi) the emnapi setup follows @solidjs/compiler's build.rs (solidjs/solid,
3+
// packages/compiler), since napi-build 2.4 hard-exports emnapi v2 symbols that emnapi 1.x does not define.
4+
use std::env;
5+
use std::path::{Path, PathBuf};
6+
use std::process::Command;
7+
18
fn main() {
2-
// the Node-API build links against the host's symbols (src/node.rs)
3-
if std::env::var_os("CARGO_FEATURE_NODE").is_some() {
9+
if env::var_os("CARGO_FEATURE_NODE").is_none() {
10+
return;
11+
}
12+
let target = env::var("TARGET").unwrap_or_default();
13+
if target.contains("wasm32") {
14+
// napi-build 2.4.1 hard-exports `emnapi_create_env` / `emnapi_delete_env`
15+
// for emnapi v2 archives. We still ship emnapi 1.x, which does not
16+
// define those symbols, and rust-lld 1.95+ errors on a missing
17+
// `--export`. Replicate the WASI setup with `--export-if-defined`.
18+
setup_wasi_emnapi_v1(&target);
19+
} else {
420
napi_build::setup();
521
}
622
}
23+
24+
fn setup_wasi_emnapi_v1(target: &str) {
25+
let link_dir = env::var("EMNAPI_LINK_DIR").expect("EMNAPI_LINK_DIR must be set");
26+
let has_threads = target.ends_with("-threads")
27+
|| matches!(
28+
target,
29+
"wasm32-wasi" | "wasm32-wasi-preview1-threads" | "wasm32-wasip1-threads"
30+
);
31+
let emnapi_library = if has_threads {
32+
"emnapi-napi-rs-mt"
33+
} else {
34+
"emnapi-basic-napi-rs"
35+
};
36+
let emnapi_archive = Path::new(&link_dir).join(format!("lib{emnapi_library}.a"));
37+
assert!(
38+
emnapi_archive.is_file(),
39+
"emnapi archive for {target} is missing at {}",
40+
emnapi_archive.display()
41+
);
42+
43+
println!("cargo:rerun-if-env-changed=EMNAPI_LINK_DIR");
44+
println!("cargo:rerun-if-env-changed=RUSTC");
45+
println!("cargo:rerun-if-env-changed=TARGET");
46+
println!("cargo:rerun-if-env-changed=WASI_SDK_PATH");
47+
println!("cargo:rustc-link-search={link_dir}");
48+
println!("cargo:rustc-link-lib=static={emnapi_library}");
49+
println!("cargo:rustc-link-arg=--export=malloc");
50+
println!("cargo:rustc-link-arg=--export=free");
51+
println!("cargo:rustc-link-arg=--export-if-defined=emnapi_create_env");
52+
println!("cargo:rustc-link-arg=--export-if-defined=emnapi_delete_env");
53+
println!("cargo:rustc-link-arg=--export=napi_register_wasm_v1");
54+
println!("cargo:rustc-link-arg=--export-if-defined=napi_prepare_wasm_env_cleanup");
55+
println!("cargo:rustc-link-arg=--export-if-defined=napi_wasm_env_cleanup_pending");
56+
println!("cargo:rustc-link-arg=--export-if-defined=node_api_module_get_api_version_v1");
57+
println!("cargo:rustc-link-arg=--export-table");
58+
if has_threads {
59+
println!("cargo:rustc-link-arg=--export-if-defined=emnapi_async_worker_create");
60+
println!("cargo:rustc-link-arg=--export-if-defined=emnapi_async_worker_init");
61+
}
62+
println!("cargo:rustc-link-arg=--export-if-defined=emnapi_thread_crashed");
63+
println!("cargo:rustc-link-arg=--import-memory");
64+
println!("cargo:rustc-link-arg=--import-undefined");
65+
println!("cargo:rustc-link-arg=--max-memory=4294967296");
66+
println!("cargo:rustc-link-arg=-zstack-size=64000000");
67+
println!("cargo:rustc-link-arg=--no-check-features");
68+
69+
let rustc = env::var_os("RUSTC").expect("RUSTC must be set by Cargo");
70+
let sysroot = rustc_sysroot(&rustc);
71+
let crt_reactor_path = sysroot
72+
.join("lib")
73+
.join("rustlib")
74+
.join(target)
75+
.join("lib")
76+
.join("self-contained")
77+
.join("crt1-reactor.o");
78+
assert!(
79+
crt_reactor_path.is_file(),
80+
"failed to locate crt1-reactor.o for {target} at {}",
81+
crt_reactor_path.display()
82+
);
83+
println!("cargo:rustc-link-arg={}", crt_reactor_path.display());
84+
println!("cargo:rustc-link-arg=--export=_initialize");
85+
86+
if let Ok(wasi_sdk_path) = env::var("WASI_SDK_PATH") {
87+
let wasi_target = if has_threads {
88+
"wasm32-wasip1-threads"
89+
} else {
90+
"wasm32-wasip1"
91+
};
92+
let wasi_lib_dir = Path::new(&wasi_sdk_path)
93+
.join("share")
94+
.join("wasi-sysroot")
95+
.join("lib")
96+
.join(wasi_target);
97+
println!("cargo:rustc-link-search=native={}", wasi_lib_dir.display());
98+
if wasi_lib_dir.join("libsetjmp.a").is_file() {
99+
println!("cargo:rustc-link-lib=static=setjmp");
100+
}
101+
}
102+
}
103+
104+
fn rustc_sysroot(rustc: &std::ffi::OsStr) -> PathBuf {
105+
let output = Command::new(rustc)
106+
.args(["--print", "sysroot"])
107+
.output()
108+
.unwrap_or_else(|err| panic!("failed to execute rustc --print sysroot: {err}"));
109+
assert!(
110+
output.status.success(),
111+
"rustc --print sysroot failed: {}",
112+
String::from_utf8_lossy(&output.stderr)
113+
);
114+
PathBuf::from(
115+
String::from_utf8(output.stdout)
116+
.expect("sysroot utf8")
117+
.trim(),
118+
)
119+
}

0 commit comments

Comments
 (0)