Skip to main content

wasmedge_sdk/
import.rs

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