callback function creation improvements

This commit is contained in:
vms 2020-04-30 00:52:37 +03:00
parent 8fb0b31a71
commit 8773271e73
4 changed files with 23 additions and 34 deletions

View File

@ -23,7 +23,6 @@
unreachable_patterns unreachable_patterns
)] )]
#![warn(rust_2018_idioms)] #![warn(rust_2018_idioms)]
mod vm; mod vm;
pub use vm::config::Config; pub use vm::config::Config;

View File

@ -43,61 +43,46 @@ impl Frank {
/// Extracts ABI of a module into Namespace. /// Extracts ABI of a module into Namespace.
fn create_import_object(module: &FrankModule, config: &Config) -> Namespace { fn create_import_object(module: &FrankModule, config: &Config) -> Namespace {
let mut namespace = Namespace::new(); let mut namespace = Namespace::new();
let module_abi = module.get_abi();
// TODO: introduce a macro for such things // TODO: introduce a macro for such things
let allocate = module.abi.allocate.clone(); let allocate = module_abi.allocate.clone().unwrap();
namespace.insert( namespace.insert(
config.allocate_fn_name.clone(), config.allocate_fn_name.clone(),
func!(move |_ctx: &mut Ctx, size: i32| -> i32 { func!(move |_ctx: &mut Ctx, size: i32| -> i32 {
allocate allocate.call(size).expect("allocate failed")
.as_ref()
.unwrap()
.call(size)
.expect("allocate failed")
}), }),
); );
let invoke = module.abi.invoke.clone(); let invoke = module_abi.invoke.clone().unwrap();
namespace.insert( namespace.insert(
config.invoke_fn_name.clone(), config.invoke_fn_name.clone(),
func!(move |_ctx: &mut Ctx, offset: i32, size: i32| -> i32 { func!(move |_ctx: &mut Ctx, offset: i32, size: i32| -> i32 {
invoke invoke.call(offset, size).expect("invoke failed")
.as_ref()
.unwrap()
.call(offset, size)
.expect("invoke failed")
}), }),
); );
let deallocate = module.abi.deallocate.clone(); let deallocate = module_abi.deallocate.clone().unwrap();
namespace.insert( namespace.insert(
config.deallocate_fn_name.clone(), config.deallocate_fn_name.clone(),
func!(move |_ctx: &mut Ctx, ptr: i32, size: i32| { func!(move |_ctx: &mut Ctx, ptr: i32, size: i32| {
deallocate deallocate.call(ptr, size).expect("deallocate failed");
.as_ref()
.unwrap()
.call(ptr, size)
.expect("deallocate failed");
}), }),
); );
let store = module.abi.store.clone(); let store = module_abi.store.clone().unwrap();
namespace.insert( namespace.insert(
config.store_fn_name.clone(), config.store_fn_name.clone(),
func!(move |_ctx: &mut Ctx, offset: i32, value: i32| { func!(move |_ctx: &mut Ctx, offset: i32, value: i32| {
store store.call(offset, value).expect("store failed")
.as_ref()
.unwrap()
.call(offset, value)
.expect("store failed")
}), }),
); );
let load = module.abi.load.clone(); let load = module_abi.load.clone().unwrap();
namespace.insert( namespace.insert(
config.load_fn_name.clone(), config.load_fn_name.clone(),
func!(move |_ctx: &mut Ctx, offset: i32| -> i32 { func!(move |_ctx: &mut Ctx, offset: i32| -> i32 {
load.as_ref().unwrap().call(offset).expect("load failed") load.call(offset).expect("load failed")
}), }),
); );

View File

@ -28,22 +28,23 @@ use wasmer_runtime::Func;
/// 4. read a result from the res by reading 4 bytes as little-endian result_size /// 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. /// and the read result_size bytes as the final result.
/// 5. deallocate(res, strlen(sql)) to clean memory. /// 5. deallocate(res, strlen(sql)) to clean memory.
pub(crate) struct ModuleABI { #[derive(Clone)]
pub(crate) struct ModuleABI<'a> {
// It is safe to use unwrap() while calling these functions because Option is used here // 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 // just to allow partially initialization. And all Option fields will contain Some if
// invoking Frank::new has been succeed. // invoking Frank::new has been succeed.
/// Allocates a region of memory inside a module. Used for passing argument inside the module. /// Allocates a region of memory inside a module. Used for passing argument inside the module.
pub(crate) allocate: Option<Func<'static, i32, i32>>, pub(crate) allocate: Option<Func<'a, i32, i32>>,
/// Deallocates previously allocated memory region. /// Deallocates previously allocated memory region.
pub(crate) deallocate: Option<Func<'static, (i32, i32), ()>>, pub(crate) deallocate: Option<Func<'a, (i32, i32), ()>>,
/// Calls the main entry point of a module called invoke. /// Calls the main entry point of a module called invoke.
pub(crate) invoke: Option<Func<'static, (i32, i32), i32>>, pub(crate) invoke: Option<Func<'a, (i32, i32), i32>>,
/// Stores one given byte on provided address. /// Stores one given byte on provided address.
pub(crate) store: Option<Func<'static, (i32, i32)>>, pub(crate) store: Option<Func<'a, (i32, i32)>>,
/// Loads one bytes from provided address. /// Loads one bytes from provided address.
pub(crate) load: Option<Func<'static, i32, i32>>, pub(crate) load: Option<Func<'a, i32, i32>>,
} }

View File

@ -28,7 +28,7 @@ use wasmer_wasi::generate_import_object_for_version;
pub(crate) struct FrankModule { pub(crate) struct FrankModule {
instance: &'static Instance, instance: &'static Instance,
pub(crate) abi: ModuleABI, abi: ModuleABI<'static>,
} }
impl FrankModule { impl FrankModule {
@ -68,6 +68,10 @@ impl FrankModule {
Ok(Self { instance, abi }) Ok(Self { instance, abi })
} }
pub fn get_abi(&self) -> &ModuleABI<'static> {
&self.abi
}
/// Prints utf8 string of the given size from the given offset. Called from the wasm. /// Prints utf8 string of the given size from the given offset. Called from the wasm.
fn logger_log_utf8_string(ctx: &mut Ctx, offset: i32, size: i32) { fn logger_log_utf8_string(ctx: &mut Ctx, offset: i32, size: i32) {
let wasm_ptr = WasmPtr::<u8, Array>::new(offset as _); let wasm_ptr = WasmPtr::<u8, Array>::new(offset as _);