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
13 changes: 8 additions & 5 deletions Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,11 @@ public class ExportSwift {

func liftParameter(param: Parameter) throws {
parameters.append(param)
let escapedName = param.escapedSwiftIdentifier
let liftingInfo = try param.type.liftParameterInfo()
let argumentsToLift: [String]
if liftingInfo.parameters.count == 1 {
argumentsToLift = [param.name]
argumentsToLift = [escapedName]
} else {
argumentsToLift = liftingInfo.parameters.map { (name, _) in param.name + name.capitalizedFirstLetter }
}
Expand All @@ -227,7 +228,9 @@ public class ExportSwift {
switch param.type {
case .closure(let signature, _):
typeNameForIntrinsic = param.type.swiftType
liftingExpr = ExprSyntax("_BJS_Closure_\(raw: signature.mangleName).bridgeJSLift(\(raw: param.name))")
liftingExpr = ExprSyntax(
"_BJS_Closure_\(raw: signature.mangleName).bridgeJSLift(\(raw: escapedName))"
)
case .swiftStruct(let structName):
typeNameForIntrinsic = structName
liftingExpr = ExprSyntax("\(raw: structName).bridgeJSLiftParameter()")
Expand All @@ -252,7 +255,7 @@ public class ExportSwift {
)
}

parameterBindings.insert("let \(raw: param.name) = \(liftingExpr)", at: 0)
parameterBindings.insert("let \(raw: param.escapedSwiftBindingIdentifier) = \(liftingExpr)", at: 0)
for (name, type) in zip(argumentsToLift, liftingInfo.parameters.map { $0.type }) {
abiParameterSignatures.append((name, type))
}
Expand All @@ -268,14 +271,14 @@ public class ExportSwift {
}

private func removeFirstLiftedParameter() -> String {
return parameters.removeFirst().name
return parameters.removeFirst().escapedSwiftIdentifier
}

private func renderCallStatement(callee: ExprSyntax, returnType: BridgeType) -> CodeBlockItemSyntax {
let labeledParams = parameters.map { param in
LabeledExprSyntax(
label: param.label,
expression: DeclReferenceExprSyntax(baseName: .identifier(param.name))
expression: DeclReferenceExprSyntax(baseName: .identifier(param.escapedSwiftIdentifier))
)
}
var callExpr: ExprSyntax =
Expand Down
42 changes: 27 additions & 15 deletions Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public struct ImportTS {
}

func lowerParameter(param: Parameter) throws {
if let genericPush = param.type.genericStackPushStatement(value: param.name) {
let escapedName = param.escapedSwiftIdentifier
if let genericPush = param.type.genericStackPushStatement(value: escapedName) {
stackLoweringStmts.insert(genericPush, at: 0)
return
}
Expand All @@ -153,10 +154,10 @@ public struct ImportTS {
switch param.type {
case .closure(let signature, useJSTypedClosure: false):
let jsTypedClosureType = BridgeType.closure(signature, useJSTypedClosure: true).swiftType
body.write("let \(param.name) = \(jsTypedClosureType)(\(param.name))")
body.write("let \(param.escapedSwiftBindingIdentifier) = \(jsTypedClosureType)(\(escapedName))")
// The just created JSObject is not owned by the caller unlike those passed in parameters,
// so we need to extend its lifetime during the call to ensure the JSObject.id is valid.
valuesToExtendLifetimeDuringCall.append(param.name)
valuesToExtendLifetimeDuringCall.append(escapedName)
default:
break
}
Expand All @@ -177,7 +178,7 @@ public struct ImportTS {
if loweringInfo.useBorrowing {
let returnVariableName = "ret\(borrowedArguments.count)"
let assign = needsReturnVariable ? "let \(returnVariableName) = " : ""
body.write("\(assign)\(param.name).bridgeJSWithLoweredParameter { \(pattern) in")
body.write("\(assign)\(escapedName).bridgeJSWithLoweredParameter { \(pattern) in")
body.indent()
borrowedArguments.append(
BorrowedArgument(
Expand All @@ -190,10 +191,10 @@ public struct ImportTS {
)
} else if case .nullable(.swiftProtocol(let protocolName), _) = param.type, context == .exportSwift {
body.write("let \(pattern): (Int32, Int32)")
body.write("if let \(param.name) {")
body.write("if let \(param.escapedSwiftBindingIdentifier) {")
body.indent {
body.write(
"\(pattern) = (1, _bridgeJSUnwrapProtocolExportable(\(param.name), \"\(protocolName)\").bridgeJSLowerAsProtocolReturn())"
"\(pattern) = (1, _bridgeJSUnwrapProtocolExportable(\(escapedName), \"\(protocolName)\").bridgeJSLowerAsProtocolReturn())"
)
}
body.write("} else {")
Expand All @@ -205,10 +206,10 @@ public struct ImportTS {
let initializerExpr: ExprSyntax
if case .swiftProtocol(let protocolName) = param.type, context == .exportSwift {
initializerExpr = ExprSyntax(
"_bridgeJSUnwrapProtocolExportable(\(raw: param.name), \"\(raw: protocolName)\").bridgeJSLowerAsProtocolReturn()"
"_bridgeJSUnwrapProtocolExportable(\(raw: escapedName), \"\(raw: protocolName)\").bridgeJSLowerAsProtocolReturn()"
)
} else {
initializerExpr = ExprSyntax("\(raw: param.name).bridgeJSLowerParameter()")
initializerExpr = ExprSyntax("\(raw: escapedName).bridgeJSLowerParameter()")
}

let binding = loweringInfo.loweredParameters.isEmpty ? "_" : pattern
Expand All @@ -218,10 +219,10 @@ public struct ImportTS {

// Add to signatures and forwardings (unified for both single and multiple)
for (index, (paramName, type)) in loweringInfo.loweredParameters.enumerated() {
// For single parameter, use param.name; for multiple, use constructed name
// For single parameter, use the escaped name; for multiple, use constructed name
let abiParamName: String
if loweringInfo.loweredParameters.count == 1 {
abiParamName = param.name
abiParamName = escapedName
} else {
abiParamName = "\(param.name)\(paramName.capitalizedFirstLetter)"
}
Expand Down Expand Up @@ -723,20 +724,21 @@ struct SwiftSignatureBuilder {
var out = "("
out += parameters.map { param in
let label = param.label ?? param.name
let escapedName = param.escapedSwiftIdentifier
let paramType = buildParameterTypeSyntax(from: param.type)

if useWildcardLabels {
// Always use wildcard labels: "_ name: Type"
return "_ \(param.name): \(paramType)"
} else if label == param.name {
return "_ \(escapedName): \(paramType)"
} else if SwiftToSkeleton.normalizeIdentifier(label) == param.name {
// External label same as parameter name: "count: Int"
return "\(param.name): \(paramType)"
return "\(escapedName): \(paramType)"
} else if param.label == nil {
// No label specified: use wildcard "_ name: Type"
return "_ \(param.name): \(paramType)"
return "_ \(escapedName): \(paramType)"
} else {
// External label differs: "label count: Int"
return "\(label) \(param.name): \(paramType)"
return "\(label) \(escapedName): \(paramType)"
}
}.joined(separator: ", ")
out += ")"
Expand Down Expand Up @@ -1065,3 +1067,13 @@ extension String {
return self.isValidSwiftIdentifier(for: .variableName) ? self : "`\(self)`"
}
}

extension Parameter {
var escapedSwiftIdentifier: String {
name == "self" ? name : name.backtickIfNeeded()
}

var escapedSwiftBindingIdentifier: String {
name.backtickIfNeeded()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ private final class ExportSwiftAPICollector: SyntaxAnyVisitor {
continue
}

let name = param.secondName?.text ?? param.firstName.text
let name = SwiftToSkeleton.normalizeIdentifier(param.secondName?.text ?? param.firstName.text)
let label = param.firstName.text

let defaultValue: DefaultValue?
Expand Down
Loading
Loading