Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 8 additions & 56 deletions Benchmarks/Sources/Generated/BridgeJS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1578,16 +1578,7 @@ public func _bjs_ArrayRoundtrip_roundtripNestedPointArray(_ _self: UnsafeMutable
@_cdecl("bjs_ArrayRoundtrip_takeOptionalIntArray")
public func _bjs_ArrayRoundtrip_takeOptionalIntArray(_ _self: UnsafeMutableRawPointer) -> Void {
#if arch(wasm32)
ArrayRoundtrip.bridgeJSLiftParameter(_self).takeOptionalIntArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<Int>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<Int>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
ArrayRoundtrip.bridgeJSLiftParameter(_self).takeOptionalIntArray(_: [Optional<Int>].bridgeJSStackPop())
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -1598,10 +1589,7 @@ public func _bjs_ArrayRoundtrip_takeOptionalIntArray(_ _self: UnsafeMutableRawPo
public func _bjs_ArrayRoundtrip_makeOptionalIntArray(_ _self: UnsafeMutableRawPointer) -> Void {
#if arch(wasm32)
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).makeOptionalIntArray()
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -1611,20 +1599,8 @@ public func _bjs_ArrayRoundtrip_makeOptionalIntArray(_ _self: UnsafeMutableRawPo
@_cdecl("bjs_ArrayRoundtrip_roundtripOptionalIntArray")
public func _bjs_ArrayRoundtrip_roundtripOptionalIntArray(_ _self: UnsafeMutableRawPointer) -> Void {
#if arch(wasm32)
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).roundtripOptionalIntArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<Int>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<Int>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).roundtripOptionalIntArray(_: [Optional<Int>].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -1634,16 +1610,7 @@ public func _bjs_ArrayRoundtrip_roundtripOptionalIntArray(_ _self: UnsafeMutable
@_cdecl("bjs_ArrayRoundtrip_takeOptionalPointArray")
public func _bjs_ArrayRoundtrip_takeOptionalPointArray(_ _self: UnsafeMutableRawPointer) -> Void {
#if arch(wasm32)
ArrayRoundtrip.bridgeJSLiftParameter(_self).takeOptionalPointArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<Point>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<Point>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
ArrayRoundtrip.bridgeJSLiftParameter(_self).takeOptionalPointArray(_: [Optional<Point>].bridgeJSStackPop())
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -1654,10 +1621,7 @@ public func _bjs_ArrayRoundtrip_takeOptionalPointArray(_ _self: UnsafeMutableRaw
public func _bjs_ArrayRoundtrip_makeOptionalPointArray(_ _self: UnsafeMutableRawPointer) -> Void {
#if arch(wasm32)
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).makeOptionalPointArray()
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -1667,20 +1631,8 @@ public func _bjs_ArrayRoundtrip_makeOptionalPointArray(_ _self: UnsafeMutableRaw
@_cdecl("bjs_ArrayRoundtrip_roundtripOptionalPointArray")
public func _bjs_ArrayRoundtrip_roundtripOptionalPointArray(_ _self: UnsafeMutableRawPointer) -> Void {
#if arch(wasm32)
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).roundtripOptionalPointArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<Point>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<Point>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
let ret = ArrayRoundtrip.bridgeJSLiftParameter(_self).roundtripOptionalPointArray(_: [Optional<Point>].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand Down
66 changes: 1 addition & 65 deletions Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -713,14 +713,12 @@ struct StackCodegen {
switch type {
case .string, .int, .uint, .bool, .float, .double,
.jsObject(nil), .jsValue, .swiftStruct, .swiftHeapObject, .unsafePointer,
.swiftProtocol, .caseEnum, .associatedValueEnum, .rawValueEnum:
.swiftProtocol, .caseEnum, .associatedValueEnum, .rawValueEnum, .array:
return "\(raw: type.swiftType).bridgeJSStackPop()"
case .jsObject(let className?):
return "\(raw: className)(unsafelyWrapping: JSObject.bridgeJSStackPop())"
case .nullable(let wrappedType, let kind):
return liftNullableExpression(wrappedType: wrappedType, kind: kind)
case .array(let elementType):
return liftArrayExpression(elementType: elementType)
case .dictionary(let valueType):
return liftDictionaryExpression(valueType: valueType)
case .closure:
Expand All @@ -730,36 +728,6 @@ struct StackCodegen {
}
}

func liftArrayExpression(elementType: BridgeType) -> ExprSyntax {
switch elementType {
case .jsObject(let className?) where className != "JSObject":
return "[JSObject].bridgeJSStackPop().map { \(raw: className)(unsafelyWrapping: $0) }"
case .nullable, .closure:
return liftArrayExpressionInline(elementType: elementType)
case .void, .namespaceEnum:
fatalError("Invalid array element type: \(elementType)")
default:
return "[\(raw: elementType.swiftType)].bridgeJSStackPop()"
}
}

private func liftArrayExpressionInline(elementType: BridgeType) -> ExprSyntax {
let elementLift = liftExpression(for: elementType)
let swiftTypeName = elementType.swiftType
return """
{
let __count = Int(_swift_js_pop_i32())
var __result: [\(raw: swiftTypeName)] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(\(elementLift))
}
__result.reverse()
return __result
}()
"""
}

func liftDictionaryExpression(valueType: BridgeType) -> ExprSyntax {
switch valueType {
case .jsObject(let className?) where className != "JSObject":
Expand Down Expand Up @@ -845,47 +813,15 @@ struct StackCodegen {
varPrefix: String
) -> [CodeBlockItemSyntax] {
switch elementType {
case .jsObject(let className?) where className != "JSObject":
return ["\(raw: accessor).map { $0.jsObject }.bridgeJSStackPush()"]
case .swiftProtocol:
return ["\(raw: accessor).map { $0 as! \(raw: elementType.swiftType) }.bridgeJSStackPush()"]
case .nullable, .closure:
return lowerArrayStatementsInline(
elementType: elementType,
accessor: accessor,
varPrefix: varPrefix
)
case .void, .namespaceEnum:
fatalError("Invalid array element type: \(elementType)")
default:
return ["\(raw: accessor).bridgeJSStackPush()"]
}
}

private func lowerArrayStatementsInline(
elementType: BridgeType,
accessor: String,
varPrefix: String
) -> [CodeBlockItemSyntax] {
var statements: [String] = []
let elementVarName = "__bjs_elem_\(varPrefix)"
statements.append("for \(elementVarName) in \(accessor) {")

let elementStatements = lowerStatements(
for: elementType,
accessor: elementVarName,
varPrefix: "\(varPrefix)_elem"
)
for stmt in elementStatements {
statements.append(stmt.description)
}

statements.append("}")
statements.append("_swift_js_push_i32(Int32(\(accessor).count))")
let parsed: CodeBlockItemListSyntax = "\(raw: statements.joined(separator: "\n"))"
return Array(parsed)
}

private func lowerDictionaryStatements(
valueType: BridgeType,
accessor: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,8 @@ public func _bjs_processOpaquePointerArray() -> Void {
@_cdecl("bjs_processOptionalIntArray")
public func _bjs_processOptionalIntArray() -> Void {
#if arch(wasm32)
let ret = processOptionalIntArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<Int>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<Int>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
let ret = processOptionalIntArray(_: [Optional<Int>].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -251,20 +239,8 @@ public func _bjs_processOptionalIntArray() -> Void {
@_cdecl("bjs_processOptionalStringArray")
public func _bjs_processOptionalStringArray() -> Void {
#if arch(wasm32)
let ret = processOptionalStringArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<String>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<String>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
let ret = processOptionalStringArray(_: [Optional<String>].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -285,20 +261,8 @@ public func _bjs_processOptionalArray() -> Void {
@_cdecl("bjs_processOptionalPointArray")
public func _bjs_processOptionalPointArray() -> Void {
#if arch(wasm32)
let ret = processOptionalPointArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<Point>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<Point>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
let ret = processOptionalPointArray(_: [Optional<Point>].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -308,20 +272,8 @@ public func _bjs_processOptionalPointArray() -> Void {
@_cdecl("bjs_processOptionalDirectionArray")
public func _bjs_processOptionalDirectionArray() -> Void {
#if arch(wasm32)
let ret = processOptionalDirectionArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<Direction>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<Direction>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
let ret = processOptionalDirectionArray(_: [Optional<Direction>].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -331,20 +283,8 @@ public func _bjs_processOptionalDirectionArray() -> Void {
@_cdecl("bjs_processOptionalStatusArray")
public func _bjs_processOptionalStatusArray() -> Void {
#if arch(wasm32)
let ret = processOptionalStatusArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<Status>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<Status>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
let ret = processOptionalStatusArray(_: [Optional<Status>].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand Down Expand Up @@ -420,20 +360,8 @@ public func _bjs_processJSObjectArray() -> Void {
@_cdecl("bjs_processOptionalJSObjectArray")
public func _bjs_processOptionalJSObjectArray() -> Void {
#if arch(wasm32)
let ret = processOptionalJSObjectArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<JSObject>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<JSObject>.bridgeJSStackPop())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
let ret = processOptionalJSObjectArray(_: [Optional<JSObject>].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public func _bjs_makeFoo() -> Int32 {
@_cdecl("bjs_processFooArray")
public func _bjs_processFooArray() -> Void {
#if arch(wasm32)
let ret = processFooArray(_: [JSObject].bridgeJSStackPop().map { Foo(unsafelyWrapping: $0) })
ret.map { $0.jsObject }.bridgeJSStackPush()
let ret = processFooArray(_: [Foo].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand All @@ -86,20 +86,8 @@ public func _bjs_processFooArray() -> Void {
@_cdecl("bjs_processOptionalFooArray")
public func _bjs_processOptionalFooArray() -> Void {
#if arch(wasm32)
let ret = processOptionalFooArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [Optional<Foo>] = []
__result.reserveCapacity(__count)
for _ in 0..<__count {
__result.append(Optional<JSObject>.bridgeJSStackPop().map { Foo(unsafelyWrapping: $0) })
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSStackPush()
}
_swift_js_push_i32(Int32(ret.count))
let ret = processOptionalFooArray(_: [Optional<Foo>].bridgeJSStackPop())
ret.bridgeJSStackPush()
#else
fatalError("Only available on WebAssembly")
#endif
Expand Down
Loading