@@ -193,6 +193,30 @@ impl ModuleInstance {
193193
194194 /// Instantiate the module in the given store (without running the start function)
195195 ///
196+ /// ## Example
197+ /// ```rust
198+ /// # fn main() -> tinywasm::Result<()> {
199+ /// # use tinywasm::{ModuleInstance, Store};
200+ /// # let wasm = wat::parse_str(r#"
201+ /// # (module
202+ /// # (global $g (mut i32) (i32.const 0))
203+ /// # (func $start
204+ /// # i32.const 42
205+ /// # global.set $g)
206+ /// # (start $start)
207+ /// # (export "g" (global $g)))
208+ /// # "#).expect("valid wat");
209+ /// # let module = tinywasm::parse_bytes(&wasm)?;
210+ /// let mut store = Store::default();
211+ /// let instance = ModuleInstance::instantiate_no_start(&mut store, &module, None)?;
212+ ///
213+ /// assert_eq!(instance.global_get(&store, "g")?, 0.into());
214+ /// instance.start(&mut store)?;
215+ /// assert_eq!(instance.global_get(&store, "g")?, 42.into());
216+ /// # Ok(())
217+ /// # }
218+ /// ```
219+ ///
196220 /// See <https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation>
197221 pub fn instantiate_no_start ( store : & mut Store , module : & Module , imports : Option < Imports > ) -> Result < Self > {
198222 let idx = store. next_module_instance_idx ( ) ;
@@ -251,6 +275,33 @@ impl ModuleInstance {
251275 }
252276
253277 /// Returns an iterator over all exported extern values for this instance.
278+ ///
279+ /// ## Example
280+ /// ```rust
281+ /// # fn main() -> tinywasm::Result<()> {
282+ /// # use tinywasm::{ExternItem, ModuleInstance, Store};
283+ /// # let wasm = wat::parse_str(r#"
284+ /// # (module
285+ /// # (func (export "f"))
286+ /// # (memory (export "mem") 1))
287+ /// # "#).expect("valid wat");
288+ /// # let module = tinywasm::parse_bytes(&wasm)?;
289+ /// # let mut store = Store::default();
290+ /// let instance = ModuleInstance::instantiate(&mut store, &module, None)?;
291+ ///
292+ /// let mut saw_func = false;
293+ /// let mut saw_memory = false;
294+ /// for (name, item) in instance.exports() {
295+ /// match (name, item) {
296+ /// ("f", ExternItem::Func(_)) => saw_func = true,
297+ /// ("mem", ExternItem::Memory(_)) => saw_memory = true,
298+ /// _ => {}
299+ /// }
300+ /// }
301+ /// assert!(saw_func && saw_memory);
302+ /// # Ok(())
303+ /// # }
304+ /// ```
254305 pub fn exports ( & self ) -> impl Iterator < Item = ( & str , ExternItem ) > + ' _ {
255306 self . 0 . exports . iter ( ) . map ( move |export| {
256307 let item = match export. kind {
@@ -302,6 +353,26 @@ impl ModuleInstance {
302353 }
303354
304355 /// Get any exported extern value by name.
356+ ///
357+ /// ## Example
358+ /// ```rust
359+ /// # fn main() -> tinywasm::Result<()> {
360+ /// # use tinywasm::{ExternItem, ModuleInstance, Store};
361+ /// # let wasm = wat::parse_str(r#"
362+ /// # (module
363+ /// # (global (export "answer") i32 (i32.const 42)))
364+ /// # "#).expect("valid wat");
365+ /// # let module = tinywasm::parse_bytes(&wasm)?;
366+ /// # let mut store = Store::default();
367+ /// let instance = ModuleInstance::instantiate(&mut store, &module, None)?;
368+ ///
369+ /// let ExternItem::Global(global) = instance.extern_item("answer")? else {
370+ /// panic!("expected global export");
371+ /// };
372+ /// assert_eq!(global.get(&store)?, 42.into());
373+ /// # Ok(())
374+ /// # }
375+ /// ```
305376 pub fn extern_item ( & self , name : & str ) -> Result < ExternItem > {
306377 match self . require_export ( name) ? {
307378 ExternVal :: Func ( addr) => {
@@ -530,6 +601,23 @@ impl ModuleInstance {
530601 /// Returns None if the module has no start function
531602 /// If no start function is specified, also checks for a `_start` function in the exports
532603 ///
604+ /// ## Example
605+ /// ```rust
606+ /// # fn main() -> tinywasm::Result<()> {
607+ /// # use tinywasm::{ModuleInstance, Store};
608+ /// # let wasm = wat::parse_str(r#"
609+ /// # (module
610+ /// # (func (export "_start"))
611+ /// # )
612+ /// # "#).expect("valid wat");
613+ /// # let module = tinywasm::parse_bytes(&wasm)?;
614+ /// # let mut store = Store::default();
615+ /// let instance = ModuleInstance::instantiate_no_start(&mut store, &module, None)?;
616+ /// assert!(instance.start_func(&store)?.is_some());
617+ /// # Ok(())
618+ /// # }
619+ /// ```
620+ ///
533621 /// See <https://webassembly.github.io/spec/core/syntax/modules.html#start-function>
534622 pub fn start_func ( & self , store : & Store ) -> Result < Option < Function > > {
535623 self . validate_store ( store) ?;
@@ -564,6 +652,29 @@ impl ModuleInstance {
564652 ///
565653 /// Returns `None` if the module has no start function
566654 ///
655+ /// ## Example
656+ /// ```rust
657+ /// # fn main() -> tinywasm::Result<()> {
658+ /// # use tinywasm::{ModuleInstance, Store};
659+ /// # let wasm = wat::parse_str(r#"
660+ /// # (module
661+ /// # (global $g (mut i32) (i32.const 0))
662+ /// # (func (export "_start")
663+ /// # i32.const 7
664+ /// # global.set $g)
665+ /// # (export "g" (global $g)))
666+ /// # "#).expect("valid wat");
667+ /// # let module = tinywasm::parse_bytes(&wasm)?;
668+ /// let mut store = Store::default();
669+ /// let instance = ModuleInstance::instantiate_no_start(&mut store, &module, None)?;
670+ ///
671+ /// assert_eq!(instance.global_get(&store, "g")?, 0.into());
672+ /// assert_eq!(instance.start(&mut store)?, Some(()));
673+ /// assert_eq!(instance.global_get(&store, "g")?, 7.into());
674+ /// # Ok(())
675+ /// # }
676+ /// ```
677+ ///
567678 /// See <https://webassembly.github.io/spec/core/syntax/modules.html#syntax-start>
568679 pub fn start ( & self , store : & mut Store ) -> Result < Option < ( ) > > {
569680 match self . start_func ( store) ? {
0 commit comments