Table operation; refactor Callable

This commit is contained in:
Yury Delendik
2019-08-27 11:44:02 -05:00
committed by Dan Gohman
parent e60bf7f7e8
commit de1c0f63eb
17 changed files with 641 additions and 114 deletions

View File

@@ -1,44 +1,61 @@
use crate::runtime::Store;
use crate::trap::Trap;
use crate::types::FuncType;
use crate::values::Val;
use core::any::Any;
use std::cell::RefCell;
use std::rc::Rc;
use crate::trampoline::generate_func_export;
use cranelift_codegen::ir;
use wasmtime_runtime::{VMContext, VMFunctionBody};
use wasmtime_jit::InstanceHandle;
use wasmtime_runtime::Export;
pub trait Callable: Any {
pub trait Callable {
fn call(&self, params: &[Val], results: &mut [Val]) -> Result<(), Rc<RefCell<Trap>>>;
}
pub(crate) trait WrappedCallable {
fn call(&self, params: &[Val], results: &mut [Val]) -> Result<(), Rc<RefCell<Trap>>>;
fn signature(&self) -> &ir::Signature {
match self.wasmtime_export() {
Export::Function { signature, .. } => signature,
_ => panic!("unexpected export type in Callable"),
}
}
fn wasmtime_handle(&self) -> &InstanceHandle;
fn wasmtime_export(&self) -> &Export;
}
pub(crate) struct WasmtimeFn {
store: Rc<RefCell<Store>>,
signature: ir::Signature,
body: *const VMFunctionBody,
vmctx: *mut VMContext,
instance: InstanceHandle,
export: Export,
}
impl WasmtimeFn {
pub fn new(
store: Rc<RefCell<Store>>,
signature: ir::Signature,
body: *const VMFunctionBody,
vmctx: *mut VMContext,
) -> WasmtimeFn {
pub fn new(store: Rc<RefCell<Store>>, instance: InstanceHandle, export: Export) -> WasmtimeFn {
WasmtimeFn {
store,
signature,
body,
vmctx,
instance,
export,
}
}
}
impl Callable for WasmtimeFn {
impl WrappedCallable for WasmtimeFn {
fn call(&self, params: &[Val], results: &mut [Val]) -> Result<(), Rc<RefCell<Trap>>> {
use core::cmp::max;
use core::{mem, ptr};
let (vmctx, body, signature) = match self.wasmtime_export() {
Export::Function {
vmctx,
address,
signature,
} => (*vmctx, *address, signature.clone()),
_ => panic!("unexpected export type in Callable"),
};
let mut store = self.store.borrow_mut();
let context = store.context();
@@ -63,13 +80,13 @@ impl Callable for WasmtimeFn {
// Get the trampoline to call for this function.
let exec_code_buf = context
.compiler()
.get_published_trampoline(self.body, &self.signature, value_size)
.get_published_trampoline(body, &signature, value_size)
.map_err(|_| Rc::new(RefCell::new(Trap::fake())))?; //was ActionError::Setup)?;
// Call the trampoline.
if let Err(message) = unsafe {
wasmtime_runtime::wasmtime_call_trampoline(
self.vmctx,
vmctx,
exec_code_buf,
values_vec.as_mut_ptr() as *mut u8,
)
@@ -78,7 +95,7 @@ impl Callable for WasmtimeFn {
}
// Load the return values out of `values_vec`.
for (index, abi_param) in self.signature.returns.iter().enumerate() {
for (index, abi_param) in signature.returns.iter().enumerate() {
unsafe {
let ptr = values_vec.as_ptr().add(index);
@@ -94,4 +111,44 @@ impl Callable for WasmtimeFn {
Ok(())
}
fn wasmtime_handle(&self) -> &InstanceHandle {
&self.instance
}
fn wasmtime_export(&self) -> &Export {
&self.export
}
}
pub struct NativeCallable {
callable: Rc<dyn Callable + 'static>,
instance: InstanceHandle,
export: Export,
}
impl NativeCallable {
pub(crate) fn new(
callable: Rc<dyn Callable + 'static>,
ft: &FuncType,
store: &Rc<RefCell<Store>>,
) -> Self {
let (instance, export) =
generate_func_export(ft, &callable, store).expect("generated func");
NativeCallable {
callable,
instance,
export,
}
}
}
impl WrappedCallable for NativeCallable {
fn call(&self, params: &[Val], results: &mut [Val]) -> Result<(), Rc<RefCell<Trap>>> {
self.callable.call(params, results)
}
fn wasmtime_handle(&self) -> &InstanceHandle {
&self.instance
}
fn wasmtime_export(&self) -> &Export {
&self.export
}
}