summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/rust/Cargo.toml11
-rwxr-xr-xexamples/rust/build.sh3
-rw-r--r--examples/rust/src/tinywasm.rs2
-rw-r--r--examples/rust/src/tinywasm_no_std.rs17
-rw-r--r--examples/wasm-rust.rs59
5 files changed, 86 insertions, 6 deletions
diff --git a/examples/rust/Cargo.toml b/examples/rust/Cargo.toml
index f691a1b..5c6b710 100644
--- a/examples/rust/Cargo.toml
+++ b/examples/rust/Cargo.toml
@@ -10,8 +10,13 @@ forced-target="wasm32-unknown-unknown"
edition="2021"
[dependencies]
-tinywasm={path="../../crates/tinywasm", features=["parser", "std"]}
+tinywasm={path="../../crates/tinywasm", default-features=false, features=["parser", "archive"]}
argon2={version="0.5"}
+lol_alloc="0.4.1"
+
+[features]
+default=["std"]
+std=["tinywasm/std"]
[[bin]]
name="hello"
@@ -26,6 +31,10 @@ name="tinywasm"
path="src/tinywasm.rs"
[[bin]]
+name="tinywasm_no_std"
+path="src/tinywasm_no_std.rs"
+
+[[bin]]
name="fibonacci"
path="src/fibonacci.rs"
diff --git a/examples/rust/build.sh b/examples/rust/build.sh
index ceb1de6..d1e6285 100755
--- a/examples/rust/build.sh
+++ b/examples/rust/build.sh
@@ -12,6 +12,9 @@ wasmopt_features="--enable-reference-types --enable-bulk-memory --enable-mutable
# ensure out dir exists
mkdir -p "$dest_dir"
+# build no_std
+cargo build --target wasm32-unknown-unknown --package rust-wasm-examples --profile=wasm --bin tinywasm_no_std --no-default-features
+
for bin in "${bins[@]}"; do
RUSTFLAGS="-C target-feature=$rust_features -C panic=abort" cargo build --target wasm32-unknown-unknown --package rust-wasm-examples --profile=wasm --bin "$bin"
diff --git a/examples/rust/src/tinywasm.rs b/examples/rust/src/tinywasm.rs
index 08c8135..68e6375 100644
--- a/examples/rust/src/tinywasm.rs
+++ b/examples/rust/src/tinywasm.rs
@@ -12,7 +12,7 @@ pub extern "C" fn hello() {
}
fn run() -> tinywasm::Result<()> {
- let module = tinywasm::Module::parse_bytes(include_bytes!("../out/print.wasm"))?;
+ let module = tinywasm::Module::parse_stream(&include_bytes!("../out/print.wasm")[..])?;
let mut store = tinywasm::Store::default();
let mut imports = tinywasm::Imports::new();
diff --git a/examples/rust/src/tinywasm_no_std.rs b/examples/rust/src/tinywasm_no_std.rs
index 9f2b28c..c20e243 100644
--- a/examples/rust/src/tinywasm_no_std.rs
+++ b/examples/rust/src/tinywasm_no_std.rs
@@ -1,7 +1,19 @@
#![no_main]
#![no_std]
+use lol_alloc::{AssumeSingleThreaded, FreeListAllocator};
use tinywasm::{Extern, FuncContext};
+extern crate alloc;
+
+#[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")]
extern "C" {
fn printi32(x: i32);
@@ -13,10 +25,13 @@ pub extern "C" fn hello() {
}
fn run() -> tinywasm::Result<()> {
- let module = tinywasm::Module::parse_bytes(include_bytes!("../out/print.wasm"))?;
let mut store = tinywasm::Store::default();
let mut imports = tinywasm::Imports::new();
+ let res = tinywasm::parser::Parser::new().parse_module_bytes(include_bytes!("../out/print.wasm"))?;
+ let twasm = res.serialize_twasm();
+ let module = tinywasm::Module::parse_bytes(&twasm)?;
+
imports.define(
"env",
"printi32",
diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs
index 169fed5..a9c7572 100644
--- a/examples/wasm-rust.rs
+++ b/examples/wasm-rust.rs
@@ -43,6 +43,7 @@ fn main() -> Result<()> {
"printi32" => printi32()?,
"fibonacci" => fibonacci()?,
"tinywasm" => tinywasm()?,
+ "tinywasm_no_std" => tinywasm_no_std()?,
"argon2id" => argon2id()?,
"all" => {
println!("Running all examples");
@@ -54,6 +55,8 @@ fn main() -> Result<()> {
fibonacci()?;
println!("\ntinywasm.wasm:");
tinywasm()?;
+ println!("\ntinywasm_no_std.wasm:");
+ tinywasm_no_std()?;
println!("argon2id.wasm:");
argon2id()?;
}
@@ -64,7 +67,22 @@ fn main() -> Result<()> {
}
fn tinywasm() -> Result<()> {
- let module = Module::parse_file("./examples/rust/out/tinywasm.wasm")?;
+ let module = Module::parse_file("./examples/rust/out/tinywasm.opt.wasm")?;
+ let mut store = Store::default();
+
+ let mut imports = Imports::new();
+ imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _x: i32| Ok(())))?;
+ let instance = module.instantiate(&mut store, Some(black_box(imports)))?;
+
+ let hello = instance.exported_func::<(), ()>(&store, "hello")?;
+ hello.call(&mut store, black_box(()))?;
+ hello.call(&mut store, black_box(()))?;
+ hello.call(&mut store, black_box(()))?;
+ Ok(())
+}
+
+fn tinywasm_no_std() -> Result<()> {
+ let module = Module::parse_file("./examples/rust/out/tinywasm_no_std.opt.wasm")?;
let mut store = Store::default();
let mut imports = Imports::new();
@@ -79,7 +97,7 @@ fn tinywasm() -> Result<()> {
}
fn hello() -> Result<()> {
- let module = Module::parse_file("./examples/rust/out/hello.wasm")?;
+ let module = Module::parse_file("./examples/rust/out/hello.opt.wasm")?;
let mut store = Store::default();
let mut imports = Imports::new();
@@ -108,7 +126,7 @@ fn hello() -> Result<()> {
}
fn printi32() -> Result<()> {
- let module = Module::parse_file("./examples/rust/out/print.wasm")?;
+ let module = Module::parse_file("./examples/rust/out/print.opt.wasm")?;
let mut store = Store::default();
let mut imports = Imports::new();
@@ -151,3 +169,38 @@ fn argon2id() -> Result<()> {
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_hello() {
+ hello().unwrap();
+ }
+
+ #[test]
+ fn test_printi32() {
+ printi32().unwrap();
+ }
+
+ #[test]
+ fn test_fibonacci() {
+ fibonacci().unwrap();
+ }
+
+ #[test]
+ fn test_tinywasm() {
+ tinywasm().unwrap();
+ }
+
+ #[test]
+ fn test_tinywasm_no_std() {
+ tinywasm_no_std().unwrap();
+ }
+
+ #[test]
+ fn test_argon2id() {
+ argon2id().unwrap();
+ }
+}