@@ -52,31 +52,26 @@ final class ExecutionTests: XCTestCase {
5252 XCTAssertEqual ( results, [ . i32( 42 ) ] )
5353 }
5454
55- func testBacktrace ( ) throws {
55+ func expectTrap ( _ wat : String , assertTrap : ( Trap ) throws -> Void ) throws {
5656 let module = try parseWasm (
57- bytes: wat2wasm (
58- """
59- (module
60- (memory (export " memory " ) 1)
61- (func $foo (result i32)
62- unreachable
63- )
64- (func $bar (result i32)
65- (call $foo)
66- )
67- (func (export " _start " )
68- (call $bar)
69- (drop)
70- )
71- )
72- """ ,
73- options: EncodeOptions ( nameSection: true )
74- )
57+ bytes: wat2wasm ( wat, options: EncodeOptions ( nameSection: true ) )
7558 )
7659
7760 let engine = Engine ( )
7861 let store = Store ( engine: engine)
79- let instance = try module. instantiate ( store: store)
62+ var imports = Imports ( )
63+ for importEntry in module. imports {
64+ guard case . function( let type) = importEntry. descriptor else { continue }
65+ let function = try Function (
66+ store: store,
67+ type: module. resolveFunctionType ( type) ,
68+ body: { _, _ in
69+ return [ ]
70+ }
71+ )
72+ imports. define ( importEntry, . function( function) )
73+ }
74+ let instance = try module. instantiate ( store: store, imports: imports)
8075 let _start = try XCTUnwrap ( instance. exports [ function: " _start " ] )
8176
8277 let trap : Trap
@@ -87,13 +82,59 @@ final class ExecutionTests: XCTestCase {
8782 } catch let error {
8883 trap = try XCTUnwrap ( error as? Trap )
8984 }
85+ try assertTrap ( trap)
86+ }
9087
91- XCTAssertEqual (
92- trap. backtrace? . symbols. compactMap ( \.?. name) ,
93- [
94- " foo " ,
95- " bar " ,
96- " _start " ,
97- ] )
88+ func testBacktraceBasic( ) throws {
89+ try expectTrap (
90+ """
91+ (module
92+ (func $foo
93+ unreachable
94+ )
95+ (func $bar
96+ (call $foo)
97+ )
98+ (func (export " _start " )
99+ (call $bar)
100+ )
101+ )
102+ """
103+ ) { trap in
104+ XCTAssertEqual (
105+ trap. backtrace? . symbols. compactMap ( \.?. name) ,
106+ [
107+ " foo " ,
108+ " bar " ,
109+ " _start " ,
110+ ] )
111+ }
112+ }
113+
114+ func testBacktraceWithImports( ) throws {
115+ try expectTrap (
116+ """
117+ (module
118+ (func (import " env " " bar " ))
119+ (func
120+ unreachable
121+ )
122+ (func $bar
123+ (call 1)
124+ )
125+ (func (export " _start " )
126+ (call $bar)
127+ )
128+ )
129+ """
130+ ) { trap in
131+ XCTAssertEqual (
132+ trap. backtrace? . symbols. compactMap ( \.?. name) ,
133+ [
134+ " wasm function[1] " ,
135+ " bar " ,
136+ " _start " ,
137+ ] )
138+ }
98139 }
99140}
0 commit comments