Struct wasmedge_sdk::ImportObjectBuilder
source · pub struct ImportObjectBuilder { /* private fields */ }
Expand description
Creates a normal or wasi import object.
Example
This example shows how to create a normal import object that contains a host function, a global variable, a memory and a table. The import object is named “extern”.
// If the version of rust used is less than v1.63, please uncomment the follow attribute.
// #![feature(explicit_generic_args_with_impl_trait)]
use wasmedge_sdk::{
types::Val,
Global, ImportObjectBuilder, Memory, Table,
error::HostFuncError, WasmValue, GlobalType,
MemoryType, Mutability, RefType, TableType,
ValType, Caller, host_function,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// a native function to be imported as host function
#[host_function]
fn real_add(_: Caller, inputs: Vec<WasmValue>) -> std::result::Result<Vec<WasmValue>, HostFuncError> {
if inputs.len() != 2 {
return Err(HostFuncError::User(1));
}
let a = if inputs[0].ty() == ValType::I32 {
inputs[0].to_i32()
} else {
return Err(HostFuncError::User(2));
};
let b = if inputs[1].ty() == ValType::I32 {
inputs[1].to_i32()
} else {
return Err(HostFuncError::User(3));
};
let c = a + b;
Ok(vec![WasmValue::from_i32(c)])
}
// create a Const global instance to be imported
let global_const = Global::new(
GlobalType::new(ValType::F32, Mutability::Const),
Val::F32(3.5),
)?;
// create a memory instance to be imported
let memory = Memory::new(MemoryType::new(10, Some(20), false)?)?;
// create a table instance to be imported
let table = Table::new(TableType::new(RefType::FuncRef, 10, Some(20)))?;
// create an import object
let module_name = "extern";
let _import = ImportObjectBuilder::new()
// add a function
.with_func::<(i32, i32), i32>("add", real_add)?
// add a global
.with_global("global", global_const)?
// add a memory
.with_memory("memory", memory)?
// add a table
.with_table("table", table)?
.build(module_name)?;
Ok(())
}
Implementations§
source§impl ImportObjectBuilder
impl ImportObjectBuilder
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new ImportObjectBuilder.
sourcepub fn with_func<Args, Rets>(
self,
name: impl AsRef<str>,
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
pub fn with_func<Args, Rets>(
self,
name: impl AsRef<str>,
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
Adds a host function to the ImportObject to create.
N.B. that this function can be used in thread-safe scenarios.
Arguments
-
name
- The exported name of the host function to add. -
real_func
- The native function.
error
If fail to create or add the host function, then an error is returned.
sourcepub fn with_func_by_type(
self,
name: impl AsRef<str>,
ty: FuncType,
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static
) -> WasmEdgeResult<Self>
pub fn with_func_by_type(
self,
name: impl AsRef<str>,
ty: FuncType,
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> + Send + Sync + 'static
) -> WasmEdgeResult<Self>
Adds a host function to the ImportObject to create.
N.B. that this function can be used in thread-safe scenarios.
Arguments
-
name
- The exported name of the host function to add. -
ty
- The function type. -
real_func
- The native function.
error
If fail to create or add the host function, then an error is returned.
sourcepub fn with_func_async<Args, Rets>(
self,
name: impl AsRef<str>,
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
pub fn with_func_async<Args, Rets>(
self,
name: impl AsRef<str>,
real_func: impl Fn(CallingFrame, Vec<WasmValue>) -> Box<dyn Future<Output = Result<Vec<WasmValue>, HostFuncError>> + Send> + Send + Sync + 'static
) -> WasmEdgeResult<Self>where
Args: WasmValTypeList,
Rets: WasmValTypeList,
Adds an async host function to the ImportObject to create.
N.B. that this function can be used in thread-safe scenarios.
Arguments
-
name
- The exported name of the host function to add. -
real_func
- The native function.
error
If fail to create or add the host function, then an error is returned.
sourcepub fn with_global(
self,
name: impl AsRef<str>,
global: Global
) -> WasmEdgeResult<Self>
pub fn with_global(
self,
name: impl AsRef<str>,
global: Global
) -> WasmEdgeResult<Self>
Adds a global to the ImportObject to create.
Arguments
-
name
- The exported name of the global to add. -
global
- The wasm global instance to add.
Error
If fail to create or add the global, then an error is returned.
sourcepub fn with_memory(
self,
name: impl AsRef<str>,
memory: Memory
) -> WasmEdgeResult<Self>
pub fn with_memory(
self,
name: impl AsRef<str>,
memory: Memory
) -> WasmEdgeResult<Self>
Adds a memory to the ImportObject to create.
Arguments
-
name
- The exported name of the memory to add. -
memory
- The wasm memory instance to add.
Error
If fail to create or add the memory, then an error is returned.
sourcepub fn with_table(
self,
name: impl AsRef<str>,
table: Table
) -> WasmEdgeResult<Self>
pub fn with_table(
self,
name: impl AsRef<str>,
table: Table
) -> WasmEdgeResult<Self>
Adds a table to the ImportObject to create.
Arguments
-
name
- The exported name of the table to add. -
table
- The wasm table instance to add.
Error
If fail to create or add the table, then an error is returned.
sourcepub fn build(self, name: impl AsRef<str>) -> WasmEdgeResult<ImportObject>
pub fn build(self, name: impl AsRef<str>) -> WasmEdgeResult<ImportObject>
Creates a new ImportObject.
Argument
name
- The name of the ImportObject to create.
Error
If fail to create the ImportObject, then an error is returned.