[NFC] BridgeJS: Make JSGlueGen and IntrinsicJSFragment methods throwable#606
[NFC] BridgeJS: Make JSGlueGen and IntrinsicJSFragment methods throwable#606kateinoigakukun merged 2 commits intomainfrom
Conversation
Instead of crashing on errors in JS code generation for BridgeJSLink, we should prefer propagating errors.
There was a problem hiding this comment.
Pull request overview
This PR updates BridgeJSLink’s JS glue code generation to propagate failures via Swift throws instead of crashing (previously via try! / forced failures), improving error handling during BridgeJSLink codegen.
Changes:
- Make
IntrinsicJSFragment.printCodeand several BridgeJSLink generation helpersthrows, updating call sites to usetry. - Update intrinsic registration APIs (
JSIntrinsicRegistry.register,JSGlueVariableScope.registerIntrinsic) to accept throwing builders (rethrows). - Replace prior
try!usage across codegen paths (arrays/dictionaries/struct helpers/closure helpers) with error propagation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift |
Converts intrinsic fragment emission to throwing, removing try! and propagating codegen errors upward. |
Plugins/BridgeJS/Sources/BridgeJSLink/CodeFragmentPrinter.swift |
Allows intrinsic builders to throw (rethrows) during registration. |
Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift |
Updates JS glue generation to call throwing fragment builders and propagate errors via throws. |
Comments suppressed due to low confidence (1)
Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift:3873
- The generated JS for optional associated-value enum struct fields includes an extra trailing comma in the
.lift(...)call (e.g....lift(caseIdVar, );), which will produce a JavaScript syntax error at runtime. Remove the stray comma so the generated call is valid; this same pattern also appears in the associated-value-enum lifting path used elsewhere in this file.
try printer.indent {
// Special handling for associated value enum - in struct fields, case ID is pushed to i32Stack
if case .associatedValueEnum(let enumName) = wrappedType {
let base = enumName.components(separatedBy: ".").last ?? enumName
let caseIdVar = scope.variable("enumCaseId")
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| case .array(let innerElementType): | ||
| return try! arrayLift(elementType: innerElementType) | ||
| return try arrayLift(elementType: innerElementType) | ||
| case .dictionary(let valueType): | ||
| return try! dictionaryLift(valueType: valueType) | ||
| return try dictionaryLift(valueType: valueType) | ||
| case .nullable(let wrappedType, let kind): |
There was a problem hiding this comment.
This change converts several previously-try! codegen paths into throwing ones (e.g. lifting/lowering arrays/dictionaries now propagates BridgeJSLinkError instead of crashing). There are snapshot tests for successful codegen, but there doesn't appear to be coverage asserting that unsupported/invalid BridgeType shapes cause BridgeJSLink.link() to throw (and that the thrown error is surfaced) rather than crashing. Add at least one negative test that constructs an unsupported type scenario and verifies the error is propagated.
Instead of crashing on errors in JS code generation for BridgeJSLink, we should prefer propagating errors.