Skip to content

Commit d5125a0

Browse files
committed
Auto-generate _toJSValue functions for closures
1 parent 840e883 commit d5125a0

File tree

4 files changed

+174
-55
lines changed

4 files changed

+174
-55
lines changed

Sources/ECMAScript/ToJSValue.swift

Lines changed: 0 additions & 45 deletions
This file was deleted.

Sources/WebAPIBase/Generated.swift

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
486486
}
487487
}
488488

489+
@inlinable public func _toJSValue<ReturnType>(_ value: @escaping () -> ReturnType) -> JSValue
490+
where ReturnType: JSValueCompatible
491+
{
492+
JSClosure { _ in
493+
_toJSValue(value())
494+
}.jsValue
495+
}
496+
489497
@propertyWrapper public final class ClosureAttribute0Optional<ReturnType>
490498
where ReturnType: JSValueCompatible
491499
{
@@ -521,6 +529,14 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
521529
}
522530
}
523531

532+
@inlinable public func _toJSValue<ReturnType>(_ value: (() -> ReturnType)?) -> JSValue
533+
where ReturnType: JSValueCompatible
534+
{
535+
JSClosure { _ in
536+
_toJSValue(value?())
537+
}.jsValue
538+
}
539+
524540
@propertyWrapper public final class ClosureAttribute0OptionalVoid {
525541
@usableFromInline let jsObject: JSObject
526542
@usableFromInline let name: JSString
@@ -555,6 +571,13 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
555571
}
556572
}
557573

574+
@inlinable public func _toJSValue(_ value: (() -> Void)?) -> JSValue {
575+
JSClosure { _ in
576+
value?()
577+
return .undefined
578+
}.jsValue
579+
}
580+
558581
@propertyWrapper public final class ClosureAttribute0Void {
559582
@usableFromInline let jsObject: JSObject
560583
@usableFromInline let name: JSString
@@ -583,6 +606,13 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
583606
}
584607
}
585608

609+
@inlinable public func _toJSValue(_ value: @escaping () -> Void) -> JSValue {
610+
JSClosure { _ in
611+
value()
612+
return .undefined
613+
}.jsValue
614+
}
615+
586616
@propertyWrapper public final class ClosureAttribute1<A0, ReturnType>
587617
where A0: JSValueCompatible, ReturnType: JSValueCompatible
588618
{
@@ -612,6 +642,14 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
612642
}
613643
}
614644

645+
@inlinable public func _toJSValue<A0, ReturnType>(_ value: @escaping (A0) -> ReturnType) -> JSValue
646+
where A0: JSValueCompatible, ReturnType: JSValueCompatible
647+
{
648+
JSClosure {
649+
_toJSValue(value($0[0].fromJSValue()!))
650+
}.jsValue
651+
}
652+
615653
@propertyWrapper public final class ClosureAttribute1Optional<A0, ReturnType>
616654
where A0: JSValueCompatible, ReturnType: JSValueCompatible
617655
{
@@ -647,6 +685,14 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
647685
}
648686
}
649687

688+
@inlinable public func _toJSValue<A0, ReturnType>(_ value: ((A0) -> ReturnType)?) -> JSValue
689+
where A0: JSValueCompatible, ReturnType: JSValueCompatible
690+
{
691+
JSClosure {
692+
_toJSValue(value?($0[0].fromJSValue()!))
693+
}.jsValue
694+
}
695+
650696
@propertyWrapper public final class ClosureAttribute1OptionalVoid<A0>
651697
where A0: JSValueCompatible
652698
{
@@ -683,6 +729,15 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
683729
}
684730
}
685731

732+
@inlinable public func _toJSValue<A0>(_ value: ((A0) -> Void)?) -> JSValue
733+
where A0: JSValueCompatible
734+
{
735+
JSClosure {
736+
value?($0[0].fromJSValue()!)
737+
return .undefined
738+
}.jsValue
739+
}
740+
686741
@propertyWrapper public final class ClosureAttribute1Void<A0>
687742
where A0: JSValueCompatible
688743
{
@@ -713,6 +768,15 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
713768
}
714769
}
715770

771+
@inlinable public func _toJSValue<A0>(_ value: @escaping (A0) -> Void) -> JSValue
772+
where A0: JSValueCompatible
773+
{
774+
JSClosure {
775+
value($0[0].fromJSValue()!)
776+
return .undefined
777+
}.jsValue
778+
}
779+
716780
@propertyWrapper public final class ClosureAttribute2<A0, A1, ReturnType>
717781
where A0: JSValueCompatible, A1: JSValueCompatible, ReturnType: JSValueCompatible
718782
{
@@ -742,6 +806,14 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
742806
}
743807
}
744808

