diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2025-02-12 19:13:23 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2025-02-12 19:13:23 +0100 |
| commit | d6493c277eb1104aaf72a8e0e6959ce3d1cc4932 (patch) | |
| tree | a82452aeb44be6b79d15fc83ddfcbd12a965520a /examples/rust | |
| parent | 7e668f9e321c941f38395e47a29501dce0fc81a9 (diff) | |
fix: fix build, improve error handling
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples/rust')
| -rw-r--r-- | examples/rust/Cargo.toml | 4 | ||||
| -rwxr-xr-x | examples/rust/build.sh | 4 | ||||
| -rw-r--r-- | examples/rust/src/argon2id.rs | 2 | ||||
| -rw-r--r-- | examples/rust/src/fibonacci.rs | 4 | ||||
| -rw-r--r-- | examples/rust/src/hello.rs | 8 | ||||
| -rw-r--r-- | examples/rust/src/host_fn.rs | 11 | ||||
| -rw-r--r-- | examples/rust/src/print.rs | 4 | ||||
| -rw-r--r-- | examples/rust/src/tinywasm.rs | 4 | ||||
| -rw-r--r-- | examples/rust/src/tinywasm_no_std.rs | 6 |
9 files changed, 31 insertions, 16 deletions
diff --git a/examples/rust/Cargo.toml b/examples/rust/Cargo.toml index 5c6b710..da238ad 100644 --- a/examples/rust/Cargo.toml +++ b/examples/rust/Cargo.toml @@ -23,6 +23,10 @@ name="hello" path="src/hello.rs" [[bin]] +name="host_fn" +path="src/host_fn.rs" + +[[bin]] name="print" path="src/print.rs" diff --git a/examples/rust/build.sh b/examples/rust/build.sh index d0415ac..e1d320f 100755 --- a/examples/rust/build.sh +++ b/examples/rust/build.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash -cd "$(dirname "$0")" +cd "$(dirname "$0")" || exit -bins=("hello" "fibonacci" "print" "tinywasm" "argon2id") +bins=("host_fn" "hello" "fibonacci" "print" "tinywasm" "argon2id") exclude_wat=("tinywasm") out_dir="./target/wasm32-unknown-unknown/wasm" dest_dir="out" diff --git a/examples/rust/src/argon2id.rs b/examples/rust/src/argon2id.rs index 01ea7ca..ff17d04 100644 --- a/examples/rust/src/argon2id.rs +++ b/examples/rust/src/argon2id.rs @@ -1,6 +1,6 @@ #![no_main] -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn argon2id(m_cost: i32, t_cost: i32, p_cost: i32) -> i32 { let password = b"password"; let salt = b"some random salt"; diff --git a/examples/rust/src/fibonacci.rs b/examples/rust/src/fibonacci.rs index b847ad5..ec4a371 100644 --- a/examples/rust/src/fibonacci.rs +++ b/examples/rust/src/fibonacci.rs @@ -1,6 +1,6 @@ #![no_main] -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn fibonacci(n: i32) -> i32 { let mut sum = 0; let mut last = 0; @@ -13,7 +13,7 @@ pub extern "C" fn fibonacci(n: i32) -> i32 { sum } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn fibonacci_recursive(n: i32) -> i32 { if n <= 1 { return n; diff --git a/examples/rust/src/hello.rs b/examples/rust/src/hello.rs index c8e2ac3..3d9f400 100644 --- a/examples/rust/src/hello.rs +++ b/examples/rust/src/hello.rs @@ -1,23 +1,23 @@ #![no_main] #[link(wasm_import_module = "env")] -extern "C" { +unsafe extern "C" { fn print_utf8(location: i64, len: i32); } const ARG: &[u8] = &[0u8; 100]; -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn arg_ptr() -> i32 { ARG.as_ptr() as i32 } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn arg_size() -> i32 { ARG.len() as i32 } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn hello(len: i32) { let arg = core::str::from_utf8(&ARG[0..len as usize]).unwrap(); let res = format!("Hello, {}!", arg).as_bytes().to_vec(); diff --git a/examples/rust/src/host_fn.rs b/examples/rust/src/host_fn.rs new file mode 100644 index 0000000..ada74bd --- /dev/null +++ b/examples/rust/src/host_fn.rs @@ -0,0 +1,11 @@ +#![no_main] + +#[link(wasm_import_module = "env")] +unsafe extern "C" { + fn bar(left: i64, right: i32) -> i32; +} + +#[unsafe(no_mangle)] +pub fn foo() -> i32 { + unsafe { bar(1, 2) } +} diff --git a/examples/rust/src/print.rs b/examples/rust/src/print.rs index d04daa3..d2934f0 100644 --- a/examples/rust/src/print.rs +++ b/examples/rust/src/print.rs @@ -1,11 +1,11 @@ #![no_main] #[link(wasm_import_module = "env")] -extern "C" { +unsafe extern "C" { fn printi32(x: i32); } -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn add_and_print(lh: i32, rh: i32) { printi32(lh + rh); } diff --git a/examples/rust/src/tinywasm.rs b/examples/rust/src/tinywasm.rs index c3bf2d2..d3b3f8c 100644 --- a/examples/rust/src/tinywasm.rs +++ b/examples/rust/src/tinywasm.rs @@ -2,11 +2,11 @@ use tinywasm::{Extern, FuncContext}; #[link(wasm_import_module = "env")] -extern "C" { +unsafe extern "C" { fn printi32(x: i32); } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn hello() { let _ = run(); } diff --git a/examples/rust/src/tinywasm_no_std.rs b/examples/rust/src/tinywasm_no_std.rs index 9a31d20..46f1785 100644 --- a/examples/rust/src/tinywasm_no_std.rs +++ b/examples/rust/src/tinywasm_no_std.rs @@ -16,11 +16,11 @@ static ALLOCATOR: AssumeSingleThreaded<FreeListAllocator> = unsafe { AssumeSingleThreaded::new(FreeListAllocator::new()) }; #[link(wasm_import_module = "env")] -extern "C" { +unsafe extern "C" { fn printi32(x: i32); } -#[no_mangle] +#[unsafe(no_mangle)] pub extern "C" fn hello() { let _ = run(); } @@ -30,7 +30,7 @@ fn run() -> tinywasm::Result<()> { let mut imports = tinywasm::Imports::new(); let res = tinywasm::parser::Parser::new().parse_module_bytes(include_bytes!("./print.wasm"))?; - let twasm = res.serialize_twasm(); + let twasm = res.serialize_twasm()?; let module = tinywasm::Module::parse_bytes(&twasm)?; imports.define( |
