Initial reorg.

This is largely the same as #305, but updated for the current tree.
This commit is contained in:
Dan Gohman
2019-11-07 17:11:06 -08:00
parent 2c69546a24
commit 22641de629
351 changed files with 52 additions and 52 deletions

View File

@@ -0,0 +1,43 @@
//! Example of instantiating of the WebAssembly module and
//! invoking its exported function.
use anyhow::{format_err, Result};
use std::fs::read;
use wasmtime_api::*;
fn main() -> Result<()> {
let wasm = read("examples/gcd.wasm")?;
// Instantiate engine and store.
let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(&engine));
// Load a module.
let module = HostRef::new(Module::new(&store, &wasm)?);
// Find index of the `gcd` export.
let gcd_index = module
.borrow()
.exports()
.iter()
.enumerate()
.find(|(_, export)| export.name().to_string() == "gcd")
.unwrap()
.0;
// Instantiate the module.
let instance = HostRef::new(Instance::new(&store, &module, &[])?);
// Invoke `gcd` export
let gcd = instance.borrow().exports()[gcd_index]
.func()
.expect("gcd")
.clone();
let result = gcd
.borrow()
.call(&[Val::from(6i32), Val::from(27i32)])
.map_err(|e| format_err!("call error: {:?}", e))?;
println!("{:?}", result);
Ok(())
}

Binary file not shown.

View File

@@ -0,0 +1,68 @@
//! Translation of hello example
extern crate alloc;
use alloc::rc::Rc;
use anyhow::{ensure, format_err, Context as _, Result};
use core::cell::Ref;
use std::fs::read;
use wasmtime_api::*;
struct HelloCallback;
impl Callable for HelloCallback {
fn call(&self, _params: &[Val], _results: &mut [Val]) -> Result<(), HostRef<Trap>> {
println!("Calling back...");
println!("> Hello World!");
Ok(())
}
}
fn main() -> Result<()> {
// Initialize.
println!("Initializing...");
let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(&engine));
// Load binary.
println!("Loading binary...");
let binary = read("examples/hello.wasm")?;
// Compile.
println!("Compiling module...");
let module = HostRef::new(Module::new(&store, &binary).context("> Error compiling module!")?);
// Create external print functions.
println!("Creating callback...");
let hello_type = FuncType::new(Box::new([]), Box::new([]));
let hello_func = HostRef::new(Func::new(&store, hello_type, Rc::new(HelloCallback)));
// Instantiate.
println!("Instantiating module...");
let imports = vec![hello_func.into()];
let instance = HostRef::new(
Instance::new(&store, &module, imports.as_slice())
.context("> Error instantiating module!")?,
);
// Extract export.
println!("Extracting export...");
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
ensure!(!exports.is_empty(), "> Error accessing exports!");
let run_func = exports[0].func().context("> Error accessing exports!")?;
// Call.
println!("Calling export...");
run_func
.borrow()
.call(&[])
.map_err(|e| format_err!("> Error calling function: {:?}", e))?;
// Shut down.
println!("Shutting down...");
drop(store);
// All done.
println!("Done.");
Ok(())
}

Binary file not shown.

View File

@@ -0,0 +1,4 @@
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)

View File

