@@ -34,7 +34,7 @@ impl Extern {
3434 /// Returns the underlying `Func`, if this external is a function.
3535 ///
3636 /// Returns `None` if this is not a function.
37- pub fn func ( & self ) -> Option < & Func > {
37+ pub fn into_func ( self ) -> Option < Func > {
3838 match self {
3939 Extern :: Func ( func) => Some ( func) ,
4040 _ => None ,
@@ -44,7 +44,7 @@ impl Extern {
4444 /// Returns the underlying `Global`, if this external is a global.
4545 ///
4646 /// Returns `None` if this is not a global.
47- pub fn global ( & self ) -> Option < & Global > {
47+ pub fn into_global ( self ) -> Option < Global > {
4848 match self {
4949 Extern :: Global ( global) => Some ( global) ,
5050 _ => None ,
@@ -54,7 +54,7 @@ impl Extern {
5454 /// Returns the underlying `Table`, if this external is a table.
5555 ///
5656 /// Returns `None` if this is not a table.
57- pub fn table ( & self ) -> Option < & Table > {
57+ pub fn into_table ( self ) -> Option < Table > {
5858 match self {
5959 Extern :: Table ( table) => Some ( table) ,
6060 _ => None ,
@@ -64,7 +64,7 @@ impl Extern {
6464 /// Returns the underlying `Memory`, if this external is a memory.
6565 ///
6666 /// Returns `None` if this is not a memory.
67- pub fn memory ( & self ) -> Option < & Memory > {
67+ pub fn into_memory ( self ) -> Option < Memory > {
6868 match self {
6969 Extern :: Memory ( memory) => Some ( memory) ,
7070 _ => None ,
@@ -74,10 +74,10 @@ impl Extern {
7474 /// Returns the type associated with this `Extern`.
7575 pub fn ty ( & self ) -> ExternType {
7676 match self {
77- Extern :: Func ( ft) => ExternType :: Func ( ft. ty ( ) . clone ( ) ) ,
78- Extern :: Memory ( ft) => ExternType :: Memory ( ft. ty ( ) . clone ( ) ) ,
79- Extern :: Table ( tt) => ExternType :: Table ( tt. ty ( ) . clone ( ) ) ,
80- Extern :: Global ( gt) => ExternType :: Global ( gt. ty ( ) . clone ( ) ) ,
77+ Extern :: Func ( ft) => ExternType :: Func ( ft. ty ( ) ) ,
78+ Extern :: Memory ( ft) => ExternType :: Memory ( ft. ty ( ) ) ,
79+ Extern :: Table ( tt) => ExternType :: Table ( tt. ty ( ) ) ,
80+ Extern :: Global ( gt) => ExternType :: Global ( gt. ty ( ) ) ,
8181 }
8282 }
8383
@@ -91,11 +91,11 @@ impl Extern {
9191 }
9292
9393 pub ( crate ) fn from_wasmtime_export (
94+ wasmtime_export : wasmtime_runtime:: Export ,
9495 store : & Store ,
9596 instance_handle : InstanceHandle ,
96- export : wasmtime_runtime:: Export ,
9797 ) -> Extern {
98- match export {
98+ match wasmtime_export {
9999 wasmtime_runtime:: Export :: Function ( f) => {
100100 Extern :: Func ( Func :: from_wasmtime_function ( f, store, instance_handle) )
101101 }
@@ -164,7 +164,6 @@ impl From<Table> for Extern {
164164#[ derive( Clone ) ]
165165pub struct Global {
166166 store : Store ,
167- ty : GlobalType ,
168167 wasmtime_export : wasmtime_runtime:: ExportGlobal ,
169168 wasmtime_handle : InstanceHandle ,
170169}
@@ -191,27 +190,44 @@ impl Global {
191190 let ( wasmtime_handle, wasmtime_export) = generate_global_export ( store, & ty, val) ?;
192191 Ok ( Global {
193192 store : store. clone ( ) ,
194- ty,
195193 wasmtime_export,
196194 wasmtime_handle,
197195 } )
198196 }
199197
200198 /// Returns the underlying type of this `global`.
201- pub fn ty ( & self ) -> & GlobalType {
202- & self . ty
199+ pub fn ty ( & self ) -> GlobalType {
200+ // The original export is coming from wasmtime_runtime itself we should
201+ // support all the types coming out of it, so assert such here.
202+ GlobalType :: from_wasmtime_global ( & self . wasmtime_export . global )
203+ . expect ( "core wasm global type should be supported" )
204+ }
205+
206+ /// Returns the value type of this `global`.
207+ pub fn val_type ( & self ) -> ValType {
208+ ValType :: from_wasmtime_type ( self . wasmtime_export . global . ty )
209+ . expect ( "core wasm type should be supported" )
210+ }
211+
212+ /// Returns the underlying mutability of this `global`.
213+ pub fn mutability ( & self ) -> Mutability {
214+ if self . wasmtime_export . global . mutability {
215+ Mutability :: Var
216+ } else {
217+ Mutability :: Const
218+ }
203219 }
204220
205221 /// Returns the current [`Val`] of this global.
206222 pub fn get ( & self ) -> Val {
207223 unsafe {
208224 let definition = & mut * self . wasmtime_export . definition ;
209- match self . ty ( ) . content ( ) {
225+ match self . val_type ( ) {
210226 ValType :: I32 => Val :: from ( * definition. as_i32 ( ) ) ,
211227 ValType :: I64 => Val :: from ( * definition. as_i64 ( ) ) ,
212228 ValType :: F32 => Val :: F32 ( * definition. as_u32 ( ) ) ,
213229 ValType :: F64 => Val :: F64 ( * definition. as_u64 ( ) ) ,
214- _ => unimplemented ! ( "Global::get for {:?}" , self . ty ( ) . content ( ) ) ,
230+ ty => unimplemented ! ( "Global::get for {:?}" , ty ) ,
215231 }
216232 }
217233 }
@@ -223,15 +239,12 @@ impl Global {
223239 /// Returns an error if this global has a different type than `Val`, or if
224240 /// it's not a mutable global.
225241 pub fn set ( & self , val : Val ) -> Result < ( ) > {
226- if self . ty ( ) . mutability ( ) != Mutability :: Var {
242+ if self . mutability ( ) != Mutability :: Var {
227243 bail ! ( "immutable global cannot be set" ) ;
228244 }
229- if val. ty ( ) != * self . ty ( ) . content ( ) {
230- bail ! (
231- "global of type {:?} cannot be set to {:?}" ,
232- self . ty( ) . content( ) ,
233- val. ty( )
234- ) ;
245+ let ty = self . val_type ( ) ;
246+ if val. ty ( ) != ty {
247+ bail ! ( "global of type {:?} cannot be set to {:?}" , ty, val. ty( ) ) ;
235248 }
236249 if !val. comes_from_same_store ( & self . store ) {
237250 bail ! ( "cross-`Store` values are not supported" ) ;
@@ -254,13 +267,8 @@ impl Global {
254267 store : & Store ,
255268 wasmtime_handle : InstanceHandle ,
256269 ) -> Global {
257- // The original export is coming from wasmtime_runtime itself we should
258- // support all the types coming out of it, so assert such here.
259- let ty = GlobalType :: from_wasmtime_global ( & wasmtime_export. global )
260- . expect ( "core wasm global type should be supported" ) ;
261270 Global {
262271 store : store. clone ( ) ,
263- ty : ty,
264272 wasmtime_export,
265273 wasmtime_handle,
266274 }
@@ -285,7 +293,6 @@ impl Global {
285293#[ derive( Clone ) ]
286294pub struct Table {
287295 store : Store ,
288- ty : TableType ,
289296 wasmtime_handle : InstanceHandle ,
290297 wasmtime_export : wasmtime_runtime:: ExportTable ,
291298}
@@ -326,16 +333,15 @@ impl Table {
326333
327334 Ok ( Table {
328335 store : store. clone ( ) ,
329- ty,
330336 wasmtime_handle,
331337 wasmtime_export,
332338 } )
333339 }
334340
335341 /// Returns the underlying type of this table, including its element type as
336342 /// well as the maximum/minimum lower bounds.
337- pub fn ty ( & self ) -> & TableType {
338- & self . ty
343+ pub fn ty ( & self ) -> TableType {
344+ TableType :: from_wasmtime_table ( & self . wasmtime_export . table . table )
339345 }
340346
341347 fn wasmtime_table_index ( & self ) -> wasm:: DefinedTableIndex {
@@ -368,7 +374,7 @@ impl Table {
368374
369375 /// Returns the current size of this table.
370376 pub fn size ( & self ) -> u32 {
371- unsafe { ( & * self . wasmtime_export . definition ) . current_elements }
377+ unsafe { ( * self . wasmtime_export . definition ) . current_elements }
372378 }
373379
374380 /// Grows the size of this table by `delta` more elements, initialization
@@ -432,10 +438,8 @@ impl Table {
432438 store : & Store ,
433439 wasmtime_handle : wasmtime_runtime:: InstanceHandle ,
434440 ) -> Table {
435- let ty = TableType :: from_wasmtime_table ( & wasmtime_export. table . table ) ;
436441 Table {
437442 store : store. clone ( ) ,
438- ty,
439443 wasmtime_handle,
440444 wasmtime_export,
441445 }
@@ -651,7 +655,6 @@ impl Table {
651655#[ derive( Clone ) ]
652656pub struct Memory {
653657 store : Store ,
654- ty : MemoryType ,
655658 wasmtime_handle : InstanceHandle ,
656659 wasmtime_export : wasmtime_runtime:: ExportMemory ,
657660}
@@ -684,7 +687,6 @@ impl Memory {
684687 generate_memory_export ( store, & ty) . expect ( "generated memory" ) ;
685688 Memory {
686689 store : store. clone ( ) ,
687- ty,
688690 wasmtime_handle,
689691 wasmtime_export,
690692 }
@@ -700,14 +702,14 @@ impl Memory {
700702 /// let store = Store::default();
701703 /// let module = Module::new(&store, "(module (memory (export \"mem\") 1))")?;
702704 /// let instance = Instance::new(&module, &[])?;
703- /// let memory = instance.get_export ("mem").unwrap().memory( ).unwrap();
705+ /// let memory = instance.get_memory ("mem").unwrap();
704706 /// let ty = memory.ty();
705707 /// assert_eq!(ty.limits().min(), 1);
706708 /// # Ok(())
707709 /// # }
708710 /// ```
709- pub fn ty ( & self ) -> & MemoryType {
710- & self . ty
711+ pub fn ty ( & self ) -> MemoryType {
712+ MemoryType :: from_wasmtime_memory ( & self . wasmtime_export . memory . memory )
711713 }
712714
713715 /// Returns this memory as a slice view that can be read natively in Rust.
@@ -812,7 +814,7 @@ impl Memory {
812814 /// let store = Store::default();
813815 /// let module = Module::new(&store, "(module (memory (export \"mem\") 1 2))")?;
814816 /// let instance = Instance::new(&module, &[])?;
815- /// let memory = instance.get_export ("mem").unwrap().memory( ).unwrap();
817+ /// let memory = instance.get_memory ("mem").unwrap();
816818 ///
817819 /// assert_eq!(memory.size(), 1);
818820 /// assert_eq!(memory.grow(1)?, 1);
@@ -838,10 +840,8 @@ impl Memory {
838840 store : & Store ,
839841 wasmtime_handle : wasmtime_runtime:: InstanceHandle ,
840842 ) -> Memory {
841- let ty = MemoryType :: from_wasmtime_memory ( & wasmtime_export. memory . memory ) ;
842843 Memory {
843844 store : store. clone ( ) ,
844- ty : ty,
845845 wasmtime_handle,
846846 wasmtime_export,
847847 }
@@ -890,3 +890,66 @@ pub unsafe trait MemoryCreator: Send + Sync {
890890 /// Create new LinearMemory
891891 fn new_memory ( & self , ty : MemoryType ) -> Result < Box < dyn LinearMemory > , String > ;
892892}
893+
894+ // Exports
895+
896+ /// An exported WebAssembly value.
897+ ///
898+ /// This type is primarily accessed from the
899+ /// [`Instance::exports`](crate::Instance::exports) accessor and describes what
900+ /// names and items are exported from a wasm instance.
901+ #[ derive( Clone ) ]
902+ pub struct Export < ' instance > {
903+ /// The name of the export.
904+ name : & ' instance str ,
905+
906+ /// The definition of the export.
907+ definition : Extern ,
908+ }
909+
910+ impl < ' instance > Export < ' instance > {
911+ /// Creates a new export which is exported with the given `name` and has the
912+ /// given `definition`.
913+ pub ( crate ) fn new ( name : & ' instance str , definition : Extern ) -> Export < ' instance > {
914+ Export { name, definition }
915+ }
916+
917+ /// Returns the name by which this export is known.
918+ pub fn name ( & self ) -> & ' instance str {
919+ self . name
920+ }
921+
922+ /// Return the `ExternType` of this export.
923+ pub fn ty ( & self ) -> ExternType {
924+ self . definition . ty ( )
925+ }
926+
927+ /// Consume this `Export` and return the contained `Extern`.
928+ pub fn into_extern ( self ) -> Extern {
929+ self . definition
930+ }
931+
932+ /// Consume this `Export` and return the contained `Func`, if it's a function,
933+ /// or `None` otherwise.
934+ pub fn into_func ( self ) -> Option < Func > {
935+ self . definition . into_func ( )
936+ }
937+
938+ /// Consume this `Export` and return the contained `Table`, if it's a table,
939+ /// or `None` otherwise.
940+ pub fn into_table ( self ) -> Option < Table > {
941+ self . definition . into_table ( )
942+ }
943+
944+ /// Consume this `Export` and return the contained `Memory`, if it's a memory,
945+ /// or `None` otherwise.
946+ pub fn into_memory ( self ) -> Option < Memory > {
947+ self . definition . into_memory ( )
948+ }
949+
950+ /// Consume this `Export` and return the contained `Global`, if it's a global,
951+ /// or `None` otherwise.
952+ pub fn into_global ( self ) -> Option < Global > {
953+ self . definition . into_global ( )
954+ }
955+ }
0 commit comments