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