Skip to content

Commit 61ca366

Browse files
feat: the wasm runtime is a feature that cargo wasm turns on
1 parent d5849fc commit 61ca366

9 files changed

Lines changed: 31 additions & 16 deletions

File tree

‎.cargo/config.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# `cargo wasm` build edge-python release WebAssembly artifact.
22
[alias]
3-
wasm = "build --release --target wasm32-unknown-unknown -p edge-python --no-default-features"
3+
wasm = "rustc --release --target wasm32-unknown-unknown -p edge-python --lib --no-default-features --features runtime --crate-type cdylib,rlib"
44
# `cargo wasm-cli` builds the speed-tuned copy released CLIs embed.
5-
wasm-cli = "build --profile cli --target wasm32-unknown-unknown -p edge-python --no-default-features"
5+
wasm-cli = "rustc --profile cli --target wasm32-unknown-unknown -p edge-python --lib --no-default-features --features runtime --crate-type cdylib,rlib"

‎.github/actions/compiler/action.yml‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ runs:
3737

3838
- name: Clippy (wasm)
3939
shell: bash
40-
run: cargo clippy --locked --lib --no-default-features --target wasm32-unknown-unknown -p edge-python -p slugify-mod -- -D warnings
40+
run: |
41+
cargo clippy --locked --lib --no-default-features --features edge-python/runtime --target wasm32-unknown-unknown -p edge-python -p slugify-mod -- -D warnings
42+
cargo clippy --locked --lib --no-default-features --target wasm32-unknown-unknown -p edge-python -- -D warnings
4143
4244
# build-std needs nightly plus rust-src.
4345
- name: Toolchain (nightly)
@@ -58,12 +60,14 @@ runs:
5860
shell: bash
5961
run: |
6062
RUSTFLAGS="-Z location-detail=none -Z fmt-debug=none -Z unstable-options -C panic=immediate-abort" \
61-
cargo +nightly build \
63+
cargo +nightly rustc \
6264
--target wasm32-unknown-unknown \
6365
--lib \
6466
--release \
6567
-p edge-python \
6668
--no-default-features \
69+
--features runtime \
70+
--crate-type cdylib,rlib \
6771
-Z build-std=std,panic_abort \
6872
-Z build-std-features=optimize_for_size
6973
@@ -110,7 +114,7 @@ runs:
110114
- name: Build (CLI)
111115
shell: bash
112116
run: |
113-
cargo build --locked --profile cli --target wasm32-unknown-unknown -p edge-python --no-default-features
117+
cargo rustc --locked --profile cli --target wasm32-unknown-unknown -p edge-python --lib --no-default-features --features runtime --crate-type cdylib,rlib
114118
CLI_WASM=target/wasm32-unknown-unknown/cli/compiler.wasm
115119
wasm-opt -O3 --enable-bulk-memory --enable-bulk-memory-opt --enable-nontrapping-float-to-int --enable-sign-ext --enable-mutable-globals --enable-multivalue --enable-reference-types -o /tmp/compiler-cli.wasm "$CLI_WASM"
116120
mv /tmp/compiler-cli.wasm "$CLI_WASM"

‎CONTRIBUTING.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Run these from the repo root before sending. The maintainer runs CI once the PR
2424
cargo wasm
2525
cargo test --release
2626
cargo clippy --all-targets -- -D warnings
27-
cargo clippy --lib --no-default-features --target wasm32-unknown-unknown -p edge-python -p slugify-mod -- -D warnings
27+
cargo clippy --lib --no-default-features --features edge-python/runtime --target wasm32-unknown-unknown -p edge-python -p slugify-mod -- -D warnings
28+
cargo clippy --lib --no-default-features --target wasm32-unknown-unknown -p edge-python -- -D warnings
2829
cargo shear
2930
```
3031

@@ -38,9 +39,11 @@ The root Cargo workspace holds the engine, `abi`, `pdk`, `skill`, `lang` and `be
3839

3940
```bash
4041
cargo wasm # compiler.wasm, CI ships a smaller build
41-
cargo build --release # .rlib and cdylib for Rust embedders
42+
cargo build --release # the .rlib Rust embedders link
4243
```
4344

45+
`cargo wasm` turns on the `runtime` feature, the wrapper that makes `compiler.wasm` with its exports, host imports, allocator and panic handler. Without it the crate is the engine alone, for a wasm that links it as a library, such as a plugin that only parses.
46+
4447
`cli/` embeds `compiler.wasm` and the JS host from `js/dist` at build time. Build them first, or point `EDGE_COMPILER_WASM` and `EDGE_JS_DIST` at copies. Releases embed the speed build from `cargo wasm-cli`.
4548

4649
The system calls in `js/src/system` run in SpiderMonkey inside the CLI, through `mozjs`, which compiles SpiderMonkey from source on the first build and wants clang, python3 and `llvm-objdump` on the path. On macOS a symlink named `llvm-objdump` to `/usr/bin/objdump` serves, and CI sets `MOZJS_FROM_SOURCE=1` on every target. The static Linux binary builds inside `rust:alpine` through [`.github/actions/cli/musl.sh`](.github/actions/cli/musl.sh), since musl-gcc has no C++.

‎Cargo.toml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ readme = "README.md"
99
autotests = false
1010
autobins = false
1111

12+
# `cargo wasm` builds compiler.wasm as a cdylib, so a crate depending on this one links a plain rlib.
1213
[lib]
1314
name = "compiler"
14-
crate-type = ["cdylib", "rlib"]
1515
doctest = false
1616

