@@ -241,82 +241,3 @@ public struct HostModule {
241241 /// Names of functions exported by this module mapped to corresponding host functions.
242242 public var functions : [ String : HostFunction ]
243243}
244-
245- protocol ConstEvaluationContextProtocol {
246- func functionRef( _ index: FunctionIndex ) throws -> Reference
247- func globalValue( _ index: GlobalIndex ) throws -> Value
248- }
249-
250- extension InternalInstance : ConstEvaluationContextProtocol {
251- func functionRef( _ index: FunctionIndex ) throws -> Reference {
252- return try . function( from: self . functions [ validating: Int ( index) ] )
253- }
254- func globalValue( _ index: GlobalIndex ) throws -> Value {
255- return try self . globals [ validating: Int ( index) ] . value
256- }
257- }
258-
259- struct ConstEvaluationContext : ConstEvaluationContextProtocol {
260- let functions : ImmutableArray < InternalFunction >
261- var globals : [ Value ]
262- func functionRef( _ index: FunctionIndex ) throws -> Reference {
263- return try . function( from: self . functions [ validating: Int ( index) ] )
264- }
265- func globalValue( _ index: GlobalIndex ) throws -> Value {
266- guard index < globals. count else {
267- throw GlobalEntity . createOutOfBoundsError ( index: Int ( index) , count: globals. count)
268- }
269- return self . globals [ Int ( index) ]
270- }
271- }
272-
273- extension ConstExpression {
274- func evaluate< C: ConstEvaluationContextProtocol > ( context: C ) throws -> Value {
275- guard self . last == . end, self . count == 2 else {
276- throw InstantiationError . unsupported ( " Expect `end` at the end of offset expression " )
277- }
278- let constInst = self [ 0 ]
279- switch constInst {
280- case . i32Const( let value) : return . i32( UInt32 ( bitPattern: value) )
281- case . i64Const( let value) : return . i64( UInt64 ( bitPattern: value) )
282- case . f32Const( let value) : return . f32( value. bitPattern)
283- case . f64Const( let value) : return . f64( value. bitPattern)
284- case . globalGet( let globalIndex) :
285- return try context. globalValue ( globalIndex)
286- case . refNull( let type) :
287- switch type {
288- case . externRef: return . ref( . extern( nil ) )
289- case . funcRef: return . ref( . function( nil ) )
290- }
291- case . refFunc( let functionIndex) :
292- return try . ref( context. functionRef ( functionIndex) )
293- default :
294- throw InstantiationError . unsupported ( " illegal const expression instruction: \( constInst) " )
295- }
296- }
297- }
298-
299- extension WasmParser . ElementSegment {
300- func evaluateInits< C: ConstEvaluationContextProtocol > ( context: C ) throws -> [ Reference ] {
301- try self . initializer. map { expression -> Reference in
302- switch expression [ 0 ] {
303- case let . refFunc( index) :
304- return try context. functionRef ( index)
305- case . refNull( . funcRef) :
306- return . function( nil )
307- case . refNull( . externRef) :
308- return . extern( nil )
309- case . globalGet( let index) :
310- let value = try context. globalValue ( index)
311- switch value {
312- case . ref( . function( let addr) ) :
313- return . function( addr)
314- default :
315- throw Trap . _raw ( " Unexpected global value type for element initializer expression " )
316- }
317- default :
318- throw Trap . _raw ( " Unexpected element initializer expression: \( expression) " )
319- }
320- }
321- }
322- }
0 commit comments