@@ -0,0 +1,153 @@
//! Translation of the memory example
use anyhow::{bail, ensure, Context as _, Error};
use core::cell::Ref;
use std::fs::read;
use wasmtime_api::*;
fn get_export_memory(exports: &[Extern], i: usize) -> Result<HostRef<Memory>, Error> {
if exports.len() <= i {
bail!("> Error accessing memory export {}!", i);
}
Ok(exports[i]
.memory()
.with_context(|| format!("> Error accessing memory export {}!", i))?
.clone())
}
fn get_export_func(exports: &[Extern], i: usize) -> Result<HostRef<Func>, Error> {
if exports.len() <= i {
bail!("> Error accessing function export {}!", i);
}
Ok(exports[i]
.func()
.with_context(|| format!("> Error accessing function export {}!", i))?
.clone())
}
macro_rules! check {
($actual:expr, $expected:expr) => {
if $actual != $expected {
bail!("> Error on result, expected {}, got {}", $expected, $actual);
}
};
}
macro_rules! check_ok {
($func:expr, $($p:expr),*) => {
if let Err(_) = $func.borrow().call(&[$($p.into()),*]) {
bail!("> Error on result, expected return");
}
}
}
macro_rules! check_trap {
($func:expr, $($p:expr),*) => {
if let Ok(_) = $func.borrow().call(&[$($p.into()),*]) {
bail!("> Error on result, expected trap");
}
}
}
macro_rules! call {
($func:expr, $($p:expr),*) => {
match $func.borrow().call(&[$($p.into()),*]) {
Ok(result) => {
let result: i32 = result[0].clone().into();
result
}
Err(_) => { bail!("> Error on result, expected return"); }
}
}
}
fn main() -> Result<(), Error> {
// Initialize.
println!("Initializing...");
let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(&engine));
// Load binary.
println!("Loading binary...");
let binary = read("examples/memory.wasm")?;
// Compile.
println!("Compiling module...");
let module = HostRef::new(Module::new(&store, &binary).context("> Error compiling module!")?);
// Instantiate.
println!("Instantiating module...");
let instance =
HostRef::new(Instance::new(&store, &module, &[]).context("> Error instantiating module!")?);
// Extract export.
println!("Extracting export...");
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
ensure!(!exports.is_empty(), "> Error accessing exports!");
let memory = get_export_memory(&exports, 0)?;
let size_func = get_export_func(&exports, 1)?;
let load_func = get_export_func(&exports, 2)?;
let store_func = get_export_func(&exports, 3)?;
// Try cloning.
check!(memory.clone().ptr_eq(&memory), true);
// Check initial memory.
println!("Checking memory...");
check!(memory.borrow().size(), 2u32);
check!(memory.borrow().data_size(), 0x20000usize);
check!(unsafe { memory.borrow().data()[0] }, 0);
check!(unsafe { memory.borrow().data()[0x1000] }, 1);
check!(unsafe { memory.borrow().data()[0x1003] }, 4);
check!(call!(size_func,), 2);
check!(call!(load_func, 0), 0);
check!(call!(load_func, 0x1000), 1);
check!(call!(load_func, 0x1003), 4);
check!(call!(load_func, 0x1ffff), 0);
check_trap!(load_func, 0x20000);
// Mutate memory.
println!("Mutating memory...");
unsafe {
memory.borrow_mut().data()[0x1003] = 5;
}
check_ok!(store_func, 0x1002, 6);
check_trap!(store_func, 0x20000, 0);
check!(unsafe { memory.borrow().data()[0x1002] }, 6);
check!(unsafe { memory.borrow().data()[0x1003] }, 5);
check!(call!(load_func, 0x1002), 6);
check!(call!(load_func, 0x1003), 5);
// Grow memory.
println!("Growing memory...");
check!(memory.borrow_mut().grow(1), true);
check!(memory.borrow().size(), 3u32);
check!(memory.borrow().data_size(), 0x30000usize);
check!(call!(load_func, 0x20000), 0);
check_ok!(store_func, 0x20000, 0);
check_trap!(load_func, 0x30000);
check_trap!(store_func, 0x30000, 0);
check!(memory.borrow_mut().grow(1), false);
check!(memory.borrow_mut().grow(0), true);
// Create stand-alone memory.
// TODO(wasm+): Once Wasm allows multiple memories, turn this into import.
println!("Creating stand-alone memory...");
let memorytype = MemoryType::new(Limits::new(5, 5));
let mut memory2 = Memory::new(&store, memorytype);
check!(memory2.size(), 5u32);
check!(memory2.grow(1), false);
check!(memory2.grow(0), true);
// Shut down.
println!("Shutting down...");
drop(store);
println!("Done.");
Ok(())
}

Binary file not shown.

View File

@@ -0,0 +1,11 @@
(module
(memory (export "memory") 2 3)
(func (export "size") (result i32) (memory.size))
(func (export "load") (param i32) (result i32) (i32.load8_s (local.get 0)))
(func (export "store") (param i32 i32)
(i32.store8 (local.get 0) (local.get 1))
)
(data (i32.const 0x1000) "\01\02\03\04")
)

View File

@@ -0,0 +1,81 @@
//! Translation of multi example
extern crate alloc;
use alloc::rc::Rc;
use anyhow::{ensure, format_err, Context as _, Result};
use core::cell::Ref;
use std::fs::read;
use wasmtime_api::*;
struct Callback;
impl Callable for Callback {
fn call(&self, args: &[Val], results: &mut [Val]) -> Result<(), HostRef<Trap>> {
println!("Calling back...");
println!("> {} {}", args[0].i32(), args[1].i64());
results[0] = Val::I64(args[1].i64() + 1);
results[1] = Val::I32(args[0].i32() + 1);
Ok(())
}
}
fn main() -> Result<()> {
// Initialize.
println!("Initializing...");
let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(&engine));
// Load binary.
println!("Loading binary...");
let binary = read("examples/multi.wasm")?;
// Compile.
println!("Compiling module...");
let module = HostRef::new(Module::new(&store, &binary).context("Error compiling module!")?);
// Create external print functions.
println!("Creating callback...");
let callback_type = FuncType::new(
Box::new([ValType::I32, ValType::I64]),
Box::new([ValType::I64, ValType::I32]),
);
let callback_func = HostRef::new(Func::new(&store, callback_type, Rc::new(Callback)));
// Instantiate.
println!("Instantiating module...");
let imports = vec![callback_func.into()];
let instance = HostRef::new(
Instance::new(&store, &module, imports.as_slice())
.context("Error instantiating module!")?,
);
// Extract export.
println!("Extracting export...");
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
ensure!(!exports.is_empty(), "Error accessing exports!");
let run_func = exports[0].func().context("Error accessing exports!")?;
// Call.
println!("Calling export...");
let args = vec![Val::I32(1), Val::I64(3)];
let results = run_func
.borrow()
.call(&args)
.map_err(|e| format_err!("> Error calling function: {:?}", e))?;
println!("Printing result...");
println!("> {} {}", results[0].i64(), results[1].i32());
debug_assert!(results[0].i64() == 4);
debug_assert!(results[1].i32() == 2);
// Shut down.
println!("Shutting down...");
drop(store);
// All done.
println!("Done.");
Ok(())
}

Binary file not shown.

View File

@@ -0,0 +1,7 @@
(module
(func $f (import "" "f") (param i32 i64) (result i64 i32))
(func $g (export "g") (param i32 i64) (result i64 i32)
(call $f (local.get 0) (local.get 1))
)
)