@@ -155,8 +155,8 @@ extension CompilerPlugin {
155
155
156
156
// Open a message channel for communicating with the plugin host.
157
157
let connection = PluginHostConnection (
158
- inputStream: inputFD,
159
- outputStream: outputFD
158
+ inputStream: fdopen ( inputFD, " r " ) ,
159
+ outputStream: fdopen ( outputFD, " w " )
160
160
)
161
161
162
162
// Handle messages from the host until the input stream is closed,
@@ -181,8 +181,8 @@ extension CompilerPlugin {
181
181
}
182
182
183
183
internal struct PluginHostConnection : MessageConnection {
184
- fileprivate let inputStream : CInt
185
- fileprivate let outputStream : CInt
184
+ fileprivate let inputStream : _ss_ptr_FILE
185
+ fileprivate let outputStream : _ss_ptr_FILE
186
186
187
187
func sendMessage< TX: Encodable > ( _ message: TX ) throws {
188
188
// Encode the message as JSON.
@@ -200,6 +200,8 @@ internal struct PluginHostConnection: MessageConnection {
200
200
try payload. withUnsafeBytes { buffer in
201
201
try _write ( outputStream, contentsOf: buffer)
202
202
}
203
+
204
+ fflush ( outputStream)
203
205
}
204
206
205
207
func waitForNextMessage< RX: Decodable > ( _ ty: RX . Type ) throws -> RX ? {
@@ -237,41 +239,27 @@ private func describe(errno: CInt) -> String {
237
239
return String ( describing: errno)
238
240
}
239
241
240
- private func _write( _ fd: CInt , contentsOf buffer: UnsafeRawBufferPointer ) throws {
241
- var ptr = buffer. baseAddress!
242
- var remaining = buffer. count
243
- while remaining > 0 {
244
- switch write ( fd, ptr, numericCast ( remaining) ) {
245
- case 0 :
246
- throw CompilerPluginError ( message: " write(2) closed " )
247
- case - 1 :
248
- throw CompilerPluginError ( message: " read(2) failed: \( describe ( errno: _ss_errno ( ) ) ) " )
249
- case let result:
250
- ptr += Int ( result)
251
- remaining -= Int ( result)
252
- }
242
+ private func _write( _ stream: _ss_ptr_FILE , contentsOf buffer: UnsafeRawBufferPointer ) throws {
243
+ let result = fwrite ( buffer. baseAddress, 1 , buffer. count, stream)
244
+ if result < buffer. count {
245
+ throw CompilerPluginError ( message: " write(3) failed: \( describe ( errno: _ss_errno ( ) ) ) " )
253
246
}
254
247
}
255
248
256
- private func _reading< T> ( _ fd : CInt , count: Int , _ fn: ( UnsafeRawBufferPointer ) throws -> T ) throws -> T {
249
+ private func _reading< T> ( _ stream : _ss_ptr_FILE , count: Int , _ fn: ( UnsafeRawBufferPointer ) throws -> T ) throws -> T {
257
250
guard count > 0 else {
258
251
return try fn ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
259
252
}
260
253
let buffer = UnsafeMutableRawBufferPointer . allocate ( byteCount: count, alignment: 1 )
261
254
defer { buffer. deallocate ( ) }
262
255
263
- var ptr = buffer. baseAddress!
264
- var remaining = buffer. count
265
- while remaining > 0 {
266
- switch read ( fd, ptr, numericCast ( remaining) ) {
267
- case 0 :
256
+ let result = fread ( buffer. baseAddress, 1 , count, stream)
257
+ if result < count {
258
+ if ferror ( stream) == 0 {
268
259
// Input is closed.
269
260
return try fn ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
270
- case - 1 :
261
+ } else {
271
262
throw CompilerPluginError ( message: " read(2) failed: \( describe ( errno: _ss_errno ( ) ) ) " )
272
- case let result:
273
- ptr += Int ( result)
274
- remaining -= Int ( result)
275
263
}
276
264
}
277
265
return try fn ( UnsafeRawBufferPointer ( buffer) )
0 commit comments