Skip to content

Commit 0662e3b

Browse files
Revert "BridgeJS: Enforce throws(JSException) on @js protocol methods" (#565)
1 parent a070ba0 commit 0662e3b

File tree

12 files changed

+154
-359
lines changed

12 files changed

+154
-359
lines changed

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ struct ProtocolCodegen {
13461346
let builder = ImportTS.CallJSEmission(
13471347
moduleName: moduleName,
13481348
abiName: "_extern_\(method.name)",
1349-
context: .exportSwiftProtocol
1349+
context: .exportSwift
13501350
)
13511351
try builder.lowerParameter(param: Parameter(label: nil, name: "jsObject", type: .jsObject(nil)))
13521352
for param in method.parameters {
@@ -1359,7 +1359,7 @@ struct ProtocolCodegen {
13591359
let signature = SwiftSignatureBuilder.buildFunctionSignature(
13601360
parameters: method.parameters,
13611361
returnType: method.returnType,
1362-
effects: method.effects
1362+
effects: nil
13631363
)
13641364

13651365
// Build extern declaration using helper function

Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ public struct ImportTS {
187187
body.append("let ret = \(raw: callExpr)")
188188
}
189189

190-
// Add exception check for contexts that call INTO JavaScript
191-
if context == .importTS || context == .exportSwiftProtocol {
190+
// Add exception check for ImportTS context
191+
if context == .importTS {
192192
body.append("if let error = _swift_js_take_exception() { throw error }")
193193
}
194194
}
@@ -887,7 +887,7 @@ extension BridgeType {
887887
Swift classes can only be used in @JS protocols where Swift owns the instance.
888888
"""
889889
)
890-
case .exportSwift, .exportSwiftProtocol:
890+
case .exportSwift:
891891
return LoweringParameterInfo(loweredParameters: [("pointer", .pointer)])
892892
}
893893
case .swiftProtocol:
@@ -896,30 +896,30 @@ extension BridgeType {
896896
switch context {
897897
case .importTS:
898898
throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports")
899-
case .exportSwift, .exportSwiftProtocol:
899+
case .exportSwift:
900900
return LoweringParameterInfo(loweredParameters: [("value", .i32)])
901901
}
902902
case .rawValueEnum(_, let rawType):
903903
switch context {
904904
case .importTS:
905905
return LoweringParameterInfo(loweredParameters: [("value", rawType.wasmCoreType ?? .i32)])
906-
case .exportSwift, .exportSwiftProtocol:
906+
case .exportSwift:
907907
// For protocol export we return .i32 for String raw value type instead of nil
908908
return LoweringParameterInfo(loweredParameters: [("value", rawType.wasmCoreType ?? .i32)])
909909
}
910910
case .associatedValueEnum:
911911
switch context {
912912
case .importTS:
913913
throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports")
914-
case .exportSwift, .exportSwiftProtocol:
914+
case .exportSwift:
915915
return LoweringParameterInfo(loweredParameters: [("caseId", .i32)])
916916
}
917917
case .swiftStruct:
918918
switch context {
919919
case .importTS:
920920
// Swift structs are bridged as JS objects (object IDs) in imported signatures.
921921
return LoweringParameterInfo(loweredParameters: [("objectId", .i32)])
922-
case .exportSwift, .exportSwiftProtocol:
922+
case .exportSwift:
923923
return LoweringParameterInfo(loweredParameters: [])
924924
}
925925
case .namespaceEnum:
@@ -928,7 +928,7 @@ extension BridgeType {
928928
switch context {
929929
case .importTS:
930930
throw BridgeJSCoreError("Optional types are not yet supported in TypeScript imports")
931-
case .exportSwift, .exportSwiftProtocol:
931+
case .exportSwift:
932932
let wrappedInfo = try wrappedType.loweringParameterInfo(context: context)
933933
var params = [("isSome", WasmCoreType.i32)]
934934
params.append(contentsOf: wrappedInfo.loweredParameters)
@@ -938,7 +938,7 @@ extension BridgeType {
938938
switch context {
939939
case .importTS:
940940
throw BridgeJSCoreError("Array types are not yet supported in TypeScript imports")
941-
case .exportSwift, .exportSwiftProtocol:
941+
case .exportSwift:
942942
return LoweringParameterInfo(loweredParameters: [])
943943
}
944944
}
@@ -981,7 +981,7 @@ extension BridgeType {
981981
JavaScript cannot create Swift heap objects.
982982
"""
983983
)
984-
case .exportSwift, .exportSwiftProtocol:
984+
case .exportSwift:
985985
return LiftingReturnInfo(valueToLift: .pointer)
986986
}
987987
case .swiftProtocol:
@@ -990,30 +990,30 @@ extension BridgeType {
990990
switch context {
991991
case .importTS:
992992
throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports")
993-
case .exportSwift, .exportSwiftProtocol:
993+
case .exportSwift:
994994
return LiftingReturnInfo(valueToLift: .i32)
995995
}
996996
case .rawValueEnum(_, let rawType):
997997
switch context {
998998
case .importTS:
999999
return LiftingReturnInfo(valueToLift: rawType.wasmCoreType ?? .i32)
1000-
case .exportSwift, .exportSwiftProtocol:
1000+
case .exportSwift:
10011001
// For protocol export we return .i32 for String raw value type instead of nil
10021002
return LiftingReturnInfo(valueToLift: rawType.wasmCoreType ?? .i32)
10031003
}
10041004
case .associatedValueEnum:
10051005
switch context {
10061006
case .importTS:
10071007
throw BridgeJSCoreError("Enum types are not yet supported in TypeScript imports")
1008-
case .exportSwift, .exportSwiftProtocol:
1008+
case .exportSwift:
10091009
return LiftingReturnInfo(valueToLift: .i32)
10101010
}
10111011
case .swiftStruct:
10121012
switch context {
10131013
case .importTS:
10141014
// Swift structs are bridged as JS objects (object IDs) in imported signatures.
10151015
return LiftingReturnInfo(valueToLift: .i32)
1016-
case .exportSwift, .exportSwiftProtocol:
1016+
case .exportSwift:
10171017
return LiftingReturnInfo(valueToLift: nil)
10181018
}
10191019
case .namespaceEnum:
@@ -1022,15 +1022,15 @@ extension BridgeType {
10221022
switch context {
10231023
case .importTS:
10241024
throw BridgeJSCoreError("Optional types are not yet supported in TypeScript imports")
1025-
case .exportSwift, .exportSwiftProtocol:
1025+
case .exportSwift:
10261026
let wrappedInfo = try wrappedType.liftingReturnInfo(context: context)
10271027
return LiftingReturnInfo(valueToLift: wrappedInfo.valueToLift)
10281028
}
10291029
case .array:
10301030
switch context {
10311031
case .importTS:
10321032
throw BridgeJSCoreError("Array types are not yet supported in TypeScript imports")
1033-
case .exportSwift, .exportSwiftProtocol:
1033+
case .exportSwift:
10341034
return LiftingReturnInfo(valueToLift: nil)
10351035
}
10361036
}

Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,15 +1608,6 @@ private final class ExportSwiftAPICollector: SyntaxAnyVisitor {
16081608
return nil
16091609
}
16101610

1611-
guard effects.isThrows else {
1612-
diagnose(
1613-
node: node,
1614-
message: "@JS protocol methods must be throws.",
1615-
hint: "Declare the method as 'throws(JSException)'."
1616-
)
1617-
return nil
1618-
}
1619-
16201611
return ExportedFunction(
16211612
name: name,
16221613
abiName: abiName,

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ struct IntrinsicJSFragment: Sendable {
14771477
throw BridgeJSLinkError(
14781478
message: "swiftHeapObject '\(name)' can only be used in protocol exports, not in \(context)"
14791479
)
1480-
case .exportSwift, .exportSwiftProtocol:
1480+
case .exportSwift:
14811481
return .swiftHeapObjectLiftParameter(name)
14821482
}
14831483
case .swiftProtocol: return .jsObjectLiftParameter
@@ -1491,7 +1491,7 @@ struct IntrinsicJSFragment: Sendable {
14911491
throw BridgeJSLinkError(
14921492
message: "Optional types are not supported for imported JS functions: \(wrappedType)"
14931493
)
1494-
case .exportSwift, .exportSwiftProtocol:
1494+
case .exportSwift:
14951495
return try .optionalLiftParameter(wrappedType: wrappedType)
14961496
}
14971497
case .caseEnum: return .identity
@@ -1508,7 +1508,7 @@ struct IntrinsicJSFragment: Sendable {
15081508
message:
15091509
"Associated value enums are not supported to be passed as parameters to imported JS functions: \(fullName)"
15101510
)
1511-
case .exportSwift, .exportSwiftProtocol:
1511+
case .exportSwift:
15121512
let base = fullName.components(separatedBy: ".").last ?? fullName
15131513
return IntrinsicJSFragment(
15141514
parameters: ["caseId"],
@@ -1526,7 +1526,7 @@ struct IntrinsicJSFragment: Sendable {
15261526
switch context {
15271527
case .importTS:
15281528
return .jsObjectLiftRetainedObjectId
1529-
case .exportSwift, .exportSwiftProtocol:
1529+
case .exportSwift:
15301530
let base = fullName.components(separatedBy: ".").last ?? fullName
15311531
return IntrinsicJSFragment(
15321532
parameters: [],
@@ -1558,7 +1558,7 @@ struct IntrinsicJSFragment: Sendable {
15581558
throw BridgeJSLinkError(
15591559
message: "Arrays are not yet supported to be passed as parameters to imported JS functions"
15601560
)
1561-
case .exportSwift, .exportSwiftProtocol:
1561+
case .exportSwift:
15621562
return try arrayLift(elementType: elementType)
15631563
}
15641564
}
@@ -1578,7 +1578,7 @@ struct IntrinsicJSFragment: Sendable {
15781578
throw BridgeJSLinkError(
15791579
message: "swiftHeapObject '\(name)' can only be used in protocol exports, not in \(context)"
15801580
)
1581-
case .exportSwift, .exportSwiftProtocol:
1581+
case .exportSwift:
15821582
return .swiftHeapObjectLowerReturn
15831583
}
15841584
case .swiftProtocol: return .jsObjectLowerReturn
@@ -1589,7 +1589,7 @@ struct IntrinsicJSFragment: Sendable {
15891589
throw BridgeJSLinkError(
15901590
message: "Optional types are not supported for imported JS functions: \(wrappedType)"
15911591
)
1592-
case .exportSwift, .exportSwiftProtocol:
1592+
case .exportSwift:
15931593
return try .optionalLowerReturn(wrappedType: wrappedType)
15941594
}
15951595
case .caseEnum: return .identity
@@ -1606,15 +1606,15 @@ struct IntrinsicJSFragment: Sendable {
16061606
message:
16071607
"Associated value enums are not supported to be returned from imported JS functions: \(fullName)"
16081608
)
1609-
case .exportSwift, .exportSwiftProtocol:
1609+
case .exportSwift:
16101610
return associatedValueLowerReturn(fullName: fullName)
16111611
}
16121612
case .swiftStruct(let fullName):
16131613
switch context {
16141614
case .importTS:
16151615
// ImportTS expects Swift structs to come back as a retained JS object ID.
16161616
return .jsObjectLowerReturn
1617-
case .exportSwift, .exportSwiftProtocol:
1617+
case .exportSwift:
16181618
return swiftStructLowerReturn(fullName: fullName)
16191619
}
16201620
case .closure:
@@ -1640,7 +1640,7 @@ struct IntrinsicJSFragment: Sendable {
16401640
throw BridgeJSLinkError(
16411641
message: "Arrays are not yet supported to be returned from imported JS functions"
16421642
)
1643-
case .exportSwift, .exportSwiftProtocol:
1643+
case .exportSwift:
16441644
return try arrayLower(elementType: elementType)
16451645
}
16461646
}

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public struct ABINameGenerator {
7171
public enum BridgeContext: Sendable {
7272
case importTS
7373
case exportSwift
74-
case exportSwiftProtocol
7574
}
7675

7776
public struct ClosureSignature: Codable, Equatable, Hashable, Sendable {

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Protocol.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ import JavaScriptKit
4747
var directionOptional: Direction? { get set }
4848
var priority: Priority { get set }
4949
var priorityOptional: Priority? { get set }
50-
func onSomethingHappened() throws(JSException)
51-
func onValueChanged(_ value: String) throws(JSException)
52-
func onCountUpdated(count: Int) throws(JSException) -> Bool
53-
func onLabelUpdated(_ prefix: String, _ suffix: String) throws(JSException)
54-
func isCountEven() throws(JSException) -> Bool
55-
func onHelperUpdated(_ helper: Helper) throws(JSException)
56-
func createHelper() throws(JSException) -> Helper
57-
func onOptionalHelperUpdated(_ helper: Helper?) throws(JSException)
58-
func createOptionalHelper() throws(JSException) -> Helper?
59-
func createEnum() throws(JSException) -> ExampleEnum
60-
func handleResult(_ result: Result) throws(JSException)
61-
func getResult() throws(JSException) -> Result
50+
func onSomethingHappened()
51+
func onValueChanged(_ value: String)
52+
func onCountUpdated(count: Int) -> Bool
53+
func onLabelUpdated(_ prefix: String, _ suffix: String)
54+
func isCountEven() -> Bool
55+
func onHelperUpdated(_ helper: Helper)
56+
func createHelper() -> Helper
57+
func onOptionalHelperUpdated(_ helper: Helper?)
58+
func createOptionalHelper() -> Helper?
59+
func createEnum() -> ExampleEnum
60+
func handleResult(_ result: Result)
61+
func getResult() -> Result
6262
}
6363

6464
@JS class MyViewController {

0 commit comments

Comments
 (0)