Skip to content

Commit

Permalink
feat(rust-sdk): implement PluginModuleBuilder and PluginModule
Browse files Browse the repository at this point in the history
Signed-off-by: Xin Liu <[email protected]>
  • Loading branch information
apepkuss committed Jun 27, 2023
1 parent 94dcca1 commit 01bc6eb
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ mod tests {
fn test_func_basic() {
// create an ImportModule
let result = ImportObjectBuilder::<NeverType>::new()
.with_func::<(i32, i32), i32>("add", real_add, None)
.with_func::<(i32, i32), i32>("add", real_add)
.expect("failed to add host func")
.build("extern");
assert!(result.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion src/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod tests {

// create an import object
let result = ImportObjectBuilder::<NeverType>::new()
.with_func::<(i32, i32), i32>("add", real_add, None)
.with_func::<(i32, i32), i32>("add", real_add)
.expect("failed to add host func")
.with_table("table", table)
.build("extern");
Expand Down
43 changes: 12 additions & 31 deletions src/import.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#[cfg(all(feature = "async", target_os = "linux"))]
use crate::r#async::AsyncHostFn;
use crate::{
io::WasmValTypeList, Finalizer, FuncType, Global, HostFn, Memory, Table, WasmEdgeResult,
};
use crate::{io::WasmValTypeList, FuncType, Global, HostFn, Memory, Table, WasmEdgeResult};
use wasmedge_sys::{self as sys, AsImport};

/// Creates a normal or wasi [import object](crate::ImportObject).
Expand Down Expand Up @@ -64,7 +62,7 @@ use wasmedge_sys::{self as sys, AsImport};
/// let module_name = "extern";
/// let _import = ImportObjectBuilder::<NeverType>::new()
/// // add a function
/// .with_func::<(i32, i32), i32>("add", real_add, None)?
/// .with_func::<(i32, i32), i32>("add", real_add)?
/// // add a global
/// .with_global("global", global_const)
/// // add a memory
Expand All @@ -85,7 +83,6 @@ pub struct ImportObjectBuilder<T: Send + Sync + Clone> {
memories: Vec<(String, sys::Memory)>,
tables: Vec<(String, sys::Table)>,
host_data: Option<Box<T>>,
finalizer: Option<Finalizer>,
}
impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
/// Creates a new [ImportObjectBuilder].
Expand All @@ -96,7 +93,6 @@ impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
memories: Vec::new(),
tables: Vec::new(),
host_data: None,
finalizer: None,
}
}

Expand All @@ -110,16 +106,13 @@ impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
///
/// * `real_func` - The native function.
///
/// * `data` - The additional data object to set to this host function context.
///
/// # error
///
/// If fail to create or add the [host function](crate::Func), then an error is returned.
pub fn with_func<Args, Rets>(
mut self,
name: impl AsRef<str>,
real_func: HostFn<T>,
data: Option<&mut T>,
) -> WasmEdgeResult<Self>
where
Args: WasmValTypeList,
Expand All @@ -128,7 +121,7 @@ impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
let args = Args::wasm_types();
let returns = Rets::wasm_types();
let ty = FuncType::new(Some(args.to_vec()), Some(returns.to_vec()));
let inner_func = sys::Function::create::<T>(&ty.into(), real_func, data, 0)?;
let inner_func = sys::Function::create::<T>(&ty.into(), real_func, None, 0)?;
self.funcs.push((name.as_ref().to_owned(), inner_func));
Ok(self)
}
Expand All @@ -145,8 +138,6 @@ impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
///
/// * `real_func` - The native function.
///
/// * `data` - The additional data object to set to this host function context.
///
/// # error
///
/// If fail to create or add the [host function](crate::Func), then an error is returned.
Expand All @@ -155,9 +146,8 @@ impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
name: impl AsRef<str>,
ty: FuncType,
real_func: HostFn<T>,
data: Option<&mut T>,
) -> WasmEdgeResult<Self> {
let inner_func = sys::Function::create::<T>(&ty.into(), real_func, data, 0)?;
let inner_func = sys::Function::create::<T>(&ty.into(), real_func, None, 0)?;
self.funcs.push((name.as_ref().to_owned(), inner_func));
Ok(self)
}
Expand All @@ -180,7 +170,6 @@ impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
mut self,
name: impl AsRef<str>,
real_func: AsyncHostFn<T>,
ctx_data: Option<&mut T>,
) -> WasmEdgeResult<Self>
where
Args: WasmValTypeList,
Expand All @@ -189,7 +178,7 @@ impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
let args = Args::wasm_types();
let returns = Rets::wasm_types();
let ty = FuncType::new(Some(args.to_vec()), Some(returns.to_vec()));
let inner_func = sys::Function::create_async(&ty.into(), real_func, ctx_data, 0)?;
let inner_func = sys::Function::create_async(&ty.into(), real_func, None, 0)?;
self.funcs.push((name.as_ref().to_owned(), inner_func));
Ok(self)
}
Expand Down Expand Up @@ -239,11 +228,8 @@ impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
///
/// * `host_data` - The host data to be stored in the module instance.
///
/// * `finalizer` - The function to drop the host data.
///
pub fn with_host_data(mut self, host_data: Box<T>, finalizer: Option<Finalizer>) -> Self {
pub fn with_host_data(mut self, host_data: Box<T>) -> Self {
self.host_data = Some(host_data);
self.finalizer = finalizer;
self
}

Expand All @@ -257,12 +243,7 @@ impl<T: Send + Sync + Clone> ImportObjectBuilder<T> {
///
/// If fail to create the [ImportObject], then an error is returned.
pub fn build(self, name: impl AsRef<str>) -> WasmEdgeResult<ImportObject<T>> {
let mut inner = match self.host_data {
Some(host_data) => {
sys::ImportModule::create_with_data(name.as_ref(), host_data, self.finalizer)?
}
None => sys::ImportModule::create(name.as_ref())?,
};
let mut inner = sys::ImportModule::create(name.as_ref(), self.host_data)?;

// add func
for (name, func) in self.funcs.into_iter() {
Expand Down Expand Up @@ -353,7 +334,7 @@ mod tests {
let circle = Box::new(Circle { radius: 10 });

let result = ImportObjectBuilder::<Circle>::new()
.with_host_data(circle, None)
.with_host_data(circle)
.build("extern");
assert!(result.is_ok());

Expand Down Expand Up @@ -412,7 +393,7 @@ mod tests {

// create an import object
let result = ImportObjectBuilder::<NeverType>::new()
.with_func::<(i32, i32), i32>("add", real_add, None)
.with_func::<(i32, i32), i32>("add", real_add)
.expect("failed to add host func")
.build("extern");
assert!(result.is_ok());
Expand Down Expand Up @@ -658,7 +639,7 @@ mod tests {

// create an import object
let result = ImportObjectBuilder::<NeverType>::new()
.with_func::<(i32, i32), i32>("add", real_add, None)
.with_func::<(i32, i32), i32>("add", real_add)
.expect("failed to add host func")
.with_table("table", table)
.build("extern");
Expand Down Expand Up @@ -785,7 +766,7 @@ mod tests {

// create an ImportModule instance
let result = ImportObjectBuilder::<NeverType>::new()
.with_func::<(i32, i32), i32>("add", real_add, None)
.with_func::<(i32, i32), i32>("add", real_add)
.expect("failed to add host function")
.with_global("global", global_const)
.with_memory("memory", memory)
Expand Down Expand Up @@ -903,7 +884,7 @@ mod tests {

// create an import object
let result = ImportObjectBuilder::<NeverType>::new()
.with_func::<(i32, i32), i32>("add", real_add, None)
.with_func::<(i32, i32), i32>("add", real_add)
.expect("failed to add host function")
.with_global("global", global_const)
.with_memory("memory", memory)
Expand Down
2 changes: 1 addition & 1 deletion src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ mod tests {

// create an ImportModule instance
let result = ImportObjectBuilder::<NeverType>::new()
.with_func::<(i32, i32), i32>("add", real_add, None)
.with_func::<(i32, i32), i32>("add", real_add)
.expect("failed to add host function")
.with_global("global", global_const)
.with_memory("mem", memory)
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! {
//! // create an import module
//! let import = ImportObjectBuilder::<NeverType>::new()
//! .with_func::<(), ()>("say_hello", say_hello, None)?
//! .with_func::<(), ()>("say_hello", say_hello)?
//! .build("env")?;
//!
//! let wasm_bytes = wat2wasm(
Expand Down Expand Up @@ -165,7 +165,7 @@ pub type CallingFrame = wasmedge_sys::CallingFrame;

pub type HostFn<T> = wasmedge_sys::HostFn<T>;

pub type Finalizer = wasmedge_sys::Finalizer;
pub type Finalizer = wasmedge_sys::plugin::Finalizer;

#[cfg(all(feature = "async", target_os = "linux"))]
pub mod r#async {
Expand Down
Loading

0 comments on commit 01bc6eb

Please sign in to comment.