Skip to content
Closed
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: 4 additions & 20 deletions packages/nitrogen/src/autolinking/ios/createSwiftCxxBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import {
import type { SourceFile } from '../../syntax/SourceFile.js'
import { getReferencedTypes } from '../../syntax/getReferencedTypes.js'
import { SwiftCxxBridgedType } from '../../syntax/swift/SwiftCxxBridgedType.js'
import type { SwiftCxxHelper } from '../../syntax/swift/SwiftCxxTypeHelper.js'
import { filterDuplicateHelperBridges, indent } from '../../utils.js'
import { getTypeAs } from '../../syntax/types/getTypeAs.js'
import { HybridObjectType } from '../../syntax/types/HybridObjectType.js'
import { getForwardDeclaration } from '../../syntax/c++/getForwardDeclaration.js'
import { getHybridObjectName } from '../../syntax/getHybridObjectName.js'

export function createSwiftCxxBridge(): SourceFile[] {
const bridgeName = NitroConfig.current.getSwiftBridgeHeaderName()
Expand All @@ -28,8 +25,8 @@ export function createSwiftCxxBridge(): SourceFile[] {
return bridge.getRequiredBridge()
})
})
.filter((b) => b != null)
.flatMap((b) => [b, ...b?.dependencies])
.filter((b): b is SwiftCxxHelper => b != null)
.flatMap((b) => [b, ...b.dependencies])
.filter(filterDuplicateHelperBridges)
const headerHelperFunctions = bridges
.map((b) => `// pragma MARK: ${b.cxxType}\n${b.cxxHeader.code}`)
Expand Down Expand Up @@ -62,20 +59,7 @@ export function createSwiftCxxBridge(): SourceFile[] {
.filter(isNotDuplicate)

const forwardDeclaredSwiftTypes = types
.filter((t) => t.type.kind === 'hybrid-object')
.map((t) => {
const hybridObject = getTypeAs(t.type, HybridObjectType)
const hybridObjectModuleName =
hybridObject.sourceConfig.getIosModuleName()
const { HybridTSpecCxx } = getHybridObjectName(
hybridObject.hybridObjectName
)
return getForwardDeclaration(
'class',
HybridTSpecCxx,
hybridObjectModuleName
)
})
.flatMap((t) => t.type.getForwardDeclarations?.() ?? [])
.filter(isNotDuplicate)

const header = `
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { NitroConfig } from '../../config/NitroConfig.js'
import { getForwardDeclaration } from '../../syntax/c++/getForwardDeclaration.js'
import { includeHeader } from '../../syntax/c++/includeNitroHeader.js'
import { getAllKnownTypes } from '../../syntax/createType.js'
import { getHybridObjectName } from '../../syntax/getHybridObjectName.js'
import {
createFileMetadataString,
isNotDuplicate,
} from '../../syntax/helpers.js'
import type { SourceFile } from '../../syntax/SourceFile.js'
import { getTypeAs } from '../../syntax/types/getTypeAs.js'
import { HybridObjectType } from '../../syntax/types/HybridObjectType.js'

export function getUmbrellaHeaderName(): string {
const moduleName = NitroConfig.current.getIosModuleName()
Expand All @@ -23,18 +19,7 @@ export function createSwiftUmbrellaHeader(): SourceFile {
const types = getAllKnownTypes('swift')

const swiftForwardDeclares = types
.filter((t) => t.kind === 'hybrid-object')
.map((t) => {
const hybridObjectType = getTypeAs(t, HybridObjectType)
const hybridObjectModuleName =
hybridObjectType.sourceConfig.getIosModuleName()
const name = getHybridObjectName(hybridObjectType.hybridObjectName)
return getForwardDeclaration(
'class',
name.HybridTSpecCxx,
hybridObjectModuleName
)
})
.flatMap((t) => t.getForwardDeclarations?.() ?? [])
.filter(isNotDuplicate)

const imports = types.flatMap((t) => t.getRequiredImports('c++'))
Expand Down
6 changes: 6 additions & 0 deletions packages/nitrogen/src/syntax/types/HybridObjectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,10 @@ export class HybridObjectType implements Type {

return imports
}

getForwardDeclarations(): string[] {
const name = getHybridObjectName(this.hybridObjectName)
const moduleName = this.sourceConfig.getIosModuleName()
return [getForwardDeclaration('class', name.HybridTSpecCxx, moduleName)]
}
}
11 changes: 11 additions & 0 deletions packages/nitrogen/src/syntax/types/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ export interface Type {
* Get all required extra imports that need to be **imported** for this type to properly work.
*/
getRequiredImports(language: Language): SourceImport[]
/**
* Get any C++ forward declarations this type needs beyond those already
* carried by {@linkcode getRequiredImports | getRequiredImports()}.
*
* This is an optional capability: only some types declare any (currently
* `HybridObjectType`, to forward-declare its Swift-generated `*Cxx` wrapper
* class). Letting the type own this keeps dependency collection polymorphic
* instead of callers hardcoding `kind`-checks and reaching into concrete
* type classes.
*/
getForwardDeclarations?(): string[]
}

export interface NamedType extends Type {
Expand Down