Skip to content

Commit bab03bd

Browse files
authored
Merge pull request RustPython#6283 from youknowone/test-wasm32-without-js
Test wasm32 without js
2 parents 916d3ba + 9134cca commit bab03bd

File tree

14 files changed

+266
-38
lines changed

14 files changed

+266
-38
lines changed

.cspell.dict/rust-more.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ unsync
8282
wasip1
8383
wasip2
8484
wasmbind
85+
wasmer
8586
wasmtime
8687
widestring
8788
winapi

.github/workflows/ci.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,13 @@ jobs:
404404
with: { wabt-version: "1.0.36" }
405405
- name: check wasm32-unknown without js
406406
run: |
407-
cd wasm/wasm-unknown-test
408-
cargo build --release --verbose
409-
if wasm-objdump -xj Import target/wasm32-unknown-unknown/release/wasm_unknown_test.wasm; then
410-
echo "ERROR: wasm32-unknown module expects imports from the host environment" >2
407+
cd example_projects/wasm32_without_js/rustpython-without-js
408+
cargo build
409+
cd ..
410+
if wasm-objdump -xj Import rustpython-without-js/target/wasm32-unknown-unknown/debug/rustpython_without_js.wasm; then
411+
echo "ERROR: wasm32-unknown module expects imports from the host environment" >&2
411412
fi
413+
cargo run --release --manifest-path wasm-runtime/Cargo.toml rustpython-without-js/target/wasm32-unknown-unknown/debug/rustpython_without_js.wasm
412414
- name: build notebook demo
413415
if: github.ref == 'refs/heads/release'
414416
run: |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*/target/
2+
*/Cargo.lock
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# RustPython wasm32 build without JS
2+
3+
To test, build rustpython to wasm32-unknown-unknown target first.
4+
5+
```shell
6+
cd rustpython-without-js # due to `.cargo/config.toml`
7+
cargo build
8+
cd ..
9+
```
10+
11+
Then there will be `rustpython-without-js/target/wasm32-unknown-unknown/debug/rustpython_without_js.wasm` file.
12+
13+
Now we can run the wasm file with wasm runtime:
14+
15+
```shell
16+
cargo run --release --manifest-path wasm-runtime/Cargo.toml rustpython-without-js/target/wasm32-unknown-unknown/debug/rustpython_without_js.wasm
17+
```
18+

wasm/wasm-unknown-test/.cargo/config.toml renamed to example_projects/wasm32_without_js/rustpython-without-js/.cargo/config.toml

File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "rustpython-without-js"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
getrandom = "0.3"
11+
rustpython-vm = { path = "../../../crates/vm", default-features = false, features = ["compiler"] }
12+
13+
[workspace]
14+
15+
[patch.crates-io]

wasm/wasm-unknown-test/README.md renamed to example_projects/wasm32_without_js/rustpython-without-js/README.md

File renamed without changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use rustpython_vm::{Interpreter};
2+
3+
unsafe extern "C" {
4+
fn kv_get(kp: i32, kl: i32, vp: i32, vl: i32) -> i32;
5+
6+
/// kp and kl are the key pointer and length in wasm memory, vp and vl are for the value
7+
fn kv_put(kp: i32, kl: i32, vp: i32, vl: i32) -> i32;
8+
9+
fn print(p: i32, l: i32) -> i32;
10+
}
11+
12+
#[unsafe(no_mangle)]
13+
pub unsafe extern "C" fn eval(s: *const u8, l: usize) -> i32 {
14+
// let src = unsafe { std::slice::from_raw_parts(s, l) };
15+
// let src = std::str::from_utf8(src).unwrap();
16+
// TODO: use src
17+
let src = "1 + 3";
18+
19+
// 2. Execute Python code
20+
let interpreter = Interpreter::without_stdlib(Default::default());
21+
let result = interpreter.enter(|vm| {
22+
let scope = vm.new_scope_with_builtins();
23+
let res = match vm.run_block_expr(scope, src) {
24+
Ok(val) => val,
25+
Err(_) => return Err(-1), // Python execution error
26+
};
27+
let repr_str = match res.repr(vm) {
28+
Ok(repr) => repr.as_str().to_string(),
29+
Err(_) => return Err(-1), // Failed to get string representation
30+
};
31+
Ok(repr_str)
32+
});
33+
let result = match result {
34+
Ok(r) => r,
35+
Err(code) => return code,
36+
};
37+
38+
let msg = format!("eval result: {result}");
39+
40+
unsafe {
41+
print(
42+
msg.as_str().as_ptr() as usize as i32,
43+
msg.len() as i32,
44+
)
45+
};
46+
47+
0
48+
}
49+
50+
#[unsafe(no_mangle)]
51+
unsafe extern "Rust" fn __getrandom_v03_custom(
52+
_dest: *mut u8,
53+
_len: usize,
54+
) -> Result<(), getrandom::Error> {
55+
// Err(getrandom::Error::UNSUPPORTED)
56+
57+
// WARNING: This function **MUST** perform proper getrandom
58+
Ok(())
59+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.wasm
2+
target
3+
Cargo.lock
4+
!wasm/rustpython.wasm
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "wasm-runtime"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
wasmer = "6.1.0"
8+
9+
[workspace]

0 commit comments

Comments
 (0)