diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rust/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/rust/src/tinywasm.rs | 18 | ||||
| -rw-r--r-- | examples/rust/src/tinywasm_no_std.rs | 26 |
3 files changed, 20 insertions, 26 deletions
diff --git a/examples/rust/Cargo.toml b/examples/rust/Cargo.toml index 6bf8a8f..73aa815 100644 --- a/examples/rust/Cargo.toml +++ b/examples/rust/Cargo.toml @@ -12,7 +12,7 @@ edition="2021" [dependencies] tinywasm={path="../../crates/tinywasm", default-features=false, features=["parser", "archive"]} argon2={version="0.5"} -lol_alloc="0.4.1" +dlmalloc={version="0.2", features=["global"]} [features] default=["std"] diff --git a/examples/rust/src/tinywasm.rs b/examples/rust/src/tinywasm.rs index 7de0dbc..0ccb026 100644 --- a/examples/rust/src/tinywasm.rs +++ b/examples/rust/src/tinywasm.rs @@ -14,19 +14,17 @@ pub extern "C" fn hello() { fn run() -> tinywasm::Result<()> { let module = tinywasm::Module::parse_stream(&include_bytes!("./print.wasm")[..])?; let mut store = tinywasm::Store::default(); + + let printi32 = HostFunction::from(&mut store, |_: FuncContext<'_>, v: i32| { + unsafe { printi32(v) } + Ok(()) + }); + let mut imports = tinywasm::Imports::new(); + imports.define("env", "printi32", printi32); - imports.define( - "env", - "printi32", - HostFunction::from(&mut store, |_: FuncContext<'_>, v: i32| { - unsafe { printi32(v) } - Ok(()) - }), - )?; let instance = module.instantiate(&mut store, Some(imports))?; - - let add_and_print = instance.func_typed::<(i32, i32), ()>(&store, "add_and_print")?; + let add_and_print = instance.func::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) } diff --git a/examples/rust/src/tinywasm_no_std.rs b/examples/rust/src/tinywasm_no_std.rs index 5444280..6d7e73c 100644 --- a/examples/rust/src/tinywasm_no_std.rs +++ b/examples/rust/src/tinywasm_no_std.rs @@ -1,20 +1,19 @@ #![no_main] #![no_std] -use lol_alloc::{AssumeSingleThreaded, FreeListAllocator}; +use dlmalloc::GlobalDlmalloc; use tinywasm::{FuncContext, HostFunction}; extern crate alloc; +#[global_allocator] +static ALLOCATOR: GlobalDlmalloc = GlobalDlmalloc; + #[cfg(not(feature = "std"))] #[panic_handler] fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} } -#[global_allocator] -static ALLOCATOR: AssumeSingleThreaded<FreeListAllocator> = - unsafe { AssumeSingleThreaded::new(FreeListAllocator::new()) }; - #[link(wasm_import_module = "env")] unsafe extern "C" { fn printi32(x: i32); @@ -33,17 +32,14 @@ fn run() -> tinywasm::Result<()> { let twasm = res.serialize_twasm()?; let module = tinywasm::Module::parse_bytes(&twasm)?; - imports.define( - "env", - "printi32", - HostFunction::from(&mut store, |_: FuncContext<'_>, v: i32| { - unsafe { printi32(v) } - Ok(()) - }), - )?; - let instance = module.instantiate(&mut store, Some(imports))?; + let printi32 = HostFunction::from(&mut store, |_: FuncContext<'_>, v: i32| { + unsafe { printi32(v) } + Ok(()) + }); - let add_and_print = instance.func_typed::<(i32, i32), ()>(&store, "add_and_print")?; + imports.define("env", "printi32", printi32); + let instance = module.instantiate(&mut store, Some(imports))?; + let add_and_print = instance.func::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) } |
