2017-12-13 13:25:24 +01:00
|
|
|
use std::rc::Rc;
|
2017-12-11 16:28:05 +01:00
|
|
|
use std::collections::HashMap;
|
2017-12-14 15:33:40 +01:00
|
|
|
use std::collections::hash_map::Entry;
|
2017-12-11 18:38:09 +01:00
|
|
|
use elements::{FunctionType, ValueType, GlobalType, MemoryType, TableType};
|
2017-12-13 18:31:40 +01:00
|
|
|
use interpreter::module::{ExternVal, ModuleInstance};
|
2017-12-13 18:28:34 +01:00
|
|
|
use interpreter::func::FuncInstance;
|
2017-12-13 18:19:42 +01:00
|
|
|
use interpreter::global::GlobalInstance;
|
2017-12-13 14:36:06 +01:00
|
|
|
use interpreter::memory::MemoryInstance;
|
|
|
|
use interpreter::table::TableInstance;
|
2017-12-11 16:28:05 +01:00
|
|
|
use interpreter::value::RuntimeValue;
|
|
|
|
use interpreter::Error;
|
2017-12-14 15:33:40 +01:00
|
|
|
use interpreter::ImportResolver;
|
2017-12-11 19:22:45 +01:00
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
pub type HostFunc<St> = Fn(&mut St, &[RuntimeValue]) -> Result<Option<RuntimeValue>, Error>;
|
|
|
|
|
2017-12-11 19:22:45 +01:00
|
|
|
pub struct HostModuleBuilder<St> {
|
2017-12-15 18:23:54 +03:00
|
|
|
exports: HashMap<String, ExternVal<St>>,
|
2017-12-11 16:28:05 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
impl<St> HostModuleBuilder<St> {
|
2017-12-11 19:22:45 +01:00
|
|
|
pub fn new() -> Self {
|
2017-12-11 16:28:05 +01:00
|
|
|
HostModuleBuilder {
|
2017-12-14 15:33:40 +01:00
|
|
|
exports: HashMap::new(),
|
2017-12-11 16:28:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
// pub fn insert_func0<
|
|
|
|
// Cl: Fn(&mut St) -> Result<Option<Ret>, Error> + AnyFunc<St> + 'static,
|
|
|
|
// Ret: AsReturnVal + 'static,
|
|
|
|
// F: Into<Cl>,
|
|
|
|
// N: Into<String>,
|
|
|
|
// >(
|
|
|
|
// &mut self,
|
|
|
|
// name: N,
|
|
|
|
// f: Box<F>,
|
|
|
|
// ) {
|
|
|
|
// let func_type = FunctionType::new(vec![], Ret::value_type());
|
|
|
|
// let host_func = Rc::new(f) as Rc<AnyFunc<St>>;
|
|
|
|
// let func = FuncInstance::alloc_host(Rc::new(func_type), host_func);
|
|
|
|
// self.insert_func(name, func);
|
|
|
|
// }
|
2017-12-11 16:28:05 +01:00
|
|
|
|
2017-12-12 10:32:51 +01:00
|
|
|
pub fn with_func2<
|
2017-12-13 15:00:54 +01:00
|
|
|
Cl: Fn(&mut St, P1, P2) -> Result<Option<Ret>, Error> + 'static,
|
2017-12-12 10:32:51 +01:00
|
|
|
Ret: AsReturnVal + 'static,
|
|
|
|
P1: FromArg + 'static,
|
|
|
|
P2: FromArg + 'static,
|
2017-12-12 15:18:35 +01:00
|
|
|
N: Into<String>,
|
2017-12-12 10:32:51 +01:00
|
|
|
>(
|
|
|
|
&mut self,
|
2017-12-12 15:18:35 +01:00
|
|
|
name: N,
|
2017-12-15 18:23:54 +03:00
|
|
|
f: Cl,
|
2017-12-12 10:32:51 +01:00
|
|
|
) {
|
2017-12-15 18:23:54 +03:00
|
|
|
let func_type = FunctionType::new(vec![P1::value_type(), P2::value_type()], Ret::value_type());
|
|
|
|
let host_func = Rc::new(move |state: &mut St, args: &[RuntimeValue]| -> Result<Option<RuntimeValue>, Error> {
|
|
|
|
let p1 = P1::from_arg(&args[0]);
|
|
|
|
let p2 = P2::from_arg(&args[1]);
|
|
|
|
let result = f(state, p1, p2);
|
|
|
|
result.map(|r| r.and_then(|r| r.as_return_val()))
|
|
|
|
});
|
|
|
|
|
2017-12-14 15:33:40 +01:00
|
|
|
let func = FuncInstance::alloc_host(Rc::new(func_type), host_func);
|
|
|
|
self.insert_func(name, func);
|
|
|
|
}
|
2017-12-12 10:32:51 +01:00
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
pub fn insert_func<N: Into<String>>(&mut self, name: N, func: Rc<FuncInstance<St>>) {
|
2017-12-14 15:33:40 +01:00
|
|
|
self.insert(name, ExternVal::Func(func));
|
2017-12-12 10:32:51 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 15:33:40 +01:00
|
|
|
pub fn insert_global<N: Into<String>>(&mut self, name: N, global: Rc<GlobalInstance>) {
|
|
|
|
self.insert(name, ExternVal::Global(global));
|
2017-12-11 18:38:09 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 15:33:40 +01:00
|
|
|
pub fn insert_memory<N: Into<String>>(&mut self, name: N, memory: Rc<MemoryInstance>) {
|
|
|
|
self.insert(name, ExternVal::Memory(memory));
|
2017-12-11 18:38:09 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
pub fn insert_table<N: Into<String>>(&mut self, name: N, table: Rc<TableInstance<St>>) {
|
2017-12-14 15:33:40 +01:00
|
|
|
self.insert(name, ExternVal::Table(table));
|
2017-12-11 19:22:45 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 15:33:40 +01:00
|
|
|
pub fn with_global<N: Into<String>>(mut self, name: N, global: Rc<GlobalInstance>) -> Self {
|
|
|
|
self.insert_global(name, global);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_memory<N: Into<String>>(mut self, name: N, memory: Rc<MemoryInstance>) -> Self {
|
|
|
|
self.insert_memory(name, memory);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
pub fn with_table<N: Into<String>>(mut self, name: N, table: Rc<TableInstance<St>>) -> Self {
|
2017-12-14 15:33:40 +01:00
|
|
|
self.insert_table(name, table);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
fn insert<N: Into<String>>(&mut self, name: N, extern_val: ExternVal<St>) {
|
2017-12-14 15:33:40 +01:00
|
|
|
match self.exports.entry(name.into()) {
|
|
|
|
Entry::Vacant(v) => v.insert(extern_val),
|
|
|
|
Entry::Occupied(o) => panic!("Duplicate export name {}", o.key()),
|
|
|
|
};
|
2017-12-12 15:18:35 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
pub fn build(self) -> HostModule<St> {
|
2017-12-14 15:33:40 +01:00
|
|
|
let internal_instance = Rc::new(ModuleInstance::with_exports(self.exports));
|
2017-12-11 19:22:45 +01:00
|
|
|
HostModule {
|
2017-12-14 15:33:40 +01:00
|
|
|
internal_instance
|
2017-12-11 19:22:45 +01:00
|
|
|
}
|
2017-12-11 18:38:09 +01:00
|
|
|
}
|
2017-12-11 19:22:45 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
pub struct HostModule<St> {
|
|
|
|
internal_instance: Rc<ModuleInstance<St>>,
|
2017-12-11 19:22:45 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
impl<St> HostModule<St> {
|
|
|
|
pub fn export_by_name(&self, name: &str) -> Option<ExternVal<St>> {
|
2017-12-14 15:33:40 +01:00
|
|
|
self.internal_instance.export_by_name(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
impl<St> ImportResolver<St> for HostModule<St> {
|
2017-12-14 15:33:40 +01:00
|
|
|
fn resolve_func(
|
|
|
|
&self,
|
|
|
|
field_name: &str,
|
|
|
|
func_type: &FunctionType,
|
2017-12-15 18:23:54 +03:00
|
|
|
) -> Result<Rc<FuncInstance<St>>, Error> {
|
2017-12-14 15:33:40 +01:00
|
|
|
self.internal_instance.resolve_func(field_name, func_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_global(
|
|
|
|
&self,
|
|
|
|
field_name: &str,
|
|
|
|
global_type: &GlobalType,
|
|
|
|
) -> Result<Rc<GlobalInstance>, Error> {
|
|
|
|
self.internal_instance.resolve_global(field_name, global_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_memory(
|
|
|
|
&self,
|
|
|
|
field_name: &str,
|
|
|
|
memory_type: &MemoryType,
|
|
|
|
) -> Result<Rc<MemoryInstance>, Error> {
|
|
|
|
self.internal_instance.resolve_memory(field_name, memory_type)
|
|
|
|
}
|
2017-12-11 19:22:45 +01:00
|
|
|
|
2017-12-14 15:33:40 +01:00
|
|
|
fn resolve_table(
|
|
|
|
&self,
|
|
|
|
field_name: &str,
|
|
|
|
table_type: &TableType,
|
2017-12-15 18:23:54 +03:00
|
|
|
) -> Result<Rc<TableInstance<St>>, Error> {
|
2017-12-14 15:33:40 +01:00
|
|
|
self.internal_instance.resolve_table(field_name, table_type)
|
2017-12-11 16:28:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
pub trait AnyFunc<St> {
|
2017-12-11 16:28:05 +01:00
|
|
|
fn call_as_any(
|
|
|
|
&self,
|
2017-12-15 18:23:54 +03:00
|
|
|
state: &mut St,
|
2017-12-11 16:28:05 +01:00
|
|
|
args: &[RuntimeValue],
|
|
|
|
) -> Result<Option<RuntimeValue>, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait FromArg {
|
|
|
|
fn from_arg(arg: &RuntimeValue) -> Self;
|
|
|
|
fn value_type() -> ValueType;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromArg for i32 {
|
|
|
|
fn from_arg(arg: &RuntimeValue) -> Self {
|
|
|
|
match arg {
|
|
|
|
&RuntimeValue::I32(v) => v,
|
2017-12-11 18:39:25 +01:00
|
|
|
unexpected => panic!("Expected I32, got {:?}", unexpected),
|
2017-12-11 16:28:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn value_type() -> ValueType {
|
|
|
|
ValueType::I32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 19:22:45 +01:00
|
|
|
pub trait AsReturnVal {
|
2017-12-11 16:28:05 +01:00
|
|
|
fn as_return_val(self) -> Option<RuntimeValue>;
|
|
|
|
fn value_type() -> Option<ValueType>;
|
|
|
|
}
|
|
|
|
|
2017-12-11 19:22:45 +01:00
|
|
|
impl AsReturnVal for i32 {
|
2017-12-11 16:28:05 +01:00
|
|
|
fn as_return_val(self) -> Option<RuntimeValue> {
|
|
|
|
Some(self.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn value_type() -> Option<ValueType> {
|
|
|
|
Some(ValueType::I32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 19:22:45 +01:00
|
|
|
impl AsReturnVal for () {
|
2017-12-11 16:28:05 +01:00
|
|
|
fn as_return_val(self) -> Option<RuntimeValue> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn value_type() -> Option<ValueType> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
impl<St, Ret: AsReturnVal> AnyFunc<St> for Fn(&mut St) -> Result<Option<Ret>, Error> {
|
2017-12-12 10:32:51 +01:00
|
|
|
fn call_as_any(
|
|
|
|
&self,
|
2017-12-15 18:23:54 +03:00
|
|
|
state: &mut St,
|
2017-12-12 15:18:35 +01:00
|
|
|
_args: &[RuntimeValue],
|
2017-12-12 10:32:51 +01:00
|
|
|
) -> Result<Option<RuntimeValue>, Error> {
|
2017-12-15 18:23:54 +03:00
|
|
|
let result = self(state);
|
2017-12-11 16:28:05 +01:00
|
|
|
result.map(|r| r.and_then(|r| r.as_return_val()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:54 +03:00
|
|
|
impl<St, Ret: AsReturnVal, P1: FromArg, P2: FromArg> AnyFunc<St> for Fn(&mut St, P1, P2) -> Result<Option<Ret>, Error> {
|
2017-12-12 10:32:51 +01:00
|
|
|
fn call_as_any(
|
|
|
|
&self,
|
2017-12-15 18:23:54 +03:00
|
|
|
state: &mut St,
|
2017-12-12 10:32:51 +01:00
|
|
|
args: &[RuntimeValue],
|
|
|
|
) -> Result<Option<RuntimeValue>, Error> {
|
|
|
|
let p1 = P1::from_arg(&args[0]);
|
|
|
|
let p2 = P2::from_arg(&args[1]);
|
2017-12-15 18:23:54 +03:00
|
|
|
let result = self(state, p1, p2);
|
2017-12-12 10:32:51 +01:00
|
|
|
result.map(|r| r.and_then(|r| r.as_return_val()))
|
|
|
|
}
|
|
|
|
}
|