|
1 | | -#if canImport(Testing) |
2 | | - import WAT |
3 | | - import Testing |
| 1 | +import WAT |
| 2 | +import Testing |
4 | 3 |
|
5 | | - @testable import WasmKit |
| 4 | +@testable import WasmKit |
6 | 5 |
|
7 | | - @Suite |
8 | | - struct ExecutionTests { |
| 6 | +@Suite |
| 7 | +struct ExecutionTests { |
9 | 8 |
|
10 | | - @Test |
11 | | - func dropWithRelinkingOptimization() throws { |
12 | | - let module = try parseWasm( |
13 | | - bytes: wat2wasm( |
14 | | - """ |
15 | | - (module |
16 | | - (func (export "_start") (result i32) (local $x i32) |
17 | | - (i32.const 42) |
18 | | - (i32.const 0) |
19 | | - (i32.eqz) |
20 | | - (drop) |
21 | | - (local.set $x) |
22 | | - (local.get $x) |
23 | | - ) |
| 9 | + @Test |
| 10 | + func dropWithRelinkingOptimization() throws { |
| 11 | + let module = try parseWasm( |
| 12 | + bytes: wat2wasm( |
| 13 | + """ |
| 14 | + (module |
| 15 | + (func (export "_start") (result i32) (local $x i32) |
| 16 | + (i32.const 42) |
| 17 | + (i32.const 0) |
| 18 | + (i32.eqz) |
| 19 | + (drop) |
| 20 | + (local.set $x) |
| 21 | + (local.get $x) |
24 | 22 | ) |
25 | | - """ |
26 | 23 | ) |
| 24 | + """ |
27 | 25 | ) |
28 | | - let engine = Engine() |
29 | | - let store = Store(engine: engine) |
30 | | - let instance = try module.instantiate(store: store) |
31 | | - let _start = try #require(instance.exports[function: "_start"]) |
32 | | - let results = try _start() |
33 | | - #expect(results == [.i32(42)]) |
34 | | - } |
| 26 | + ) |
| 27 | + let engine = Engine() |
| 28 | + let store = Store(engine: engine) |
| 29 | + let instance = try module.instantiate(store: store) |
| 30 | + let _start = try #require(instance.exports[function: "_start"]) |
| 31 | + let results = try _start() |
| 32 | + #expect(results == [.i32(42)]) |
| 33 | + } |
35 | 34 |
|
36 | | - @Test |
37 | | - func updateCurrentMemoryCacheOnGrow() throws { |
38 | | - let module = try parseWasm( |
39 | | - bytes: wat2wasm( |
40 | | - """ |
41 | | - (module |
42 | | - (memory 0) |
43 | | - (func (export "_start") (result i32) |
44 | | - (drop (memory.grow (i32.const 1))) |
45 | | - (i32.store (i32.const 1) (i32.const 42)) |
46 | | - (i32.load (i32.const 1)) |
47 | | - ) |
| 35 | + @Test |
| 36 | + func updateCurrentMemoryCacheOnGrow() throws { |
| 37 | + let module = try parseWasm( |
| 38 | + bytes: wat2wasm( |
| 39 | + """ |
| 40 | + (module |
| 41 | + (memory 0) |
| 42 | + (func (export "_start") (result i32) |
| 43 | + (drop (memory.grow (i32.const 1))) |
| 44 | + (i32.store (i32.const 1) (i32.const 42)) |
| 45 | + (i32.load (i32.const 1)) |
48 | 46 | ) |
49 | | - """ |
50 | 47 | ) |
| 48 | + """ |
51 | 49 | ) |
52 | | - let engine = Engine() |
53 | | - let store = Store(engine: engine) |
54 | | - let instance = try module.instantiate(store: store) |
55 | | - let _start = try #require(instance.exports[function: "_start"]) |
56 | | - let results = try _start() |
57 | | - #expect(results == [.i32(42)]) |
58 | | - } |
| 50 | + ) |
| 51 | + let engine = Engine() |
| 52 | + let store = Store(engine: engine) |
| 53 | + let instance = try module.instantiate(store: store) |
| 54 | + let _start = try #require(instance.exports[function: "_start"]) |
| 55 | + let results = try _start() |
| 56 | + #expect(results == [.i32(42)]) |
| 57 | + } |
59 | 58 |
|
60 | | - func expectTrap(_ wat: String, assertTrap: (Trap) throws -> Void) throws { |
61 | | - let module = try parseWasm( |
62 | | - bytes: wat2wasm(wat, options: EncodeOptions(nameSection: true)) |
63 | | - ) |
| 59 | + func expectTrap(_ wat: String, assertTrap: (Trap) throws -> Void) throws { |
| 60 | + let module = try parseWasm( |
| 61 | + bytes: wat2wasm(wat, options: EncodeOptions(nameSection: true)) |
| 62 | + ) |
64 | 63 |
|
65 | | - let engine = Engine() |
66 | | - let store = Store(engine: engine) |
67 | | - var imports = Imports() |
68 | | - for importEntry in module.imports { |
69 | | - guard case .function(let type) = importEntry.descriptor else { continue } |
70 | | - let function = try Function( |
71 | | - store: store, |
72 | | - type: module.resolveFunctionType(type), |
73 | | - body: { _, _ in |
74 | | - return [] |
75 | | - } |
76 | | - ) |
77 | | - imports.define(importEntry, .function(function)) |
78 | | - } |
79 | | - let instance = try module.instantiate(store: store, imports: imports) |
80 | | - let _start = try #require(instance.exports[function: "_start"]) |
| 64 | + let engine = Engine() |
| 65 | + let store = Store(engine: engine) |
| 66 | + var imports = Imports() |
| 67 | + for importEntry in module.imports { |
| 68 | + guard case .function(let type) = importEntry.descriptor else { continue } |
| 69 | + let function = try Function( |
| 70 | + store: store, |
| 71 | + type: module.resolveFunctionType(type), |
| 72 | + body: { _, _ in |
| 73 | + return [] |
| 74 | + } |
| 75 | + ) |
| 76 | + imports.define(importEntry, .function(function)) |
| 77 | + } |
| 78 | + let instance = try module.instantiate(store: store, imports: imports) |
| 79 | + let _start = try #require(instance.exports[function: "_start"]) |
81 | 80 |
|
82 | | - let trap: Trap |
83 | | - do { |
84 | | - let _ = try _start() |
85 | | - #expect((false), "Expected trap") |
86 | | - return |
87 | | - } catch let _trap as Trap { |
88 | | - trap = _trap |
89 | | - } catch { |
90 | | - #expect((false), "Expected trap: \(error)") |
91 | | - return |
92 | | - } |
93 | | - try assertTrap(trap) |
| 81 | + let trap: Trap |
| 82 | + do { |
| 83 | + let _ = try _start() |
| 84 | + #expect((false), "Expected trap") |
| 85 | + return |
| 86 | + } catch let _trap as Trap { |
| 87 | + trap = _trap |
| 88 | + } catch { |
| 89 | + #expect((false), "Expected trap: \(error)") |
| 90 | + return |
94 | 91 | } |
| 92 | + try assertTrap(trap) |
| 93 | + } |
95 | 94 |
|
96 | | - @Test |
97 | | - func backtraceBasic() throws { |
98 | | - try expectTrap( |
99 | | - """ |
100 | | - (module |
101 | | - (func $foo |
102 | | - unreachable |
103 | | - ) |
104 | | - (func $bar |
105 | | - (call $foo) |
106 | | - ) |
107 | | - (func (export "_start") |
108 | | - (call $bar) |
109 | | - ) |
| 95 | + @Test |
| 96 | + func backtraceBasic() throws { |
| 97 | + try expectTrap( |
| 98 | + """ |
| 99 | + (module |
| 100 | + (func $foo |
| 101 | + unreachable |
110 | 102 | ) |
111 | | - """ |
112 | | - ) { trap in |
113 | | - #expect( |
114 | | - trap.backtrace?.symbols.compactMap(\.?.name) == [ |
115 | | - "foo", |
116 | | - "bar", |
117 | | - "_start", |
118 | | - ]) |
119 | | - } |
| 103 | + (func $bar |
| 104 | + (call $foo) |
| 105 | + ) |
| 106 | + (func (export "_start") |
| 107 | + (call $bar) |
| 108 | + ) |
| 109 | + ) |
| 110 | + """ |
| 111 | + ) { trap in |
| 112 | + #expect( |
| 113 | + trap.backtrace?.symbols.compactMap(\.?.name) == [ |
| 114 | + "foo", |
| 115 | + "bar", |
| 116 | + "_start", |
| 117 | + ]) |
120 | 118 | } |
| 119 | + } |
121 | 120 |
|
122 | | - @Test |
123 | | - func backtraceWithImports() throws { |
124 | | - try expectTrap( |
125 | | - """ |
126 | | - (module |
127 | | - (func (import "env" "bar")) |
128 | | - (func |
129 | | - unreachable |
130 | | - ) |
131 | | - (func $bar |
132 | | - (call 1) |
133 | | - ) |
134 | | - (func (export "_start") |
135 | | - (call $bar) |
136 | | - ) |
| 121 | + @Test |
| 122 | + func backtraceWithImports() throws { |
| 123 | + try expectTrap( |
| 124 | + """ |
| 125 | + (module |
| 126 | + (func (import "env" "bar")) |
| 127 | + (func |
| 128 | + unreachable |
137 | 129 | ) |
138 | | - """ |
139 | | - ) { trap in |
140 | | - #expect( |
141 | | - trap.backtrace?.symbols.compactMap(\.?.name) == [ |
142 | | - "wasm function[1]", |
143 | | - "bar", |
144 | | - "_start", |
145 | | - ]) |
146 | | - } |
| 130 | + (func $bar |
| 131 | + (call 1) |
| 132 | + ) |
| 133 | + (func (export "_start") |
| 134 | + (call $bar) |
| 135 | + ) |
| 136 | + ) |
| 137 | + """ |
| 138 | + ) { trap in |
| 139 | + #expect( |
| 140 | + trap.backtrace?.symbols.compactMap(\.?.name) == [ |
| 141 | + "wasm function[1]", |
| 142 | + "bar", |
| 143 | + "_start", |
| 144 | + ]) |
147 | 145 | } |
148 | 146 | } |
149 | | - |
150 | | -#endif |
| 147 | +} |
0 commit comments