-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
- Loading branch information
1 parent
461126c
commit f3d890a
Showing
18 changed files
with
159 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/target | ||
notes.md | ||
examples/tinywasm.wat | ||
examples/rust/out/* | ||
examples/wast/* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Examples | ||
|
||
## WasmRust | ||
|
||
These are examples using WebAssembly generated from Rust code. | ||
To run these, you first need to build the Rust code into WebAssembly, since the wasm files are not included in the repository to keep it small. | ||
This requires the `wasm32-unknown-unknown` target and `wasm-opt` to be installed (available via Binaryen). | ||
|
||
```bash | ||
$ ./examples/rust/build.sh | ||
``` | ||
|
||
Then you can run the examples: | ||
|
||
```bash | ||
$ cargo run --example wasm-rust <example> | ||
``` | ||
|
||
Where `<example>` is one of the following: | ||
|
||
- `hello`: A simple example that prints a number to the console. | ||
- `tinywasm`: Runs `hello` using TinyWasm - inside of TinyWasm itself! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 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))?; | ||
let module = tinywasm::Module::parse_bytes(include_bytes!("../../wasm/hello.wasm"))?; | ||
let mut store = tinywasm::Store::default(); | ||
let mut imports = tinywasm::Imports::new(); | ||
|
||
imports.define( | ||
"env", | ||
"printi32", | ||
Extern::typed_func(|_: FuncContext<'_>, v: i32| { | ||
unsafe { printi32(v) } | ||
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.