Skip to content

Commit f3434b2

Browse files
committed
BridgeJS: Qualify generated JS helper names by module
1 parent 8fb8c16 commit f3434b2

30 files changed

Lines changed: 807 additions & 573 deletions

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,9 @@ public struct BridgeJSLink {
521521

522522
private func generateAddImports(needsImportsObject: Bool) throws -> CodeFragmentPrinter {
523523
let printer = CodeFragmentPrinter()
524-
let allStructs = skeletons.compactMap { $0.exported?.structs }.flatMap { $0 }
524+
let allStructs = skeletons.flatMap { unified in
525+
(unified.exported?.structs ?? []).map { (moduleName: unified.moduleName, structDef: $0) }
526+
}
525527
printer.write("return {")
526528
try printer.indent {
527529
printer.write(lines: [
@@ -669,19 +671,20 @@ public struct BridgeJSLink {
669671
}
670672
printer.write("}")
671673
if !allStructs.isEmpty {
672-
for structDef in allStructs {
674+
for (moduleName, structDef) in allStructs {
675+
let key = HelperNaming.qualified(base: structDef.abiName, module: moduleName)
673676
printer.write("bjs[\"swift_js_struct_lower_\(structDef.abiName)\"] = function(objectId) {")
674677
printer.indent {
675678
printer.write(
676-
"\(JSGlueVariableScope.reservedStructHelpers).\(structDef.abiName).lower(\(JSGlueVariableScope.reservedSwift).memory.getObject(objectId));"
679+
"\(JSGlueVariableScope.reservedStructHelpers).\(key).lower(\(JSGlueVariableScope.reservedSwift).memory.getObject(objectId));"
677680
)
678681
}
679682
printer.write("}")
680683

681684
printer.write("bjs[\"swift_js_struct_lift_\(structDef.abiName)\"] = function() {")
682685
printer.indent {
683686
printer.write(
684-
"const value = \(JSGlueVariableScope.reservedStructHelpers).\(structDef.abiName).lift();"
687+
"const value = \(JSGlueVariableScope.reservedStructHelpers).\(key).lift();"
685688
)
686689
printer.write("return \(JSGlueVariableScope.reservedSwift).memory.retain(value);")
687690
}
@@ -1255,12 +1258,18 @@ public struct BridgeJSLink {
12551258

12561259
let bodyPrinter = CodeFragmentPrinter()
12571260
let allStructs = exportedSkeletons.flatMap { $0.structs }
1258-
for structDef in allStructs {
1261+
for (moduleName, structDef) in skeletons.flatMap({ unified in
1262+
(unified.exported?.structs ?? []).map { (unified.moduleName, $0) }
1263+
}) {
12591264
let structPrinter = CodeFragmentPrinter()
12601265
let structScope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
1261-
let fragment = IntrinsicJSFragment.structHelper(structDefinition: structDef, allStructs: allStructs)
1266+
let fragment = IntrinsicJSFragment.structHelper(
1267+
structDefinition: structDef,
1268+
allStructs: allStructs,
1269+
moduleName: moduleName
1270+
)
12621271
_ = try fragment.printCode(
1263-
[structDef.abiName],
1272+
[],
12641273
IntrinsicJSFragment.PrintCodeContext(
12651274
scope: structScope,
12661275
printer: structPrinter,
@@ -1271,13 +1280,16 @@ public struct BridgeJSLink {
12711280
bodyPrinter.write(lines: structPrinter.lines)
12721281
}
12731282

1274-
let allAssocEnums = exportedSkeletons.flatMap {
1275-
$0.enums.filter { $0.enumType == .associatedValue }
1276-
}
1277-
for enumDef in allAssocEnums {
1283+
for (moduleName, enumDef) in skeletons.flatMap({ unified in
1284+
(unified.exported?.enums ?? []).filter { $0.enumType == .associatedValue }
1285+
.map { (unified.moduleName, $0) }
1286+
}) {
12781287
let enumPrinter = CodeFragmentPrinter()
12791288
let enumScope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
1280-
let fragment = IntrinsicJSFragment.associatedValueEnumHelperFactory(enumDefinition: enumDef)
1289+
let fragment = IntrinsicJSFragment.associatedValueEnumHelperFactory(
1290+
enumDefinition: enumDef,
1291+
moduleName: moduleName
1292+
)
12811293
_ = try fragment.printCode(
12821294
[enumDef.valuesName],
12831295
IntrinsicJSFragment.PrintCodeContext(
@@ -1419,8 +1431,8 @@ public struct BridgeJSLink {
14191431
return (outputJs, outputDts)
14201432
}
14211433

1422-
/// Maps every type name a `BridgeType` can carry to the module that declares
1423-
/// it, so identifiers minted from type names can be module-qualified.
1434+
/// Maps exported Swift `@JS` type names to their declaring modules so
1435+
/// identifiers minted from those names can be module-qualified.
14241436
///
14251437
/// A name declared by two modules is a pre-existing ambiguity in the
14261438
/// skeleton format (`BridgeType` carries only the name), so the first
@@ -1451,24 +1463,20 @@ public struct BridgeJSLink {
14511463
record(protocolDef.name, moduleName)
14521464
}
14531465
}
1454-
for file in unified.imported?.children ?? [] {
1455-
for type in file.types {
1456-
record(type.name, moduleName)
1457-
}
1458-
}
14591466
}
14601467
return result
14611468
}
14621469

14631470
private func enumHelperAssignments() -> CodeFragmentPrinter {
14641471
let printer = CodeFragmentPrinter()
14651472

1466-
for skeleton in skeletons.compactMap(\.exported) {
1473+
for unified in skeletons {
1474+
guard let skeleton = unified.exported else { continue }
14671475
for enumDef in skeleton.enums where enumDef.enumType == .associatedValue {
1468-
printer.write(
1469-
"const \(enumDef.name)Helpers = __bjs_create\(enumDef.valuesName)Helpers();"
1470-
)
1471-
printer.write("\(JSGlueVariableScope.reservedEnumHelpers).\(enumDef.name) = \(enumDef.name)Helpers;")
1476+
let key = HelperNaming.qualified(base: enumDef.name, module: unified.moduleName)
1477+
let local = HelperNaming.helperConstant(key)
1478+
printer.write("const \(local) = \(HelperNaming.enumHelperFactory(key))();")
1479+
printer.write("\(JSGlueVariableScope.reservedEnumHelpers).\(key) = \(local);")
14721480
printer.nextLine()
14731481
}
14741482
}
@@ -1479,14 +1487,13 @@ public struct BridgeJSLink {
14791487
private func structHelperAssignments() -> CodeFragmentPrinter {
14801488
let printer = CodeFragmentPrinter()
14811489

1482-
for skeleton in skeletons.compactMap(\.exported) {
1490+
for unified in skeletons {
1491+
guard let skeleton = unified.exported else { continue }
14831492
for structDef in skeleton.structs {
1484-
printer.write(
1485-
"const \(structDef.abiName)Helpers = __bjs_create\(structDef.abiName)Helpers();"
1486-
)
1487-
printer.write(
1488-
"\(JSGlueVariableScope.reservedStructHelpers).\(structDef.abiName) = \(structDef.abiName)Helpers;"
1489-
)
1493+
let key = HelperNaming.qualified(base: structDef.abiName, module: unified.moduleName)
1494+
let local = HelperNaming.helperConstant(key)
1495+
printer.write("const \(local) = \(HelperNaming.structHelperFactory(key))();")
1496+
printer.write("\(JSGlueVariableScope.reservedStructHelpers).\(key) = \(local);")
14901497
printer.nextLine()
14911498
}
14921499
}
@@ -4278,8 +4285,10 @@ private struct DocCComment {
42784285
}
42794286
}
42804287

4281-
struct BridgeJSLinkError: Error {
4288+
struct BridgeJSLinkError: Error, CustomStringConvertible {
42824289
let message: String
4290+
4291+
var description: String { message }
42834292
}
42844293

42854294
extension BridgeType {

0 commit comments

Comments
 (0)