| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -82,6 +82,7 @@ unsync | |
| wasip1 | ||
| wasip2 | ||
| wasmbind | ||
| wasmer | ||
| wasmtime | ||
| widestring | ||
| winapi | ||
| Expand Down | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| */target/ | ||
| */Cargo.lock |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # RustPython wasm32 build without JS | ||
|
|
||
| To test, build rustpython to wasm32-unknown-unknown target first. | ||
|
|
||
| ```shell | ||
| cd rustpython-without-js # due to `.cargo/config.toml` | ||
| cargo build | ||
| cd .. | ||
| ``` | ||
|
|
||
| Then there will be `rustpython-without-js/target/wasm32-unknown-unknown/debug/rustpython_without_js.wasm` file. | ||
|
|
||
| Now we can run the wasm file with wasm runtime: | ||
|
|
||
| ```shell | ||
| cargo run --release --manifest-path wasm-runtime/Cargo.toml rustpython-without-js/target/wasm32-unknown-unknown/debug/rustpython_without_js.wasm | ||
| ``` | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [package] | ||
| name = "rustpython-without-js" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| getrandom = "0.3" | ||
| rustpython-vm = { path = "../../../crates/vm", default-features = false, features = ["compiler"] } | ||
|
|
||
| [workspace] | ||
|
|
||
| [patch.crates-io] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| use rustpython_vm::{Interpreter}; | ||
|
|
||
| unsafe extern "C" { | ||
| fn kv_get(kp: i32, kl: i32, vp: i32, vl: i32) -> i32; | ||
|
|
||
| /// kp and kl are the key pointer and length in wasm memory, vp and vl are for the value | ||
| fn kv_put(kp: i32, kl: i32, vp: i32, vl: i32) -> i32; | ||
|
|
||
| fn print(p: i32, l: i32) -> i32; | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub unsafe extern "C" fn eval(s: *const u8, l: usize) -> i32 { | ||
| // let src = unsafe { std::slice::from_raw_parts(s, l) }; | ||
| // let src = std::str::from_utf8(src).unwrap(); | ||
| // TODO: use src | ||
| let src = "1 + 3"; | ||
|
|
||
| // 2. Execute Python code | ||
| let interpreter = Interpreter::without_stdlib(Default::default()); | ||
| let result = interpreter.enter(|vm| { | ||
| let scope = vm.new_scope_with_builtins(); | ||
| let res = match vm.run_block_expr(scope, src) { | ||
| Ok(val) => val, | ||
| Err(_) => return Err(-1), // Python execution error | ||
| }; | ||
| let repr_str = match res.repr(vm) { | ||
| Ok(repr) => repr.as_str().to_string(), | ||
| Err(_) => return Err(-1), // Failed to get string representation | ||
| }; | ||
| Ok(repr_str) | ||
| }); | ||
| let result = match result { | ||
| Ok(r) => r, | ||
| Err(code) => return code, | ||
| }; | ||
|
|
||
| let msg = format!("eval result: {result}"); | ||
|
|
||
| unsafe { | ||
| print( | ||
| msg.as_str().as_ptr() as usize as i32, | ||
| msg.len() as i32, | ||
| ) | ||
| }; | ||
|
|
||
| 0 | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| unsafe extern "Rust" fn __getrandom_v03_custom( | ||
| _dest: *mut u8, | ||
| _len: usize, | ||
| ) -> Result<(), getrandom::Error> { | ||
| // Err(getrandom::Error::UNSUPPORTED) | ||
|
|
||
| // WARNING: This function **MUST** perform proper getrandom | ||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| *.wasm | ||
| target | ||
| Cargo.lock | ||
| !wasm/rustpython.wasm |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,9 @@ | ||||||
| [package] | ||||||
| name = "wasm-runtime" | ||||||
| version = "0.1.0" | ||||||
| edition = "2024" | ||||||
|
Comment thread
Copy link
Copy Markdown
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality⚠️ Potential issue | 🔴 Critical Fix invalid Rust edition. Edition 2024 does not exist. The latest stable Rust edition is 2021. This will cause a compilation failure. Apply this diff to fix: -edition = "2024"
+edition = "2021"
Suggested change
In example_projects/wasm32_without_js/wasm-runtime/Cargo.toml around line 4, the Cargo.toml incorrectly sets edition = "2024"; change it to the latest valid Rust edition by replacing the value with "2021" so the crate compiles successfully.
Sorry, something went wrong.
All reactions
|
||||||
|
|
||||||
| [dependencies] | ||||||
| wasmer = "6.1.0" | ||||||
|
|
||||||
| [workspace] | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Simple WASM Runtime | ||
|
|
||
| WebAssembly runtime POC with wasmer with HashMap-based KV store. | ||
| First make sure to install wat2wasm and rust. | ||
|
|
||
| ```bash | ||
| # following command installs rust | ||
| curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh | ||
|
|
||
| cargo run --release <wasm binary> | ||
| ``` | ||
|
|
||
| ## WASM binary requirements | ||
|
|
||
| Entry point is `eval(code_ptr: i32, code_len: i32) -> i32`, following are exported functions, on error return -1: | ||
|
|
||
| - `kv_put(key_ptr: i32, key_len: i32, val_ptr: i32, val_len: i32) -> i32` | ||
| - `kv_get(key_ptr: i32, key_len: i32, val_ptr: i32, val_len: i32) -> i32` | ||
| - `print(msg_ptr: i32, msg_len: i32) -> i32` |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,133 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use std::collections::HashMap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use wasmer::{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Function, FunctionEnv, FunctionEnvMut, Instance, Memory, Module, Store, Value, imports, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Ctx { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kv: HashMap<Vec<u8>, Vec<u8>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mem: Option<Memory>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// kp and kl are the key pointer and length in wasm memory, vp and vl are for the return value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// if read value is bigger than vl then it will be truncated to vl, returns read bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn kv_get(mut ctx: FunctionEnvMut<Ctx>, kp: i32, kl: i32, vp: i32, vl: i32) -> i32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let (c, s) = ctx.data_and_store_mut(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut key = vec![0u8; kl as usize]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if c.mem | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .as_ref() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .view(&s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .read(kp as u64, &mut key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .is_err() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match c.kv.get(&key) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Some(val) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let len = val.len().min(vl as usize); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if c.mem | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .as_ref() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .view(&s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .write(vp as u64, &val[..len]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .is_err() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| len as i32 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| None => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment thread
Comment on lines
+13
to
+41
Copy link
Copy Markdown
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality⚠️ Potential issue | 🟠 Major Replace unwrap() with proper error handling. Lines 18 and 30 call unwrap() on c.mem.as_ref(), which will panic if the memory reference is None. While this should never happen if the module is correctly initialized, it creates a potential panic point. Consider using expect() with a descriptive message or returning an error code: fn kv_get(mut ctx: FunctionEnvMut<Ctx>, kp: i32, kl: i32, vp: i32, vl: i32) -> i32 {
let (c, s) = ctx.data_and_store_mut();
let mut key = vec![0u8; kl as usize];
- if c.mem
- .as_ref()
- .unwrap()
- .view(&s)
+ let mem = c.mem.as_ref().expect("memory not initialized");
+ if mem.view(&s)
.read(kp as u64, &mut key)
.is_err()
{
return -1;
}
match c.kv.get(&key) {
Some(val) => {
let len = val.len().min(vl as usize);
- if c.mem
- .as_ref()
- .unwrap()
- .view(&s)
+ if mem.view(&s)
.write(vp as u64, &val[..len])
.is_err()
{
return -1;
}
len as i32
}
None => 0,
}
}In example_projects/wasm32_without_js/wasm-runtime/src/main.rs around lines 13 to 41, replace the two uses of c.mem.as_ref().unwrap() with safe error handling: check whether c.mem.as_ref() is Some and if not return an appropriate error code (e.g. -1) or use expect() with a clear message if you prefer a hard failure; then use the unwrapped reference afterward (or bind it to a variable) so the read/write calls operate on a guaranteed memory reference and avoid potential panics.
Sorry, something went wrong.
All reactions
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// kp and kl are the key pointer and length in wasm memory, vp and vl are for the value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn kv_put(mut ctx: FunctionEnvMut<Ctx>, kp: i32, kl: i32, vp: i32, vl: i32) -> i32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let (c, s) = ctx.data_and_store_mut(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut key = vec![0u8; kl as usize]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut val = vec![0u8; vl as usize]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let m = c.mem.as_ref().unwrap().view(&s); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if m.read(kp as u64, &mut key).is_err() || m.read(vp as u64, &mut val).is_err() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c.kv.insert(key, val); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment thread
Comment on lines
+44
to
+54
Copy link
Copy Markdown
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality⚠️ Potential issue | 🟠 Major Replace unwrap() with proper error handling. Line 48 calls unwrap() on c.mem.as_ref(), which creates a potential panic point if memory is not initialized. Apply similar fix as suggested for kv_get: fn kv_put(mut ctx: FunctionEnvMut<Ctx>, kp: i32, kl: i32, vp: i32, vl: i32) -> i32 {
let (c, s) = ctx.data_and_store_mut();
let mut key = vec![0u8; kl as usize];
let mut val = vec![0u8; vl as usize];
- let m = c.mem.as_ref().unwrap().view(&s);
+ let m = c.mem.as_ref().expect("memory not initialized").view(&s);
if m.read(kp as u64, &mut key).is_err() || m.read(vp as u64, &mut val).is_err() {
return -1;
}
c.kv.insert(key, val);
0
}
Suggested change
In example_projects/wasm32_without_js/wasm-runtime/src/main.rs around lines 44 to 54, replace the direct unwrap() on c.mem.as_ref() with safe handling: check whether c.mem is Some and if not return an error code (e.g., -1) like kv_get does; if Some(mem) obtain the memory view (let m = mem.view(&s)) and continue with the existing reads and inserts, preserving the current read error checks and returning -1 on failure and 0 on success.
Sorry, something went wrong.
All reactions
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // // p and l are the buffer pointer and length in wasm memory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // fn get_code(mut ctx:FunctionEnvMut<Ctx>, p: i32, l: i32) -> i32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // let file_name = std::env::args().nth(2).expect("file_name is not given"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // let code : String = std::fs::read_to_string(file_name).expect("file read failed"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // if code.len() > l as usize { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // eprintln!("code is too long"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // return -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // let (c, s) = ctx.data_and_store_mut(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // let m = c.mem.as_ref().unwrap().view(&s); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // if m.write(p as u64, code.as_bytes()).is_err() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // return -2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // p and l are the message pointer and length in wasm memory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn print(mut ctx: FunctionEnvMut<Ctx>, p: i32, l: i32) -> i32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let (c, s) = ctx.data_and_store_mut(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut msg = vec![0u8; l as usize]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let m = c.mem.as_ref().unwrap().view(&s); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if m.read(p as u64, &mut msg).is_err() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let s = std::str::from_utf8(&msg).expect("print got non-utf8 str"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| println!("{s}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment thread
Comment on lines
+75
to
+85
Copy link
Copy Markdown
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality⚠️ Potential issue | 🟠 Major Improve error handling for memory access and UTF-8 validation. Two issues:
Consider this more robust approach: fn print(mut ctx: FunctionEnvMut<Ctx>, p: i32, l: i32) -> i32 {
let (c, s) = ctx.data_and_store_mut();
let mut msg = vec![0u8; l as usize];
- let m = c.mem.as_ref().unwrap().view(&s);
+ let m = c.mem.as_ref().expect("memory not initialized").view(&s);
if m.read(p as u64, &mut msg).is_err() {
return -1;
}
- let s = std::str::from_utf8(&msg).expect("print got non-utf8 str");
- println!("{s}");
- 0
+ match std::str::from_utf8(&msg) {
+ Ok(s) => {
+ println!("{s}");
+ 0
+ }
+ Err(_) => {
+ eprintln!("print received non-UTF8 data");
+ -1
+ }
+ }
}
Suggested change
Sorry, something went wrong.
All reactions
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn main() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut store = Store::default(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let module = Module::new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| &store, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| &std::fs::read(&std::env::args().nth(1).unwrap()).unwrap(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment thread
Comment on lines
+87
to
+93
Copy link
Copy Markdown
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality⚠️ Potential issue | 🟠 Major Add command-line argument validation. Line 91 calls nth(1).unwrap() without checking if the argument exists, which will panic if no WASM file path is provided. Consider adding proper error handling. Apply this diff: fn main() {
+ let wasm_path = std::env::args()
+ .nth(1)
+ .expect("Usage: wasm-runtime <wasm_file_path>");
+
let mut store = Store::default();
- let module = Module::new(
- &store,
- &std::fs::read(&std::env::args().nth(1).unwrap()).unwrap(),
- )
- .unwrap();
+ let wasm_bytes = std::fs::read(&wasm_path)
+ .expect("Failed to read WASM file");
+ let module = Module::new(&store, &wasm_bytes)
+ .expect("Failed to create WASM module");
Suggested change
In example_projects/wasm32_without_js/wasm-runtime/src/main.rs around lines 87 to 93, the code calls std::env::args().nth(1).unwrap() which will panic if no argument is provided; update the main function to validate the presence of the first command-line argument, return a clear error or print a usage message and exit when it is missing, and then use the validated path when reading the file (e.g., check args().nth(1).ok_or_else(|| ...) or pattern-match and handle the None case before calling std::fs::read and Module::new).
Sorry, something went wrong.
All reactions
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Prepare initial KV store with Python code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut initial_kv = HashMap::new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| initial_kv.insert( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| b"code".to_vec(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| b"a=10;b='str';f'{a}{b}'".to_vec(), // Python code to execute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let env = FunctionEnv::new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| &mut store, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ctx { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kv: initial_kv, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mem: None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let imports = imports! { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "env" => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "kv_get" => Function::new_typed_with_env(&mut store, &env, kv_get), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "kv_put" => Function::new_typed_with_env(&mut store, &env, kv_put), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // "get_code" => Function::new_typed_with_env(&mut store, &env, get_code), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "print" => Function::new_typed_with_env(&mut store, &env, print), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let inst = Instance::new(&mut store, &module, &imports).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| env.as_mut(&mut store).mem = inst.exports.get_memory("memory").ok().cloned(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let res = inst | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .exports | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .get_function("eval") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: actually pass source code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .call(&mut store, &[wasmer::Value::I32(0), wasmer::Value::I32(0)]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| println!( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Result: {}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match res[0] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Value::I32(v) => v, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ => -1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file was deleted.
This file was deleted.
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality⚠️ Potential issue | 🔴 Critical
Fix no_mangle attribute, use the eval input, and print the full message
There are a few issues in this block:
A minimal fix that keeps the current structure but makes it usable would look like:
This keeps the ABI, actually uses the caller‑provided code, and prints the prefixed message, while preserving your simple i32 error convention.
📝 Committable suggestionIn example_projects/wasm32_without_js/rustpython-without-js/src/lib.rs around lines 13 to 49, fix three issues: change the invalid attribute to #[no_mangle] (keep the function as pub unsafe extern "C" fn eval(...)), actually read the caller-provided code by creating a slice from_raw_parts(s, l) and converting it to &str (return an error code on null/utf8 failure), and build the full message using the formatted prefix (e.g. "eval result: {repr}") then pass that message's pointer and length to print (cast pointer/len to the i32 ABI types you use) so the printed output contains the prefix and the evaluated result.Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.