nitrogen 0.2.23 → 0.29.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -109
- package/lib/Logger.d.ts +10 -0
- package/lib/Logger.js +56 -0
- package/lib/autolinking/Autolinking.d.ts +8 -0
- package/lib/autolinking/Autolinking.js +1 -0
- package/lib/autolinking/android/createCMakeExtension.d.ts +6 -0
- package/lib/autolinking/android/createCMakeExtension.js +106 -0
- package/lib/autolinking/android/createGradleExtension.d.ts +5 -0
- package/lib/autolinking/android/createGradleExtension.js +36 -0
- package/lib/autolinking/android/createHybridObjectInitializer.d.ts +2 -0
- package/lib/autolinking/android/createHybridObjectInitializer.js +159 -0
- package/lib/autolinking/createAndroidAutolinking.d.ts +11 -0
- package/lib/autolinking/createAndroidAutolinking.js +13 -0
- package/lib/autolinking/createIOSAutolinking.d.ts +5 -0
- package/lib/autolinking/createIOSAutolinking.js +19 -0
- package/lib/autolinking/ios/createHybridObjectInitializer.d.ts +9 -0
- package/lib/autolinking/ios/createHybridObjectInitializer.js +97 -0
- package/lib/autolinking/ios/createPodspecRubyExtension.d.ts +5 -0
- package/lib/autolinking/ios/createPodspecRubyExtension.js +69 -0
- package/lib/autolinking/ios/createSwiftCxxBridge.d.ts +2 -0
- package/lib/autolinking/ios/createSwiftCxxBridge.js +117 -0
- package/lib/autolinking/ios/createSwiftUmbrellaHeader.d.ts +3 -0
- package/lib/autolinking/ios/createSwiftUmbrellaHeader.js +74 -0
- package/lib/config/NitroConfig.d.ts +52 -0
- package/lib/config/NitroConfig.js +112 -0
- package/lib/config/NitroUserConfig.d.ts +22 -0
- package/lib/config/NitroUserConfig.js +88 -0
- package/lib/config/getConfig.d.ts +2 -0
- package/lib/config/getConfig.js +84 -0
- package/lib/createGitAttributes.d.ts +1 -0
- package/lib/createGitAttributes.js +11 -0
- package/lib/createPlatformSpec.d.ts +4 -0
- package/lib/createPlatformSpec.js +127 -0
- package/lib/getFiles.d.ts +1 -0
- package/lib/getFiles.js +28 -0
- package/lib/getPlatformSpecs.d.ts +17 -0
- package/lib/getPlatformSpecs.js +153 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +113 -10
- package/lib/init.d.ts +1 -0
- package/lib/init.js +123 -0
- package/lib/nitrogen.d.ts +11 -0
- package/lib/nitrogen.js +165 -0
- package/lib/prettifyDirectory.d.ts +1 -0
- package/lib/prettifyDirectory.js +27 -0
- package/lib/syntax/BridgedType.d.ts +41 -0
- package/lib/syntax/BridgedType.js +1 -0
- package/lib/syntax/CodeNode.d.ts +23 -0
- package/lib/syntax/CodeNode.js +1 -0
- package/lib/syntax/HybridObjectSpec.d.ts +13 -0
- package/lib/syntax/HybridObjectSpec.js +1 -0
- package/lib/syntax/Method.d.ts +41 -0
- package/lib/syntax/Method.js +108 -0
- package/lib/syntax/Parameter.d.ts +15 -0
- package/lib/syntax/Parameter.js +65 -0
- package/lib/syntax/Property.d.ts +50 -0
- package/lib/syntax/Property.js +147 -0
- package/lib/syntax/SourceFile.d.ts +70 -0
- package/lib/syntax/SourceFile.js +7 -0
- package/lib/syntax/c++/CppEnum.d.ts +6 -0
- package/lib/syntax/c++/CppEnum.js +110 -0
- package/lib/syntax/c++/CppHybridObject.d.ts +3 -0
- package/lib/syntax/c++/CppHybridObject.js +146 -0
- package/lib/syntax/c++/CppHybridObjectRegistration.d.ts +17 -0
- package/lib/syntax/c++/CppHybridObjectRegistration.js +18 -0
- package/lib/syntax/c++/CppStruct.d.ts +3 -0
- package/lib/syntax/c++/CppStruct.js +108 -0
- package/lib/syntax/c++/CppUnion.d.ts +6 -0
- package/lib/syntax/c++/CppUnion.js +88 -0
- package/lib/syntax/c++/getForwardDeclaration.d.ts +3 -0
- package/lib/syntax/c++/getForwardDeclaration.js +14 -0
- package/lib/syntax/c++/includeNitroHeader.d.ts +7 -0
- package/lib/syntax/c++/includeNitroHeader.js +34 -0
- package/lib/syntax/createType.d.ts +15 -0
- package/lib/syntax/createType.js +303 -0
- package/lib/syntax/getAllTypes.d.ts +3 -0
- package/lib/syntax/getAllTypes.js +11 -0
- package/lib/syntax/getCustomTypeConfig.d.ts +8 -0
- package/lib/syntax/getCustomTypeConfig.js +53 -0
- package/lib/syntax/getHybridObjectName.d.ts +36 -0
- package/lib/syntax/getHybridObjectName.js +10 -0
- package/lib/syntax/getInterfaceProperties.d.ts +4 -0
- package/lib/syntax/getInterfaceProperties.js +9 -0
- package/lib/syntax/getReferencedTypes.d.ts +2 -0
- package/lib/syntax/getReferencedTypes.js +47 -0
- package/lib/syntax/helpers.d.ts +13 -0
- package/lib/syntax/helpers.js +53 -0
- package/lib/syntax/isCoreType.d.ts +9 -0
- package/lib/syntax/isCoreType.js +47 -0
- package/lib/syntax/kotlin/FbjniHybridObject.d.ts +3 -0
- package/lib/syntax/kotlin/FbjniHybridObject.js +261 -0
- package/lib/syntax/kotlin/JNINativeRegistrations.d.ts +8 -0
- package/lib/syntax/kotlin/JNINativeRegistrations.js +7 -0
- package/lib/syntax/kotlin/KotlinBoxedPrimitive.d.ts +7 -0
- package/lib/syntax/kotlin/KotlinBoxedPrimitive.js +17 -0
- package/lib/syntax/kotlin/KotlinCxxBridgedType.d.ts +19 -0
- package/lib/syntax/kotlin/KotlinCxxBridgedType.js +893 -0
- package/lib/syntax/kotlin/KotlinEnum.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinEnum.js +113 -0
- package/lib/syntax/kotlin/KotlinFunction.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinFunction.js +256 -0
- package/lib/syntax/kotlin/KotlinHybridObject.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinHybridObject.js +177 -0
- package/lib/syntax/kotlin/KotlinHybridObjectRegistration.d.ts +17 -0
- package/lib/syntax/kotlin/KotlinHybridObjectRegistration.js +26 -0
- package/lib/syntax/kotlin/KotlinStruct.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinStruct.js +172 -0
- package/lib/syntax/kotlin/KotlinVariant.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinVariant.js +191 -0
- package/lib/syntax/swift/SwiftCxxBridgedType.d.ts +21 -0
- package/lib/syntax/swift/SwiftCxxBridgedType.js +819 -0
- package/lib/syntax/swift/SwiftCxxTypeHelper.d.ts +17 -0
- package/lib/syntax/swift/SwiftCxxTypeHelper.js +613 -0
- package/lib/syntax/swift/SwiftEnum.d.ts +3 -0
- package/lib/syntax/swift/SwiftEnum.js +52 -0
- package/lib/syntax/swift/SwiftFunction.d.ts +3 -0
- package/lib/syntax/swift/SwiftFunction.js +83 -0
- package/lib/syntax/swift/SwiftHybridObject.d.ts +3 -0
- package/lib/syntax/swift/SwiftHybridObject.js +103 -0
- package/lib/syntax/swift/SwiftHybridObjectBridge.d.ts +11 -0
- package/lib/syntax/swift/SwiftHybridObjectBridge.js +451 -0
- package/lib/syntax/swift/SwiftHybridObjectRegistration.d.ts +19 -0
- package/lib/syntax/swift/SwiftHybridObjectRegistration.js +42 -0
- package/lib/syntax/swift/SwiftStruct.d.ts +3 -0
- package/lib/syntax/swift/SwiftStruct.js +75 -0
- package/lib/syntax/swift/SwiftVariant.d.ts +3 -0
- package/lib/syntax/swift/SwiftVariant.js +58 -0
- package/lib/syntax/types/ArrayBufferType.d.ts +10 -0
- package/lib/syntax/types/ArrayBufferType.js +37 -0
- package/lib/syntax/types/ArrayType.d.ts +12 -0
- package/lib/syntax/types/ArrayType.js +52 -0
- package/lib/syntax/types/BigIntType.d.ts +10 -0
- package/lib/syntax/types/BigIntType.js +27 -0
- package/lib/syntax/types/BooleanType.d.ts +10 -0
- package/lib/syntax/types/BooleanType.js +27 -0
- package/lib/syntax/types/CustomType.d.ts +14 -0
- package/lib/syntax/types/CustomType.js +36 -0
- package/lib/syntax/types/DateType.d.ts +10 -0
- package/lib/syntax/types/DateType.js +35 -0
- package/lib/syntax/types/EnumType.d.ts +23 -0
- package/lib/syntax/types/EnumType.js +101 -0
- package/lib/syntax/types/ErrorType.d.ts +11 -0
- package/lib/syntax/types/ErrorType.js +37 -0
- package/lib/syntax/types/FunctionType.d.ts +29 -0
- package/lib/syntax/types/FunctionType.js +147 -0
- package/lib/syntax/types/HybridObjectBaseType.d.ts +11 -0
- package/lib/syntax/types/HybridObjectBaseType.js +38 -0
- package/lib/syntax/types/HybridObjectType.d.ts +23 -0
- package/lib/syntax/types/HybridObjectType.js +131 -0
- package/lib/syntax/types/MapType.d.ts +10 -0
- package/lib/syntax/types/MapType.js +37 -0
- package/lib/syntax/types/NamedWrappingType.d.ts +14 -0
- package/lib/syntax/types/NamedWrappingType.js +27 -0
- package/lib/syntax/types/NullType.d.ts +10 -0
- package/lib/syntax/types/NullType.js +23 -0
- package/lib/syntax/types/NumberType.d.ts +10 -0
- package/lib/syntax/types/NumberType.js +27 -0
- package/lib/syntax/types/OptionalType.d.ts +13 -0
- package/lib/syntax/types/OptionalType.js +59 -0
- package/lib/syntax/types/PromiseType.d.ts +16 -0
- package/lib/syntax/types/PromiseType.js +62 -0
- package/lib/syntax/types/RecordType.d.ts +13 -0
- package/lib/syntax/types/RecordType.js +47 -0
- package/lib/syntax/types/ResultWrappingType.d.ts +13 -0
- package/lib/syntax/types/ResultWrappingType.js +44 -0
- package/lib/syntax/types/StringType.d.ts +10 -0
- package/lib/syntax/types/StringType.js +35 -0
- package/lib/syntax/types/StructType.d.ts +14 -0
- package/lib/syntax/types/StructType.js +61 -0
- package/lib/syntax/types/TupleType.d.ts +12 -0
- package/lib/syntax/types/TupleType.js +39 -0
- package/lib/syntax/types/Type.d.ts +55 -0
- package/lib/syntax/types/Type.js +1 -0
- package/lib/syntax/types/VariantType.d.ts +19 -0
- package/lib/syntax/types/VariantType.js +75 -0
- package/lib/syntax/types/VoidType.d.ts +10 -0
- package/lib/syntax/types/VoidType.js +27 -0
- package/lib/syntax/types/getTypeAs.d.ts +2 -0
- package/lib/syntax/types/getTypeAs.js +12 -0
- package/lib/utils.d.ts +22 -0
- package/lib/utils.js +126 -0
- package/lib/views/CppHybridViewComponent.d.ts +14 -0
- package/lib/views/CppHybridViewComponent.js +254 -0
- package/lib/views/createHostComponentJs.d.ts +3 -0
- package/lib/views/createHostComponentJs.js +27 -0
- package/lib/views/kotlin/KotlinHybridViewManager.d.ts +3 -0
- package/lib/views/kotlin/KotlinHybridViewManager.js +229 -0
- package/lib/views/swift/SwiftHybridViewManager.d.ts +3 -0
- package/lib/views/swift/SwiftHybridViewManager.js +131 -0
- package/lib/writeFile.d.ts +5 -0
- package/lib/writeFile.js +19 -0
- package/package.json +58 -29
- package/src/Logger.ts +63 -0
- package/src/autolinking/Autolinking.ts +9 -0
- package/src/autolinking/android/createCMakeExtension.ts +123 -0
- package/src/autolinking/android/createGradleExtension.ts +43 -0
- package/src/autolinking/android/createHybridObjectInitializer.ts +174 -0
- package/src/autolinking/createAndroidAutolinking.ts +28 -0
- package/src/autolinking/createIOSAutolinking.ts +24 -0
- package/src/autolinking/ios/createHybridObjectInitializer.ts +112 -0
- package/src/autolinking/ios/createPodspecRubyExtension.ts +76 -0
- package/src/autolinking/ios/createSwiftCxxBridge.ts +137 -0
- package/src/autolinking/ios/createSwiftUmbrellaHeader.ts +90 -0
- package/src/config/NitroConfig.ts +139 -0
- package/src/config/NitroUserConfig.ts +105 -0
- package/src/config/getConfig.ts +91 -0
- package/src/createGitAttributes.ts +15 -0
- package/src/createPlatformSpec.ts +176 -0
- package/src/getFiles.ts +31 -0
- package/src/getPlatformSpecs.ts +202 -0
- package/src/index.ts +146 -0
- package/src/init.ts +186 -0
- package/src/nitrogen.ts +246 -0
- package/src/prettifyDirectory.ts +32 -0
- package/src/syntax/BridgedType.ts +59 -0
- package/src/syntax/CodeNode.ts +24 -0
- package/src/syntax/HybridObjectSpec.ts +14 -0
- package/src/syntax/Method.ts +154 -0
- package/src/syntax/Parameter.ts +81 -0
- package/src/syntax/Property.ts +203 -0
- package/src/syntax/SourceFile.ts +80 -0
- package/src/syntax/c++/CppEnum.ts +128 -0
- package/src/syntax/c++/CppHybridObject.ts +165 -0
- package/src/syntax/c++/CppHybridObjectRegistration.ts +39 -0
- package/src/syntax/c++/CppStruct.ts +129 -0
- package/src/syntax/c++/CppUnion.ts +105 -0
- package/src/syntax/c++/getForwardDeclaration.ts +19 -0
- package/src/syntax/c++/includeNitroHeader.ts +40 -0
- package/src/syntax/createType.ts +365 -0
- package/src/syntax/getAllTypes.ts +18 -0
- package/src/syntax/getCustomTypeConfig.ts +71 -0
- package/src/syntax/getHybridObjectName.ts +48 -0
- package/src/syntax/getInterfaceProperties.ts +21 -0
- package/src/syntax/getReferencedTypes.ts +57 -0
- package/src/syntax/helpers.ts +79 -0
- package/src/syntax/isCoreType.ts +60 -0
- package/src/syntax/kotlin/FbjniHybridObject.ts +313 -0
- package/src/syntax/kotlin/JNINativeRegistrations.ts +19 -0
- package/src/syntax/kotlin/KotlinBoxedPrimitive.ts +19 -0
- package/src/syntax/kotlin/KotlinCxxBridgedType.ts +942 -0
- package/src/syntax/kotlin/KotlinEnum.ts +130 -0
- package/src/syntax/kotlin/KotlinFunction.ts +277 -0
- package/src/syntax/kotlin/KotlinHybridObject.ts +205 -0
- package/src/syntax/kotlin/KotlinHybridObjectRegistration.ts +51 -0
- package/src/syntax/kotlin/KotlinStruct.ts +198 -0
- package/src/syntax/kotlin/KotlinVariant.ts +212 -0
- package/src/syntax/swift/SwiftCxxBridgedType.ts +874 -0
- package/src/syntax/swift/SwiftCxxTypeHelper.ts +674 -0
- package/src/syntax/swift/SwiftEnum.ts +65 -0
- package/src/syntax/swift/SwiftFunction.ts +91 -0
- package/src/syntax/swift/SwiftHybridObject.ts +121 -0
- package/src/syntax/swift/SwiftHybridObjectBridge.ts +522 -0
- package/src/syntax/swift/SwiftHybridObjectRegistration.ts +75 -0
- package/src/syntax/swift/SwiftStruct.ts +85 -0
- package/src/syntax/swift/SwiftVariant.ts +67 -0
- package/src/syntax/types/ArrayBufferType.ts +49 -0
- package/src/syntax/types/ArrayType.ts +62 -0
- package/src/syntax/types/BigIntType.ts +35 -0
- package/src/syntax/types/BooleanType.ts +35 -0
- package/src/syntax/types/CustomType.ts +47 -0
- package/src/syntax/types/DateType.ts +43 -0
- package/src/syntax/types/EnumType.ts +130 -0
- package/src/syntax/types/ErrorType.ts +44 -0
- package/src/syntax/types/FunctionType.ts +167 -0
- package/src/syntax/types/HybridObjectBaseType.ts +54 -0
- package/src/syntax/types/HybridObjectType.ts +198 -0
- package/src/syntax/types/MapType.ts +49 -0
- package/src/syntax/types/NamedWrappingType.ts +33 -0
- package/src/syntax/types/NullType.ts +30 -0
- package/src/syntax/types/NumberType.ts +34 -0
- package/src/syntax/types/OptionalType.ts +66 -0
- package/src/syntax/types/PromiseType.ts +72 -0
- package/src/syntax/types/RecordType.ts +56 -0
- package/src/syntax/types/ResultWrappingType.ts +53 -0
- package/src/syntax/types/StringType.ts +44 -0
- package/src/syntax/types/StructType.ts +83 -0
- package/src/syntax/types/TupleType.ts +53 -0
- package/src/syntax/types/Type.ts +82 -0
- package/src/syntax/types/VariantType.ts +92 -0
- package/src/syntax/types/VoidType.ts +34 -0
- package/src/syntax/types/getTypeAs.ts +15 -0
- package/src/utils.ts +162 -0
- package/src/views/CppHybridViewComponent.ts +301 -0
- package/src/views/createHostComponentJs.ts +34 -0
- package/src/views/kotlin/KotlinHybridViewManager.ts +258 -0
- package/src/views/swift/SwiftHybridViewManager.ts +153 -0
- package/src/writeFile.ts +27 -0
- package/.jshintignore +0 -6
- package/.jshintrc +0 -3
- package/.npmignore +0 -3
- package/.travis.yml +0 -13
- package/LICENSE +0 -13
- package/browser/nitrogen-min.js +0 -3
- package/browser/nitrogen.js +0 -6369
- package/lib/apiKey.js +0 -67
- package/lib/blob.js +0 -57
- package/lib/commandManager.js +0 -350
- package/lib/device.js +0 -19
- package/lib/memoryStore.js +0 -24
- package/lib/message.js +0 -298
- package/lib/permission.js +0 -121
- package/lib/principal.js +0 -330
- package/lib/service.js +0 -349
- package/lib/session.js +0 -494
- package/lib/user.js +0 -20
- package/publish +0 -2
- package/scripts/build-documentation +0 -4
- package/scripts/build-module +0 -27
- package/scripts/module.js +0 -12
- package/scripts/postamble.js +0 -1
- package/scripts/preamble.js +0 -2
- package/scripts/run-test-server +0 -9
- package/test/config.js +0 -12
- package/test/fixtures/images/image.jpg +0 -0
- package/test/fixtures/images/motion0.jpg +0 -0
- package/test/fixtures/images/motion1.jpg +0 -0
- package/test/fixtures/images/motion2.jpg +0 -0
- package/test/fixtures/index.js +0 -76
- package/test/main.js +0 -5
- package/test/memoryStore.js +0 -22
- package/test/mocha.opts +0 -3
- package/test/units/apiKey.js +0 -46
- package/test/units/blob.js +0 -35
- package/test/units/commandManager.js +0 -67
- package/test/units/device.js +0 -26
- package/test/units/heartbeat.js +0 -28
- package/test/units/message.js +0 -79
- package/test/units/permissions.js +0 -43
- package/test/units/principal.js +0 -116
- package/test/units/service.js +0 -92
- package/test/units/session.js +0 -97
- package/test/units/user.js +0 -48
- package/yuidoc.json +0 -8
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
import type { HybridObjectSpec } from '../HybridObjectSpec.js'
|
|
2
|
+
import { SwiftCxxBridgedType } from './SwiftCxxBridgedType.js'
|
|
3
|
+
import type { Property } from '../Property.js'
|
|
4
|
+
import { indent } from '../../utils.js'
|
|
5
|
+
import type { Method } from '../Method.js'
|
|
6
|
+
import {
|
|
7
|
+
createFileMetadataString,
|
|
8
|
+
escapeCppName,
|
|
9
|
+
isNotDuplicate,
|
|
10
|
+
} from '../helpers.js'
|
|
11
|
+
import type { SourceFile } from '../SourceFile.js'
|
|
12
|
+
import { getHybridObjectName } from '../getHybridObjectName.js'
|
|
13
|
+
import { getForwardDeclaration } from '../c++/getForwardDeclaration.js'
|
|
14
|
+
import { NitroConfig } from '../../config/NitroConfig.js'
|
|
15
|
+
import { includeHeader } from '../c++/includeNitroHeader.js'
|
|
16
|
+
import { getUmbrellaHeaderName } from '../../autolinking/ios/createSwiftUmbrellaHeader.js'
|
|
17
|
+
import { HybridObjectType } from '../types/HybridObjectType.js'
|
|
18
|
+
import { addKnownType } from '../createType.js'
|
|
19
|
+
import { ResultWrappingType } from '../types/ResultWrappingType.js'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a Swift class that bridges Swift over to C++.
|
|
23
|
+
* We need this because not all Swift types are accessible in C++, and vice versa.
|
|
24
|
+
*
|
|
25
|
+
* For example, Enums need to be converted to Int32 (because of a Swift compiler bug),
|
|
26
|
+
* Promise<..> has to be converted to a Promise<..>, exceptions have to be handled
|
|
27
|
+
* via custom Result types, etc..
|
|
28
|
+
*/
|
|
29
|
+
export function createSwiftHybridObjectCxxBridge(
|
|
30
|
+
spec: HybridObjectSpec
|
|
31
|
+
): SourceFile[] {
|
|
32
|
+
const name = getHybridObjectName(spec.name)
|
|
33
|
+
const moduleName = spec.config.getIosModuleName()
|
|
34
|
+
const bridgeNamespace = spec.config.getSwiftBridgeNamespace('swift')
|
|
35
|
+
|
|
36
|
+
const propertiesBridge = spec.properties.map((p) =>
|
|
37
|
+
getPropertyForwardImplementation(p)
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
const methodsBridge = spec.methods.map((m) =>
|
|
41
|
+
getMethodForwardImplementation(m)
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
const baseClasses = spec.baseTypes.map((base) => {
|
|
45
|
+
const baseName = getHybridObjectName(base.name)
|
|
46
|
+
return baseName.HybridTSpecCxx
|
|
47
|
+
})
|
|
48
|
+
const hasBase = baseClasses.length > 0
|
|
49
|
+
|
|
50
|
+
if (spec.isHybridView && !hasBase) {
|
|
51
|
+
methodsBridge.push(
|
|
52
|
+
`
|
|
53
|
+
public final func getView() -> UnsafeMutableRawPointer {
|
|
54
|
+
return Unmanaged.passRetained(__implementation.view).toOpaque()
|
|
55
|
+
}
|
|
56
|
+
`.trim(),
|
|
57
|
+
`
|
|
58
|
+
public final func beforeUpdate() {
|
|
59
|
+
__implementation.beforeUpdate()
|
|
60
|
+
}
|
|
61
|
+
`.trim(),
|
|
62
|
+
`
|
|
63
|
+
public final func afterUpdate() {
|
|
64
|
+
__implementation.afterUpdate()
|
|
65
|
+
}
|
|
66
|
+
`.trim()
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const hybridObject = new HybridObjectType(spec)
|
|
71
|
+
const bridgedType = new SwiftCxxBridgedType(hybridObject)
|
|
72
|
+
const bridge = bridgedType.getRequiredBridge()
|
|
73
|
+
if (bridge == null) throw new Error(`HybridObject Type should have a bridge!`)
|
|
74
|
+
|
|
75
|
+
const weakifyBridge = bridge.dependencies.find((d) =>
|
|
76
|
+
d.funcName.startsWith('weakify')
|
|
77
|
+
)
|
|
78
|
+
if (weakifyBridge == null)
|
|
79
|
+
throw new Error(
|
|
80
|
+
`HybridObject ${spec.name} does not have a weakify_..() bridge!`
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
const cppWeakPtrName = escapeCppName(
|
|
84
|
+
hybridObject.getCode('c++', { mode: 'weak' })
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
const baseGetCxxPartOverrides = spec.baseTypes.map((base) => {
|
|
88
|
+
const baseHybridObject = new HybridObjectType(base)
|
|
89
|
+
const bridgedBase = new SwiftCxxBridgedType(baseHybridObject)
|
|
90
|
+
const baseBridge = bridgedBase.getRequiredBridge()
|
|
91
|
+
if (baseBridge == null)
|
|
92
|
+
throw new Error(`HybridObject ${base.name}'s bridge cannot be null!`)
|
|
93
|
+
|
|
94
|
+
const upcastBridge = bridge.dependencies.find(
|
|
95
|
+
(b) =>
|
|
96
|
+
b.funcName.includes(escapeCppName(spec.name)) &&
|
|
97
|
+
b.funcName.includes(escapeCppName(base.name))
|
|
98
|
+
)
|
|
99
|
+
if (upcastBridge == null)
|
|
100
|
+
throw new Error(
|
|
101
|
+
`HybridObject ${spec.name}'s upcast-bridge cannot be found! ${JSON.stringify(baseBridge)}`
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
return `
|
|
105
|
+
public override func getCxxPart() -> bridge.${baseBridge.specializationName} {
|
|
106
|
+
let ownCxxPart: bridge.${bridge.specializationName} = getCxxPart()
|
|
107
|
+
return bridge.${upcastBridge.funcName}(ownCxxPart)
|
|
108
|
+
}`.trim()
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
const imports = ['import NitroModules']
|
|
112
|
+
const extraSwiftImports = [
|
|
113
|
+
...spec.properties.flatMap((p) => p.getRequiredImports('swift')),
|
|
114
|
+
...spec.methods.flatMap((m) => m.getRequiredImports('swift')),
|
|
115
|
+
]
|
|
116
|
+
imports.push(
|
|
117
|
+
...extraSwiftImports.map((i) => `import ${i.name}`).filter(isNotDuplicate)
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
const swiftCxxWrapperCode = `
|
|
121
|
+
${createFileMetadataString(`${name.HybridTSpecCxx}.swift`)}
|
|
122
|
+
|
|
123
|
+
import Foundation
|
|
124
|
+
${imports.join('\n')}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A class implementation that bridges ${name.HybridTSpec} over to C++.
|
|
128
|
+
* In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined.
|
|
129
|
+
*
|
|
130
|
+
* Also, some Swift types need to be bridged with special handling:
|
|
131
|
+
* - Enums need to be wrapped in Structs, otherwise they cannot be accessed bi-directionally (Swift bug: https://github.com/swiftlang/swift/issues/75330)
|
|
132
|
+
* - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper
|
|
133
|
+
* - Throwing methods need to be wrapped with a Result<T, Error> type, as exceptions cannot be propagated to C++
|
|
134
|
+
*/
|
|
135
|
+
${hasBase ? `open class ${name.HybridTSpecCxx} : ${baseClasses.join(', ')}` : `open class ${name.HybridTSpecCxx}`} {
|
|
136
|
+
/**
|
|
137
|
+
* The Swift <> C++ bridge's namespace (\`${NitroConfig.current.getSwiftBridgeNamespace('c++')}\`)
|
|
138
|
+
* from \`${moduleName}-Swift-Cxx-Bridge.hpp\`.
|
|
139
|
+
* This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
|
|
140
|
+
*/
|
|
141
|
+
public typealias bridge = ${bridgeNamespace}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Holds an instance of the \`${name.HybridTSpec}\` Swift protocol.
|
|
145
|
+
*/
|
|
146
|
+
private var __implementation: any ${name.HybridTSpec}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Holds a weak pointer to the C++ class that wraps the Swift class.
|
|
150
|
+
*/
|
|
151
|
+
private var __cxxPart: bridge.${cppWeakPtrName}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Create a new \`${name.HybridTSpecCxx}\` that wraps the given \`${name.HybridTSpec}\`.
|
|
155
|
+
* All properties and methods bridge to C++ types.
|
|
156
|
+
*/
|
|
157
|
+
public init(_ implementation: any ${name.HybridTSpec}) {
|
|
158
|
+
self.__implementation = implementation
|
|
159
|
+
self.__cxxPart = .init()
|
|
160
|
+
${hasBase ? 'super.init(implementation)' : '/* no base class */'}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Get the actual \`${name.HybridTSpec}\` instance this class wraps.
|
|
165
|
+
*/
|
|
166
|
+
@inline(__always)
|
|
167
|
+
public func get${name.HybridTSpec}() -> any ${name.HybridTSpec} {
|
|
168
|
+
return __implementation
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
173
|
+
* This acquires one additional strong reference on the object!
|
|
174
|
+
*/
|
|
175
|
+
public ${hasBase ? 'override func' : 'func'} toUnsafe() -> UnsafeMutableRawPointer {
|
|
176
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Casts an unsafe pointer to a \`${name.HybridTSpecCxx}\`.
|
|
181
|
+
* The pointer has to be a retained opaque \`Unmanaged<${name.HybridTSpecCxx}>\`.
|
|
182
|
+
* This removes one strong reference from the object!
|
|
183
|
+
*/
|
|
184
|
+
public ${hasBase ? 'override class func' : 'class func'} fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> ${name.HybridTSpecCxx} {
|
|
185
|
+
return Unmanaged<${name.HybridTSpecCxx}>.fromOpaque(pointer).takeRetainedValue()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Gets (or creates) the C++ part of this Hybrid Object.
|
|
190
|
+
* The C++ part is a \`${bridge.cxxType}\`.
|
|
191
|
+
*/
|
|
192
|
+
public func getCxxPart() -> bridge.${bridge.specializationName} {
|
|
193
|
+
let cachedCxxPart = self.__cxxPart.lock()
|
|
194
|
+
if cachedCxxPart.__convertToBool() {
|
|
195
|
+
return cachedCxxPart
|
|
196
|
+
} else {
|
|
197
|
+
let newCxxPart = bridge.${bridge.funcName}(self.toUnsafe())
|
|
198
|
+
__cxxPart = bridge.${weakifyBridge.funcName}(newCxxPart)
|
|
199
|
+
return newCxxPart
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
${indent(baseGetCxxPartOverrides.join('\n'), ' ')}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Get the memory size of the Swift class (plus size of any other allocations)
|
|
207
|
+
* so the JS VM can properly track it and garbage-collect the JS object if needed.
|
|
208
|
+
*/
|
|
209
|
+
@inline(__always)
|
|
210
|
+
public ${hasBase ? 'override var' : 'var'} memorySize: Int {
|
|
211
|
+
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Call dispose() on the Swift class.
|
|
216
|
+
* This _may_ be called manually from JS.
|
|
217
|
+
*/
|
|
218
|
+
@inline(__always)
|
|
219
|
+
public ${hasBase ? 'override func' : 'func'} dispose() {
|
|
220
|
+
self.__implementation.dispose()
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Properties
|
|
224
|
+
${indent(propertiesBridge.join('\n\n'), ' ')}
|
|
225
|
+
|
|
226
|
+
// Methods
|
|
227
|
+
${indent(methodsBridge.join('\n\n'), ' ')}
|
|
228
|
+
}
|
|
229
|
+
`
|
|
230
|
+
|
|
231
|
+
const cppProperties = spec.properties
|
|
232
|
+
.map((p) => {
|
|
233
|
+
const bridged = new SwiftCxxBridgedType(p.type)
|
|
234
|
+
let getter: string
|
|
235
|
+
let setter: string
|
|
236
|
+
|
|
237
|
+
const getterName = p.getGetterName('swift')
|
|
238
|
+
const setterName = p.getSetterName('swift')
|
|
239
|
+
if (bridged.needsSpecialHandling) {
|
|
240
|
+
// we need custom C++ -> Swift conversion code
|
|
241
|
+
getter = `
|
|
242
|
+
auto __result = _swiftPart.${getterName}();
|
|
243
|
+
return ${bridged.parseFromSwiftToCpp('__result', 'c++')};
|
|
244
|
+
`
|
|
245
|
+
setter = `_swiftPart.${setterName}(${bridged.parseFromCppToSwift(p.name, 'c++')});`
|
|
246
|
+
} else {
|
|
247
|
+
// just forward value directly
|
|
248
|
+
getter = `return _swiftPart.${getterName}();`
|
|
249
|
+
setter = `_swiftPart.${setterName}(std::forward<decltype(${p.name})>(${p.name}));`
|
|
250
|
+
}
|
|
251
|
+
return p.getCode(
|
|
252
|
+
'c++',
|
|
253
|
+
{ inline: true, override: true, noexcept: true },
|
|
254
|
+
{
|
|
255
|
+
getter: getter.trim(),
|
|
256
|
+
setter: setter.trim(),
|
|
257
|
+
}
|
|
258
|
+
)
|
|
259
|
+
})
|
|
260
|
+
.join('\n')
|
|
261
|
+
|
|
262
|
+
const cppMethods = spec.methods
|
|
263
|
+
.map((m) => {
|
|
264
|
+
const params = m.parameters
|
|
265
|
+
.map((p) => {
|
|
266
|
+
const bridged = new SwiftCxxBridgedType(p.type)
|
|
267
|
+
if (bridged.needsSpecialHandling) {
|
|
268
|
+
// we need custom C++ -> Swift conversion code
|
|
269
|
+
return bridged.parseFromCppToSwift(p.name, 'c++')
|
|
270
|
+
} else {
|
|
271
|
+
// just forward value directly
|
|
272
|
+
return `std::forward<decltype(${p.name})>(${p.name})`
|
|
273
|
+
}
|
|
274
|
+
})
|
|
275
|
+
.join(', ')
|
|
276
|
+
const bridgedReturnType = new SwiftCxxBridgedType(m.returnType, true)
|
|
277
|
+
const hasResult = m.returnType.kind !== 'void'
|
|
278
|
+
let body: string
|
|
279
|
+
if (hasResult) {
|
|
280
|
+
// func returns something
|
|
281
|
+
body = `
|
|
282
|
+
auto __result = _swiftPart.${m.name}(${params});
|
|
283
|
+
if (__result.hasError()) [[unlikely]] {
|
|
284
|
+
std::rethrow_exception(__result.error());
|
|
285
|
+
}
|
|
286
|
+
auto __value = std::move(__result.value());
|
|
287
|
+
return ${bridgedReturnType.parseFromSwiftToCpp('__value', 'c++')};
|
|
288
|
+
`.trim()
|
|
289
|
+
} else {
|
|
290
|
+
// void func
|
|
291
|
+
body = `
|
|
292
|
+
auto __result = _swiftPart.${m.name}(${params});
|
|
293
|
+
if (__result.hasError()) [[unlikely]] {
|
|
294
|
+
std::rethrow_exception(__result.error());
|
|
295
|
+
}
|
|
296
|
+
`.trim()
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return m.getCode('c++', { inline: true, override: true }, body)
|
|
300
|
+
})
|
|
301
|
+
.join('\n')
|
|
302
|
+
|
|
303
|
+
const allBridgedTypes = [
|
|
304
|
+
...spec.properties.flatMap((p) => new SwiftCxxBridgedType(p.type)),
|
|
305
|
+
...spec.methods.flatMap((m) => {
|
|
306
|
+
const bridgedReturn = new SwiftCxxBridgedType(m.returnType)
|
|
307
|
+
const bridgedParams = m.parameters.map(
|
|
308
|
+
(p) => new SwiftCxxBridgedType(p.type)
|
|
309
|
+
)
|
|
310
|
+
return [bridgedReturn, ...bridgedParams]
|
|
311
|
+
}),
|
|
312
|
+
]
|
|
313
|
+
const cxxNamespace = spec.config.getCxxNamespace('c++')
|
|
314
|
+
const iosModuleName = spec.config.getIosModuleName()
|
|
315
|
+
const extraImports = allBridgedTypes.flatMap((b) =>
|
|
316
|
+
b.getRequiredImports('c++')
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
const cppBaseClasses = [`public virtual ${name.HybridTSpec}`]
|
|
320
|
+
const cppBaseCtorCalls = [`HybridObject(${name.HybridTSpec}::TAG)`]
|
|
321
|
+
for (const base of spec.baseTypes) {
|
|
322
|
+
const baseName = getHybridObjectName(base.name)
|
|
323
|
+
cppBaseClasses.push(`public virtual ${baseName.HybridTSpecSwift}`)
|
|
324
|
+
cppBaseCtorCalls.push(`${baseName.HybridTSpecSwift}(swiftPart)`)
|
|
325
|
+
extraImports.push({
|
|
326
|
+
language: 'c++',
|
|
327
|
+
name: `${baseName.HybridTSpecSwift}.hpp`,
|
|
328
|
+
space: 'user',
|
|
329
|
+
forwardDeclaration: getForwardDeclaration(
|
|
330
|
+
'class',
|
|
331
|
+
baseName.HybridTSpecSwift,
|
|
332
|
+
cxxNamespace
|
|
333
|
+
),
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const extraForwardDeclarations = extraImports
|
|
338
|
+
.map((i) => i.forwardDeclaration)
|
|
339
|
+
.filter((v) => v != null)
|
|
340
|
+
.filter(isNotDuplicate)
|
|
341
|
+
const extraIncludes = extraImports
|
|
342
|
+
.map((i) => includeHeader(i))
|
|
343
|
+
.filter(isNotDuplicate)
|
|
344
|
+
|
|
345
|
+
// TODO: Remove forward declaration once Swift fixes the wrong order in generated -Swift.h headers!
|
|
346
|
+
const cppHybridObjectCode = `
|
|
347
|
+
${createFileMetadataString(`${name.HybridTSpecSwift}.hpp`)}
|
|
348
|
+
|
|
349
|
+
#pragma once
|
|
350
|
+
|
|
351
|
+
#include "${name.HybridTSpec}.hpp"
|
|
352
|
+
|
|
353
|
+
${getForwardDeclaration('class', name.HybridTSpecCxx, iosModuleName)}
|
|
354
|
+
|
|
355
|
+
${extraForwardDeclarations.join('\n')}
|
|
356
|
+
|
|
357
|
+
${extraIncludes.join('\n')}
|
|
358
|
+
|
|
359
|
+
#include "${getUmbrellaHeaderName()}"
|
|
360
|
+
|
|
361
|
+
namespace ${cxxNamespace} {
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* The C++ part of ${name.HybridTSpecCxx}.swift.
|
|
365
|
+
*
|
|
366
|
+
* ${name.HybridTSpecSwift} (C++) accesses ${name.HybridTSpecCxx} (Swift), and might
|
|
367
|
+
* contain some additional bridging code for C++ <> Swift interop.
|
|
368
|
+
*
|
|
369
|
+
* Since this obviously introduces an overhead, I hope at some point in
|
|
370
|
+
* the future, ${name.HybridTSpecCxx} can directly inherit from the C++ class ${name.HybridTSpec}
|
|
371
|
+
* to simplify the whole structure and memory management.
|
|
372
|
+
*/
|
|
373
|
+
class ${name.HybridTSpecSwift}: ${cppBaseClasses.join(', ')} {
|
|
374
|
+
public:
|
|
375
|
+
// Constructor from a Swift instance
|
|
376
|
+
explicit ${name.HybridTSpecSwift}(const ${iosModuleName}::${name.HybridTSpecCxx}& swiftPart):
|
|
377
|
+
${indent(cppBaseCtorCalls.join(',\n'), ' ')},
|
|
378
|
+
_swiftPart(swiftPart) { }
|
|
379
|
+
|
|
380
|
+
public:
|
|
381
|
+
// Get the Swift part
|
|
382
|
+
inline ${iosModuleName}::${name.HybridTSpecCxx}& getSwiftPart() noexcept {
|
|
383
|
+
return _swiftPart;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
public:
|
|
387
|
+
inline size_t getExternalMemorySize() noexcept override {
|
|
388
|
+
return _swiftPart.getMemorySize();
|
|
389
|
+
}
|
|
390
|
+
void dispose() noexcept override {
|
|
391
|
+
_swiftPart.dispose();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
public:
|
|
395
|
+
// Properties
|
|
396
|
+
${indent(cppProperties, ' ')}
|
|
397
|
+
|
|
398
|
+
public:
|
|
399
|
+
// Methods
|
|
400
|
+
${indent(cppMethods, ' ')}
|
|
401
|
+
|
|
402
|
+
private:
|
|
403
|
+
${iosModuleName}::${name.HybridTSpecCxx} _swiftPart;
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
} // namespace ${cxxNamespace}
|
|
407
|
+
`
|
|
408
|
+
const cppHybridObjectCodeCpp = `
|
|
409
|
+
${createFileMetadataString(`${name.HybridTSpecSwift}.cpp`)}
|
|
410
|
+
|
|
411
|
+
#include "${name.HybridTSpecSwift}.hpp"
|
|
412
|
+
|
|
413
|
+
namespace ${cxxNamespace} {
|
|
414
|
+
} // namespace ${cxxNamespace}
|
|
415
|
+
`
|
|
416
|
+
|
|
417
|
+
const files: SourceFile[] = []
|
|
418
|
+
files.push(...allBridgedTypes.flatMap((b) => b.getExtraFiles()))
|
|
419
|
+
files.push({
|
|
420
|
+
content: swiftCxxWrapperCode,
|
|
421
|
+
language: 'swift',
|
|
422
|
+
name: `${name.HybridTSpecCxx}.swift`,
|
|
423
|
+
subdirectory: [],
|
|
424
|
+
platform: 'ios',
|
|
425
|
+
})
|
|
426
|
+
files.push({
|
|
427
|
+
content: cppHybridObjectCode,
|
|
428
|
+
language: 'c++',
|
|
429
|
+
name: `${name.HybridTSpecSwift}.hpp`,
|
|
430
|
+
subdirectory: [],
|
|
431
|
+
platform: 'ios',
|
|
432
|
+
})
|
|
433
|
+
files.push({
|
|
434
|
+
content: cppHybridObjectCodeCpp,
|
|
435
|
+
language: 'c++',
|
|
436
|
+
name: `${name.HybridTSpecSwift}.cpp`,
|
|
437
|
+
subdirectory: [],
|
|
438
|
+
platform: 'ios',
|
|
439
|
+
})
|
|
440
|
+
return files
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function getPropertyForwardImplementation(property: Property): string {
|
|
444
|
+
const bridgedType = new SwiftCxxBridgedType(property.type)
|
|
445
|
+
const convertToCpp = bridgedType.parseFromSwiftToCpp(
|
|
446
|
+
`self.__implementation.${property.name}`,
|
|
447
|
+
'swift'
|
|
448
|
+
)
|
|
449
|
+
const convertFromCpp = bridgedType.parseFromCppToSwift('newValue', 'swift')
|
|
450
|
+
const getter = `
|
|
451
|
+
@inline(__always)
|
|
452
|
+
get {
|
|
453
|
+
return ${indent(convertToCpp, ' ')}
|
|
454
|
+
}
|
|
455
|
+
`.trim()
|
|
456
|
+
const setter = `
|
|
457
|
+
@inline(__always)
|
|
458
|
+
set {
|
|
459
|
+
self.__implementation.${property.name} = ${indent(convertFromCpp, ' ')}
|
|
460
|
+
}
|
|
461
|
+
`.trim()
|
|
462
|
+
|
|
463
|
+
const body = [getter]
|
|
464
|
+
if (!property.isReadonly) {
|
|
465
|
+
body.push(setter)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const code = `
|
|
469
|
+
public final var ${property.name}: ${bridgedType.getTypeCode('swift')} {
|
|
470
|
+
${indent(body.join('\n'), ' ')}
|
|
471
|
+
}
|
|
472
|
+
`
|
|
473
|
+
return code.trim()
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function getMethodForwardImplementation(method: Method): string {
|
|
477
|
+
// wrapped return in a std::expected
|
|
478
|
+
const resultType = new ResultWrappingType(method.returnType)
|
|
479
|
+
addKnownType(`expected_${resultType.getCode('c++')}`, resultType, 'swift')
|
|
480
|
+
const bridgedResultType = new SwiftCxxBridgedType(resultType, true)
|
|
481
|
+
const resultBridge = bridgedResultType.getRequiredBridge()
|
|
482
|
+
if (resultBridge == null)
|
|
483
|
+
throw new Error(
|
|
484
|
+
`Result type (${bridgedResultType.getTypeCode('c++')}) does not have a bridge!`
|
|
485
|
+
)
|
|
486
|
+
const bridgedErrorType = new SwiftCxxBridgedType(resultType.error, true)
|
|
487
|
+
|
|
488
|
+
const returnType = new SwiftCxxBridgedType(method.returnType, true)
|
|
489
|
+
const params = method.parameters.map((p) => {
|
|
490
|
+
const bridgedType = new SwiftCxxBridgedType(p.type)
|
|
491
|
+
return `${p.name}: ${bridgedType.getTypeCode('swift')}`
|
|
492
|
+
})
|
|
493
|
+
const passParams = method.parameters.map((p) => {
|
|
494
|
+
const bridgedType = new SwiftCxxBridgedType(p.type)
|
|
495
|
+
return `${p.name}: ${bridgedType.parseFromCppToSwift(p.name, 'swift')}`
|
|
496
|
+
})
|
|
497
|
+
let body: string
|
|
498
|
+
if (returnType.hasType) {
|
|
499
|
+
body = `
|
|
500
|
+
let __result = try self.__implementation.${method.name}(${passParams.join(', ')})
|
|
501
|
+
let __resultCpp = ${returnType.parseFromSwiftToCpp('__result', 'swift')}
|
|
502
|
+
return bridge.${resultBridge.funcName}(__resultCpp)
|
|
503
|
+
`.trim()
|
|
504
|
+
} else {
|
|
505
|
+
body = `
|
|
506
|
+
try self.__implementation.${method.name}(${passParams.join(', ')})
|
|
507
|
+
return bridge.${resultBridge.funcName}()
|
|
508
|
+
`.trim()
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return `
|
|
512
|
+
@inline(__always)
|
|
513
|
+
public final func ${method.name}(${params.join(', ')}) -> ${bridgedResultType.getTypeCode('swift')} {
|
|
514
|
+
do {
|
|
515
|
+
${indent(body, ' ')}
|
|
516
|
+
} catch (let __error) {
|
|
517
|
+
let __exceptionPtr = ${indent(bridgedErrorType.parseFromSwiftToCpp('__error', 'swift'), ' ')}
|
|
518
|
+
return bridge.${resultBridge.funcName}(__exceptionPtr)
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
`.trim()
|
|
522
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { NitroConfig } from '../../config/NitroConfig.js'
|
|
2
|
+
import { indent } from '../../utils.js'
|
|
3
|
+
import { getHybridObjectName } from '../getHybridObjectName.js'
|
|
4
|
+
import type { SourceImport } from '../SourceFile.js'
|
|
5
|
+
import { HybridObjectType } from '../types/HybridObjectType.js'
|
|
6
|
+
import { SwiftCxxBridgedType } from './SwiftCxxBridgedType.js'
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
/**
|
|
10
|
+
* The name of the Hybrid Object under which it should be registered and exposed to JS to.
|
|
11
|
+
*/
|
|
12
|
+
hybridObjectName: string
|
|
13
|
+
/**
|
|
14
|
+
* The name of the Swift class that will be default-constructed
|
|
15
|
+
*/
|
|
16
|
+
swiftClassName: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface SwiftHybridObjectRegistration {
|
|
20
|
+
cppCode: string
|
|
21
|
+
swiftFunction: string
|
|
22
|
+
requiredImports: SourceImport[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getHybridObjectConstructorCall(
|
|
26
|
+
hybridObjectName: string
|
|
27
|
+
): string {
|
|
28
|
+
const swiftNamespace = NitroConfig.current.getIosModuleName()
|
|
29
|
+
const autolinkingClassName = `${swiftNamespace}Autolinking`
|
|
30
|
+
return `${swiftNamespace}::${autolinkingClassName}::create${hybridObjectName}();`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createSwiftHybridObjectRegistration({
|
|
34
|
+
hybridObjectName,
|
|
35
|
+
swiftClassName,
|
|
36
|
+
}: Props): SwiftHybridObjectRegistration {
|
|
37
|
+
const { HybridTSpecCxx, HybridTSpecSwift, HybridTSpec } =
|
|
38
|
+
getHybridObjectName(hybridObjectName)
|
|
39
|
+
|
|
40
|
+
const type = new HybridObjectType(
|
|
41
|
+
hybridObjectName,
|
|
42
|
+
'swift',
|
|
43
|
+
[],
|
|
44
|
+
NitroConfig.current
|
|
45
|
+
)
|
|
46
|
+
const bridge = new SwiftCxxBridgedType(type)
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
swiftFunction: `
|
|
50
|
+
/**
|
|
51
|
+
* Creates an instance of a Swift class that implements \`${HybridTSpec}\`,
|
|
52
|
+
* and wraps it in a Swift class that can directly interop with C++ (\`${HybridTSpecCxx}\`)
|
|
53
|
+
*
|
|
54
|
+
* This is generated by Nitrogen and will initialize the class specified
|
|
55
|
+
* in the \`"autolinking"\` property of \`nitro.json\` (in this case, \`${swiftClassName}\`).
|
|
56
|
+
*/
|
|
57
|
+
public static func create${hybridObjectName}() -> ${bridge.getTypeCode('swift')} {
|
|
58
|
+
let hybridObject = ${swiftClassName}()
|
|
59
|
+
return ${indent(bridge.parseFromSwiftToCpp('hybridObject', 'swift'), ' ')}
|
|
60
|
+
}
|
|
61
|
+
`.trim(),
|
|
62
|
+
requiredImports: [
|
|
63
|
+
{ name: `${HybridTSpecSwift}.hpp`, language: 'c++', space: 'user' },
|
|
64
|
+
],
|
|
65
|
+
cppCode: `
|
|
66
|
+
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
67
|
+
"${hybridObjectName}",
|
|
68
|
+
[]() -> std::shared_ptr<HybridObject> {
|
|
69
|
+
${type.getCode('c++')} hybridObject = ${getHybridObjectConstructorCall(hybridObjectName)}
|
|
70
|
+
return hybridObject;
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
`.trim(),
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { NitroConfig } from '../../config/NitroConfig.js'
|
|
2
|
+
import { indent } from '../../utils.js'
|
|
3
|
+
import { createFileMetadataString } from '../helpers.js'
|
|
4
|
+
import { Parameter } from '../Parameter.js'
|
|
5
|
+
import type { FileWithReferencedTypes } from '../SourceFile.js'
|
|
6
|
+
import { StructType } from '../types/StructType.js'
|
|
7
|
+
import { SwiftCxxBridgedType } from './SwiftCxxBridgedType.js'
|
|
8
|
+
|
|
9
|
+
export function createSwiftStructBridge(
|
|
10
|
+
struct: StructType
|
|
11
|
+
): FileWithReferencedTypes {
|
|
12
|
+
const fullName = NitroConfig.current.getCxxNamespace(
|
|
13
|
+
'swift',
|
|
14
|
+
struct.structName
|
|
15
|
+
)
|
|
16
|
+
const bridgeNamespace = NitroConfig.current.getSwiftBridgeNamespace('swift')
|
|
17
|
+
const init = createSwiftBridgedConstructor(struct)
|
|
18
|
+
const bridgedProps = struct.properties
|
|
19
|
+
.map((p) => {
|
|
20
|
+
const bridge = new SwiftCxxBridgedType(p, true)
|
|
21
|
+
const cppName = `self.__${p.escapedName}`
|
|
22
|
+
return `
|
|
23
|
+
var ${p.escapedName}: ${p.getCode('swift')} {
|
|
24
|
+
@inline(__always)
|
|
25
|
+
get {
|
|
26
|
+
return ${indent(bridge.parseFromCppToSwift(cppName, 'swift'), ' ')}
|
|
27
|
+
}
|
|
28
|
+
@inline(__always)
|
|
29
|
+
set {
|
|
30
|
+
${cppName} = ${indent(bridge.parseFromSwiftToCpp('newValue', 'swift'), ' ')}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
`.trim()
|
|
34
|
+
})
|
|
35
|
+
.join('\n\n')
|
|
36
|
+
|
|
37
|
+
const code = `
|
|
38
|
+
${createFileMetadataString(`${struct.structName}.swift`)}
|
|
39
|
+
|
|
40
|
+
import NitroModules
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Represents an instance of \`${struct.structName}\`, backed by a C++ struct.
|
|
44
|
+
*/
|
|
45
|
+
public typealias ${struct.structName} = ${fullName}
|
|
46
|
+
|
|
47
|
+
public extension ${struct.structName} {
|
|
48
|
+
private typealias bridge = ${bridgeNamespace}
|
|
49
|
+
|
|
50
|
+
${indent(init, ' ')}
|
|
51
|
+
|
|
52
|
+
${indent(bridgedProps, ' ')}
|
|
53
|
+
}
|
|
54
|
+
`.trim()
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
content: code,
|
|
58
|
+
language: 'swift',
|
|
59
|
+
name: `${struct.structName}.swift`,
|
|
60
|
+
platform: 'ios',
|
|
61
|
+
subdirectory: [],
|
|
62
|
+
referencedTypes: struct.properties,
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function createSwiftBridgedConstructor(struct: StructType): string {
|
|
67
|
+
const params = struct.properties
|
|
68
|
+
.map((p) => new Parameter(p.escapedName, p))
|
|
69
|
+
.map((p) => p.getCode('swift'))
|
|
70
|
+
.join(', ')
|
|
71
|
+
const paramsForward = struct.properties
|
|
72
|
+
.map((p) => {
|
|
73
|
+
const bridged = new SwiftCxxBridgedType(p, true)
|
|
74
|
+
return bridged.parseFromSwiftToCpp(p.escapedName, 'swift')
|
|
75
|
+
})
|
|
76
|
+
.join(', ')
|
|
77
|
+
return `
|
|
78
|
+
/**
|
|
79
|
+
* Create a new instance of \`${struct.structName}\`.
|
|
80
|
+
*/
|
|
81
|
+
init(${params}) {
|
|
82
|
+
self.init(${indent(paramsForward, ' ')})
|
|
83
|
+
}
|
|
84
|
+
`.trim()
|
|
85
|
+
}
|