809+
@inlinable public func _toJSValue<A0, A1, ReturnType>(_ value: @escaping (A0, A1) -> ReturnType) -> JSValue
810+
where A0: JSValueCompatible, A1: JSValueCompatible, ReturnType: JSValueCompatible
811+
{
812+
JSClosure {
813+
_toJSValue(value($0[0].fromJSValue()!, $0[1].fromJSValue()!))
814+
}.jsValue
815+
}
816+
745817
@propertyWrapper public final class ClosureAttribute2Optional<A0, A1, ReturnType>
746818
where A0: JSValueCompatible, A1: JSValueCompatible, ReturnType: JSValueCompatible
747819
{
@@ -777,6 +849,14 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
777849
}
778850
}
779851

852+
@inlinable public func _toJSValue<A0, A1, ReturnType>(_ value: ((A0, A1) -> ReturnType)?) -> JSValue
853+
where A0: JSValueCompatible, A1: JSValueCompatible, ReturnType: JSValueCompatible
854+
{
855+
JSClosure {
856+
_toJSValue(value?($0[0].fromJSValue()!, $0[1].fromJSValue()!))
857+
}.jsValue
858+
}
859+
780860
@propertyWrapper public final class ClosureAttribute2OptionalVoid<A0, A1>
781861
where A0: JSValueCompatible, A1: JSValueCompatible
782862
{
@@ -813,6 +893,15 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
813893
}
814894
}
815895

896+
@inlinable public func _toJSValue<A0, A1>(_ value: ((A0, A1) -> Void)?) -> JSValue
897+
where A0: JSValueCompatible, A1: JSValueCompatible
898+
{
899+
JSClosure {
900+
value?($0[0].fromJSValue()!, $0[1].fromJSValue()!)
901+
return .undefined
902+
}.jsValue
903+
}
904+
816905
@propertyWrapper public final class ClosureAttribute2Void<A0, A1>
817906
where A0: JSValueCompatible, A1: JSValueCompatible
818907
{
@@ -843,6 +932,15 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
843932
}
844933
}
845934

935+
@inlinable public func _toJSValue<A0, A1>(_ value: @escaping (A0, A1) -> Void) -> JSValue
936+
where A0: JSValueCompatible, A1: JSValueCompatible
937+
{
938+
JSClosure {
939+
value($0[0].fromJSValue()!, $0[1].fromJSValue()!)
940+
return .undefined
941+
}.jsValue
942+
}
943+
846944
@propertyWrapper public final class ClosureAttribute3<A0, A1, A2, ReturnType>
847945
where A0: JSValueCompatible, A1: JSValueCompatible, A2: JSValueCompatible, ReturnType: JSValueCompatible
848946
{
@@ -872,6 +970,14 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
872970
}
873971
}
874972

973+
@inlinable public func _toJSValue<A0, A1, A2, ReturnType>(_ value: @escaping (A0, A1, A2) -> ReturnType) -> JSValue
974+
where A0: JSValueCompatible, A1: JSValueCompatible, A2: JSValueCompatible, ReturnType: JSValueCompatible
975+
{
976+
JSClosure {
977+
_toJSValue(value($0[0].fromJSValue()!, $0[1].fromJSValue()!, $0[2].fromJSValue()!))
978+
}.jsValue
979+
}
980+
875981
@propertyWrapper public final class ClosureAttribute3Optional<A0, A1, A2, ReturnType>
876982
where A0: JSValueCompatible, A1: JSValueCompatible, A2: JSValueCompatible, ReturnType: JSValueCompatible
877983
{
@@ -907,6 +1013,14 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
9071013
}
9081014
}
9091015

1016+
@inlinable public func _toJSValue<A0, A1, A2, ReturnType>(_ value: ((A0, A1, A2) -> ReturnType)?) -> JSValue
1017+
where A0: JSValueCompatible, A1: JSValueCompatible, A2: JSValueCompatible, ReturnType: JSValueCompatible
1018+
{
1019+
JSClosure {
1020+
_toJSValue(value?($0[0].fromJSValue()!, $0[1].fromJSValue()!, $0[2].fromJSValue()!))
1021+
}.jsValue
1022+
}
1023+
9101024
@propertyWrapper public final class ClosureAttribute5<A0, A1, A2, A3, A4, ReturnType>
9111025
where A0: JSValueCompatible, A1: JSValueCompatible, A2: JSValueCompatible, A3: JSValueCompatible, A4: JSValueCompatible, ReturnType: JSValueCompatible
9121026
{
@@ -936,6 +1050,14 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
9361050
}
9371051
}
9381052

