summaryrefslogtreecommitdiff
path: root/examples/rust/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rust/src')
-rw-r--r--examples/rust/src/tinywasm.rs18
-rw-r--r--examples/rust/src/tinywasm_no_std.rs26
2 files changed, 19 insertions, 25 deletions
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(())
}