@@ -90,56 +90,45 @@ impl Debug for HostFunction {
9090/// An external value
9191pub enum Extern {
9292 /// A global value
93- Global ( ExternGlobal ) ,
93+ Global {
94+ /// The type of the global value.
95+ ty : GlobalType ,
96+ /// The actual value of the global, encapsulated in `WasmValue`.
97+ val : WasmValue ,
98+ } ,
9499
95100 /// A table
96- Table ( ExternTable ) ,
101+ Table {
102+ /// Defines the type of the table, including its element type and limits.
103+ ty : TableType ,
104+ /// The initial value of the table.
105+ init : WasmValue ,
106+ } ,
97107
98108 /// A memory
99- Memory ( ExternMemory ) ,
109+ Memory {
110+ /// Defines the type of the memory, including its limits and the type of its pages.
111+ ty : MemoryType ,
112+ } ,
100113
101114 /// A function
102115 Function ( Function ) ,
103116}
104117
105- /// A function
106- #[ derive( Debug , Clone ) ]
107- pub struct ExternFunc ( pub ( crate ) HostFunction ) ;
108-
109- /// A global value
110- #[ derive( Debug , Clone ) ]
111- pub struct ExternGlobal {
112- pub ( crate ) ty : GlobalType ,
113- pub ( crate ) val : WasmValue ,
114- }
115-
116- /// A table
117- #[ derive( Debug , Clone ) ]
118- pub struct ExternTable {
119- pub ( crate ) ty : TableType ,
120- pub ( crate ) val : WasmValue ,
121- }
122-
123- /// A memory
124- #[ derive( Debug , Clone ) ]
125- pub struct ExternMemory {
126- pub ( crate ) ty : MemoryType ,
127- }
128-
129118impl Extern {
130119 /// Create a new global import
131120 pub fn global ( val : WasmValue , mutable : bool ) -> Self {
132- Self :: Global ( ExternGlobal { ty : GlobalType { ty : val. val_type ( ) , mutable } , val } )
121+ Self :: Global { ty : GlobalType { ty : val. val_type ( ) , mutable } , val }
133122 }
134123
135124 /// Create a new table import
136- pub fn table ( ty : TableType , val : WasmValue ) -> Self {
137- Self :: Table ( ExternTable { ty, val } )
125+ pub fn table ( ty : TableType , init : WasmValue ) -> Self {
126+ Self :: Table { ty, init }
138127 }
139128
140129 /// Create a new memory import
141130 pub fn memory ( ty : MemoryType ) -> Self {
142- Self :: Memory ( ExternMemory { ty } )
131+ Self :: Memory { ty }
143132 }
144133
145134 /// Create a new function import
@@ -174,10 +163,10 @@ impl Extern {
174163
175164 pub ( crate ) fn kind ( & self ) -> ExternalKind {
176165 match self {
177- Self :: Global ( _ ) => ExternalKind :: Global ,
178- Self :: Table ( _ ) => ExternalKind :: Table ,
179- Self :: Memory ( _ ) => ExternalKind :: Memory ,
180- Self :: Function ( _ ) => ExternalKind :: Func ,
166+ Self :: Global { .. } => ExternalKind :: Global ,
167+ Self :: Table { .. } => ExternalKind :: Table ,
168+ Self :: Memory { .. } => ExternalKind :: Memory ,
169+ Self :: Function { .. } => ExternalKind :: Func ,
181170 }
182171 }
183172}
@@ -197,6 +186,38 @@ impl From<&Import> for ExternName {
197186
198187#[ derive( Debug , Default ) ]
199188/// Imports for a module instance
189+ ///
190+ /// This is used to link a module instance to its imports
191+ ///
192+ /// ## Example
193+ /// ```rust
194+ /// # fn main() -> tinywasm::Result<()> {
195+ /// use tinywasm::{Imports, Extern};
196+ /// use tinywasm::types::{ValType, TableType, MemoryType, WasmValue};
197+ /// let mut imports = Imports::new();
198+ ///
199+ /// // function args can be either a single
200+ /// // value that implements `TryFrom<WasmValue>` or a tuple of them
201+ /// let print_i32 = Extern::typed_func(|_ctx: tinywasm::FuncContext<'_>, arg: i32| {
202+ /// log::debug!("print_i32: {}", arg);
203+ /// Ok(())
204+ /// });
205+ ///
206+ /// let table_type = TableType::new(ValType::RefFunc, 10, Some(20));
207+ /// let table_init = WasmValue::default_for(ValType::RefFunc);
208+ ///
209+ /// imports
210+ /// .define("my_module", "print_i32", print_i32)?
211+ /// .define("my_module", "table", Extern::table(table_type, table_init))?
212+ /// .define("my_module", "memory", Extern::memory(MemoryType::new_32(1, Some(2))))?
213+ /// .define("my_module", "global_i32", Extern::global(WasmValue::I32(666), false))?
214+ /// .link_module("my_other_module", 0)?;
215+ /// # Ok(())
216+ /// # }
217+ /// ```
218+ ///
219+ /// Note that module instance addresses for [`Imports::link_module`] can be obtained from [`crate::ModuleInstance::id`].
220+ /// Now, the imports object can be passed to [`crate::ModuleInstance::instantiate`].
200221pub struct Imports {
201222 values : BTreeMap < ExternName , Extern > ,
202223 modules : BTreeMap < String , ModuleInstanceAddr > ,
@@ -334,17 +355,17 @@ impl Imports {
334355 match val {
335356 // A link to something that needs to be added to the store
336357 ResolvedExtern :: Extern ( ex) => match ( ex, & import. kind ) {
337- ( Extern :: Global ( extern_global ) , ImportKind :: Global ( ty ) ) => {
338- Self :: compare_types ( import, & extern_global . ty , ty ) ?;
339- imports. globals . push ( store. add_global ( extern_global . ty , extern_global . val . into ( ) , idx) ?) ;
358+ ( Extern :: Global { ty , val } , ImportKind :: Global ( import_ty ) ) => {
359+ Self :: compare_types ( import, & ty, import_ty ) ?;
360+ imports. globals . push ( store. add_global ( ty, val. into ( ) , idx) ?) ;
340361 }
341- ( Extern :: Table ( extern_table ) , ImportKind :: Table ( ty ) ) => {
342- Self :: compare_table_types ( import, & extern_table . ty , ty ) ?;
343- imports. tables . push ( store. add_table ( extern_table . ty , idx) ?) ;
362+ ( Extern :: Table { ty , .. } , ImportKind :: Table ( import_ty ) ) => {
363+ Self :: compare_table_types ( import, & ty, import_ty ) ?;
364+ imports. tables . push ( store. add_table ( ty, idx) ?) ;
344365 }
345- ( Extern :: Memory ( extern_memory ) , ImportKind :: Memory ( ty ) ) => {
346- Self :: compare_memory_types ( import, & extern_memory . ty , ty , None ) ?;
347- imports. memories . push ( store. add_mem ( extern_memory . ty , idx) ?) ;
366+ ( Extern :: Memory { ty } , ImportKind :: Memory ( import_ty ) ) => {
367+ Self :: compare_memory_types ( import, & ty, import_ty , None ) ?;
368+ imports. memories . push ( store. add_mem ( ty, idx) ?) ;
348369 }
349370 ( Extern :: Function ( extern_func) , ImportKind :: Function ( ty) ) => {
350371 let import_func_type = module
0 commit comments