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
24 changes: 18 additions & 6 deletions Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,10 @@ struct StackCodegen {
func liftExpression(for type: BridgeType) -> ExprSyntax {
switch type {
case .string, .int, .uint, .bool, .float, .double,
.jsObject, .swiftStruct, .swiftHeapObject:
.jsObject(nil), .swiftStruct, .swiftHeapObject:
return "\(raw: type.swiftType).bridgeJSLiftParameter()"
case .jsObject(let className?):
return "\(raw: className)(unsafelyWrapping: JSObject.bridgeJSLiftParameter())"
case .unsafePointer:
return "\(raw: type.swiftType).bridgeJSLiftParameter()"
case .swiftProtocol(let protocolName):
Expand Down Expand Up @@ -830,9 +832,11 @@ struct StackCodegen {
func liftArrayExpression(elementType: BridgeType) -> ExprSyntax {
switch elementType {
case .int, .uint, .float, .double, .string, .bool,
.jsObject, .swiftStruct, .caseEnum, .swiftHeapObject,
.jsObject(nil), .swiftStruct, .caseEnum, .swiftHeapObject,
.unsafePointer, .rawValueEnum, .associatedValueEnum:
return "[\(raw: elementType.swiftType)].bridgeJSLiftParameter()"
case .jsObject(_?):
return liftArrayExpressionInline(elementType: elementType)
case .swiftProtocol(let protocolName):
return "[Any\(raw: protocolName)].bridgeJSLiftParameter()"
case .optional, .array, .closure:
Expand Down Expand Up @@ -861,9 +865,11 @@ struct StackCodegen {

private func liftOptionalExpression(wrappedType: BridgeType) -> ExprSyntax {
switch wrappedType {
case .string, .int, .uint, .bool, .float, .double, .jsObject,
case .string, .int, .uint, .bool, .float, .double, .jsObject(nil),
.swiftStruct, .swiftHeapObject, .caseEnum, .associatedValueEnum, .rawValueEnum:
return "Optional<\(raw: wrappedType.swiftType)>.bridgeJSLiftParameter()"
case .jsObject(let className?):
return "Optional<JSObject>.bridgeJSLiftParameter().map { \(raw: className)(unsafelyWrapping: $0) }"
case .array(let elementType):
let arrayLift = liftArrayExpression(elementType: elementType)
let swiftTypeName = elementType.swiftType
Expand Down Expand Up @@ -896,8 +902,10 @@ struct StackCodegen {
switch type {
case .string, .int, .uint, .bool, .float, .double:
return ["\(raw: accessor).bridgeJSLowerStackReturn()"]
case .jsObject:
case .jsObject(nil):
return ["\(raw: accessor).bridgeJSLowerStackReturn()"]
case .jsObject(_?):
return ["\(raw: accessor).jsObject.bridgeJSLowerStackReturn()"]
case .swiftHeapObject, .unsafePointer, .closure:
return ["\(raw: accessor).bridgeJSLowerStackReturn()"]
case .swiftProtocol(let protocolName):
Expand All @@ -923,9 +931,11 @@ struct StackCodegen {
) -> [CodeBlockItemSyntax] {
switch elementType {
case .int, .uint, .float, .double, .string, .bool,
.jsObject, .swiftStruct, .caseEnum, .swiftHeapObject,
.jsObject(nil), .swiftStruct, .caseEnum, .swiftHeapObject,
.unsafePointer, .rawValueEnum, .associatedValueEnum:
return ["\(raw: accessor).bridgeJSLowerReturn()"]
case .jsObject(_?):
return ["\(raw: accessor).map { $0.jsObject }.bridgeJSLowerReturn()"]
case .swiftProtocol(let protocolName):
return ["\(raw: accessor).map { $0 as! Any\(raw: protocolName) }.bridgeJSLowerReturn()"]
case .optional, .array, .closure:
Expand Down Expand Up @@ -1003,8 +1013,10 @@ struct StackCodegen {
case .associatedValueEnum:
// Push payloads via bridgeJSLowerParameter(), then push the returned case ID
return ["_swift_js_push_i32(\(raw: unwrappedVar).bridgeJSLowerParameter())"]
case .jsObject:
case .jsObject(nil):
return ["\(raw: unwrappedVar).bridgeJSLowerStackReturn()"]
case .jsObject(_?):
return ["\(raw: unwrappedVar).jsObject.bridgeJSLowerStackReturn()"]
case .array(let elementType):
return lowerArrayStatements(elementType: elementType, accessor: unwrappedVar, varPrefix: varPrefix)
default:
Expand Down
1 change: 0 additions & 1 deletion Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2474,7 +2474,6 @@ struct IntrinsicJSFragment: Sendable {
let idVar = scope.variable("objId")
printer.write("const \(idVar) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(value));")
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(idVar));")
cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));")
return []
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
@JS func processItemArray(_ items: [Item]) -> [Item]
@JS func processNestedItemArray(_ items: [[Item]]) -> [[Item]]

@JS func processJSObjectArray(_ objects: [JSObject]) -> [JSObject]
@JS func processOptionalJSObjectArray(_ objects: [JSObject?]) -> [JSObject?]
@JS func processNestedJSObjectArray(_ objects: [[JSObject]]) -> [[JSObject]]

@JSFunction func checkArray(_ a: JSObject) throws(JSException) -> Void
@JSFunction func checkArrayWithLength(_ a: JSObject, _ b: Double) throws(JSException) -> Void
@JSFunction func checkArray(_ a: JSObject) throws(JSException) -> Void
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@
@JS func makeFoo() throws(JSException) -> Foo {
return try Foo()
}

@JS func processFooArray(_ foos: [Foo]) -> [Foo]
@JS func processOptionalFooArray(_ foos: [Foo?]) -> [Foo?]

@JS struct FooContainer {
var foo: Foo
var optionalFoo: Foo?
}

@JS func roundtripFooContainer(_ container: FooContainer) -> FooContainer
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@
@JS static var computedSetting: String { "Config: \(defaultConfig)" }
@JS static func update(_ timeout: Double) -> Double
}

@JS struct Container {
var object: JSObject
var optionalObject: JSObject?
}

@JS func roundtripContainer(_ container: Container) -> Container
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,121 @@
}
}
}
},
{
"abiName" : "bjs_processJSObjectArray",
"effects" : {
"isAsync" : false,
"isStatic" : false,
"isThrows" : false
},
"name" : "processJSObjectArray",
"parameters" : [
{
"label" : "_",
"name" : "objects",
"type" : {
"array" : {
"_0" : {
"jsObject" : {

}
}
}
}
}
],
"returnType" : {
"array" : {
"_0" : {
"jsObject" : {

}
}
}
}
},
{
"abiName" : "bjs_processOptionalJSObjectArray",
"effects" : {
"isAsync" : false,
"isStatic" : false,
"isThrows" : false
},
"name" : "processOptionalJSObjectArray",
"parameters" : [
{
"label" : "_",
"name" : "objects",
"type" : {
"array" : {
"_0" : {
"optional" : {
"_0" : {
"jsObject" : {

}
}
}
}
}
}
}
],
"returnType" : {
"array" : {
"_0" : {
"optional" : {
"_0" : {
"jsObject" : {

}
}
}
}
}
}
},
{
"abiName" : "bjs_processNestedJSObjectArray",
"effects" : {
"isAsync" : false,
"isStatic" : false,
"isThrows" : false
},
"name" : "processNestedJSObjectArray",
"parameters" : [
{
"label" : "_",
"name" : "objects",
"type" : {
"array" : {
"_0" : {
"array" : {
"_0" : {
"jsObject" : {

}
}
}
}
}
}
}
],
"returnType" : {
"array" : {
"_0" : {
"array" : {
"_0" : {
"jsObject" : {

}
}
}
}
}
}
}
],
"protocols" : [
Expand Down Expand Up @@ -1027,24 +1142,6 @@
"returnType" : {
"void" : {

}
}
},
{
"name" : "checkArray",
"parameters" : [
{
"name" : "a",
"type" : {
"jsObject" : {

}
}
}
],
"returnType" : {
"void" : {

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,64 @@ public func _bjs_processNestedItemArray() -> Void {
#endif
}

@_expose(wasm, "bjs_processJSObjectArray")
@_cdecl("bjs_processJSObjectArray")
public func _bjs_processJSObjectArray() -> Void {
#if arch(wasm32)
let ret = processJSObjectArray(_: [JSObject].bridgeJSLiftParameter())
ret.bridgeJSLowerReturn()
#else
fatalError("Only available on WebAssembly")
#endif
}

@_expose(wasm, "bjs_processOptionalJSObjectArray")
@_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>.bridgeJSLiftParameter())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
let __bjs_isSome_ret_elem = __bjs_elem_ret != nil
if let __bjs_unwrapped_ret_elem = __bjs_elem_ret {
__bjs_unwrapped_ret_elem.bridgeJSLowerStackReturn()}
_swift_js_push_i32(__bjs_isSome_ret_elem ? 1 : 0)}
_swift_js_push_i32(Int32(ret.count))
#else
fatalError("Only available on WebAssembly")
#endif
}

@_expose(wasm, "bjs_processNestedJSObjectArray")
@_cdecl("bjs_processNestedJSObjectArray")
public func _bjs_processNestedJSObjectArray() -> Void {
#if arch(wasm32)
let ret = processNestedJSObjectArray(_: {
let __count = Int(_swift_js_pop_i32())
var __result: [[JSObject]] = []
__result.reserveCapacity(__count)
for _ in 0 ..< __count {
__result.append([JSObject].bridgeJSLiftParameter())
}
__result.reverse()
return __result
}())
for __bjs_elem_ret in ret {
__bjs_elem_ret.bridgeJSLowerReturn()}
_swift_js_push_i32(Int32(ret.count))
#else
fatalError("Only available on WebAssembly")
#endif
}

@_expose(wasm, "bjs_Item_deinit")
@_cdecl("bjs_Item_deinit")
public func _bjs_Item_deinit(_ pointer: UnsafeMutableRawPointer) -> Void {
Expand Down Expand Up @@ -523,21 +581,4 @@ func _$checkArrayWithLength(_ a: JSObject, _ b: Double) throws(JSException) -> V
if let error = _swift_js_take_exception() {
throw error
}
}

#if arch(wasm32)
@_extern(wasm, module: "TestModule", name: "bjs_checkArray")
fileprivate func bjs_checkArray(_ a: Int32) -> Void
#else
fileprivate func bjs_checkArray(_ a: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif

func _$checkArray(_ a: JSObject) throws(JSException) -> Void {
let aValue = a.bridgeJSLowerParameter()
bjs_checkArray(aValue)
if let error = _swift_js_take_exception() {
throw error
}
}
Loading