summaryrefslogtreecommitdiff
path: root/examples/rust/src
diff options
context:
space:
mode:
authorHenry Gressmann <mail@henrygressmann.de>2025-02-12 19:13:23 +0100
committerHenry Gressmann <mail@henrygressmann.de>2025-02-12 19:13:23 +0100
commitd6493c277eb1104aaf72a8e0e6959ce3d1cc4932 (patch)
treea82452aeb44be6b79d15fc83ddfcbd12a965520a /examples/rust/src
parent7e668f9e321c941f38395e47a29501dce0fc81a9 (diff)
fix: fix build, improve error handling
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples/rust/src')
-rw-r--r--examples/rust/src/argon2id.rs2
-rw-r--r--examples/rust/src/fibonacci.rs4
-rw-r--r--examples/rust/src/hello.rs8
-rw-r--r--examples/rust/src/host_fn.rs11
-rw-r--r--examples/rust/src/print.rs4
-rw-r--r--examples/rust/src/tinywasm.rs4
-rw-r--r--examples/rust/src/tinywasm_no_std.rs6
7 files changed, 25 insertions, 14 deletions
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(