@@ -360,11 +360,16 @@ extension StoreAllocator {
360360 allocateHandle: { m, _ in try allocate ( memoryType: m, resourceLimiter: resourceLimiter) }
361361 )
362362
363+ var functionRefs : Set < InternalFunction > = [ ]
363364 // Step 5.
364365 let constEvalContext = ConstEvaluationContext (
365366 functions: functions,
366- globals: importedGlobals. map ( \. value)
367+ globals: importedGlobals. map ( \. value) ,
368+ onFunctionReferenced: { function in
369+ functionRefs. insert ( function)
370+ }
367371 )
372+
368373 let globals = try allocateEntities (
369374 imports: importedGlobals,
370375 internals: module. globals,
@@ -379,13 +384,13 @@ extension StoreAllocator {
379384 // Step 6.
380385 let elements = try ImmutableArray < InternalElementSegment > ( allocator: arrayAllocator, count: module. elements. count) { buffer in
381386 for (index, element) in module. elements. enumerated ( ) {
382- let references : [ Reference ]
387+ // TODO: Avoid evaluating element expr twice in `Module.instantiate` and here.
388+ var references = try element. evaluateInits ( context: constEvalContext)
383389 switch element. mode {
384390 case . active, . declarative:
385391 // active & declarative segments are unavailable at runtime
386392 references = [ ]
387- case . passive:
388- references = try element. evaluateInits ( context: constEvalContext)
393+ case . passive: break
389394 }
390395 let handle = allocate ( elementType: element. type, references: references)
391396 buffer. initializeElement ( at: index, to: handle)
@@ -449,6 +454,7 @@ extension StoreAllocator {
449454 elementSegments: elements,
450455 dataSegments: dataSegments,
451456 exports: exports,
457+ functionRefs: functionRefs,
452458 features: module. features,
453459 hasDataCount: module. hasDataCount
454460 )
0 commit comments