summaryrefslogtreecommitdiff
path: root/examples/rust/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rust/src')
-rw-r--r--examples/rust/src/hello.rs12
-rw-r--r--examples/rust/src/print.rs4
-rw-r--r--examples/rust/src/print.twasmbin154 -> 154 bytes
-rw-r--r--examples/rust/src/tinywasm_precompiled.rs30
4 files changed, 10 insertions, 36 deletions
diff --git a/examples/rust/src/hello.rs b/examples/rust/src/hello.rs
index 3d9f400..06ef5c9 100644
--- a/examples/rust/src/hello.rs
+++ b/examples/rust/src/hello.rs
@@ -19,10 +19,12 @@ pub unsafe extern "C" fn arg_size() -> i32 {
#[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();
+ unsafe {
+ let arg = core::str::from_utf8(&ARG[0..len as usize]).unwrap();
+ let res = format!("Hello, {}!", arg).as_bytes().to_vec();
- let len = res.len() as i32;
- let ptr = res.leak().as_ptr() as i64;
- print_utf8(ptr, len);
+ let len = res.len() as i32;
+ let ptr = res.leak().as_ptr() as i64;
+ print_utf8(ptr, len);
+ }
}
diff --git a/examples/rust/src/print.rs b/examples/rust/src/print.rs
index d2934f0..991ea4a 100644
--- a/examples/rust/src/print.rs
+++ b/examples/rust/src/print.rs
@@ -7,5 +7,7 @@ unsafe extern "C" {
#[unsafe(no_mangle)]
pub unsafe extern "C" fn add_and_print(lh: i32, rh: i32) {
- printi32(lh + rh);
+ unsafe {
+ printi32(lh + rh);
+ }
}
diff --git a/examples/rust/src/print.twasm b/examples/rust/src/print.twasm
index f0fa9a8..7841329 100644
--- a/examples/rust/src/print.twasm
+++ b/examples/rust/src/print.twasm
Binary files differ
diff --git a/examples/rust/src/tinywasm_precompiled.rs b/examples/rust/src/tinywasm_precompiled.rs
deleted file mode 100644
index bc0e0c1..0000000
--- a/examples/rust/src/tinywasm_precompiled.rs
+++ /dev/null
@@ -1,30 +0,0 @@
-#![no_main]
-use tinywasm::{FuncContext, HostFunction, Module, ModuleInstance};
-
-#[link(wasm_import_module = "env")]
-unsafe extern "C" {
- fn printi32(x: i32);
-}
-
-#[unsafe(no_mangle)]
-pub extern "C" fn hello() {
- let _ = run();
-}
-
-fn run() -> tinywasm::Result<()> {
- let module = Module::try_from_twasm(include_bytes!("./print.twasm"))?;
- let mut store = tinywasm::Store::default();
-
- let printi32 = HostFunction::from(|_: FuncContext<'_>, v: i32| {
- unsafe { printi32(v) }
- Ok(())
- });
-
- let mut imports = tinywasm::Imports::new();
- imports.define("env", "printi32", printi32);
-
- let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?;
- let add_and_print = instance.func::<(i32, i32), ()>(&store, "add_and_print")?;
- add_and_print.call(&mut store, (1, 2))?;
- Ok(())
-}