Skip to content

Commit 34ed5b0

Browse files
committed
use stdio for ipc
1 parent 150f3d2 commit 34ed5b0

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

tools/swift-plugin-server/Sources/swift-wasm-plugin-server/WasmEngine.swift

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import WASI
1414
import WasmTypes
15+
import SystemPackage
1516
import Foundation
1617

1718
protocol WasmEngine {
@@ -29,11 +30,16 @@ typealias DefaultWasmPlugin = WasmEnginePlugin<DefaultWasmEngine>
2930

3031
// a WasmPlugin implementation that delegates to a WasmEngine
3132
struct WasmEnginePlugin<Engine: WasmEngine>: WasmPlugin {
33+
private let hostToPlugin = Pipe()
34+
private let pluginToHost = Pipe()
3235
let engine: Engine
3336

3437
init(wasm: UnsafeByteBuffer) async throws {
35-
// TODO: we should air-gap this bridge. Wasm macros don't need IO.
36-
let bridge = try WASIBridgeToHost()
38+
let bridge = try WASIBridgeToHost(
39+
stdin: FileDescriptor(rawValue: hostToPlugin.fileHandleForReading.fileDescriptor),
40+
stdout: FileDescriptor(rawValue: pluginToHost.fileHandleForWriting.fileDescriptor),
41+
stderr: .standardError
42+
)
3743
engine = try await Engine(wasm: wasm, imports: bridge)
3844
try checkABIVersion()
3945
_ = try engine.invoke("_start", [])
@@ -47,7 +53,7 @@ struct WasmEnginePlugin<Engine: WasmEngine>: WasmPlugin {
4753
}
4854

4955
private func abiVersion() throws -> UInt32 {
50-
let sectionName = "wacro_abi"
56+
let sectionName = "swift_wasm_macro_abi"
5157
let sections = try engine.customSections(named: sectionName)
5258
switch sections.count {
5359
case 0:
@@ -71,23 +77,18 @@ struct WasmEnginePlugin<Engine: WasmEngine>: WasmPlugin {
7177
}
7278

7379
func handleMessage(_ json: Data) async throws -> Data {
74-
let memory = engine.memory
75-
76-
let jsonLen = UInt32(json.count)
77-
let inAddr = try engine.invoke("wacro_malloc", [jsonLen])[0]
78-
let rawInAddr = UnsafeGuestPointer<UInt8>(memorySpace: memory, offset: inAddr)
79-
_ = UnsafeGuestBufferPointer(baseAddress: rawInAddr, count: jsonLen)
80-
.withHostPointer { $0.initialize(from: json) }
81-
82-
let outAddr = try engine.invoke("wacro_parse", [inAddr, jsonLen])[0]
83-
let outLen = UnsafeGuestPointer<UInt32>(memorySpace: memory, offset: outAddr).pointee
84-
let outBase = UnsafeGuestPointer<UInt8>(memorySpace: memory, offset: outAddr + 4)
85-
let out = UnsafeGuestBufferPointer(baseAddress: outBase, count: outLen)
86-
.withHostPointer { Data($0) }
87-
88-
_ = try engine.invoke("wacro_free", [outAddr])
89-
90-
return out
80+
let writeHandle = hostToPlugin.fileHandleForWriting
81+
let count = withUnsafeBytes(of: UInt64(json.count).littleEndian) { Data($0) }
82+
try writeHandle.write(contentsOf: count)
83+
try writeHandle.write(contentsOf: json)
84+
85+
_ = try engine.invoke("swift_wasm_macro_pump", [])
86+
87+
let readHandle = pluginToHost.fileHandleForReading
88+
let lengthRaw = try readHandle.read(upToCount: 8) ?? Data()
89+
let length = lengthRaw.withUnsafeBytes { $0.assumingMemoryBound(to: UInt64.self).baseAddress?.pointee }
90+
guard let length else { throw WasmEngineError(message: "Bad byte length") }
91+
return try readHandle.read(upToCount: Int(length)) ?? Data()
9192
}
9293
}
9394

tools/swift-plugin-server/Sources/swift-wasm-plugin-server/swift-wasm-plugin-server.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ final class SwiftPluginServer {
5656
}
5757
}
5858
} catch {
59+
try? FileHandle.standardError.write(contentsOf: Data("Error: \(error)\n".utf8))
5960
try sendMessage(.expandMacroResult(expandedSource: nil, diagnostics: []))
6061
return
6162
}

0 commit comments

Comments
 (0)