Skip to content

Commit 35e47e6

Browse files
BridgeJS: Remove inline array lifting/lowering codegen
1 parent 3badf17 commit 35e47e6

File tree

3 files changed

+25
-221
lines changed

3 files changed

+25
-221
lines changed

Benchmarks/Sources/Generated/BridgeJS.swift

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,16 +1578,7 @@ public func _bjs_ArrayRoundtrip_roundtripNestedPointArray(_ _self: UnsafeMutable
15781578
@_cdecl("bjs_ArrayRoundtrip_takeOptionalIntArray")
15791579
public func _bjs_ArrayRoundtrip_takeOptionalIntArray(_ _self: UnsafeMutableRawPointer) -> Void {
15801580
#if arch(wasm32)
1581-
ArrayRoundtrip.bridgeJSLiftParameter(_self).takeOptionalIntArray(_: {
1582-
let __count = Int(_swift_js_pop_i32())
1583-
var __result: [Optional<Int>] = []
1584-
__result.reserveCapacity(__count)
1585-
for _ in 0..<__count {
1586-
__result.append(Optional<Int>.bridgeJSStackPop())
1587-
}
1588-
__result.reverse()
1589-
return __result
1590-
}())
1581+
ArrayRoundtrip.bridgeJSLiftParameter(_self).takeOptionalIntArray(_: [Optional<Int>].bridgeJSStackPop())
15911582
#else
15921583
fatalError("Only available on WebAssembly")
15931584
#endif
@@ -1598,10 +1589,7 @@ public func _bjs_ArrayRoundtrip_takeOptionalIntArray(_ _self: UnsafeMutableRawPo
15981589
public func _bjs_ArrayRoundtrip_makeOptionalIntArray(_ _self: UnsafeMutableRawPointer) -> Void {
15991590
#if arch(wasm32)
16001591
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).makeOptionalIntArray()
1601-
for __bjs_elem_ret in ret {
1602-
__bjs_elem_ret.bridgeJSStackPush()
1603-
}
1604-
_swift_js_push_i32(Int32(ret.count))
1592+
ret.bridgeJSStackPush()
16051593
#else
16061594
fatalError("Only available on WebAssembly")
16071595
#endif
@@ -1611,20 +1599,8 @@ public func _bjs_ArrayRoundtrip_makeOptionalIntArray(_ _self: UnsafeMutableRawPo
16111599
@_cdecl("bjs_ArrayRoundtrip_roundtripOptionalIntArray")
16121600
public func _bjs_ArrayRoundtrip_roundtripOptionalIntArray(_ _self: UnsafeMutableRawPointer) -> Void {
16131601
#if arch(wasm32)
1614-
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).roundtripOptionalIntArray(_: {
1615-
let __count = Int(_swift_js_pop_i32())
1616-
var __result: [Optional<Int>] = []
1617-
__result.reserveCapacity(__count)
1618-
for _ in 0..<__count {
1619-
__result.append(Optional<Int>.bridgeJSStackPop())
1620-
}
1621-
__result.reverse()
1622-
return __result
1623-
}())
1624-
for __bjs_elem_ret in ret {
1625-
__bjs_elem_ret.bridgeJSStackPush()
1626-
}
1627-
_swift_js_push_i32(Int32(ret.count))
1602+
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).roundtripOptionalIntArray(_: [Optional<Int>].bridgeJSStackPop())
1603+
ret.bridgeJSStackPush()
16281604
#else
16291605
fatalError("Only available on WebAssembly")
16301606
#endif
@@ -1634,16 +1610,7 @@ public func _bjs_ArrayRoundtrip_roundtripOptionalIntArray(_ _self: UnsafeMutable
16341610
@_cdecl("bjs_ArrayRoundtrip_takeOptionalPointArray")
16351611
public func _bjs_ArrayRoundtrip_takeOptionalPointArray(_ _self: UnsafeMutableRawPointer) -> Void {
16361612
#if arch(wasm32)
1637-
ArrayRoundtrip.bridgeJSLiftParameter(_self).takeOptionalPointArray(_: {
1638-
let __count = Int(_swift_js_pop_i32())
1639-
var __result: [Optional<Point>] = []
1640-
__result.reserveCapacity(__count)
1641-
for _ in 0..<__count {
1642-
__result.append(Optional<Point>.bridgeJSStackPop())
1643-
}
1644-
__result.reverse()
1645-
return __result
1646-
}())
1613+
ArrayRoundtrip.bridgeJSLiftParameter(_self).takeOptionalPointArray(_: [Optional<Point>].bridgeJSStackPop())
16471614
#else
16481615
fatalError("Only available on WebAssembly")
16491616
#endif
@@ -1654,10 +1621,7 @@ public func _bjs_ArrayRoundtrip_takeOptionalPointArray(_ _self: UnsafeMutableRaw
16541621
public func _bjs_ArrayRoundtrip_makeOptionalPointArray(_ _self: UnsafeMutableRawPointer) -> Void {
16551622
#if arch(wasm32)
16561623
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).makeOptionalPointArray()
1657-
for __bjs_elem_ret in ret {
1658-
__bjs_elem_ret.bridgeJSStackPush()
1659-
}
1660-
_swift_js_push_i32(Int32(ret.count))
1624+
ret.bridgeJSStackPush()
16611625
#else
16621626
fatalError("Only available on WebAssembly")
16631627
#endif
@@ -1667,20 +1631,8 @@ public func _bjs_ArrayRoundtrip_makeOptionalPointArray(_ _self: UnsafeMutableRaw
16671631
@_cdecl("bjs_ArrayRoundtrip_roundtripOptionalPointArray")
16681632
public func _bjs_ArrayRoundtrip_roundtripOptionalPointArray(_ _self: UnsafeMutableRawPointer) -> Void {
16691633
#if arch(wasm32)
1670-
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).roundtripOptionalPointArray(_: {
1671-
let __count = Int(_swift_js_pop_i32())
1672-
var __result: [Optional<Point>] = []
1673-
__result.reserveCapacity(__count)
1674-
for _ in 0..<__count {
1675-
__result.append(Optional<Point>.bridgeJSStackPop())
1676-
}
1677-
__result.reverse()
1678-
return __result
1679-
}())
1680-
for __bjs_elem_ret in ret {
1681-
__bjs_elem_ret.bridgeJSStackPush()
1682-
}
1683-
_swift_js_push_i32(Int32(ret.count))
1634+
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).roundtripOptionalPointArray(_: [Optional<Point>].bridgeJSStackPop())
1635+
ret.bridgeJSStackPush()
16841636
#else
16851637
fatalError("Only available on WebAssembly")
16861638
#endif

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -713,14 +713,12 @@ struct StackCodegen {
713713
switch type {
714714
case .string, .int, .uint, .bool, .float, .double,
715715
.jsObject(nil), .jsValue, .swiftStruct, .swiftHeapObject, .unsafePointer,
716-
.swiftProtocol, .caseEnum, .associatedValueEnum, .rawValueEnum:
716+
.swiftProtocol, .caseEnum, .associatedValueEnum, .rawValueEnum, .array:
717717
return "\(raw: type.swiftType).bridgeJSStackPop()"
718718
case .jsObject(let className?):
719719
return "\(raw: className)(unsafelyWrapping: JSObject.bridgeJSStackPop())"
720720
case .nullable(let wrappedType, let kind):
721721
return liftNullableExpression(wrappedType: wrappedType, kind: kind)
722-
case .array(let elementType):
723-
return liftArrayExpression(elementType: elementType)
724722
case .dictionary(let valueType):
725723
return liftDictionaryExpression(valueType: valueType)
726724
case .closure:
@@ -730,36 +728,6 @@ struct StackCodegen {
730728
}
731729
}
732730

733-
func liftArrayExpression(elementType: BridgeType) -> ExprSyntax {
734-
switch elementType {
735-
case .jsObject(let className?) where className != "JSObject":
736-
return "[JSObject].bridgeJSStackPop().map { \(raw: className)(unsafelyWrapping: $0) }"
737-
case .nullable, .closure:
738-
return liftArrayExpressionInline(elementType: elementType)
739-
case .void, .namespaceEnum:
740-
fatalError("Invalid array element type: \(elementType)")
741-
default:
742-
return "[\(raw: elementType.swiftType)].bridgeJSStackPop()"
743-
}
744-
}
745-
746-
private func liftArrayExpressionInline(elementType: BridgeType) -> ExprSyntax {
747-
let elementLift = liftExpression(for: elementType)
748-
let swiftTypeName = elementType.swiftType
749-
return """
750-
{
751-
let __count = Int(_swift_js_pop_i32())
752-
var __result: [\(raw: swiftTypeName)] = []
753-
__result.reserveCapacity(__count)
754-
for _ in 0..<__count {
755-
__result.append(\(elementLift))
756-
}
757-
__result.reverse()
758-
return __result
759-
}()
760-
"""
761-
}
762-
763731
func liftDictionaryExpression(valueType: BridgeType) -> ExprSyntax {
764732
switch valueType {
765733
case .jsObject(let className?) where className != "JSObject":
@@ -845,47 +813,15 @@ struct StackCodegen {
845813
varPrefix: String
846814
) -> [CodeBlockItemSyntax] {
847815
switch elementType {
848-
case .jsObject(let className?) where className != "JSObject":
849-
return ["\(raw: accessor).map { $0.jsObject }.bridgeJSStackPush()"]
850816
case .swiftProtocol:
851817
return ["\(raw: accessor).map { $0 as! \(raw: elementType.swiftType) }.bridgeJSStackPush()"]
852-
case .nullable, .closure:
853-
return lowerArrayStatementsInline(
854-
elementType: elementType,
855-
accessor: accessor,
856-
varPrefix: varPrefix
857-
)
858818
case .void, .namespaceEnum:
859819
fatalError("Invalid array element type: \(elementType)")
860820
default:
861821
return ["\(raw: accessor).bridgeJSStackPush()"]
862822
}
863823
}
864824

865-
private func lowerArrayStatementsInline(
866-
elementType: BridgeType,
867-
accessor: String,
868-
varPrefix: String
869-
) -> [CodeBlockItemSyntax] {
870-
var statements: [String] = []
871-
let elementVarName = "__bjs_elem_\(varPrefix)"
872-
statements.append("for \(elementVarName) in \(accessor) {")
873-
874-
let elementStatements = lowerStatements(
875-
for: elementType,
876-
accessor: elementVarName,
877-
varPrefix: "\(varPrefix)_elem"
878-
)
879-
for stmt in elementStatements {
880-
statements.append(stmt.description)
881-
}
882-
883-
statements.append("}")
884-
statements.append("_swift_js_push_i32(Int32(\(accessor).count))")
885-
let parsed: CodeBlockItemListSyntax = "\(raw: statements.joined(separator: "\n"))"
886-
return Array(parsed)
887-
}
888-
889825
private func lowerDictionaryStatements(
890826
valueType: BridgeType,
891827
accessor: String,

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift

Lines changed: 16 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -5692,20 +5692,8 @@ public func _bjs_roundTripGreeterArray() -> Void {
56925692
@_cdecl("bjs_roundTripOptionalIntArray")
56935693
public func _bjs_roundTripOptionalIntArray() -> Void {
56945694
#if arch(wasm32)
5695-
let ret = roundTripOptionalIntArray(_: {
5696-
let __count = Int(_swift_js_pop_i32())
5697-
var __result: [Optional<Int>] = []
5698-
__result.reserveCapacity(__count)
5699-
for _ in 0..<__count {
5700-
__result.append(Optional<Int>.bridgeJSStackPop())
5701-
}
5702-
__result.reverse()
5703-
return __result
5704-
}())
5705-
for __bjs_elem_ret in ret {
5706-
__bjs_elem_ret.bridgeJSStackPush()
5707-
}
5708-
_swift_js_push_i32(Int32(ret.count))
5695+
let ret = roundTripOptionalIntArray(_: [Optional<Int>].bridgeJSStackPop())
5696+
ret.bridgeJSStackPush()
57095697
#else
57105698
fatalError("Only available on WebAssembly")
57115699
#endif
@@ -5715,20 +5703,8 @@ public func _bjs_roundTripOptionalIntArray() -> Void {
57155703
@_cdecl("bjs_roundTripOptionalStringArray")
57165704
public func _bjs_roundTripOptionalStringArray() -> Void {
57175705
#if arch(wasm32)
5718-
let ret = roundTripOptionalStringArray(_: {
5719-
let __count = Int(_swift_js_pop_i32())
5720-
var __result: [Optional<String>] = []
5721-
__result.reserveCapacity(__count)
5722-
for _ in 0..<__count {
5723-
__result.append(Optional<String>.bridgeJSStackPop())
5724-
}
5725-
__result.reverse()
5726-
return __result
5727-
}())
5728-
for __bjs_elem_ret in ret {
5729-
__bjs_elem_ret.bridgeJSStackPush()
5730-
}
5731-
_swift_js_push_i32(Int32(ret.count))
5706+
let ret = roundTripOptionalStringArray(_: [Optional<String>].bridgeJSStackPop())
5707+
ret.bridgeJSStackPush()
57325708
#else
57335709
fatalError("Only available on WebAssembly")
57345710
#endif
@@ -5738,20 +5714,8 @@ public func _bjs_roundTripOptionalStringArray() -> Void {
57385714
@_cdecl("bjs_roundTripOptionalDataPointArray")
57395715
public func _bjs_roundTripOptionalDataPointArray() -> Void {
57405716
#if arch(wasm32)
5741-
let ret = roundTripOptionalDataPointArray(_: {
5742-
let __count = Int(_swift_js_pop_i32())
5743-
var __result: [Optional<DataPoint>] = []
5744-
__result.reserveCapacity(__count)
5745-
for _ in 0..<__count {
5746-
__result.append(Optional<DataPoint>.bridgeJSStackPop())
5747-
}
5748-
__result.reverse()
5749-
return __result
5750-
}())
5751-
for __bjs_elem_ret in ret {
5752-
__bjs_elem_ret.bridgeJSStackPush()
5753-
}
5754-
_swift_js_push_i32(Int32(ret.count))
5717+
let ret = roundTripOptionalDataPointArray(_: [Optional<DataPoint>].bridgeJSStackPop())
5718+
ret.bridgeJSStackPush()
57555719
#else
57565720
fatalError("Only available on WebAssembly")
57575721
#endif
@@ -5761,20 +5725,8 @@ public func _bjs_roundTripOptionalDataPointArray() -> Void {
57615725
@_cdecl("bjs_roundTripOptionalDirectionArray")
57625726
public func _bjs_roundTripOptionalDirectionArray() -> Void {
57635727
#if arch(wasm32)
5764-
let ret = roundTripOptionalDirectionArray(_: {
5765-
let __count = Int(_swift_js_pop_i32())
5766-
var __result: [Optional<Direction>] = []
5767-
__result.reserveCapacity(__count)
5768-
for _ in 0..<__count {
5769-
__result.append(Optional<Direction>.bridgeJSStackPop())
5770-
}
5771-
__result.reverse()
5772-
return __result
5773-
}())
5774-
for __bjs_elem_ret in ret {
5775-
__bjs_elem_ret.bridgeJSStackPush()
5776-
}
5777-
_swift_js_push_i32(Int32(ret.count))
5728+
let ret = roundTripOptionalDirectionArray(_: [Optional<Direction>].bridgeJSStackPop())
5729+
ret.bridgeJSStackPush()
57785730
#else
57795731
fatalError("Only available on WebAssembly")
57805732
#endif
@@ -5784,20 +5736,8 @@ public func _bjs_roundTripOptionalDirectionArray() -> Void {
57845736
@_cdecl("bjs_roundTripOptionalStatusArray")
57855737
public func _bjs_roundTripOptionalStatusArray() -> Void {
57865738
#if arch(wasm32)
5787-
let ret = roundTripOptionalStatusArray(_: {
5788-
let __count = Int(_swift_js_pop_i32())
5789-
var __result: [Optional<Status>] = []
5790-
__result.reserveCapacity(__count)
5791-
for _ in 0..<__count {
5792-
__result.append(Optional<Status>.bridgeJSStackPop())
5793-
}
5794-
__result.reverse()
5795-
return __result
5796-
}())
5797-
for __bjs_elem_ret in ret {
5798-
__bjs_elem_ret.bridgeJSStackPush()
5799-
}
5800-
_swift_js_push_i32(Int32(ret.count))
5739+
let ret = roundTripOptionalStatusArray(_: [Optional<Status>].bridgeJSStackPop())
5740+
ret.bridgeJSStackPush()
58015741
#else
58025742
fatalError("Only available on WebAssembly")
58035743
#endif
@@ -6005,20 +5945,8 @@ public func _bjs_roundTripJSObjectArray() -> Void {
60055945
@_cdecl("bjs_roundTripOptionalJSObjectArray")
60065946
public func _bjs_roundTripOptionalJSObjectArray() -> Void {
60075947
#if arch(wasm32)
6008-
let ret = roundTripOptionalJSObjectArray(_: {
6009-
let __count = Int(_swift_js_pop_i32())
6010-
var __result: [Optional<JSObject>] = []
6011-
__result.reserveCapacity(__count)
6012-
for _ in 0..<__count {
6013-
__result.append(Optional<JSObject>.bridgeJSStackPop())
6014-
}
6015-
__result.reverse()
6016-
return __result
6017-
}())
6018-
for __bjs_elem_ret in ret {
6019-
__bjs_elem_ret.bridgeJSStackPush()
6020-
}
6021-
_swift_js_push_i32(Int32(ret.count))
5948+
let ret = roundTripOptionalJSObjectArray(_: [Optional<JSObject>].bridgeJSStackPop())
5949+
ret.bridgeJSStackPush()
60225950
#else
60235951
fatalError("Only available on WebAssembly")
60245952
#endif
@@ -6028,8 +5956,8 @@ public func _bjs_roundTripOptionalJSObjectArray() -> Void {
60285956
@_cdecl("bjs_roundTripFooArray")
60295957
public func _bjs_roundTripFooArray() -> Void {
60305958
#if arch(wasm32)
6031-
let ret = roundTripFooArray(_: [JSObject].bridgeJSStackPop().map { Foo(unsafelyWrapping: $0) })
6032-
ret.map { $0.jsObject }.bridgeJSStackPush()
5959+
let ret = roundTripFooArray(_: [Foo].bridgeJSStackPop())
5960+
ret.bridgeJSStackPush()
60335961
#else
60345962
fatalError("Only available on WebAssembly")
60355963
#endif
@@ -6039,20 +5967,8 @@ public func _bjs_roundTripFooArray() -> Void {
60395967
@_cdecl("bjs_roundTripOptionalFooArray")
60405968
public func _bjs_roundTripOptionalFooArray() -> Void {
60415969
#if arch(wasm32)
6042-
let ret = roundTripOptionalFooArray(_: {
6043-
let __count = Int(_swift_js_pop_i32())
6044-
var __result: [Optional<Foo>] = []
6045-
__result.reserveCapacity(__count)
6046-
for _ in 0..<__count {
6047-
__result.append(Optional<JSObject>.bridgeJSStackPop().map { Foo(unsafelyWrapping: $0) })
6048-
}
6049-
__result.reverse()
6050-
return __result
6051-
}())
6052-
for __bjs_elem_ret in ret {
6053-
__bjs_elem_ret.bridgeJSStackPush()
6054-
}
6055-
_swift_js_push_i32(Int32(ret.count))
5970+
let ret = roundTripOptionalFooArray(_: [Optional<Foo>].bridgeJSStackPop())
5971+
ret.bridgeJSStackPush()
60565972
#else
60575973
fatalError("Only available on WebAssembly")
60585974
#endif

0 commit comments

Comments
 (0)