@@ -82,6 +82,61 @@ struct InstanceEntity /* : ~Copyable */ {
8282
8383typealias InternalInstance = EntityHandle < InstanceEntity >
8484
85+ /// A map of exported entities by name.
86+ public struct Exports : Sequence {
87+ let store : Store
88+ let values : [ String : InternalExternalValue ]
89+
90+ /// Returns the exported entity with the given name.
91+ public subscript( _ name: String ) -> ExternalValue ? {
92+ guard let entity = values [ name] else { return nil }
93+ return ExternalValue ( handle: entity, store: store)
94+ }
95+
96+ /// Returns the exported function with the given name.
97+ public subscript( function name: String ) -> Function ? {
98+ guard case . function( let function) = self [ name] else { return nil }
99+ return function
100+ }
101+
102+ /// Returns the exported table with the given name.
103+ public subscript( table name: String ) -> Table ? {
104+ guard case . table( let table) = self [ name] else { return nil }
105+ return table
106+ }
107+
108+ /// Returns the exported memory with the given name.
109+ public subscript( memory name: String ) -> Memory ? {
110+ guard case . memory( let memory) = self [ name] else { return nil }
111+ return memory
112+ }
113+
114+ /// Returns the exported global with the given name.
115+ public subscript( global name: String ) -> Global ? {
116+ guard case . global( let global) = self [ name] else { return nil }
117+ return global
118+ }
119+
120+ public struct Iterator : IteratorProtocol {
121+ private let store : Store
122+ private var iterator : Dictionary < String , InternalExternalValue > . Iterator
123+
124+ init ( parent: Exports ) {
125+ self . store = parent. store
126+ self . iterator = parent. values. makeIterator ( )
127+ }
128+
129+ public mutating func next( ) -> ( String , ExternalValue ) ? {
130+ guard let ( name, entity) = iterator. next ( ) else { return nil }
131+ return ( name, ExternalValue ( handle: entity, store: store) )
132+ }
133+ }
134+
135+ public func makeIterator( ) -> Iterator {
136+ Iterator ( parent: self )
137+ }
138+ }
139+
85140/// A stateful instance of a WebAssembly module.
86141/// Usually instantiated by ``Runtime/instantiate(module:)``.
87142/// > Note:
@@ -113,11 +168,9 @@ public struct Instance {
113168 return function
114169 }
115170
116- public typealias Exports = [ String : ExternalValue ]
117-
118171 /// A dictionary of exported entities by name.
119172 public var exports : Exports {
120- handle . exports . mapValues { ExternalValue ( handle : $0 , store : store ) }
173+ Exports ( store : store , values : handle . exports )
121174 }
122175
123176 /// Dumps the textual representation of all functions in the instance.
0 commit comments