@@ -10,14 +10,22 @@ public typealias ExternAddress = Int
10
10
11
11
/// A collection of globals and functions that are exported from a host module.
12
12
public struct HostModule {
13
- public init ( globals: [ String : GlobalInstance ] = [ : ] , functions: [ String : HostFunction ] = [ : ] ) {
13
+ public init (
14
+ globals: [ String : GlobalInstance ] = [ : ] ,
15
+ memories: [ String : MemoryAddress ] = [ : ] ,
16
+ functions: [ String : HostFunction ] = [ : ]
17
+ ) {
14
18
self . globals = globals
19
+ self . memories = memories
15
20
self . functions = functions
16
21
}
17
22
18
23
/// Names of globals exported by this module mapped to corresponding global instances.
19
24
public let globals : [ String : GlobalInstance ]
20
25
26
+ /// Names of memories exported by this module mapped to corresponding addresses of memory instances.
27
+ public let memories : [ String : MemoryAddress ]
28
+
21
29
/// Names of functions exported by this module mapped to corresponding host functions.
22
30
public let functions : [ String : HostFunction ]
23
31
}
@@ -45,19 +53,7 @@ public final class Store {
45
53
46
54
init ( _ hostModules: [ String : HostModule ] ) {
47
55
for (moduleName, hostModule) in hostModules {
48
- var moduleExports = ModuleInstance . Exports ( )
49
-
50
- for (globalName, global) in hostModule. globals {
51
- moduleExports [ globalName] = . global( - hostGlobals. count - 1 )
52
- hostGlobals. append ( global)
53
- }
54
-
55
- for (functionName, function) in hostModule. functions {
56
- moduleExports [ functionName] = . function( - hostFunctions. count - 1 )
57
- hostFunctions. append ( function)
58
- }
59
-
60
- availableExports [ moduleName] = moduleExports
56
+ registerUniqueHostModule ( hostModule, as: moduleName)
61
57
}
62
58
}
63
59
}
@@ -121,6 +117,40 @@ extension Store {
121
117
availableExports [ name] = moduleInstance. exports
122
118
}
123
119
120
+ /// Register the given host module in this store with the given name.
121
+ ///
122
+ /// - Parameters:
123
+ /// - hostModule: A host module to register.
124
+ /// - name: A name to register the given host module.
125
+ public func register( _ hostModule: HostModule , as name: String ) throws {
126
+ guard availableExports [ name] == nil else {
127
+ throw ImportError . moduleInstanceAlreadyRegistered ( name)
128
+ }
129
+
130
+ registerUniqueHostModule ( hostModule, as: name)
131
+ }
132
+
133
+ /// Register the given host module assuming that the given name is not registered yet.
134
+ func registerUniqueHostModule( _ hostModule: HostModule , as name: String ) {
135
+ var moduleExports = ModuleInstance . Exports ( )
136
+
137
+ for (globalName, global) in hostModule. globals {
138
+ moduleExports [ globalName] = . global( - hostGlobals. count - 1 )
139
+ hostGlobals. append ( global)
140
+ }
141
+
142
+ for (functionName, function) in hostModule. functions {
143
+ moduleExports [ functionName] = . function( - hostFunctions. count - 1 )
144
+ hostFunctions. append ( function)
145
+ }
146
+
147
+ for (memoryName, memoryAddr) in hostModule. memories {
148
+ moduleExports [ memoryName] = . memory( memoryAddr)
149
+ }
150
+
151
+ availableExports [ name] = moduleExports
152
+ }
153
+
124
154
public func memory( at address: MemoryAddress ) -> MemoryInstance {
125
155
return self . memories [ address]
126
156
}
@@ -309,7 +339,7 @@ extension Store {
309
339
310
340
/// > Note:
311
341
/// <https://webassembly.github.io/spec/core/exec/modules.html#alloc-mem>
312
- func allocate( memoryType: MemoryType ) -> MemoryAddress {
342
+ public func allocate( memoryType: MemoryType ) -> MemoryAddress {
313
343
let address = memories. count
314
344
let instance = MemoryInstance ( memoryType)
315
345
memories. append ( instance)
0 commit comments