Skip to content

Commit c753a1e

Browse files
committed
added JSString support
1 parent ba66ff1 commit c753a1e

11 files changed

Lines changed: 532 additions & 8 deletions

File tree

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ struct StackCodegen {
734734
func liftExpression(for type: BridgeType) -> ExprSyntax {
735735
switch type {
736736
case .string, .int, .uint, .bool, .float, .double,
737-
.jsObject(nil), .jsValue, .swiftStruct, .swiftHeapObject, .unsafePointer,
737+
.jsObject(nil), .jsString, .jsValue, .swiftStruct, .swiftHeapObject, .unsafePointer,
738738
.swiftProtocol, .caseEnum, .associatedValueEnum, .rawValueEnum, .array, .dictionary:
739739
return "\(raw: type.swiftType).bridgeJSStackPop()"
740740
case .jsObject(let className?):
@@ -751,7 +751,7 @@ struct StackCodegen {
751751
private func liftNullableExpression(wrappedType: BridgeType, kind: JSOptionalKind) -> ExprSyntax {
752752
let typeName = kind == .null ? "Optional" : "JSUndefinedOr"
753753
switch wrappedType {
754-
case .string, .int, .uint, .bool, .float, .double, .jsObject(nil), .jsValue,
754+
case .string, .int, .uint, .bool, .float, .double, .jsObject(nil), .jsString, .jsValue,
755755
.swiftStruct, .swiftHeapObject, .caseEnum, .associatedValueEnum, .rawValueEnum,
756756
.array, .dictionary:
757757
return "\(raw: typeName)<\(raw: wrappedType.swiftType)>.bridgeJSStackPop()"
@@ -775,7 +775,7 @@ struct StackCodegen {
775775
) -> [CodeBlockItemSyntax] {
776776
switch type {
777777
case .string, .int, .uint, .bool, .float, .double, .jsValue,
778-
.jsObject(nil), .swiftHeapObject, .unsafePointer, .closure,
778+
.jsObject(nil), .jsString, .swiftHeapObject, .unsafePointer, .closure,
779779
.caseEnum, .rawValueEnum, .associatedValueEnum, .swiftStruct, .nullable:
780780
return ["\(raw: accessor).bridgeJSStackPush()"]
781781
case .jsObject(_?):
@@ -1385,6 +1385,7 @@ extension BridgeType {
13851385
case .jsValue: return "JSValue"
13861386
case .jsObject(nil): return "JSObject"
13871387
case .jsObject(let name?): return name
1388+
case .jsString: return "JSString"
13881389
case .swiftHeapObject(let name): return name
13891390
case .unsafePointer(let ptr): return ptr.swiftType
13901391
case .swiftProtocol(let name): return "Any\(name)"
@@ -1431,6 +1432,7 @@ extension BridgeType {
14311432
static let double = LiftingIntrinsicInfo(parameters: [("value", .f64)])
14321433
static let string = LiftingIntrinsicInfo(parameters: [("bytes", .i32), ("length", .i32)])
14331434
static let jsObject = LiftingIntrinsicInfo(parameters: [("value", .i32)])
1435+
static let jsString = LiftingIntrinsicInfo(parameters: [("value", .i32)])
14341436
static let jsValue = LiftingIntrinsicInfo(parameters: [("kind", .i32), ("payload1", .i32), ("payload2", .f64)])
14351437
static let swiftHeapObject = LiftingIntrinsicInfo(parameters: [("value", .pointer)])
14361438
static let unsafePointer = LiftingIntrinsicInfo(parameters: [("pointer", .pointer)])
@@ -1449,6 +1451,7 @@ extension BridgeType {
14491451
case .double: return .double
14501452
case .string: return .string
14511453
case .jsObject: return .jsObject
1454+
case .jsString: return .jsString
14521455
case .jsValue: return .jsValue
14531456
case .swiftHeapObject: return .swiftHeapObject
14541457
case .unsafePointer: return .unsafePointer
@@ -1487,6 +1490,7 @@ extension BridgeType {
14871490
static let double = LoweringIntrinsicInfo(returnType: .f64)
14881491
static let string = LoweringIntrinsicInfo(returnType: nil)
14891492
static let jsObject = LoweringIntrinsicInfo(returnType: .i32)
1493+
static let jsString = LoweringIntrinsicInfo(returnType: .i32)
14901494
static let jsValue = LoweringIntrinsicInfo(returnType: nil)
14911495
static let swiftHeapObject = LoweringIntrinsicInfo(returnType: .pointer)
14921496
static let unsafePointer = LoweringIntrinsicInfo(returnType: .pointer)
@@ -1507,6 +1511,7 @@ extension BridgeType {
15071511
case .double: return .double
15081512
case .string: return .string
15091513
case .jsObject: return .jsObject
1514+
case .jsString: return .jsString
15101515
case .jsValue: return .jsValue
15111516
case .swiftHeapObject: return .swiftHeapObject
15121517
case .unsafePointer: return .unsafePointer

Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ extension BridgeType {
807807
case .double: return .double
808808
case .string: return .string
809809
case .jsObject: return .jsObject
810+
case .jsString: return LoweringParameterInfo(loweredParameters: [("value", .i32)], useBorrowing: true)
810811
case .jsValue: return .jsValue
811812
case .void: return .void
812813
case .closure:
@@ -881,6 +882,7 @@ extension BridgeType {
881882
case .double: return .double
882883
case .string: return .string
883884
case .jsObject: return .jsObject
885+
case .jsString: return .jsObject
884886
case .jsValue: return .jsValue
885887
case .void: return .void
886888
case .closure:

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,6 +3518,8 @@ extension BridgeType {
35183518
return "boolean"
35193519
case .jsObject(let name):
35203520
return name ?? "any"
3521+
case .jsString:
3522+
return "string"
35213523
case .jsValue:
35223524
return "any"
35233525
case .swiftHeapObject(let name):

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,7 @@ struct IntrinsicJSFragment: Sendable {
12491249
return .identity
12501250
case .string: return .stringLowerParameter
12511251
case .jsObject: return .jsObjectLowerParameter
1252+
case .jsString: return .jsObjectLowerParameter
12521253
case .jsValue: return .jsValueLower
12531254
case .swiftHeapObject: return .swiftHeapObjectLowerParameter
12541255
case .swiftProtocol: return .jsObjectLowerParameter
@@ -1299,6 +1300,7 @@ struct IntrinsicJSFragment: Sendable {
12991300
return .identity
13001301
case .string: return .stringLiftReturn
13011302
case .jsObject: return .jsObjectLiftReturn
1303+
case .jsString: return .jsObjectLiftReturn
13021304
case .jsValue: return .jsValueLift
13031305
case .swiftHeapObject(let name): return .swiftHeapObjectLiftReturn(name)
13041306
case .swiftProtocol: return .jsObjectLiftReturn
@@ -1348,6 +1350,7 @@ struct IntrinsicJSFragment: Sendable {
13481350
return .identity
13491351
case .string: return .stringLiftParameter
13501352
case .jsObject: return .jsObjectLiftParameter
1353+
case .jsString: return .jsObjectLiftParameter
13511354
case .jsValue: return .jsValueLiftParameter
13521355
case .swiftHeapObject(let name):
13531356
return .swiftHeapObjectLiftParameter(name)
@@ -1432,6 +1435,7 @@ struct IntrinsicJSFragment: Sendable {
14321435
return .identity
14331436
case .string: return .stringLowerReturn
14341437
case .jsObject: return .jsObjectLowerReturn
1438+
case .jsString: return .jsObjectLowerReturn
14351439
case .jsValue: return .jsValueLowerReturn(context: context)
14361440
case .swiftHeapObject: return .swiftHeapObjectLowerReturn
14371441
case .swiftProtocol: return .jsObjectLowerReturn
@@ -1986,7 +1990,7 @@ struct IntrinsicJSFragment: Sendable {
19861990
return [objVar]
19871991
}
19881992
)
1989-
case .jsObject, .swiftProtocol:
1993+
case .jsObject, .jsString, .swiftProtocol:
19901994
return IntrinsicJSFragment(
19911995
parameters: [],
19921996
printCode: { arguments, context in
@@ -2108,7 +2112,7 @@ struct IntrinsicJSFragment: Sendable {
21082112
return []
21092113
}
21102114
)
2111-
case .jsObject, .swiftProtocol:
2115+
case .jsObject, .jsString, .swiftProtocol:
21122116
return IntrinsicJSFragment(
21132117
parameters: ["value"],
21142118
printCode: { arguments, context in
@@ -2587,6 +2591,8 @@ private extension BridgeType {
25872591
return .sideChannelReturn(.storage)
25882592
case .jsObject:
25892593
return .sideChannelReturn(.retainedObject)
2594+
case .jsString:
2595+
return .sideChannelReturn(.retainedObject)
25902596
case .jsValue:
25912597
return .inlineFlag
25922598
case .swiftHeapObject:
@@ -2621,7 +2627,7 @@ private extension BridgeType {
26212627

26222628
var nilSentinel: NilSentinel {
26232629
switch self {
2624-
case .jsObject, .swiftProtocol:
2630+
case .jsObject, .jsString, .swiftProtocol:
26252631
return .i32(0)
26262632
case .swiftHeapObject:
26272633
return .pointer
@@ -2660,6 +2666,8 @@ private extension BridgeType {
26602666
return [("bytes", .i32), ("length", .i32)]
26612667
case .jsObject:
26622668
return [("value", .i32)]
2669+
case .jsString:
2670+
return [("value", .i32)]
26632671
case .jsValue:
26642672
return [("kind", .i32), ("payload1", .i32), ("payload2", .f64)]
26652673
case .swiftHeapObject:

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public enum JSOptionalKind: String, Codable, Equatable, Hashable, Sendable {
165165
}
166166

167167
public enum BridgeType: Codable, Equatable, Hashable, Sendable {
168-
case int, uint, float, double, string, bool, jsObject(String?), jsValue, swiftHeapObject(String), void
168+
case int, uint, float, double, string, bool, jsObject(String?), jsString, jsValue, swiftHeapObject(String), void
169169
case unsafePointer(UnsafePointerType)
170170
indirect case nullable(BridgeType, JSOptionalKind)
171171
indirect case array(BridgeType)
@@ -1074,6 +1074,8 @@ extension BridgeType {
10741074
self = .void
10751075
case "JSObject":
10761076
self = .jsObject(nil)
1077+
case "JSString":
1078+
self = .jsString
10771079
case "UnsafeRawPointer":
10781080
self = .unsafePointer(.init(kind: .unsafeRawPointer))
10791081
case "UnsafeMutableRawPointer":
@@ -1093,6 +1095,7 @@ extension BridgeType {
10931095
case .float: return .f32
10941096
case .double: return .f64
10951097
case .string: return nil
1098+
case .jsString: return .i32
10961099
case .jsObject: return .i32
10971100
case .jsValue: return nil
10981101
case .swiftHeapObject:
@@ -1143,6 +1146,7 @@ extension BridgeType {
11431146
case .float: return "Sf"
11441147
case .double: return "Sd"
11451148
case .string: return "SS"
1149+
case .jsString: return "8JSStringV"
11461150
case .bool: return "Sb"
11471151
case .void: return "y"
11481152
case .jsObject(let name):
@@ -1206,7 +1210,7 @@ extension BridgeType {
12061210
}
12071211

12081212
switch wrappedType {
1209-
case .string, .int, .float, .double, .jsObject, .swiftProtocol:
1213+
case .string, .int, .float, .double, .jsObject, .jsString, .swiftProtocol:
12101214
return true
12111215
case .rawValueEnum(_, let rawType):
12121216
switch rawType {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Export: JSString parameter
2+
@JS func checkJSString(a: JSString) {}
3+
4+
// Export: JSString return
5+
@JS func getJSString() -> JSString { fatalError() }
6+
7+
// Export: JSString roundtrip
8+
@JS func roundTripJSString(_ value: JSString) -> JSString { return value }
9+
10+
// Import: JSString parameter and return
11+
@JSFunction func jsCheckJSString(_ a: JSString) throws(JSException) -> Void
12+
@JSFunction func jsGetJSString() throws(JSException) -> JSString
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"exported" : {
3+
"classes" : [
4+
5+
],
6+
"enums" : [
7+
8+
],
9+
"exposeToGlobal" : false,
10+
"functions" : [
11+
{
12+
"abiName" : "bjs_checkJSString",
13+
"effects" : {
14+
"isAsync" : false,
15+
"isStatic" : false,
16+
"isThrows" : false
17+
},
18+
"name" : "checkJSString",
19+
"parameters" : [
20+
{
21+
"label" : "a",
22+
"name" : "a",
23+
"type" : {
24+
"jsString" : {
25+
26+
}
27+
}
28+
}
29+
],
30+
"returnType" : {
31+
"void" : {
32+
33+
}
34+
}
35+
},
36+
{
37+
"abiName" : "bjs_getJSString",
38+
"effects" : {
39+
"isAsync" : false,
40+
"isStatic" : false,
41+
"isThrows" : false
42+
},
43+
"name" : "getJSString",
44+
"parameters" : [
45+
46+
],
47+
"returnType" : {
48+
"jsString" : {
49+
50+
}
51+
}
52+
},
53+
{
54+
"abiName" : "bjs_roundTripJSString",
55+
"effects" : {
56+
"isAsync" : false,
57+
"isStatic" : false,
58+
"isThrows" : false
59+
},
60+
"name" : "roundTripJSString",
61+
"parameters" : [
62+
{
63+
"label" : "_",
64+
"name" : "value",
65+
"type" : {
66+
"jsString" : {
67+
68+
}
69+
}
70+
}
71+
],
72+
"returnType" : {
73+
"jsString" : {
74+
75+
}
76+
}
77+
}
78+
],
79+
"protocols" : [
80+
81+
],
82+
"structs" : [
83+
84+
]
85+
},
86+
"imported" : {
87+
"children" : [
88+
{
89+
"functions" : [
90+
{
91+
"name" : "jsCheckJSString",
92+
"parameters" : [
93+
{
94+
"name" : "a",
95+
"type" : {
96+
"jsString" : {
97+
98+
}
99+
}
100+
}
101+
],
102+
"returnType" : {
103+
"void" : {
104+
105+
}
106+
}
107+
},
108+
{
109+
"name" : "jsGetJSString",
110+
"parameters" : [
111+
112+
],
113+
"returnType" : {
114+
"jsString" : {
115+
116+
}
117+
}
118+
}
119+
],
120+
"types" : [
121+
122+
]
123+
}
124+
]
125+
},
126+
"moduleName" : "TestModule"
127+
}

0 commit comments

Comments
 (0)