1053+
@inlinable public func _toJSValue<A0, A1, A2, A3, A4, ReturnType>(_ value: @escaping (A0, A1, A2, A3, A4) -> ReturnType) -> JSValue
1054+
where A0: JSValueCompatible, A1: JSValueCompatible, A2: JSValueCompatible, A3: JSValueCompatible, A4: JSValueCompatible, ReturnType: JSValueCompatible
1055+
{
1056+
JSClosure {
1057+
_toJSValue(value($0[0].fromJSValue()!, $0[1].fromJSValue()!, $0[2].fromJSValue()!, $0[3].fromJSValue()!, $0[4].fromJSValue()!))
1058+
}.jsValue
1059+
}
1060+
9391061
@propertyWrapper public final class ClosureAttribute5Optional<A0, A1, A2, A3, A4, ReturnType>
9401062
where A0: JSValueCompatible, A1: JSValueCompatible, A2: JSValueCompatible, A3: JSValueCompatible, A4: JSValueCompatible, ReturnType: JSValueCompatible
9411063
{
@@ -970,3 +1092,11 @@ public enum String_or_record_String_to_String_or_seq_of_seq_of_String: JSValueCo
9701092
}
9711093
}
9721094
}
1095+
1096+
@inlinable public func _toJSValue<A0, A1, A2, A3, A4, ReturnType>(_ value: ((A0, A1, A2, A3, A4) -> ReturnType)?) -> JSValue
1097+
where A0: JSValueCompatible, A1: JSValueCompatible, A2: JSValueCompatible, A3: JSValueCompatible, A4: JSValueCompatible, ReturnType: JSValueCompatible
1098+
{
1099+
JSClosure {
1100+
_toJSValue(value?($0[0].fromJSValue()!, $0[1].fromJSValue()!, $0[2].fromJSValue()!, $0[3].fromJSValue()!, $0[4].fromJSValue()!))
1101+
}.jsValue
1102+
}

Sources/WebAPIBase/ToJSValue.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import JavaScriptKit
2+
3+
// TODO: change to `package` visibility?
4+
5+
@inlinable public func _toJSValue<T: ConvertibleToJSValue>(_ value: T) -> JSValue {
6+
value.jsValue
7+
}
8+
9+
@inlinable public func _toJSValue<T: ConvertibleToJSValue>(_ value: T?) -> JSValue {
10+
value?.jsValue ?? .undefined
11+
}

Sources/WebIDLToSwift/ClosurePattern.swift

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ struct ClosurePattern: SwiftRepresentable, Equatable, Hashable, Comparable {
4646
"""
4747
}
4848

49-
private var setter: SwiftSource {
50-
let call: SwiftSource = "newValue(\(sequence: indexes.map { "$0[\($0)].fromJSValue()!" }))"
49+
private func jsClosureWrapper(name: SwiftSource) -> SwiftSource {
50+
let call: SwiftSource = "\(name)(\(sequence: indexes.map { "$0[\($0)].fromJSValue()!" }))"
5151
let body: SwiftSource
5252
if void {
5353
body = """
@@ -57,11 +57,16 @@ struct ClosurePattern: SwiftRepresentable, Equatable, Hashable, Comparable {
5757
} else {
5858
body = "_toJSValue(\(call))"
5959
}
60-
let setClosure: SwiftSource = """
61-
jsObject[name] = JSClosure { \(argCount == 0 ? "_ in" : "")
60+
61+
return """
62+
JSClosure { \(argCount == 0 ? "_ in" : "")
6263
\(body)
6364
}.jsValue
6465
"""
66+
}
67+
68+
private var setter: SwiftSource {
69+
let setClosure: SwiftSource = "jsObject[name] = \(jsClosureWrapper(name: "newValue"))"
6570

6671
if nullable {
6772
return """
@@ -78,15 +83,17 @@ struct ClosurePattern: SwiftRepresentable, Equatable, Hashable, Comparable {
7883

7984
var typeParams: SwiftSource {
8085
if typeNames.isEmpty { return "" }
81-
return """
82-
<\(sequence: typeNames)>
83-
where \(sequence: typeNames.map { "\($0): JSValueCompatible" })
84-
"""
86+
return "<\(sequence: typeNames)>"
8587
}
8688

87-
var swiftRepresentation: SwiftSource {
89+
var whereClause: SwiftSource {
90+
if typeNames.isEmpty { return "" }
91+
return "\nwhere \(sequence: typeNames.map { "\($0): JSValueCompatible" })"
92+
}
93+
94+
var propertyWrapper: SwiftSource {
8895
"""
89-
@propertyWrapper public final class \(name)\(typeParams) {
96+
@propertyWrapper public final class \(name)\(typeParams) \(whereClause) {
9097
9198
@usableFromInline let jsObject: JSObject
9299
@usableFromInline let name: JSString
@@ -112,4 +119,20 @@ struct ClosurePattern: SwiftRepresentable, Equatable, Hashable, Comparable {
112119
}
113120
"""
114121
}
122+
123+
var toJSValue: SwiftSource {
124+
let escaping: SwiftSource = nullable ? "" : "@escaping"
125+
return """
126+
@inlinable public func _toJSValue\(typeParams)(_ value: \(escaping) \(closureType)) -> JSValue \(whereClause) {
127+
\(jsClosureWrapper(name: nullable ? "value?" : "value"))
128+
}
129+
"""
130+
}
131+
132+
var swiftRepresentation: SwiftSource {
133+
"""
134+
\(propertyWrapper)
135+
\(toJSValue)
136+
"""
137+
}
115138
}

0 commit comments

Comments
 (0)