use std::error::Error;
use wasmer::{Store, Module, Instance, imports, Function};
use wasmer_as::{Read, Write, StringPtr, Env, abort};
fn main() -> Result<(), Box<dyn Error>> {
let wasm_bytes = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test-wasm/build/optimized.wasm"
));
let store = Store::default();
let module = Module::new(&store, wasm_bytes)?;
let memory = Memory::new(&store, MemoryType::new(1, None, false)).unwrap();
let import_object = imports! {
"env" => {
"abort" => Function::new_native_with_env(&store, memory, abort),
},
};
let instance = Instance::new(&module, &import_object)?;
// for the test we use simple function returning constant string:
//
// export function getString(): string {
// return "TheString";
// }
let get_string = instance.exports.get_function("getString")?;
let results = get_string.call(&[])?;
let str_ptr = results.first().expect("get pointer");
let str_ptr = StringPtr::new(str_ptr.unwrap_i32() as u32);
let memory = instance.exports.get_memory("memory").expect("get memory");
let string = str_ptr.read(memory)?;
assert_eq!(string, "$¢ह한𝌆");
let str_ptr_2 = StringPtr::alloc("hello return", &env)?;
let string = str_ptr_2.read(memory)?;
assert_eq!(string, "hello return");
Ok(())
}
forked from onsails/wasmer-as
-
Notifications
You must be signed in to change notification settings - Fork 0
Helpers for dealing with assemblyscript memory inside wasmer-runtime
License
adrien-zinger/wasmer-as
This branch is 13 commits ahead of onsails/wasmer-as:master.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Latest commitff4f5c8 · | ||||
Repository files navigation
About
Helpers for dealing with assemblyscript memory inside wasmer-runtime
Resources
License
Stars
Watchers
Forks
Packages 0
No packages published
Languages
- WebAssembly 79.4%
- Rust 18.5%
- Nix 2.1%