BridgeJS: Support protocol types in closure parameters and returns by krodak · Pull Request #699 · swiftwasm/JavaScriptKit
Overview
Protocol types in closure signatures ((Renderable) -> String, () -> Renderable, etc.) were rejected at codegen time with "swiftProtocol is not supported in imported signatures". The check in ImportTS.swift didn't distinguish between import context (where it's correct to reject) and export closure context (where protocols are just JSObject IDs and should work fine).
Changes
1. ImportTS.swift - Only reject .swiftProtocol in .importTS context, allow it in .exportSwift. Protocol params in closure bodies now lower via (param as! _BridgedSwiftProtocolExportable).bridgeJSLowerAsProtocolReturn() - same pattern the non-closure protocol thunks already use.
2. ExportSwift.swift + ClosureCodegen.swift - Added closureSwiftType that returns any Renderable instead of AnyRenderable for protocol types. Lifting still uses the wrapper internally (AnyRenderable.bridgeJSLiftParameter(id) which is-a any Renderable).
3. ClosureCodegen.swift - Protocol return lowering in the invoke handler uses the _BridgedSwiftProtocolExportable cast.
4. BridgeJSIntrinsics.swift - Added missing bridgeJSLiftReturn to _BridgedSwiftProtocolWrapper.
No JS-side changes needed - protocols are already JSObject references there.
Test Plan
- New
ProtocolInClosure.swiftsnapshot input covering protocol as param, return, both, and optional param - 4 new E2E methods on
TextProcessorwith JS assertions for roundtrip behavior and nil handling