@@ -10,21 +10,27 @@ final class WasmInterceptingMessageHandler<Base: PluginMessageHandler>: PluginMe
1010 self . base = base
1111 }
1212
13+ /// Handle the message ourselves if it references a Wasm plugin.
14+ /// Otherwise, forward it to `base`.
1315 func handleMessage( _ message: HostToPluginMessage ) -> PluginToHostMessage {
1416 switch message {
1517 case . loadPluginLibrary( let libraryPath, let moduleName) :
1618 guard libraryPath. hasSuffix ( " .wasm " ) else { break }
19+ let libraryFilePath = FilePath ( libraryPath)
1720 do {
18- loadedWasmPlugins [ moduleName] = try defaultWasmPlugin. init ( path: FilePath ( libraryPath ) )
21+ loadedWasmPlugins [ moduleName] = try defaultWasmPlugin. init ( path: libraryFilePath )
1922 } catch {
20- printError ( " Error: \( error) " )
21- return . loadPluginLibraryResult( loaded: false , diagnostics: [ ] )
23+ return . loadPluginLibraryResult(
24+ loaded: false ,
25+ diagnostics: [ PluginMessage . Diagnostic ( errorMessage: " \( error) " ) ]
26+ )
2227 }
2328 return . loadPluginLibraryResult( loaded: true , diagnostics: [ ] )
2429 case . expandAttachedMacro( let macro, _, _, _, _, _, _, _, _) ,
2530 . expandFreestandingMacro( let macro, _, _, _, _) :
26- guard let wasmPlugin = loadedWasmPlugins [ macro. moduleName] else { break }
27- return expandMacro ( plugin: wasmPlugin, message: message)
31+ if let response = expandMacro ( macro, message: message) {
32+ return response
33+ } // else break
2834 case . getCapability:
2935 break
3036#if !SWIFT_PACKAGE
@@ -36,9 +42,10 @@ final class WasmInterceptingMessageHandler<Base: PluginMessageHandler>: PluginMe
3642 }
3743
3844 private func expandMacro(
39- plugin : WasmPlugin ,
45+ _ macro : PluginMessage . MacroReference ,
4046 message: HostToPluginMessage
41- ) -> PluginToHostMessage {
47+ ) -> PluginToHostMessage ? {
48+ guard let plugin = loadedWasmPlugins [ macro. moduleName] else { return nil }
4249 do {
4350 let request = try JSON . encode ( message)
4451 let responseRaw = try plugin. handleMessage ( request)
@@ -48,21 +55,37 @@ final class WasmInterceptingMessageHandler<Base: PluginMessageHandler>: PluginMe
4855 }
4956 }
5057 } catch {
51- printError ( " Error: \( error) " )
52- return . expandMacroResult( expandedSource: nil , diagnostics: [ ] )
58+ return . expandMacroResult(
59+ expandedSource: nil ,
60+ diagnostics: [ PluginMessage . Diagnostic (
61+ errorMessage: """
62+ failed to communicate with external macro implementation type \
63+ ' \( macro. moduleName) . \( macro. typeName) ' to expand macro ' \( macro. name) ()'; \
64+ \( error)
65+ """
66+ ) ]
67+ )
5368 }
5469 }
5570}
5671
72+ extension PluginMessage . Diagnostic {
73+ fileprivate init ( errorMessage: String ) {
74+ self . init (
75+ message: errorMessage,
76+ severity: . error,
77+ position: . invalid,
78+ highlights: [ ] ,
79+ notes: [ ] ,
80+ fixIts: [ ]
81+ )
82+ }
83+ }
84+
5785protocol WasmPlugin {
5886 init ( path: FilePath ) throws
5987
6088 func handleMessage( _ json: [ UInt8 ] ) throws -> [ UInt8 ]
6189}
6290
6391private var defaultWasmPlugin : ( some WasmPlugin ) . Type { DefaultWasmPlugin . self }
64-
65- // TODO: return actual diagnostics instead of using this
66- private func printError( _ error: String ) {
67- _ = try ? FileDescriptor . standardError. writeAll ( " \( error) \n " . utf8)
68- }
0 commit comments