From 461126ce309f99e4ad2a6ddcbc175ea01c5e8fee Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Fri, 26 Jan 2024 01:26:09 +0100 Subject: feat: full no_std support Signed-off-by: Henry Gressmann --- examples/rust/Cargo.toml | 23 +++++++++++++++++++++++ examples/rust/README.md | 1 + examples/rust/src/hello.rs | 18 ++++++++++++++++++ examples/rust/src/tinywasm.rs | 41 +++++++++++++++++++++++++++++++++++++++++ examples/wasm-rust.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 examples/rust/Cargo.toml create mode 100644 examples/rust/README.md create mode 100644 examples/rust/src/hello.rs create mode 100644 examples/rust/src/tinywasm.rs create mode 100644 examples/wasm-rust.rs (limited to 'examples') diff --git a/examples/rust/Cargo.toml b/examples/rust/Cargo.toml new file mode 100644 index 0000000..91b6aed --- /dev/null +++ b/examples/rust/Cargo.toml @@ -0,0 +1,23 @@ +cargo-features=["per-package-target"] + +[package] +publish=false +name="rust-wasm-examples" +forced-target="wasm32-unknown-unknown" +edition="2021" + +[dependencies] +tinywasm={path="../../crates/tinywasm", default-features=false, features=["parser"]} +embedded-alloc={version="0.5"} + +[[bin]] +name="hello" +path="src/hello.rs" + +[[bin]] +name="tinywasm" +path="src/tinywasm.rs" + +[profile.release] +opt-level="z" +panic="abort" diff --git a/examples/rust/README.md b/examples/rust/README.md new file mode 100644 index 0000000..8ecac52 --- /dev/null +++ b/examples/rust/README.md @@ -0,0 +1 @@ +# Examples using Rust compiled to WebAssembly diff --git a/examples/rust/src/hello.rs b/examples/rust/src/hello.rs new file mode 100644 index 0000000..34f3c7f --- /dev/null +++ b/examples/rust/src/hello.rs @@ -0,0 +1,18 @@ +#![no_std] +#![no_main] + +#[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); +} + +#[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 new file mode 100644 index 0000000..4edde66 --- /dev/null +++ b/examples/rust/src/tinywasm.rs @@ -0,0 +1,41 @@ +#![no_std] +#![no_main] + +use embedded_alloc::Heap; +// use tinywasm::{Extern, FuncContext}; + +#[cfg(not(test))] +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + core::arch::wasm32::unreachable() +} + +#[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; 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. + let _ = run(); +} + +fn run() -> tinywasm::Result<()> { + // let module = tinywasm::Module::parse_bytes(include_bytes!("../out/hello.wasm"))?; + // let mut store = tinywasm::Store::default(); + + // let mut imports = tinywasm::Imports::new(); + // imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _: i32| Ok(())))?; + + // 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))?; + Ok(()) +} diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs new file mode 100644 index 0000000..21d3eb8 --- /dev/null +++ b/examples/wasm-rust.rs @@ -0,0 +1,41 @@ +use color_eyre::eyre::Result; +use tinywasm::{Extern, FuncContext, Imports, Module, Store}; + +fn main() -> Result<()> { + let args = std::env::args().collect::>(); + if args.len() < 2 { + println!("Usage: cargo run --example wasm-rust "); + println!("Available examples:"); + println!(" hello"); + return Ok(()); + } + + match args[1].as_str() { + "hello" => hello()?, + _ => {} + } + + Ok(()) +} + +fn hello() -> Result<()> { + // const HELLO_WASM: &[u8] = include_bytes!("./rust/out/hello.wasm"); + // let module = Module::parse_bytes(&HELLO_WASM)?; + // let mut store = Store::default(); + + // let mut imports = Imports::new(); + // imports.define( + // "env", + // "printi32", + // Extern::typed_func(|_: FuncContext<'_>, x: i32| { + // println!("{}", x); + // Ok(()) + // }), + // )?; + + // 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))?; + + Ok(()) +} -- cgit v1.3.1