wasmedge_sdk/async/import.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
use crate::{io::WasmValTypeList, FuncType, WasmEdgeResult};
use sys::r#async::{
function::{AsyncFn, AsyncFunction},
module::AsyncImportObject,
};
use wasmedge_sys::{self as sys};
/// Creates a [async import object](sys::r#async::module::AsyncImportObject).
///
#[derive(Debug)]
pub struct ImportObjectBuilder<Data: Send> {
import_object: AsyncImportObject<Data>,
}
impl<Data: Send> ImportObjectBuilder<Data> {
/// Creates a new [ImportObjectBuilder].
pub fn new(name: impl AsRef<str>, data: Data) -> WasmEdgeResult<Self> {
let import_object = AsyncImportObject::create(name, Box::new(data))?;
Ok(Self { import_object })
}
/// Adds a [host function](crate::Func) 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](crate::Func) to add.
///
/// * `real_func` - The native function.
///
/// * `data` - The host context data used in this function.
///
/// # 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: AsyncFn<Data>,
) -> WasmEdgeResult<&mut Self>
where
Args: WasmValTypeList,
Rets: WasmValTypeList,
{
let args = Args::wasm_types();
let returns = Rets::wasm_types();
let ty = FuncType::new(args.to_vec(), returns.to_vec());
let func = AsyncFunction::create_async_func(
&ty,
real_func,
self.import_object.get_host_data_mut(),
0,
)?;
self.import_object.add_async_func(name, func);
Ok(self)
}
/// Adds a [host function](crate::Func) 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](crate::Func) to add.
///
/// * `ty` - The function type.
///
/// * `real_func` - The native function.
///
/// * `data` - The host context data used in this function.
///
/// # error
///
/// If fail to create or add the [host function](crate::Func), then an error is returned.
pub fn with_func_by_type(
&mut self,
name: impl AsRef<str>,
ty: FuncType,
real_func: AsyncFn<Data>,
) -> WasmEdgeResult<&mut Self> {
let func = AsyncFunction::create_async_func(
&ty,
real_func,
self.import_object.get_host_data_mut(),
0,
)?;
self.import_object.add_async_func(name, func);
Ok(self)
}
/// Adds a [global](crate::Global) to the [ImportObject] to create.
///
/// # Arguments
///
/// * `name` - The exported name of the [global](crate::Global) to add.
///
/// * `global` - The wasm [global instance](crate::Global) to add.
///
pub fn with_global(mut self, name: impl AsRef<str>, global: sys::Global) -> Self {
self.import_object.add_global(name, global);
self
}
/// Adds a [memory](crate::Memory) to the [ImportObject] to create.
///
/// # Arguments
///
/// * `name` - The exported name of the [memory](crate::Memory) to add.
///
/// * `memory` - The wasm [memory instance](crate::Memory) to add.
///
pub fn with_memory(mut self, name: impl AsRef<str>, memory: sys::Memory) -> Self {
self.import_object.add_memory(name, memory);
self
}
/// Adds a [table](crate::Table) to the [ImportObject] to create.
///
/// # Arguments
///
/// * `name` - The exported name of the [table](crate::Table) to add.
///
/// * `table` - The wasm [table instance](crate::Table) to add.
///
pub fn with_table(mut self, name: impl AsRef<str>, table: sys::Table) -> Self {
self.import_object.add_table(name, table);
self
}
/// Creates a new [ImportObject].
///
/// # Argument
///
/// * `name` - The name of the [ImportObject] to create.
///
/// * `host_data` - The host context data to be stored in the module instance.
///
/// # Error
///
/// If fail to create the [ImportObject], then an error is returned.
pub fn build(self) -> ImportObject<Data> {
self.import_object
}
}
/// Defines an import object that contains the required import data used when instantiating a [module](crate::Module).
///
/// An [ImportObject] instance is created with [ImportObjectBuilder](crate::ImportObjectBuilder).
pub type ImportObject<T> = sys::r#async::module::AsyncImportObject<T>;