apply new module creation scheme

This commit is contained in:
vms 2020-05-08 22:06:58 +03:00
parent dd001db7ad
commit 39c9aaa703
3 changed files with 46 additions and 16 deletions

View File

@ -28,8 +28,11 @@ use wasmer_runtime::Func;
/// 4. read a result from the res by reading 4 bytes as little-endian result_size
/// and the read result_size bytes as the final result.
/// 5. deallocate(res, strlen(sql)) to clean memory.
pub(crate) trait ModuleABI<'a> {}
#[derive(Clone)]
pub(crate) struct ModuleABI<'a> {
pub(crate) struct ABI<'a> {
// It is safe to use unwrap() while calling these functions because Option is used here
// just to allow partially initialization. And all Option fields will contain Some if
// invoking FCE::new has been succeed.

View File

@ -17,18 +17,18 @@
use crate::vm::config::Config;
use crate::vm::errors::FCEError;
use crate::vm::module::fce_result::FCEResult;
use crate::vm::module::{ModuleABI, ModuleAPI};
use crate::vm::module::{ModuleAPI, ABI};
use sha2::digest::generic_array::GenericArray;
use sha2::digest::FixedOutput;
use wasmer_runtime::{compile, func, imports, Ctx, Instance};
use wasmer_runtime::{compile, func, imports, Ctx, Func, Instance};
use wasmer_runtime_core::import::ImportObject;
use wasmer_runtime_core::memory::ptr::{Array, WasmPtr};
use wasmer_wasi::generate_import_object_for_version;
pub(crate) struct FCEModule {
instance: &'static Instance,
abi: ModuleABI<'static>,
instance: Instance,
abi: ABI<'static>,
}
impl FCEModule {
@ -52,19 +52,46 @@ impl FCEModule {
import_object.allow_missing_functions = false;
let instance = compile(&wasm_bytes)?.instantiate(&import_object)?;
let instance: &'static mut Instance = Box::leak(Box::new(instance));
let abi = ModuleABI {
allocate: Some(instance.exports.get(&config.allocate_fn_name)?),
deallocate: Some(instance.exports.get(&config.deallocate_fn_name)?),
invoke: Some(instance.exports.get(&config.invoke_fn_name)?),
store: Some(instance.exports.get(&config.store_fn_name)?),
load: Some(instance.exports.get(&config.load_fn_name)?),
unsafe {
#[rustfmt::skip]
let allocate = std::mem::transmute::<Func<'_, i32, i32>, Func<'static, i32, i32>>(
instance.exports.get(&config.allocate_fn_name)?
);
#[rustfmt::skip]
let deallocate = std::mem::transmute::<Func<'_, (i32, i32)>, Func<'static, (i32, i32)>>(
instance.exports.get(&config.deallocate_fn_name)?
);
#[rustfmt::skip]
let invoke = std::mem::transmute::<Func<'_, (i32, i32), i32>, Func<'static, (i32, i32), i32>, >(
instance.exports.get(&config.invoke_fn_name)?
);
#[rustfmt::skip]
let store = std::mem::transmute::<Func<'_, (i32, i32)>, Func<'static, _, _>>(
instance.exports.get(&config.store_fn_name)?,
);
#[rustfmt::skip]
let load = std::mem::transmute::<Func<'_, i32, i32>, Func<'static, _, _>>(
instance.exports.get(&config.load_fn_name)?,
);
let abi = ABI {
allocate: Some(allocate),
deallocate: Some(deallocate),
invoke: Some(invoke),
store: Some(store),
load: Some(load),
};
Ok(Self { instance, abi })
}
}
pub fn get_abi(&self) -> &ModuleABI<'static> {
pub fn get_abi(&self) -> &ABI<'static> {
&self.abi
}

View File

@ -19,6 +19,6 @@ mod api;
mod fce_module;
pub mod fce_result;
pub(crate) use abi::ModuleABI;
pub(crate) use abi::ABI;
pub(crate) use api::ModuleAPI;
pub(crate) use fce_module::FCEModule;