Skip to content

Instantly share code, notes, and snippets.

@hotate29
Created May 17, 2022 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotate29/fb9eedc2502e18fb118fc3ecfe06a8f7 to your computer and use it in GitHub Desktop.
Save hotate29/fb9eedc2502e18fb118fc3ecfe06a8f7 to your computer and use it in GitHub Desktop.
HelloWorld in WebAssembly
(module
(import "host" "print_char" (func $print_char (param i32)))
(func (export "main")
i32.const 72
call $print_char
i32.const 101
call $print_char
i32.const 108
call $print_char
i32.const 108
call $print_char
i32.const 111
call $print_char
i32.const 32
call $print_char
i32.const 87
call $print_char
i32.const 111
call $print_char
i32.const 114
call $print_char
i32.const 108
call $print_char
i32.const 100
call $print_char
i32.const 33
call $print_char
i32.const 10
call $print_char
)
)
use std::fs;
use wasmtime::{Engine, Func, Instance, Module, Store};
fn main() {
let wat = fs::read_to_string("a.wat").unwrap();
let engine = Engine::default();
let module = Module::new(&engine, &wat).unwrap();
let mut store = Store::new(&engine, 4);
let print_char = Func::wrap(&mut store, |x: i32| print!("{}", x as u8 as char));
let instance = Instance::new(&mut store, &module, &[print_char.into()]).unwrap();
let wasm_main = instance
.get_typed_func::<(), (), _>(&mut store, "main")
.unwrap();
wasm_main.call(&mut store, ()).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment