summaryrefslogtreecommitdiff
path: root/examples/rust
diff options
context:
space:
mode:
authorHenry Gressmann <mail@henrygressmann.de>2024-01-26 12:03:59 +0100
committerHenry Gressmann <mail@henrygressmann.de>2024-01-26 12:03:59 +0100
commitf3d890a36d6299dbd70377ce57b0bd9d61de5c1b (patch)
tree36b51022af641c0f464fd0c5d4456b722e5f735b /examples/rust
parent461126ce309f99e4ad2a6ddcbc175ea01c5e8fee (diff)
docs: working selfhosted example
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples/rust')
-rw-r--r--examples/rust/Cargo.toml7
-rwxr-xr-xexamples/rust/build.sh18
-rw-r--r--examples/rust/src/tinywasm.rs47
3 files changed, 38 insertions, 34 deletions
diff --git a/examples/rust/Cargo.toml b/examples/rust/Cargo.toml
index 91b6aed..9ff80dc 100644
--- a/examples/rust/Cargo.toml
+++ b/examples/rust/Cargo.toml
@@ -7,8 +7,7 @@ forced-target="wasm32-unknown-unknown"
edition="2021"
[dependencies]
-tinywasm={path="../../crates/tinywasm", default-features=false, features=["parser"]}
-embedded-alloc={version="0.5"}
+tinywasm={path="../../crates/tinywasm", features=["parser", "std"]}
[[bin]]
name="hello"
@@ -17,7 +16,3 @@ path="src/hello.rs"
[[bin]]
name="tinywasm"
path="src/tinywasm.rs"
-
-[profile.release]
-opt-level="z"
-panic="abort"
diff --git a/examples/rust/build.sh b/examples/rust/build.sh
new file mode 100755
index 0000000..2c8069a
--- /dev/null
+++ b/examples/rust/build.sh
@@ -0,0 +1,18 @@
+#!/usr/bin/env bash
+cd "$(dirname "$0")"
+
+bins=("hello" "tinywasm")
+exclude_wat=("tinywasm")
+out_dir="../../target/wasm32-unknown-unknown/wasm"
+dest_dir="out"
+
+for bin in "${bins[@]}"; do
+ cargo build --target wasm32-unknown-unknown --package rust-wasm-examples --profile=wasm --bin "$bin"
+
+ cp "$out_dir/$bin.wasm" "$dest_dir/"
+ wasm-opt "$dest_dir/$bin.wasm" -o "$dest_dir/$bin.wasm" -O --intrinsic-lowering -O
+
+ if [[ ! " ${exclude_wat[@]} " =~ " $bin " ]]; then
+ wasm2wat "$dest_dir/$bin.wasm" -o "$dest_dir/$bin.wat"
+ fi
+done
diff --git a/examples/rust/src/tinywasm.rs b/examples/rust/src/tinywasm.rs
index 4edde66..52af7b9 100644
--- a/examples/rust/src/tinywasm.rs
+++ b/examples/rust/src/tinywasm.rs
@@ -1,41 +1,32 @@
-#![no_std]
#![no_main]
+use tinywasm::{Extern, FuncContext};
-use embedded_alloc::Heap;
-// use tinywasm::{Extern, FuncContext};
-
-#[cfg(not(test))]
-#[panic_handler]
-fn panic(_info: &core::panic::PanicInfo) -> ! {
- core::arch::wasm32::unreachable()
+#[link(wasm_import_module = "env")]
+extern "C" {
+ fn printi32(x: i32);
}
-#[global_allocator]
-static HEAP: Heap = Heap::empty();
-
#[no_mangle]
-pub unsafe extern "C" fn _start() {
- // Initialize the allocator BEFORE you use it
- {
- use core::mem::MaybeUninit;
- const HEAP_SIZE: usize = 1024;
- static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
- unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
- }
-
- // now the allocator is ready types like Box, Vec can be used.
+pub extern "C" fn hello() {
let _ = run();
}
fn run() -> tinywasm::Result<()> {
- // let module = tinywasm::Module::parse_bytes(include_bytes!("../out/hello.wasm"))?;
- // let mut store = tinywasm::Store::default();
+ let module = tinywasm::Module::parse_bytes(include_bytes!("../../wasm/hello.wasm"))?;
+ let mut store = tinywasm::Store::default();
+ let mut imports = tinywasm::Imports::new();
- // let mut imports = tinywasm::Imports::new();
- // imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _: i32| Ok(())))?;
+ imports.define(
+ "env",
+ "printi32",
+ Extern::typed_func(|_: FuncContext<'_>, v: i32| {
+ unsafe { printi32(v) }
+ Ok(())
+ }),
+ )?;
+ let instance = module.instantiate(&mut store, Some(imports))?;
- // let instance = module.instantiate(&mut store, Some(imports))?;
- // let add_and_print = instance.typed_func::<(i32, i32), ()>(&mut store, "add_and_print")?;
- // add_and_print.call(&mut store, (1, 2))?;
+ let add_and_print = instance.typed_func::<(i32, i32), ()>(&mut store, "add_and_print")?;
+ add_and_print.call(&mut store, (1, 2))?;
Ok(())
}