2019-01-08 17:09:47 +00:00
|
|
|
use crate::recovery::call_protected;
|
|
|
|
use crate::{
|
|
|
|
backing::{ImportBacking, LocalBacking},
|
|
|
|
memory::LinearMemory,
|
|
|
|
module::{Export, Module},
|
|
|
|
table::TableBacking,
|
2019-01-10 04:43:18 +00:00
|
|
|
types::{FuncIndex, FuncSig, Memory, Table, Type, Value, MapIndex},
|
2019-01-08 17:09:47 +00:00
|
|
|
vm,
|
|
|
|
};
|
|
|
|
use hashbrown::HashMap;
|
|
|
|
use libffi::high::{arg as libffi_arg, call as libffi_call, CodePtr};
|
|
|
|
use std::iter;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub struct Instance {
|
|
|
|
pub(crate) backing: LocalBacking,
|
|
|
|
import_backing: ImportBacking,
|
|
|
|
pub module: Module,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Instance {
|
2019-01-09 02:57:28 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
module: Module,
|
|
|
|
imports: &dyn ImportResolver,
|
|
|
|
) -> Result<Box<Instance>, String> {
|
2019-01-08 17:09:47 +00:00
|
|
|
let import_backing = ImportBacking::new(&module, imports)?;
|
|
|
|
let backing = LocalBacking::new(&module, &import_backing);
|
|
|
|
|
|
|
|
let start_func = module.start_func;
|
|
|
|
|
|
|
|
let mut instance = Box::new(Instance {
|
|
|
|
backing,
|
|
|
|
import_backing,
|
|
|
|
module,
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(start_index) = start_func {
|
|
|
|
instance.call_with_index(start_index, &[])?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Call an exported webassembly function given the export name.
|
2019-01-09 23:31:11 +00:00
|
|
|
/// Pass arguments by wrapping each one in the `Value` enum.
|
|
|
|
/// The returned value is also returned in a `Value`.
|
2019-01-08 17:09:47 +00:00
|
|
|
///
|
2019-01-09 23:31:11 +00:00
|
|
|
/// This will eventually return `Result<Option<Vec<Value>>, String>` in
|
2019-01-08 17:09:47 +00:00
|
|
|
/// order to support multi-value returns.
|
|
|
|
pub fn call(&mut self, name: &str, args: &[Value]) -> Result<Option<Value>, String> {
|
|
|
|
let func_index = *self
|
|
|
|
.module
|
|
|
|
.exports
|
|
|
|
.get(name)
|
|
|
|
.ok_or_else(|| "there is no export with that name".to_string())
|
|
|
|
.and_then(|export| match export {
|
|
|
|
Export::Func(func_index) => Ok(func_index),
|
|
|
|
_ => Err("that export is not a function".to_string()),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
self.call_with_index(func_index, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_with_index(
|
|
|
|
&mut self,
|
|
|
|
func_index: FuncIndex,
|
|
|
|
args: &[Value],
|
|
|
|
) -> Result<Option<Value>, String> {
|
|
|
|
// Check the function signature.
|
|
|
|
let sig_index = *self
|
|
|
|
.module
|
|
|
|
.func_assoc
|
|
|
|
.get(func_index)
|
|
|
|
.expect("broken invariant, incorrect func index");
|
2019-01-10 04:43:18 +00:00
|
|
|
|
2019-01-08 17:09:47 +00:00
|
|
|
{
|
|
|
|
let signature = self.module.sig_registry.lookup_func_sig(sig_index);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
signature.returns.len() <= 1,
|
|
|
|
"multi-value returns not yet supported"
|
|
|
|
);
|
|
|
|
|
|
|
|
if !signature.check_sig(args) {
|
|
|
|
return Err("incorrect signature".to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the vmctx will be located at the same place on the stack the entire time that this
|
|
|
|
// wasm function is running.
|
2019-01-09 02:57:28 +00:00
|
|
|
let mut vmctx = vm::Ctx::new(&mut self.backing, &mut self.import_backing);
|
2019-01-08 17:09:47 +00:00
|
|
|
let vmctx_ptr = &mut vmctx as *mut vm::Ctx;
|
|
|
|
|
|
|
|
let libffi_args: Vec<_> = args
|
|
|
|
.iter()
|
|
|
|
.map(|val| match val {
|
|
|
|
Value::I32(ref x) => libffi_arg(x),
|
|
|
|
Value::I64(ref x) => libffi_arg(x),
|
|
|
|
Value::F32(ref x) => libffi_arg(x),
|
|
|
|
Value::F64(ref x) => libffi_arg(x),
|
|
|
|
})
|
|
|
|
.chain(iter::once(libffi_arg(&vmctx_ptr)))
|
|
|
|
.collect();
|
|
|
|
|
2019-01-10 04:43:18 +00:00
|
|
|
let func_ptr = CodePtr::from_ptr(if self.module.is_imported_function(func_index) {
|
|
|
|
let imported_func = &self.import_backing.functions[func_index.index()];
|
|
|
|
imported_func.func as *const _
|
|
|
|
} else {
|
2019-01-09 02:57:28 +00:00
|
|
|
self.module
|
2019-01-08 17:09:47 +00:00
|
|
|
.func_resolver
|
|
|
|
.get(&self.module, func_index)
|
|
|
|
.expect("broken invariant, func resolver not synced with module.exports")
|
|
|
|
.cast()
|
2019-01-10 04:43:18 +00:00
|
|
|
.as_ptr()
|
|
|
|
});
|
2019-01-08 17:09:47 +00:00
|
|
|
|
|
|
|
call_protected(|| {
|
2019-01-09 02:57:28 +00:00
|
|
|
self.module
|
|
|
|
.sig_registry
|
|
|
|
.lookup_func_sig(sig_index)
|
2019-01-08 17:09:47 +00:00
|
|
|
.returns
|
|
|
|
.first()
|
|
|
|
.map(|ty| match ty {
|
|
|
|
Type::I32 => Value::I32(unsafe { libffi_call(func_ptr, &libffi_args) }),
|
|
|
|
Type::I64 => Value::I64(unsafe { libffi_call(func_ptr, &libffi_args) }),
|
|
|
|
Type::F32 => Value::F32(unsafe { libffi_call(func_ptr, &libffi_args) }),
|
|
|
|
Type::F64 => Value::F64(unsafe { libffi_call(func_ptr, &libffi_args) }),
|
|
|
|
})
|
|
|
|
.or_else(|| {
|
|
|
|
// call with no returns
|
|
|
|
unsafe {
|
|
|
|
libffi_call::<()>(func_ptr, &libffi_args);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 03:09:09 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FuncRef(*const vm::Func);
|
|
|
|
|
|
|
|
impl FuncRef {
|
|
|
|
/// This needs to be unsafe because there is
|
|
|
|
/// no way to check whether the passed function
|
|
|
|
/// is valid and has the right signature.
|
|
|
|
pub unsafe fn new(f: *const vm::Func) -> Self {
|
|
|
|
FuncRef(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn inner(&self) -> *const vm::Func {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:09:47 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Import {
|
2019-01-09 03:09:09 +00:00
|
|
|
Func(FuncRef, FuncSig),
|
2019-01-08 17:09:47 +00:00
|
|
|
Table(Arc<TableBacking>, Table),
|
|
|
|
Memory(Arc<LinearMemory>, Memory),
|
|
|
|
Global(Value),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Imports {
|
|
|
|
map: HashMap<String, HashMap<String, Import>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Imports {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
map: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 03:23:07 +00:00
|
|
|
pub fn add(&mut self, module: impl Into<String>, name: impl Into<String>, import: Import) {
|
2019-01-08 17:09:47 +00:00
|
|
|
self.map
|
2019-01-09 03:23:07 +00:00
|
|
|
.entry(module.into())
|
|
|
|
.or_insert_with(|| HashMap::new())
|
|
|
|
.insert(name.into(), import);
|
2019-01-08 17:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, module: &str, name: &str) -> Option<&Import> {
|
|
|
|
self.map.get(module).and_then(|m| m.get(name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImportResolver for Imports {
|
|
|
|
fn get(&self, module: &str, name: &str) -> Option<&Import> {
|
|
|
|
self.get(module, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ImportResolver {
|
|
|
|
fn get(&self, module: &str, name: &str) -> Option<&Import>;
|
|
|
|
}
|
2019-01-10 01:45:48 +00:00
|
|
|
|
|
|
|
// TODO Remove this later, only needed for compilation till emscripten is updated
|
|
|
|
impl Instance {
|
2019-01-10 04:43:18 +00:00
|
|
|
pub fn memory_offset_addr(&self, _index: usize, _offset: usize) -> *const usize {
|
2019-01-10 01:45:48 +00:00
|
|
|
unimplemented!("TODO replace this emscripten stub")
|
|
|
|
}
|
|
|
|
}
|