@@ -87,15 +87,21 @@ extension Runtime {
8787 . numericConst( . i32( UInt32 ( element. initializer. count) ) ) ,
8888 . tableInit( tableIndex, elementIndex) ,
8989 . tableElementDrop( elementIndex) ,
90+ . endOfFunction,
9091 ] )
91- let initIseq = InstructionSequence ( instructions: instructions)
92- defer { initIseq. deallocate ( ) }
93- try evaluateConstExpr ( initIseq, instance: instance)
92+ try instructions. withUnsafeBufferPointer {
93+ let initIseq = InstructionSequence ( instructions: $0, maxStackHeight: 2 )
94+ try evaluateConstExpr ( initIseq, instance: instance)
95+ }
9496
9597 case . declarative:
96- let initIseq : InstructionSequence = [ . tableElementDrop( elementIndex) ]
97- defer { initIseq. deallocate ( ) }
98- try evaluateConstExpr ( initIseq, instance: instance)
98+ let instructions : [ Instruction ] = [ . tableElementDrop( elementIndex) , . endOfFunction]
99+ try instructions. withUnsafeBufferPointer {
100+ let initIseq = InstructionSequence (
101+ instructions: $0, maxStackHeight: 0
102+ )
103+ try evaluateConstExpr ( initIseq, instance: instance)
104+ }
99105
100106 case . passive:
101107 continue
@@ -122,14 +128,17 @@ extension Runtime {
122128 default :
123129 throw InstantiationError . unsupported ( " init expr in data section \( data. offset) " )
124130 }
125- let iseq = InstructionSequence ( instructions: instructions + [
131+ instructions. append ( contentsOf : [
126132 . numericConst( . i32( 0 ) ) ,
127133 . numericConst( . i32( UInt32 ( data. initializer. count) ) ) ,
128134 . memoryInit( UInt32 ( dataIndex) ) ,
129135 . memoryDataDrop( UInt32 ( dataIndex) ) ,
136+ . endOfFunction,
130137 ] )
131- defer { iseq. deallocate ( ) }
132- try evaluateConstExpr ( iseq, instance: instance)
138+ try instructions. withUnsafeBufferPointer {
139+ let iseq = InstructionSequence ( instructions: $0, maxStackHeight: 2 )
140+ try evaluateConstExpr ( iseq, instance: instance)
141+ }
133142 }
134143 } catch Trap . outOfBoundsMemoryAccess {
135144 throw InstantiationError . outOfBoundsMemoryAccess
@@ -141,6 +150,7 @@ extension Runtime {
141150 if let startIndex = module. start {
142151 try withExecution { initExecution in
143152 var stack = Stack ( )
153+ defer { stack. deallocate ( ) }
144154 try initExecution. invoke ( functionAddress: instance. functionAddresses [ Int ( startIndex) ] , runtime: self , stack: & stack)
145155 try initExecution. run ( runtime: self , stack: & stack)
146156 }
@@ -189,10 +199,12 @@ extension Runtime {
189199 default :
190200 throw InstantiationError . unsupported ( " init expr in global section \( global. initializer) " )
191201 }
192- let iseq = InstructionSequence ( instructions: instructions)
193- defer { iseq. deallocate ( ) }
194- return try evaluateConstExpr ( iseq, instance: globalModuleInstance, arity: 1 ) { _, stack in
195- return stack. popValue ( )
202+ instructions. append ( . endOfFunction)
203+ return try instructions. withUnsafeBufferPointer {
204+ let iseq = InstructionSequence ( instructions: $0, maxStackHeight: 1 )
205+ return try evaluateConstExpr ( iseq, instance: globalModuleInstance, arity: 1 ) { _, stack in
206+ return stack. popValue ( )
207+ }
196208 }
197209 }
198210
@@ -212,6 +224,7 @@ extension Runtime {
212224 ) throws -> T {
213225 try withExecution { initExecution in
214226 var stack = Stack ( )
227+ defer { stack. deallocate ( ) }
215228 try stack. pushFrame (
216229 iseq: iseq,
217230 arity: arity,
0 commit comments