From 4f80d7013ea035dd93fa88d0ef2102b41d6552c5 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Fri, 14 Apr 2023 14:42:18 -0600 Subject: [PATCH 01/13] add wasmbind feature --- vm/Cargo.toml | 2 ++ vm/src/stdlib/time.rs | 20 ++++++++++++-------- vm/src/vm/vm_object.rs | 19 +++++++++++-------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 251d482ebe..33a1af586d 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -23,6 +23,8 @@ ast = ["rustpython-ast"] codegen = ["rustpython-codegen", "ast"] parser = ["rustpython-parser", "ast"] serde = ["dep:serde"] +wasmbind = ["chrono/wasmbind", "getrandom/js"] + [dependencies] rustpython-compiler = { workspace = true, optional = true } diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index d026f2953e..75434c2f2f 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -104,15 +104,19 @@ mod decl { #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] fn _time(_vm: &VirtualMachine) -> PyResult { - use wasm_bindgen::prelude::*; - #[wasm_bindgen] - extern "C" { - type Date; - #[wasm_bindgen(static_method_of = Date)] - fn now() -> f64; + #[cfg(feature = "wasmbind")] + { + use wasm_bindgen::prelude::*; + #[wasm_bindgen] + extern "C" { + type Date; + #[wasm_bindgen(static_method_of = Date)] + fn now() -> f64; + } + // Date.now returns unix time in milliseconds, we want it in seconds + return Ok(Date::now() / 1000.0); } - // Date.now returns unix time in milliseconds, we want it in seconds - Ok(Date::now() / 1000.0) + panic!("Date not available") } #[pyfunction] diff --git a/vm/src/vm/vm_object.rs b/vm/src/vm/vm_object.rs index e3d1436882..8003ac4ae1 100644 --- a/vm/src/vm/vm_object.rs +++ b/vm/src/vm/vm_object.rs @@ -21,15 +21,18 @@ impl VirtualMachine { } #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] { - use wasm_bindgen::prelude::*; - #[wasm_bindgen] - extern "C" { - #[wasm_bindgen(js_namespace = console)] - fn error(s: &str); + #[cfg(feature = "wasmbind")] + { + use wasm_bindgen::prelude::*; + #[wasm_bindgen] + extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn error(s: &str); + } + let mut s = String::new(); + self.write_exception(&mut s, &exc).unwrap(); + error(&s); } - let mut s = String::new(); - self.write_exception(&mut s, &exc).unwrap(); - error(&s); panic!("{}; exception backtrace above", msg) } } From f1dac5087ef88c9e30fb35479380233cd7e4a4ff Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 19 Apr 2023 09:09:39 -0600 Subject: [PATCH 02/13] address pr notes --- vm/src/stdlib/time.rs | 22 +++++++++------------- vm/src/vm/vm_object.rs | 21 +++++++++------------ 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 75434c2f2f..2c421db8f8 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -102,21 +102,17 @@ mod decl { Ok(duration_since_system_now(vm)?.as_secs_f64()) } - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] + #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] fn _time(_vm: &VirtualMachine) -> PyResult { - #[cfg(feature = "wasmbind")] - { - use wasm_bindgen::prelude::*; - #[wasm_bindgen] - extern "C" { - type Date; - #[wasm_bindgen(static_method_of = Date)] - fn now() -> f64; - } - // Date.now returns unix time in milliseconds, we want it in seconds - return Ok(Date::now() / 1000.0); + use wasm_bindgen::prelude::*; + #[wasm_bindgen] + extern "C" { + type Date; + #[wasm_bindgen(static_method_of = Date)] + fn now() -> f64; } - panic!("Date not available") + // Date.now returns unix time in milliseconds, we want it in seconds + return Ok(Date::now() / 1000.0); } #[pyfunction] diff --git a/vm/src/vm/vm_object.rs b/vm/src/vm/vm_object.rs index 8003ac4ae1..31bb940e3f 100644 --- a/vm/src/vm/vm_object.rs +++ b/vm/src/vm/vm_object.rs @@ -19,20 +19,17 @@ impl VirtualMachine { self.flush_std(); panic!("{msg}") } - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] + #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] { - #[cfg(feature = "wasmbind")] - { - use wasm_bindgen::prelude::*; - #[wasm_bindgen] - extern "C" { - #[wasm_bindgen(js_namespace = console)] - fn error(s: &str); - } - let mut s = String::new(); - self.write_exception(&mut s, &exc).unwrap(); - error(&s); + use wasm_bindgen::prelude::*; + #[wasm_bindgen] + extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn error(s: &str); } + let mut s = String::new(); + self.write_exception(&mut s, &exc).unwrap(); + error(&s); panic!("{}; exception backtrace above", msg) } } From 63c9909aa0af669e5a4167c8b1a7633a852fe1b2 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 19 Apr 2023 10:10:25 -0600 Subject: [PATCH 03/13] run the first block if wasmbind is not present --- vm/src/vm/vm_object.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/vm_object.rs b/vm/src/vm/vm_object.rs index 31bb940e3f..5b5ef0526f 100644 --- a/vm/src/vm/vm_object.rs +++ b/vm/src/vm/vm_object.rs @@ -13,7 +13,7 @@ impl VirtualMachine { #[track_caller] #[cold] fn _py_panic_failed(&self, exc: PyBaseExceptionRef, msg: &str) -> ! { - #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))] + #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] { self.print_exception(exc); self.flush_std(); From dbb6794a41bdd03f409f90b308914764d08e93be Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 19 Apr 2023 10:21:15 -0600 Subject: [PATCH 04/13] add cfg for not wasmbind for time --- vm/src/stdlib/time.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 2c421db8f8..e64d51f1ab 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -97,7 +97,11 @@ mod decl { _time(vm) } - #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] + #[cfg(any( + not(target_arch = "wasm32"), + target_os = "wasi", + not(feature = "wasmbind") + ))] fn _time(vm: &VirtualMachine) -> PyResult { Ok(duration_since_system_now(vm)?.as_secs_f64()) } From d8c35770ab429f6994f150b787746a020e998f9a Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 19 Apr 2023 11:08:43 -0600 Subject: [PATCH 05/13] enable js feature of getrandom only for wasmbind --- vm/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 33a1af586d..cfe177f67d 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -73,7 +73,7 @@ thread_local = { workspace = true } memchr = { workspace = true } caseless = "0.2.1" -getrandom = { version = "0.2.12", features = ["js"] } +getrandom = { version = "0.2.12", features = ["custom"] } flamer = { version = "0.4", optional = true } half = "1.8.2" memoffset = "0.9.1" From d2a4a330f9a319a680584f966c45d56a89524f34 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 9 Aug 2024 00:58:11 +0900 Subject: [PATCH 06/13] following chrono/wasmbind convention --- vm/src/stdlib/time.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index e64d51f1ab..5c60b8ae82 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -97,16 +97,20 @@ mod decl { _time(vm) } - #[cfg(any( - not(target_arch = "wasm32"), - target_os = "wasi", - not(feature = "wasmbind") - ))] + #[cfg(not(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) + )))] fn _time(vm: &VirtualMachine) -> PyResult { Ok(duration_since_system_now(vm)?.as_secs_f64()) } - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] + #[cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) + ))] fn _time(_vm: &VirtualMachine) -> PyResult { use wasm_bindgen::prelude::*; #[wasm_bindgen] From 3f28309b7b7e81b82894ae45a65824146812d710 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 9 Aug 2024 00:58:53 +0900 Subject: [PATCH 07/13] revert unnecessary change --- vm/src/stdlib/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 5c60b8ae82..4a9e66066d 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -120,7 +120,7 @@ mod decl { fn now() -> f64; } // Date.now returns unix time in milliseconds, we want it in seconds - return Ok(Date::now() / 1000.0); + Ok(Date::now() / 1000.0) } #[pyfunction] From b6e9a3f37e456319f4419a9e80846e6922142732 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 9 Aug 2024 00:59:29 +0900 Subject: [PATCH 08/13] rustpython_wasm uses the new feature --- wasm/lib/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasm/lib/Cargo.toml b/wasm/lib/Cargo.toml index 9b71bbce25..7d46a3c8da 100644 --- a/wasm/lib/Cargo.toml +++ b/wasm/lib/Cargo.toml @@ -21,7 +21,7 @@ rustpython-common = { workspace = true } rustpython-pylib = { workspace = true, optional = true } rustpython-stdlib = { workspace = true, default-features = false, optional = true } # make sure no threading! otherwise wasm build will fail -rustpython-vm = { workspace = true, features = ["compiler", "encodings", "serde"] } +rustpython-vm = { workspace = true, features = ["compiler", "encodings", "serde", "wasmbind"] } rustpython-parser = { workspace = true } From 7dfb7604213301b6b01493dcf17b2c5f8fb58570 Mon Sep 17 00:00:00 2001 From: Noa Date: Fri, 14 Oct 2022 16:50:37 -0500 Subject: [PATCH 09/13] Make rustpython-vm compatible with non-js wasm32-unknown & add tests --- .github/workflows/ci.yaml | 15 ++++++++------- Cargo.lock | 8 ++++++++ Cargo.toml | 3 ++- stdlib/Cargo.toml | 2 +- vm/Cargo.toml | 9 ++++----- vm/src/stdlib/time.rs | 12 ++++++++++-- vm/src/vm/vm_object.rs | 19 ++++++++++++++++++- wasm/wasm-unknown-test/Cargo.toml | 11 +++++++++++ wasm/wasm-unknown-test/README.md | 1 + wasm/wasm-unknown-test/src/lib.rs | 16 ++++++++++++++++ 10 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 wasm/wasm-unknown-test/Cargo.toml create mode 100644 wasm/wasm-unknown-test/README.md create mode 100644 wasm/wasm-unknown-test/src/lib.rs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bec7eafa32..2d2ec40d4b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -216,13 +216,6 @@ jobs: - name: Check compilation for freebsd run: cargo check --target x86_64-unknown-freebsd - - uses: dtolnay/rust-toolchain@stable - with: - target: wasm32-unknown-unknown - - - name: Check compilation for wasm32 - run: cargo check --target wasm32-unknown-unknown --no-default-features - - uses: dtolnay/rust-toolchain@stable with: target: x86_64-unknown-freebsd @@ -380,6 +373,14 @@ jobs: env: NODE_OPTIONS: "--openssl-legacy-provider" working-directory: ./wasm/demo + - uses: mwilliamson/setup-wabt-action@v1 + with: { wabt-version: "1.0.30" } + - name: check wasm32-unknown without js + run: | + cargo build --release -p wasm-unknown-test --target wasm32-unknown-unknown --verbose + if wasm-objdump -xj Import target/wasm32-unknown-unknown/release/wasm_unknown_test.wasm; then + echo "ERROR: wasm32-unknown module expects imports from the host environment" >2 + fi - name: build notebook demo if: github.ref == 'refs/heads/release' run: | diff --git a/Cargo.lock b/Cargo.lock index 5fcb773d84..b5d99cb2a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3154,6 +3154,14 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +[[package]] +name = "wasm-unknown-test" +version = "0.1.0" +dependencies = [ + "getrandom", + "rustpython-vm", +] + [[package]] name = "web-sys" version = "0.3.61" diff --git a/Cargo.toml b/Cargo.toml index eec5d84a8f..741aab8856 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,7 +96,8 @@ x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md", dev-dependencies = resolver = "2" members = [ "compiler", "compiler/core", "compiler/codegen", - ".", "common", "derive", "jit", "vm", "vm/sre_engine", "pylib", "stdlib", "wasm/lib", "derive-impl", + ".", "common", "derive", "jit", "vm", "vm/sre_engine", "pylib", "stdlib", "derive-impl", + "wasm/lib", "wasm/wasm-unknown-test", ] [workspace.package] diff --git a/stdlib/Cargo.toml b/stdlib/Cargo.toml index 485855d646..1e327c4e9d 100644 --- a/stdlib/Cargo.toml +++ b/stdlib/Cargo.toml @@ -22,7 +22,7 @@ ssl-vendor = ["ssl", "openssl/vendored", "openssl-probe"] [dependencies] # rustpython crates rustpython-derive = { workspace = true } -rustpython-vm = { workspace = true } +rustpython-vm = { workspace = true, default-features = false } rustpython-common = { workspace = true } ahash = { workspace = true } diff --git a/vm/Cargo.toml b/vm/Cargo.toml index cfe177f67d..8088e732e3 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -10,7 +10,7 @@ repository.workspace = true license.workspace = true [features] -default = ["compiler"] +default = ["compiler", "wasmbind"] importlib = [] encodings = ["importlib"] vm-tracing-logging = [] @@ -23,8 +23,7 @@ ast = ["rustpython-ast"] codegen = ["rustpython-codegen", "ast"] parser = ["rustpython-parser", "ast"] serde = ["dep:serde"] -wasmbind = ["chrono/wasmbind", "getrandom/js"] - +wasmbind = ["chrono/wasmbind", "getrandom/js", "wasm-bindgen"] [dependencies] rustpython-compiler = { workspace = true, optional = true } @@ -142,8 +141,8 @@ features = [ "Win32_UI_WindowsAndMessaging", ] -[target.'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen = "0.2.92" +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] +wasm-bindgen = { version = "0.2.92", optional = true } [build-dependencies] glob = { workspace = true } diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 4a9e66066d..8ef67bacec 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -99,8 +99,7 @@ mod decl { #[cfg(not(all( target_arch = "wasm32", - feature = "wasmbind", - not(any(target_os = "emscripten", target_os = "wasi")) + not(any(target_os = "emscripten", target_os = "wasi")), )))] fn _time(vm: &VirtualMachine) -> PyResult { Ok(duration_since_system_now(vm)?.as_secs_f64()) @@ -123,6 +122,15 @@ mod decl { Ok(Date::now() / 1000.0) } + #[cfg(all( + target_arch = "wasm32", + not(feature = "wasmbind"), + not(any(target_os = "emscripten", target_os = "wasi")) + ))] + fn _time(vm: &VirtualMachine) -> PyResult { + Err(vm.new_not_implemented_error("time.time".to_owned())) + } + #[pyfunction] fn monotonic(vm: &VirtualMachine) -> PyResult { Ok(get_monotonic_time(vm)?.as_secs_f64()) diff --git a/vm/src/vm/vm_object.rs b/vm/src/vm/vm_object.rs index 5b5ef0526f..466db6f3a8 100644 --- a/vm/src/vm/vm_object.rs +++ b/vm/src/vm/vm_object.rs @@ -13,12 +13,20 @@ impl VirtualMachine { #[track_caller] #[cold] fn _py_panic_failed(&self, exc: PyBaseExceptionRef, msg: &str) -> ! { - #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))] + #[cfg(not(all( + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")), + )))] { self.print_exception(exc); self.flush_std(); panic!("{msg}") } + #[cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")), + ))] #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))] { use wasm_bindgen::prelude::*; @@ -32,6 +40,15 @@ impl VirtualMachine { error(&s); panic!("{}; exception backtrace above", msg) } + #[cfg(all( + target_arch = "wasm32", + not(feature = "wasmbind"), + not(any(target_os = "emscripten", target_os = "wasi")), + ))] + { + let _ = exc; + panic!("{}; python exception not available", msg) + } } pub(crate) fn flush_std(&self) { diff --git a/wasm/wasm-unknown-test/Cargo.toml b/wasm/wasm-unknown-test/Cargo.toml new file mode 100644 index 0000000000..b559016923 --- /dev/null +++ b/wasm/wasm-unknown-test/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "wasm-unknown-test" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +getrandom = { version = "0.2.7", features = ["custom"] } +rustpython-vm = { path = "../../vm", default-features = false, features = ["compiler"] } diff --git a/wasm/wasm-unknown-test/README.md b/wasm/wasm-unknown-test/README.md new file mode 100644 index 0000000000..3c4abd59cf --- /dev/null +++ b/wasm/wasm-unknown-test/README.md @@ -0,0 +1 @@ +A test crate to ensure that `rustpython-vm` compiles on `wasm32-unknown-unknown` without a JS host. diff --git a/wasm/wasm-unknown-test/src/lib.rs b/wasm/wasm-unknown-test/src/lib.rs new file mode 100644 index 0000000000..fd043aea3a --- /dev/null +++ b/wasm/wasm-unknown-test/src/lib.rs @@ -0,0 +1,16 @@ +use rustpython_vm::{eval, Interpreter}; + +pub unsafe extern "C" fn eval(s: *const u8, l: usize) -> u32 { + let src = std::slice::from_raw_parts(s, l); + let src = std::str::from_utf8(src).unwrap(); + Interpreter::without_stdlib(Default::default()).enter(|vm| { + let res = eval::eval(vm, src, vm.new_scope_with_builtins(), "").unwrap(); + res.try_into_value(vm).unwrap() + }) +} + +fn getrandom_always_fail(_buf: &mut [u8]) -> Result<(), getrandom::Error> { + Err(getrandom::Error::UNSUPPORTED) +} + +getrandom::register_custom_getrandom!(getrandom_always_fail); From a82982725e73441eb7ae122805ed653f9523c0e8 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 9 Aug 2024 02:20:56 +0900 Subject: [PATCH 10/13] fix getrandom --- Cargo.toml | 2 ++ vm/Cargo.toml | 5 +++-- wasm/lib/Cargo.toml | 2 +- wasm/wasm-unknown-test/Cargo.toml | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 741aab8856..fb230a780a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -147,6 +147,7 @@ cfg-if = "1.0" chrono = "0.4.37" crossbeam-utils = "0.8.19" flame = "0.2.2" +getrandom = "0.2.12" glob = "0.3" hex = "0.4.3" indexmap = { version = "2.2.6", features = ["std"] } @@ -179,6 +180,7 @@ thread_local = "1.1.4" unicode_names2 = "1.2.0" widestring = "1.1.0" windows-sys = "0.52.0" +wasm-bindgen = "0.2.92" # Lints diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 8088e732e3..78702bc8fd 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -49,6 +49,7 @@ cfg-if = { workspace = true } crossbeam-utils = { workspace = true } chrono = { workspace = true, features = ["wasmbind"] } flame = { workspace = true, optional = true } +getrandom = { workspace = true } hex = { workspace = true } indexmap = { workspace = true } itertools = { workspace = true } @@ -72,7 +73,6 @@ thread_local = { workspace = true } memchr = { workspace = true } caseless = "0.2.1" -getrandom = { version = "0.2.12", features = ["custom"] } flamer = { version = "0.4", optional = true } half = "1.8.2" memoffset = "0.9.1" @@ -142,7 +142,8 @@ features = [ ] [target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] -wasm-bindgen = { version = "0.2.92", optional = true } +wasm-bindgen = { workspace = true, optional = true } +getrandom = { workspace = true, features = ["custom"] } [build-dependencies] glob = { workspace = true } diff --git a/wasm/lib/Cargo.toml b/wasm/lib/Cargo.toml index 7d46a3c8da..1e5c37f4ef 100644 --- a/wasm/lib/Cargo.toml +++ b/wasm/lib/Cargo.toml @@ -26,11 +26,11 @@ rustpython-vm = { workspace = true, features = ["compiler", "encodings", "serde" rustpython-parser = { workspace = true } serde = { workspace = true } +wasm-bindgen = { workspace = true } console_error_panic_hook = "0.1" js-sys = "0.3" serde-wasm-bindgen = "0.3.1" -wasm-bindgen = "0.2.80" wasm-bindgen-futures = "0.4" web-sys = { version = "0.3", features = [ "console", diff --git a/wasm/wasm-unknown-test/Cargo.toml b/wasm/wasm-unknown-test/Cargo.toml index b559016923..78d67ae11d 100644 --- a/wasm/wasm-unknown-test/Cargo.toml +++ b/wasm/wasm-unknown-test/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -getrandom = { version = "0.2.7", features = ["custom"] } +getrandom = { version = "0.2.12", features = ["custom"] } rustpython-vm = { path = "../../vm", default-features = false, features = ["compiler"] } From fe63ca762f290c9eb1a25dc7f360c37fd12cd40d Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 9 Aug 2024 01:28:29 +0900 Subject: [PATCH 11/13] separate wasm-unknown-test --- .github/workflows/ci.yaml | 2 +- Cargo.lock | 8 -------- Cargo.toml | 2 +- wasm/wasm-unknown-test/Cargo.toml | 2 ++ 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2d2ec40d4b..0f39ed8343 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -377,7 +377,7 @@ jobs: with: { wabt-version: "1.0.30" } - name: check wasm32-unknown without js run: | - cargo build --release -p wasm-unknown-test --target wasm32-unknown-unknown --verbose + cargo build --release --manifest-path wasm/wasm-unknown-test/Cargo.toml --target wasm32-unknown-unknown --verbose if wasm-objdump -xj Import target/wasm32-unknown-unknown/release/wasm_unknown_test.wasm; then echo "ERROR: wasm32-unknown module expects imports from the host environment" >2 fi diff --git a/Cargo.lock b/Cargo.lock index b5d99cb2a3..5fcb773d84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3154,14 +3154,6 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" -[[package]] -name = "wasm-unknown-test" -version = "0.1.0" -dependencies = [ - "getrandom", - "rustpython-vm", -] - [[package]] name = "web-sys" version = "0.3.61" diff --git a/Cargo.toml b/Cargo.toml index fb230a780a..283698f52c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,7 +97,7 @@ resolver = "2" members = [ "compiler", "compiler/core", "compiler/codegen", ".", "common", "derive", "jit", "vm", "vm/sre_engine", "pylib", "stdlib", "derive-impl", - "wasm/lib", "wasm/wasm-unknown-test", + "wasm/lib", ] [workspace.package] diff --git a/wasm/wasm-unknown-test/Cargo.toml b/wasm/wasm-unknown-test/Cargo.toml index 78d67ae11d..f5e0b55786 100644 --- a/wasm/wasm-unknown-test/Cargo.toml +++ b/wasm/wasm-unknown-test/Cargo.toml @@ -9,3 +9,5 @@ crate-type = ["cdylib"] [dependencies] getrandom = { version = "0.2.12", features = ["custom"] } rustpython-vm = { path = "../../vm", default-features = false, features = ["compiler"] } + +[workspace] From ea11d78995c281f4eeae0513d69d8d2eb127d855 Mon Sep 17 00:00:00 2001 From: Noa Date: Fri, 14 Oct 2022 17:23:32 -0500 Subject: [PATCH 12/13] Fix compilation without compiler feature --- stdlib/src/dis.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/stdlib/src/dis.rs b/stdlib/src/dis.rs index d9322d4871..12c2ea75df 100644 --- a/stdlib/src/dis.rs +++ b/stdlib/src/dis.rs @@ -13,18 +13,23 @@ mod decl { let co = if let Ok(co) = obj.get_attr("__code__", vm) { // Method or function: PyRef::try_from_object(vm, co)? - } else if let Ok(_co_str) = PyStrRef::try_from_object(vm, obj.clone()) { + } else if let Ok(co_str) = PyStrRef::try_from_object(vm, obj.clone()) { #[cfg(not(feature = "compiler"))] - return Err(vm.new_runtime_error( - "dis.dis() with str argument requires `compiler` feature".to_owned(), - )); + { + let _ = co_str; + return Err(vm.new_runtime_error( + "dis.dis() with str argument requires `compiler` feature".to_owned(), + )); + } #[cfg(feature = "compiler")] - vm.compile( - _co_str.as_str(), - crate::vm::compiler::Mode::Exec, - "".to_owned(), - ) - .map_err(|err| vm.new_syntax_error(&err, Some(_co_str.as_str())))? + { + vm.compile( + co_str.as_str(), + crate::vm::compiler::Mode::Exec, + "".to_owned(), + ) + .map_err(|err| vm.new_syntax_error(&err, Some(co_str.as_str())))? + } } else { PyRef::try_from_object(vm, obj)? }; From 42bba6920e0ab0e0fbd576d83229e5191d92444c Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 9 Aug 2024 02:02:21 +0900 Subject: [PATCH 13/13] apply suggestion --- vm/src/vm/vm_object.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vm/src/vm/vm_object.rs b/vm/src/vm/vm_object.rs index 466db6f3a8..b687eea34c 100644 --- a/vm/src/vm/vm_object.rs +++ b/vm/src/vm/vm_object.rs @@ -46,7 +46,9 @@ impl VirtualMachine { not(any(target_os = "emscripten", target_os = "wasi")), ))] { - let _ = exc; + use crate::convert::ToPyObject; + let err_string: String = exc.to_pyobject(self).repr(self).unwrap().to_string(); + eprintln!("{err_string}"); panic!("{}; python exception not available", msg) } }