1717
[[test]]
@@ -23,6 +23,8 @@ default = ["std"]
2323
std = []
2424
# Records each instruction a run executes for `VM::ran`, method calls left unfused so every ip is the compiler's own.
2525
coverage = []
26+
# The wrapper that makes compiler.wasm, its exports, host imports, allocator and panic handler.
27+
runtime = []
2628

2729
[dependencies]
2830
wasm-abi = { workspace = true }

‎docs/03-reference/06-abi.mdx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,14 +504,20 @@ Little-endian, self-contained, versioned.
504504

505505
## Consuming the release from a Rust crate
506506

507-
The `edge-python` crate builds as a `cdylib`. A Rust host can instantiate `compiler.wasm` and call the exports above directly, the same `.wasm` the JS host loads, with the host owning I/O. The crate builds no wasm of its own and fetches nothing at build time, so `cargo build` stays offline and reproducible. Take the artifact from the tagged GitHub Release, or from `https://cdn.edgepython.com/compiler.wasm` for the current `main`. The `edge` CLI is that host, it embeds the `.wasm` precompiled by wasmtime and implements the imports in `cli/src/host`.
507+
`cargo wasm` builds the `edge-python` crate as a `cdylib`, `compiler.wasm`. A Rust host can instantiate `compiler.wasm` and call the exports above directly, the same `.wasm` the JS host loads, with the host owning I/O. The crate builds no wasm of its own and fetches nothing at build time, so `cargo build` stays offline and reproducible. Take the artifact from the tagged GitHub Release, or from `https://cdn.edgepython.com/compiler.wasm` for the current `main`. The `edge` CLI is that host, it embeds the `.wasm` precompiled by wasmtime and implements the imports in `cli/src/host`.
508508

509509
```toml
510510
# Downstream Cargo.toml
511511
[dependencies]
512512
edge-python = { git = "https://github.com/dylan-sutton-chavez/edge-python", tag = "v0.1.0" }
513513
```
514514

515+
The `runtime` feature is the wrapper that makes `compiler.wasm`, its exports, host imports, allocator and panic handler, and `cargo wasm` turns it on. A wasm that links the crate as a library, such as a plugin that only parses, leaves it off and brings its own allocator and panic handler.
516+
517+
```toml
518+
edge-python = { git = "https://github.com/dylan-sutton-chavez/edge-python", tag = "v0.1.0", default-features = false }
519+
```
520+
515521
Vendor the matching `compiler.wasm` next to your own sources and pin it by checksum, the same way the CLI pins the modules it downloads. A release asset is immutable and the CDN path is not, so a checksum is the only thing that ties a build to a known engine.
516522

517523
To add native modules from a Rust host, implement the `Resolver` trait. See [Modules](/docs/reference/modules).

‎docs/03-reference/07-embedding.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The `lang` crate is a small, clean embedding API over the Edge Python engine. Us
1818
lang = { git = "https://github.com/dylan-sutton-chavez/edge-python", tag = "v0.1.0" }
1919
```
2020

21-
`lang` is `no_std` and needs only `alloc`. The default `std` feature links the host standard library, and `default-features = false` drops it for wasm and bare-metal targets.
21+
`lang` is `no_std` and needs only `alloc`. The default `std` feature links the host standard library, and `default-features = false` drops it for wasm and bare-metal targets. On wasm the program that embeds `lang` brings its own allocator and panic handler, since `lang` takes the engine without its `runtime`.
2222

2323
## Compile and run
2424

‎src/lib.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ extern crate alloc;
44

55
pub mod abi;
66

7-
/* Host bridge behind the six plugin imports, wasm only. */
8-
#[cfg(target_arch = "wasm32")]
7+
/* Host bridge behind the six plugin imports, part of the wasm `runtime`. */
8+
#[cfg(all(target_arch = "wasm32", feature = "runtime"))]
99
pub mod bridge;
1010

11-
#[cfg(target_arch = "wasm32")]
11+
#[cfg(all(target_arch = "wasm32", feature = "runtime"))]
1212
pub mod wasm;
1313

1414
/* Internal compiler helpers (not Edge Python stdlib), separate from pipeline code. */

‎src/vm/gc.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<'a> VM<'a> {
2929
}
3030
}
3131
for &v in current_slots { self.heap.mark(v); }
32-
#[cfg(target_arch = "wasm32")]
32+
#[cfg(all(target_arch = "wasm32", feature = "runtime"))]
3333
crate::bridge::mark_handles(self as *const Self as *const u8, &mut self.heap);
3434
for &v in &self.live_slots { self.heap.mark(v); }
3535
// Closure cells live on the active call frames until the closures that capture them are built.

‎src/vm/globals/container.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ impl<'a> VM<'a> {
4747
}
4848

4949
// Build a set Val from items, rejecting unhashable elements first.
50-
#[cfg(target_arch = "wasm32")]
50+
#[cfg(all(target_arch = "wasm32", feature = "runtime"))]
5151
pub(crate) fn set_from_items(&mut self, items: Vec<Val>) -> Result<Val, VmErr> {
5252
for &v in items.iter().filter(|v| v.is_heap()) { self.require_hashable(v)?; }
5353
self.alloc_set_result(items, false)
5454
}
5555

5656
// Build a frozenset Val from items, rejecting unhashable elements first.
57-
#[cfg(target_arch = "wasm32")]
57+
#[cfg(all(target_arch = "wasm32", feature = "runtime"))]
5858
pub(crate) fn frozenset_from_items(&mut self, items: Vec<Val>) -> Result<Val, VmErr> {
5959
for &v in items.iter().filter(|v| v.is_heap()) { self.require_hashable(v)?; }
6060
self.alloc_set_result(items, true)

0 commit comments

Comments
 (0)