Commit 8abac17
authored
* feat(wasm): closes #76 — runtime WebAssembly host (PoC, wasmi)
Lets a binary built for `--target macos/linux/windows/ios/android` load
third-party `.wasm` modules at runtime via `WebAssembly.instantiate(...)`
— closing the host-side gap with Node and matching the issue's PoC scope.
Engine: wasmi 0.50 (pure-Rust interpreter), in a separate
`perry-wasm-host` crate so the default Perry build never pulls wasmi in.
Linking is auto-detected when codegen sees any `WebAssembly.*` reference;
the `--enable-wasm-runtime` flag stays available to force-link for
dlopen/FFI scenarios that have no static reference.
Surface (Perry MVP shape; standard async surface tracked as follow-up):
const bytes = embedWasm("./add.wasm"); // compile-time embed
const inst = WebAssembly.instantiate(bytes); // sync, opaque handle
inst.exports.add(2, 3); // standard JS shape
WebAssembly.callExport(inst, "add", 2, 3); // explicit helper
WebAssembly.validate(bytes); // bool
`inst.exports.<method>(...)` is recognised syntactically when the local
was tagged at var-decl time as a wasm instance (init was
`WebAssembly.instantiate(...)`) — this avoids stealing unrelated
`module.exports.foo()` calls from CJS aggregators. `embedWasm("...")`
reads the file at HIR-lower time relative to the importing source and
bakes the bytes into the binary as a `Uint8Array` literal (sidesteps the
in-flight TC39 import-attributes proposal per maintainer preference on
the issue thread).
The `js_webassembly_*` shims in perry-runtime forward to a stable C ABI
on perry-wasm-host. perry-runtime never depends on wasmi at the cargo
level — link-time resolution only. Programs without any `WebAssembly.*`
usage stay exactly the same size (verified: 0.9MB vs 1.9MB on the test).
Validation:
- cargo test -p perry-wasm-host: 2/2 (validate + add 2+3=5)
- test-files/test_wasm_add.ts compiles without --enable-wasm-runtime
(auto-detected) and prints OK
- workspace test suite green (excluding cross-host UI crates per
CLAUDE.md's exclusion list)
- default build (no WebAssembly.*) compiles + runs unchanged
Out of scope (follow-ups, not this PR):
- Standard `Promise<{module, instance}>` async surface
- Host imports beyond numeric (externref, strings, structs)
- `instantiateStreaming`
- Wasmtime engine selection (`--enable-wasm-runtime=wasmtime`)
- WASI preview-1 (`--enable-wasi`)
- `--target wasm` / `--target web` passthrough to the browser host
- AOT WASM → native (gkgoat1's suggestion in the thread)
- Configurable `Memory` max pages / per-instance fuel
- `import attributes` form (`with { type: "wasm" }`)
- Note in `docs/src/plugins/overview.md` on dylib-vs-WASM trust model
* fix(#76): merge main + close CI gaps (Module field, stable_hash, wasm-host feature gate)
- perry-codegen-arkts: add missing uses_webassembly: false in two
Module {} initialisers (cargo-test, harmonyos-smoke E0063).
- perry-hir/stable_hash.rs: include uses_webassembly in Module
destructuring/hash, and add SH arms for the three Expr::WebAssembly*
variants (compile errors after merging main, which added
stable_hash.rs).
- perry-runtime: gate `pub mod webassembly` behind a new `wasm-host`
Cargo feature. The shim TU references `perry_wasm_host_*`; with
codegen-units = 1, libperry_runtime.a was a single .o and the linker
pulled the shim in for every binary, so non-wasm programs failed
with "undefined reference to perry_wasm_host_*" (doc-tests +
harmonyos-smoke linker errors).
- perry/compile/optimized_libs.rs: push `perry-runtime/wasm-host` into
cross_features when ctx.needs_wasm_runtime, and fold the flag into
the perry-auto target-dir hash so a wasm program isn't served a
cached non-wasm libperry_runtime.a (which lacks js_webassembly_*),
and vice versa.
- cargo fmt — collapse the long single-line use re-export in
compile.rs and the .d.ts include_str! in types.rs, plus rustfmt
reflow inside expr.rs / runtime_decls.rs / lower_decl.rs /
expr_call.rs / webassembly.rs / perry-wasm-host/lib.rs (lint job).
Merging main also picks up the updated `.github/workflows/security-audit.yml`
(drops `--deny warnings`, ignores the 3 acknowledged RUSTSEC IDs), which
clears the audit failures.
1 parent 8fc837f commit 8abac17
31 files changed
Lines changed: 1430 additions & 5 deletions
File tree
- crates
- perry-codegen-arkts
- src
- tests
- perry-codegen/src
- perry-hir/src
- lower
- perry-runtime
- src
- perry-wasm-host
- src
- perry/src/commands
- compile
- docs/src/cli
- test-files
- fixtures
- types/perry
- build
- webassembly
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| |||
119 | 120 | | |
120 | 121 | | |
121 | 122 | | |
| 123 | + | |
122 | 124 | | |
123 | 125 | | |
124 | 126 | | |
| |||
291 | 293 | | |
292 | 294 | | |
293 | 295 | | |
| 296 | + | |
294 | 297 | | |
295 | 298 | | |
296 | 299 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7908 | 7908 | | |
7909 | 7909 | | |
7910 | 7910 | | |
| 7911 | + | |
7911 | 7912 | | |
7912 | 7913 | | |
7913 | 7914 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5384 | 5384 | | |
5385 | 5385 | | |
5386 | 5386 | | |
| 5387 | + | |
| 5388 | + | |
| 5389 | + | |
| 5390 | + | |
| 5391 | + | |
| 5392 | + | |
| 5393 | + | |
| 5394 | + | |
| 5395 | + | |
| 5396 | + | |
| 5397 | + | |
| 5398 | + | |
| 5399 | + | |
| 5400 | + | |
| 5401 | + | |
| 5402 | + | |
| 5403 | + | |
| 5404 | + | |
| 5405 | + | |
| 5406 | + | |
| 5407 | + | |
| 5408 | + | |
| 5409 | + | |
| 5410 | + | |
| 5411 | + | |
| 5412 | + | |
| 5413 | + | |
| 5414 | + | |
| 5415 | + | |
| 5416 | + | |
| 5417 | + | |
| 5418 | + | |
| 5419 | + | |
| 5420 | + | |
| 5421 | + | |
| 5422 | + | |
| 5423 | + | |
| 5424 | + | |
| 5425 | + | |
| 5426 | + | |
| 5427 | + | |
| 5428 | + | |
| 5429 | + | |
| 5430 | + | |
| 5431 | + | |
| 5432 | + | |
| 5433 | + | |
| 5434 | + | |
| 5435 | + | |
| 5436 | + | |
| 5437 | + | |
| 5438 | + | |
| 5439 | + | |
| 5440 | + | |
| 5441 | + | |
| 5442 | + | |
| 5443 | + | |
| 5444 | + | |
| 5445 | + | |
| 5446 | + | |
| 5447 | + | |
| 5448 | + | |
| 5449 | + | |
| 5450 | + | |
| 5451 | + | |
| 5452 | + | |
| 5453 | + | |
| 5454 | + | |
| 5455 | + | |
| 5456 | + | |
| 5457 | + | |
| 5458 | + | |
| 5459 | + | |
| 5460 | + | |
| 5461 | + | |
| 5462 | + | |
| 5463 | + | |
| 5464 | + | |
| 5465 | + | |
| 5466 | + | |
| 5467 | + | |
| 5468 | + | |
| 5469 | + | |
5387 | 5470 | | |
5388 | 5471 | | |
5389 | 5472 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
435 | 435 | | |
436 | 436 | | |
437 | 437 | | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
438 | 466 | | |
439 | 467 | | |
440 | 468 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
228 | 232 | | |
229 | 233 | | |
230 | 234 | | |
| |||
1359 | 1363 | | |
1360 | 1364 | | |
1361 | 1365 | | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
1362 | 1381 | | |
1363 | 1382 | | |
1364 | 1383 | | |
| |||
2478 | 2497 | | |
2479 | 2498 | | |
2480 | 2499 | | |
| 2500 | + | |
2481 | 2501 | | |
2482 | 2502 | | |
2483 | 2503 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
| 185 | + | |
| 186 | + | |
185 | 187 | | |
186 | 188 | | |
187 | 189 | | |
| |||
222 | 224 | | |
223 | 225 | | |
224 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
225 | 233 | | |
226 | 234 | | |
227 | 235 | | |
| |||
344 | 352 | | |
345 | 353 | | |
346 | 354 | | |
| 355 | + | |
347 | 356 | | |
348 | 357 | | |
349 | 358 | | |
| |||
358 | 367 | | |
359 | 368 | | |
360 | 369 | | |
| 370 | + | |
361 | 371 | | |
362 | 372 | | |
363 | 373 | | |
| |||
2225 | 2235 | | |
2226 | 2236 | | |
2227 | 2237 | | |
| 2238 | + | |
2228 | 2239 | | |
2229 | 2240 | | |
2230 | 2241 | | |
| |||
7277 | 7288 | | |
7278 | 7289 | | |
7279 | 7290 | | |
| 7291 | + | |
7280 | 7292 | | |
7281 | 7293 | | |
7282 | 7294 | | |
| |||
0 commit comments