@@ -14,12 +14,12 @@ import WASI
1414import WasmTypes
1515import SystemPackage
1616
17+ typealias WasmFunction = ( [ UInt32 ] ) throws -> [ UInt32 ]
18+
1719protocol WasmEngine {
1820 init ( path: FilePath , imports: WASIBridgeToHost ) async throws
1921
20- func customSections( named name: String ) throws -> [ ArraySlice < UInt8 > ]
21-
22- func invoke( _ method: String , _ args: [ UInt32 ] ) throws -> [ UInt32 ]
22+ func function( named name: String ) throws -> WasmFunction ?
2323}
2424
2525typealias DefaultWasmPlugin = WasmEnginePlugin < DefaultWasmEngine >
@@ -28,6 +28,7 @@ typealias DefaultWasmPlugin = WasmEnginePlugin<DefaultWasmEngine>
2828struct WasmEnginePlugin < Engine: WasmEngine > : WasmPlugin {
2929 private let hostToPlugin : FileDescriptor
3030 private let pluginToHost : FileDescriptor
31+ private let pumpFunction : WasmFunction
3132 let engine : Engine
3233
3334 init ( path: FilePath ) async throws {
@@ -42,39 +43,17 @@ struct WasmEnginePlugin<Engine: WasmEngine>: WasmPlugin {
4243 stderr: . standardError
4344 )
4445 engine = try await Engine ( path: path, imports: bridge)
45- try checkABIVersion ( )
46- _ = try engine. invoke ( " _start " , [ ] )
47- }
4846
49- private func checkABIVersion( ) throws {
50- let abiVersion = try abiVersion ( )
51- guard abiVersion == 1 else {
52- throw WasmEngineError ( message: " Wasm plugin has unsupported ABI version: \( abiVersion) " )
47+ let exportName = " swift_wasm_macro_v1_pump "
48+ guard let pump = try engine. function ( named: exportName) else {
49+ throw WasmEngineError ( message: " Wasm plugin has an unknown ABI (could not find ' \( exportName) ') " )
5350 }
54- }
51+ self . pumpFunction = pump
5552
56- private func abiVersion( ) throws -> UInt32 {
57- let sectionName = " swift_wasm_macro_abi "
58- let sections = try engine. customSections ( named: sectionName)
59- switch sections. count {
60- case 0 :
61- throw WasmEngineError ( message: " Wasm macro is missing a ' \( sectionName) ' section " )
62- case 1 :
63- break
64- default :
65- throw WasmEngineError ( message: " Wasm macro has too many ' \( sectionName) ' sections. Expected one, got \( sections. count) " )
66- }
67- let section = sections [ 0 ]
68- guard section. count == 4 else {
69- throw WasmEngineError ( message: """
70- Wasm macro has incorrect ' \( sectionName) ' section length. Expected 4 bytes, got \( section. count) .
71- """ )
72- }
73- return section. withUnsafeBufferPointer { buffer in
74- buffer. withMemoryRebound ( to: UInt32 . self) {
75- UInt32 ( littleEndian: $0. baseAddress!. pointee)
76- }
53+ guard let start = try engine. function ( named: " _start " ) else {
54+ throw WasmEngineError ( message: " Wasm plugin does not have a '_start' entrypoint " )
7755 }
56+ _ = try start ( [ ] )
7857 }
7958
8059 func handleMessage( _ json: [ UInt8 ] ) async throws -> [ UInt8 ] {
@@ -83,7 +62,7 @@ struct WasmEnginePlugin<Engine: WasmEngine>: WasmPlugin {
8362 }
8463 try hostToPlugin. writeAll ( json)
8564
86- _ = try engine . invoke ( " swift_wasm_macro_pump " , [ ] )
65+ _ = try pumpFunction ( [ ] )
8766
8867 let lengthRaw = try withUnsafeTemporaryAllocation ( of: UInt8 . self, capacity: 8 ) { buffer in
8968 let lengthCount = try pluginToHost. read ( into: UnsafeMutableRawBufferPointer ( buffer) )
0 commit comments