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,674 @@
|
|
|
1
|
+
import { escapeCppName, toReferenceType } from '../helpers.js'
|
|
2
|
+
import type { SourceImport } from '../SourceFile.js'
|
|
3
|
+
import { VariantType } from '../types/VariantType.js'
|
|
4
|
+
import { ArrayType } from '../types/ArrayType.js'
|
|
5
|
+
import { FunctionType } from '../types/FunctionType.js'
|
|
6
|
+
import { getTypeAs } from '../types/getTypeAs.js'
|
|
7
|
+
import { OptionalType } from '../types/OptionalType.js'
|
|
8
|
+
import { RecordType } from '../types/RecordType.js'
|
|
9
|
+
import type { NamedType, Type } from '../types/Type.js'
|
|
10
|
+
import { TupleType } from '../types/TupleType.js'
|
|
11
|
+
import { escapeComments, indent } from '../../utils.js'
|
|
12
|
+
import { PromiseType } from '../types/PromiseType.js'
|
|
13
|
+
import { SwiftCxxBridgedType } from './SwiftCxxBridgedType.js'
|
|
14
|
+
import { HybridObjectType } from '../types/HybridObjectType.js'
|
|
15
|
+
import { getHybridObjectName } from '../getHybridObjectName.js'
|
|
16
|
+
import { NitroConfig } from '../../config/NitroConfig.js'
|
|
17
|
+
import { getUmbrellaHeaderName } from '../../autolinking/ios/createSwiftUmbrellaHeader.js'
|
|
18
|
+
import { VoidType } from '../types/VoidType.js'
|
|
19
|
+
import { NamedWrappingType } from '../types/NamedWrappingType.js'
|
|
20
|
+
import { ErrorType } from '../types/ErrorType.js'
|
|
21
|
+
import { ResultWrappingType } from '../types/ResultWrappingType.js'
|
|
22
|
+
|
|
23
|
+
export interface SwiftCxxHelper {
|
|
24
|
+
cxxHeader: {
|
|
25
|
+
code: string
|
|
26
|
+
requiredIncludes: SourceImport[]
|
|
27
|
+
}
|
|
28
|
+
cxxImplementation?: {
|
|
29
|
+
code: string
|
|
30
|
+
requiredIncludes: SourceImport[]
|
|
31
|
+
}
|
|
32
|
+
funcName: string
|
|
33
|
+
specializationName: string
|
|
34
|
+
cxxType: string
|
|
35
|
+
dependencies: SwiftCxxHelper[]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createSwiftCxxHelpers(type: Type): SwiftCxxHelper | undefined {
|
|
39
|
+
switch (type.kind) {
|
|
40
|
+
case 'hybrid-object':
|
|
41
|
+
return createCxxHybridObjectSwiftHelper(getTypeAs(type, HybridObjectType))
|
|
42
|
+
case 'optional':
|
|
43
|
+
return createCxxOptionalSwiftHelper(getTypeAs(type, OptionalType))
|
|
44
|
+
case 'array':
|
|
45
|
+
return createCxxVectorSwiftHelper(getTypeAs(type, ArrayType))
|
|
46
|
+
case 'record':
|
|
47
|
+
return createCxxUnorderedMapSwiftHelper(getTypeAs(type, RecordType))
|
|
48
|
+
case 'function':
|
|
49
|
+
return createCxxFunctionSwiftHelper(getTypeAs(type, FunctionType))
|
|
50
|
+
case 'variant':
|
|
51
|
+
return createCxxVariantSwiftHelper(getTypeAs(type, VariantType))
|
|
52
|
+
case 'tuple':
|
|
53
|
+
return createCxxTupleSwiftHelper(getTypeAs(type, TupleType))
|
|
54
|
+
case 'promise':
|
|
55
|
+
return createCxxPromiseSwiftHelper(getTypeAs(type, PromiseType))
|
|
56
|
+
case 'result-wrapper':
|
|
57
|
+
return createCxxResultWrapperSwiftHelper(
|
|
58
|
+
getTypeAs(type, ResultWrappingType)
|
|
59
|
+
)
|
|
60
|
+
default:
|
|
61
|
+
return undefined
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Creates a C++ `create_HybridTSpecSwift(value)` function that can be called from Swift.
|
|
67
|
+
*/
|
|
68
|
+
function createCxxHybridObjectSwiftHelper(
|
|
69
|
+
type: HybridObjectType
|
|
70
|
+
): SwiftCxxHelper {
|
|
71
|
+
const actualType = type.getCode('c++')
|
|
72
|
+
const modulename = type.sourceConfig.getIosModuleName()
|
|
73
|
+
const { HybridTSpecCxx, HybridTSpecSwift, HybridTSpec } = getHybridObjectName(
|
|
74
|
+
type.hybridObjectName
|
|
75
|
+
)
|
|
76
|
+
const swiftWrappingType = type.sourceConfig.getCxxNamespace(
|
|
77
|
+
'c++',
|
|
78
|
+
HybridTSpecSwift
|
|
79
|
+
)
|
|
80
|
+
const swiftPartType = `${modulename}::${HybridTSpecCxx}`
|
|
81
|
+
const name = escapeCppName(actualType)
|
|
82
|
+
|
|
83
|
+
const upcastHelpers = type.baseTypes.map((base) =>
|
|
84
|
+
createCxxUpcastHelper(base, type)
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
const includes: SourceImport[] = []
|
|
88
|
+
if (!type.sourceConfig.isExternalConfig) {
|
|
89
|
+
// we are including our Swift helper internally. this is a private header so we can include just fine.
|
|
90
|
+
includes.push({
|
|
91
|
+
language: 'c++',
|
|
92
|
+
// Hybrid Object Swift C++ class wrapper
|
|
93
|
+
name: `${HybridTSpecSwift}.hpp`,
|
|
94
|
+
space: 'user',
|
|
95
|
+
})
|
|
96
|
+
} else {
|
|
97
|
+
// it's an external type - we need to include the external module's bridge as the *Swift.hpp header is private.
|
|
98
|
+
const externalBridgeName = type.sourceConfig.getSwiftBridgeHeaderName()
|
|
99
|
+
const moduleName = type.sourceConfig.getIosModuleName()
|
|
100
|
+
includes.push({
|
|
101
|
+
language: 'c++',
|
|
102
|
+
name: `${moduleName}/${externalBridgeName}.hpp`,
|
|
103
|
+
space: 'system',
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let getImplementation: string
|
|
108
|
+
let createImplementation: string
|
|
109
|
+
if (!type.sourceConfig.isExternalConfig) {
|
|
110
|
+
// We own the implementation - we call into Swift to convert it internally
|
|
111
|
+
createImplementation = `
|
|
112
|
+
${swiftPartType} swiftPart = ${swiftPartType}::fromUnsafe(swiftUnsafePointer);
|
|
113
|
+
return std::make_shared<${swiftWrappingType}>(swiftPart);
|
|
114
|
+
`.trim()
|
|
115
|
+
getImplementation = `
|
|
116
|
+
std::shared_ptr<${swiftWrappingType}> swiftWrapper = std::dynamic_pointer_cast<${swiftWrappingType}>(cppType);
|
|
117
|
+
#ifdef NITRO_DEBUG
|
|
118
|
+
if (swiftWrapper == nullptr) [[unlikely]] {
|
|
119
|
+
throw std::runtime_error("Class \\"${HybridTSpec}\\" is not implemented in Swift!");
|
|
120
|
+
}
|
|
121
|
+
#endif
|
|
122
|
+
${swiftPartType}& swiftPart = swiftWrapper->getSwiftPart();
|
|
123
|
+
return swiftPart.toUnsafe();
|
|
124
|
+
`.trim()
|
|
125
|
+
} else {
|
|
126
|
+
// It's an external type - we have to delegate the call to the external library's functions
|
|
127
|
+
const cxxNamespace = type.sourceConfig.getSwiftBridgeNamespace('c++')
|
|
128
|
+
const internalType = type.getCode('c++', { fullyQualified: false })
|
|
129
|
+
const internalName = escapeCppName(internalType)
|
|
130
|
+
createImplementation = `
|
|
131
|
+
// Implemented in ${type.sourceConfig.getIosModuleName()}
|
|
132
|
+
return ${cxxNamespace}::create_${internalName}(swiftUnsafePointer);
|
|
133
|
+
`.trim()
|
|
134
|
+
getImplementation = `
|
|
135
|
+
// Implemented in ${type.sourceConfig.getIosModuleName()}
|
|
136
|
+
return ${cxxNamespace}::get_${internalName}(cppType);
|
|
137
|
+
`.trim()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
cxxType: actualType,
|
|
142
|
+
funcName: `create_${name}`,
|
|
143
|
+
specializationName: name,
|
|
144
|
+
cxxHeader: {
|
|
145
|
+
code: `
|
|
146
|
+
/**
|
|
147
|
+
* Specialized version of \`${escapeComments(actualType)}\`.
|
|
148
|
+
*/
|
|
149
|
+
using ${name} = ${actualType};
|
|
150
|
+
${actualType} create_${name}(void* _Nonnull swiftUnsafePointer) noexcept;
|
|
151
|
+
void* _Nonnull get_${name}(${name} cppType) noexcept;
|
|
152
|
+
`.trim(),
|
|
153
|
+
requiredIncludes: type.getRequiredImports('c++'),
|
|
154
|
+
},
|
|
155
|
+
cxxImplementation: {
|
|
156
|
+
code: `
|
|
157
|
+
${actualType} create_${name}(void* _Nonnull swiftUnsafePointer) noexcept {
|
|
158
|
+
${indent(createImplementation, ' ')}
|
|
159
|
+
}
|
|
160
|
+
void* _Nonnull get_${name}(${name} cppType) noexcept {
|
|
161
|
+
${indent(getImplementation, ' ')}
|
|
162
|
+
}
|
|
163
|
+
`.trim(),
|
|
164
|
+
requiredIncludes: [
|
|
165
|
+
...includes,
|
|
166
|
+
{
|
|
167
|
+
language: 'c++',
|
|
168
|
+
// Swift umbrella header
|
|
169
|
+
name: getUmbrellaHeaderName(),
|
|
170
|
+
space: 'user',
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
},
|
|
174
|
+
dependencies: [...upcastHelpers, createCxxWeakPtrHelper(type)],
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function createCxxUpcastHelper(
|
|
179
|
+
baseType: HybridObjectType,
|
|
180
|
+
childType: HybridObjectType
|
|
181
|
+
): SwiftCxxHelper {
|
|
182
|
+
const cppBaseType = baseType.getCode('c++')
|
|
183
|
+
const cppChildType = childType.getCode('c++')
|
|
184
|
+
const funcName = escapeCppName(
|
|
185
|
+
`upcast_${childType.hybridObjectName}_to_${baseType.hybridObjectName}`
|
|
186
|
+
)
|
|
187
|
+
return {
|
|
188
|
+
cxxType: cppBaseType,
|
|
189
|
+
funcName: funcName,
|
|
190
|
+
specializationName: funcName,
|
|
191
|
+
cxxHeader: {
|
|
192
|
+
code: `
|
|
193
|
+
inline ${cppBaseType} ${funcName}(${cppChildType} child) noexcept { return child; }
|
|
194
|
+
`.trim(),
|
|
195
|
+
requiredIncludes: [],
|
|
196
|
+
},
|
|
197
|
+
dependencies: [],
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function createCxxWeakPtrHelper(type: HybridObjectType): SwiftCxxHelper {
|
|
202
|
+
const actualType = type.getCode('c++', { mode: 'weak' })
|
|
203
|
+
const specializationName = escapeCppName(actualType)
|
|
204
|
+
const funcName = `weakify_${escapeCppName(type.getCode('c++'))}`
|
|
205
|
+
const parameterType = type.getCode('c++', { mode: 'strong' })
|
|
206
|
+
return {
|
|
207
|
+
cxxType: actualType,
|
|
208
|
+
funcName: funcName,
|
|
209
|
+
specializationName: specializationName,
|
|
210
|
+
cxxHeader: {
|
|
211
|
+
code: `
|
|
212
|
+
using ${specializationName} = ${actualType};
|
|
213
|
+
inline ${specializationName} ${funcName}(const ${parameterType}& strong) noexcept { return strong; }
|
|
214
|
+
`.trim(),
|
|
215
|
+
requiredIncludes: [],
|
|
216
|
+
},
|
|
217
|
+
dependencies: [],
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Creates a C++ `create_optional<T>(value)` function that can be called from Swift.
|
|
223
|
+
*/
|
|
224
|
+
function createCxxOptionalSwiftHelper(type: OptionalType): SwiftCxxHelper {
|
|
225
|
+
const actualType = type.getCode('c++')
|
|
226
|
+
const wrappedBridge = new SwiftCxxBridgedType(type.wrappingType, true)
|
|
227
|
+
const name = escapeCppName(actualType)
|
|
228
|
+
// TODO: Remove has_ and get_ wrappers once https://github.com/swiftlang/swift/issues/83801 is fixed.
|
|
229
|
+
// TODO: Remove create_ wrapper once https://github.com/swiftlang/swift/issues/75834 is fixed.
|
|
230
|
+
return {
|
|
231
|
+
cxxType: actualType,
|
|
232
|
+
funcName: `create_${name}`,
|
|
233
|
+
specializationName: name,
|
|
234
|
+
cxxHeader: {
|
|
235
|
+
code: `
|
|
236
|
+
/**
|
|
237
|
+
* Specialized version of \`${escapeComments(actualType)}\`.
|
|
238
|
+
*/
|
|
239
|
+
using ${name} = ${actualType};
|
|
240
|
+
inline ${actualType} create_${name}(const ${wrappedBridge.getTypeCode('c++')}& value) noexcept {
|
|
241
|
+
return ${actualType}(${indent(wrappedBridge.parseFromSwiftToCpp('value', 'c++'), ' ')});
|
|
242
|
+
}
|
|
243
|
+
inline bool has_value_${name}(const ${actualType}& optional) noexcept {
|
|
244
|
+
return optional.has_value();
|
|
245
|
+
}
|
|
246
|
+
inline ${wrappedBridge.getTypeCode('c++')} get_${name}(const ${actualType}& optional) noexcept {
|
|
247
|
+
return *optional;
|
|
248
|
+
}
|
|
249
|
+
`.trim(),
|
|
250
|
+
requiredIncludes: [
|
|
251
|
+
{
|
|
252
|
+
name: 'optional',
|
|
253
|
+
space: 'system',
|
|
254
|
+
language: 'c++',
|
|
255
|
+
},
|
|
256
|
+
...wrappedBridge.getRequiredImports('c++'),
|
|
257
|
+
],
|
|
258
|
+
},
|
|
259
|
+
dependencies: [],
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Creates a C++ `create_vector_T<T>(size)` function that can be called from Swift.
|
|
265
|
+
*/
|
|
266
|
+
function createCxxVectorSwiftHelper(type: ArrayType): SwiftCxxHelper {
|
|
267
|
+
const actualType = type.getCode('c++')
|
|
268
|
+
const bridgedType = new SwiftCxxBridgedType(type)
|
|
269
|
+
const name = escapeCppName(actualType)
|
|
270
|
+
return {
|
|
271
|
+
cxxType: actualType,
|
|
272
|
+
funcName: `create_${name}`,
|
|
273
|
+
specializationName: name,
|
|
274
|
+
cxxHeader: {
|
|
275
|
+
code: `
|
|
276
|
+
/**
|
|
277
|
+
* Specialized version of \`${escapeComments(actualType)}\`.
|
|
278
|
+
*/
|
|
279
|
+
using ${name} = ${actualType};
|
|
280
|
+
inline ${actualType} create_${name}(size_t size) noexcept {
|
|
281
|
+
${actualType} vector;
|
|
282
|
+
vector.reserve(size);
|
|
283
|
+
return vector;
|
|
284
|
+
}
|
|
285
|
+
`.trim(),
|
|
286
|
+
requiredIncludes: [
|
|
287
|
+
{
|
|
288
|
+
name: 'vector',
|
|
289
|
+
space: 'system',
|
|
290
|
+
language: 'c++',
|
|
291
|
+
},
|
|
292
|
+
...bridgedType.getRequiredImports('c++'),
|
|
293
|
+
],
|
|
294
|
+
},
|
|
295
|
+
dependencies: [],
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Creates a C++ `makeUnorderedMap<T>(size)` function that can be called from Swift.
|
|
301
|
+
*/
|
|
302
|
+
function createCxxUnorderedMapSwiftHelper(type: RecordType): SwiftCxxHelper {
|
|
303
|
+
const actualType = type.getCode('c++')
|
|
304
|
+
const bridgedType = new SwiftCxxBridgedType(type)
|
|
305
|
+
const name = escapeCppName(actualType)
|
|
306
|
+
const keyType = type.keyType.getCode('c++')
|
|
307
|
+
const valueType = type.valueType.getCode('c++')
|
|
308
|
+
return {
|
|
309
|
+
cxxType: actualType,
|
|
310
|
+
funcName: `create_${name}`,
|
|
311
|
+
specializationName: name,
|
|
312
|
+
cxxHeader: {
|
|
313
|
+
code: `
|
|
314
|
+
/**
|
|
315
|
+
* Specialized version of \`${escapeComments(actualType)}\`.
|
|
316
|
+
*/
|
|
317
|
+
using ${name} = ${actualType};
|
|
318
|
+
inline ${actualType} create_${name}(size_t size) noexcept {
|
|
319
|
+
${actualType} map;
|
|
320
|
+
map.reserve(size);
|
|
321
|
+
return map;
|
|
322
|
+
}
|
|
323
|
+
inline std::vector<${keyType}> get_${name}_keys(const ${name}& map) noexcept {
|
|
324
|
+
std::vector<${keyType}> keys;
|
|
325
|
+
keys.reserve(map.size());
|
|
326
|
+
for (const auto& entry : map) {
|
|
327
|
+
keys.push_back(entry.first);
|
|
328
|
+
}
|
|
329
|
+
return keys;
|
|
330
|
+
}
|
|
331
|
+
inline ${valueType} get_${name}_value(const ${name}& map, const ${keyType}& key) noexcept {
|
|
332
|
+
return map.find(key)->second;
|
|
333
|
+
}
|
|
334
|
+
inline void emplace_${name}(${name}& map, const ${keyType}& key, const ${valueType}& value) noexcept {
|
|
335
|
+
map.emplace(key, value);
|
|
336
|
+
}
|
|
337
|
+
`.trim(),
|
|
338
|
+
requiredIncludes: [
|
|
339
|
+
{
|
|
340
|
+
name: 'unordered_map',
|
|
341
|
+
space: 'system',
|
|
342
|
+
language: 'c++',
|
|
343
|
+
},
|
|
344
|
+
...bridgedType.getRequiredImports('c++'),
|
|
345
|
+
],
|
|
346
|
+
},
|
|
347
|
+
dependencies: [],
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Creates a C++ `Func_XXXXX` specialization that can be used from Swift.
|
|
353
|
+
*/
|
|
354
|
+
function createCxxFunctionSwiftHelper(type: FunctionType): SwiftCxxHelper {
|
|
355
|
+
const actualType = type.getCode('c++')
|
|
356
|
+
const bridgedType = new SwiftCxxBridgedType(type)
|
|
357
|
+
const returnBridge = new SwiftCxxBridgedType(type.returnType)
|
|
358
|
+
const paramsSignature = type.parameters.map((p) => {
|
|
359
|
+
if (p.canBePassedByReference) {
|
|
360
|
+
return `${toReferenceType(p.getCode('c++'))} ${p.escapedName}`
|
|
361
|
+
} else {
|
|
362
|
+
return `${p.getCode('c++')} ${p.escapedName}`
|
|
363
|
+
}
|
|
364
|
+
})
|
|
365
|
+
const paramsForward = type.parameters.map((p) => {
|
|
366
|
+
const bridge = new SwiftCxxBridgedType(p)
|
|
367
|
+
return bridge.parseFromCppToSwift(p.escapedName, 'c++')
|
|
368
|
+
})
|
|
369
|
+
const name = type.specializationName
|
|
370
|
+
const wrapperName = `${name}_Wrapper`
|
|
371
|
+
const swiftClassName = `${NitroConfig.current.getIosModuleName()}::${type.specializationName}`
|
|
372
|
+
|
|
373
|
+
const callParamsForward = type.parameters.map((p) => {
|
|
374
|
+
const bridge = new SwiftCxxBridgedType(p)
|
|
375
|
+
return bridge.parseFromSwiftToCpp(p.escapedName, 'c++')
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
const callFuncReturnType = returnBridge.getTypeCode('c++')
|
|
379
|
+
const callCppFuncParamsSignature = type.parameters.map((p) => {
|
|
380
|
+
const bridge = new SwiftCxxBridgedType(p)
|
|
381
|
+
const cppType = bridge.getTypeCode('c++')
|
|
382
|
+
return `${cppType} ${p.escapedName}`
|
|
383
|
+
})
|
|
384
|
+
let callCppFuncBody: string
|
|
385
|
+
if (returnBridge.hasType) {
|
|
386
|
+
callCppFuncBody = `
|
|
387
|
+
auto __result = _function->operator()(${callParamsForward.join(', ')});
|
|
388
|
+
return ${returnBridge.parseFromCppToSwift('__result', 'c++')};
|
|
389
|
+
`.trim()
|
|
390
|
+
} else {
|
|
391
|
+
callCppFuncBody = `_function->operator()(${callParamsForward.join(', ')});`
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
let body: string
|
|
395
|
+
if (type.returnType.kind === 'void') {
|
|
396
|
+
body = `
|
|
397
|
+
swiftClosure.call(${paramsForward.join(', ')});
|
|
398
|
+
`.trim()
|
|
399
|
+
} else {
|
|
400
|
+
body = `
|
|
401
|
+
auto __result = swiftClosure.call(${paramsForward.join(', ')});
|
|
402
|
+
return ${returnBridge.parseFromSwiftToCpp('__result', 'c++')};
|
|
403
|
+
`.trim()
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// TODO: Remove our std::function wrapper once https://github.com/swiftlang/swift/issues/75844 is fixed.
|
|
407
|
+
return {
|
|
408
|
+
cxxType: actualType,
|
|
409
|
+
funcName: `create_${name}`,
|
|
410
|
+
specializationName: name,
|
|
411
|
+
cxxHeader: {
|
|
412
|
+
code: `
|
|
413
|
+
/**
|
|
414
|
+
* Specialized version of \`${type.getCode('c++', { includeNameInfo: false })}\`.
|
|
415
|
+
*/
|
|
416
|
+
using ${name} = ${actualType};
|
|
417
|
+
/**
|
|
418
|
+
* Wrapper class for a \`${escapeComments(actualType)}\`, this can be used from Swift.
|
|
419
|
+
*/
|
|
420
|
+
class ${wrapperName} final {
|
|
421
|
+
public:
|
|
422
|
+
explicit ${wrapperName}(${actualType}&& func): _function(std::make_unique<${actualType}>(std::move(func))) {}
|
|
423
|
+
inline ${callFuncReturnType} call(${callCppFuncParamsSignature.join(', ')}) const noexcept {
|
|
424
|
+
${indent(callCppFuncBody, ' ')}
|
|
425
|
+
}
|
|
426
|
+
private:
|
|
427
|
+
std::unique_ptr<${actualType}> _function;
|
|
428
|
+
} SWIFT_NONCOPYABLE;
|
|
429
|
+
${name} create_${name}(void* _Nonnull swiftClosureWrapper) noexcept;
|
|
430
|
+
inline ${wrapperName} wrap_${name}(${name} value) noexcept {
|
|
431
|
+
return ${wrapperName}(std::move(value));
|
|
432
|
+
}
|
|
433
|
+
`.trim(),
|
|
434
|
+
requiredIncludes: [
|
|
435
|
+
{
|
|
436
|
+
name: 'functional',
|
|
437
|
+
space: 'system',
|
|
438
|
+
language: 'c++',
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
name: 'memory',
|
|
442
|
+
space: 'system',
|
|
443
|
+
language: 'c++',
|
|
444
|
+
},
|
|
445
|
+
...bridgedType.getRequiredImports('c++'),
|
|
446
|
+
],
|
|
447
|
+
},
|
|
448
|
+
cxxImplementation: {
|
|
449
|
+
code: `
|
|
450
|
+
${name} create_${name}(void* _Nonnull swiftClosureWrapper) noexcept {
|
|
451
|
+
auto swiftClosure = ${swiftClassName}::fromUnsafe(swiftClosureWrapper);
|
|
452
|
+
return [swiftClosure = std::move(swiftClosure)](${paramsSignature.join(', ')}) mutable -> ${type.returnType.getCode('c++')} {
|
|
453
|
+
${indent(body, ' ')}
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
`.trim(),
|
|
457
|
+
requiredIncludes: [
|
|
458
|
+
{
|
|
459
|
+
language: 'c++',
|
|
460
|
+
// Swift umbrella header
|
|
461
|
+
name: getUmbrellaHeaderName(),
|
|
462
|
+
space: 'user',
|
|
463
|
+
},
|
|
464
|
+
],
|
|
465
|
+
},
|
|
466
|
+
dependencies: [],
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Creates multiple C++ `create_variant_A_B_C<A, B, C>(A...)` functions that can be called from Swift.
|
|
472
|
+
*/
|
|
473
|
+
function createCxxVariantSwiftHelper(type: VariantType): SwiftCxxHelper {
|
|
474
|
+
const actualType = type.getCode('c++')
|
|
475
|
+
const bridgedType = new SwiftCxxBridgedType(type)
|
|
476
|
+
const name = escapeCppName(actualType)
|
|
477
|
+
const createFunctions = type.variants.map((t) => {
|
|
478
|
+
const param = t.canBePassedByReference
|
|
479
|
+
? toReferenceType(t.getCode('c++'))
|
|
480
|
+
: t.getCode('c++')
|
|
481
|
+
|
|
482
|
+
return `
|
|
483
|
+
inline ${name} create_${name}(${param} value) noexcept {
|
|
484
|
+
return ${name}(value);
|
|
485
|
+
}
|
|
486
|
+
`.trim()
|
|
487
|
+
})
|
|
488
|
+
const getFunctions = type.variants.map((t, i) => {
|
|
489
|
+
return `
|
|
490
|
+
inline ${t.getCode('c++')} get_${i}() const noexcept {
|
|
491
|
+
return std::get<${i}>(variant);
|
|
492
|
+
}`.trim()
|
|
493
|
+
})
|
|
494
|
+
return {
|
|
495
|
+
cxxType: actualType,
|
|
496
|
+
funcName: `create_${name}`,
|
|
497
|
+
specializationName: name,
|
|
498
|
+
cxxHeader: {
|
|
499
|
+
code: `
|
|
500
|
+
/**
|
|
501
|
+
* Wrapper struct for \`${escapeComments(actualType)}\`.
|
|
502
|
+
* std::variant cannot be used in Swift because of a Swift bug.
|
|
503
|
+
* Not even specializing it works. So we create a wrapper struct.
|
|
504
|
+
*/
|
|
505
|
+
struct ${name} {
|
|
506
|
+
${actualType} variant;
|
|
507
|
+
${name}(${actualType} variant): variant(variant) { }
|
|
508
|
+
operator ${actualType}() const noexcept {
|
|
509
|
+
return variant;
|
|
510
|
+
}
|
|
511
|
+
inline size_t index() const noexcept {
|
|
512
|
+
return variant.index();
|
|
513
|
+
}
|
|
514
|
+
${indent(getFunctions.join('\n'), ' ')}
|
|
515
|
+
};
|
|
516
|
+
${createFunctions.join('\n')}
|
|
517
|
+
`.trim(),
|
|
518
|
+
|
|
519
|
+
requiredIncludes: [
|
|
520
|
+
{
|
|
521
|
+
name: 'variant',
|
|
522
|
+
space: 'system',
|
|
523
|
+
language: 'c++',
|
|
524
|
+
},
|
|
525
|
+
...bridgedType.getRequiredImports('c++'),
|
|
526
|
+
],
|
|
527
|
+
},
|
|
528
|
+
dependencies: [],
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Creates a C++ `create_tuple_A_B_C<A, B, C>(A, B, C)` function that can be called from Swift.
|
|
534
|
+
*/
|
|
535
|
+
function createCxxTupleSwiftHelper(type: TupleType): SwiftCxxHelper {
|
|
536
|
+
const actualType = type.getCode('c++')
|
|
537
|
+
const bridgedType = new SwiftCxxBridgedType(type)
|
|
538
|
+
const name = escapeCppName(actualType)
|
|
539
|
+
const typesSignature = type.itemTypes
|
|
540
|
+
.map((t, i) => {
|
|
541
|
+
const code = t.getCode('c++')
|
|
542
|
+
return `${t.canBePassedByReference ? toReferenceType(code) : code} arg${i}`
|
|
543
|
+
})
|
|
544
|
+
.join(', ')
|
|
545
|
+
const typesForward = type.itemTypes.map((_t, i) => `arg${i}`).join(', ')
|
|
546
|
+
return {
|
|
547
|
+
cxxType: actualType,
|
|
548
|
+
funcName: `create_${name}`,
|
|
549
|
+
specializationName: name,
|
|
550
|
+
cxxHeader: {
|
|
551
|
+
code: `
|
|
552
|
+
/**
|
|
553
|
+
* Specialized version of \`${escapeComments(actualType)}\`.
|
|
554
|
+
*/
|
|
555
|
+
using ${name} = ${actualType};
|
|
556
|
+
inline ${actualType} create_${name}(${typesSignature}) noexcept {
|
|
557
|
+
return ${actualType} { ${typesForward} };
|
|
558
|
+
}
|
|
559
|
+
`.trim(),
|
|
560
|
+
requiredIncludes: [
|
|
561
|
+
{
|
|
562
|
+
name: 'tuple',
|
|
563
|
+
space: 'system',
|
|
564
|
+
language: 'c++',
|
|
565
|
+
},
|
|
566
|
+
...bridgedType.getRequiredImports('c++'),
|
|
567
|
+
],
|
|
568
|
+
},
|
|
569
|
+
dependencies: [],
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Create a C++ `create_result` function that can be called from Swift to create a `Result<T>`.
|
|
575
|
+
*/
|
|
576
|
+
function createCxxResultWrapperSwiftHelper(
|
|
577
|
+
type: ResultWrappingType
|
|
578
|
+
): SwiftCxxHelper {
|
|
579
|
+
// TODO: Remove the ResultWrappingType once https://github.com/swiftlang/swift/issues/75290 is fixed.
|
|
580
|
+
const actualType = type.getCode('c++')
|
|
581
|
+
const name = escapeCppName(type.getCode('c++'))
|
|
582
|
+
const funcName = `create_${name}`
|
|
583
|
+
|
|
584
|
+
const functions: string[] = []
|
|
585
|
+
if (type.result.kind === 'void') {
|
|
586
|
+
functions.push(
|
|
587
|
+
`
|
|
588
|
+
inline ${name} ${funcName}() noexcept {
|
|
589
|
+
return ${actualType}::withValue();
|
|
590
|
+
}`.trim()
|
|
591
|
+
)
|
|
592
|
+
} else {
|
|
593
|
+
const typeParam = type.result.canBePassedByReference
|
|
594
|
+
? `const ${type.result.getCode('c++')}&`
|
|
595
|
+
: type.result.getCode('c++')
|
|
596
|
+
functions.push(
|
|
597
|
+
`
|
|
598
|
+
inline ${name} ${funcName}(${typeParam} value) noexcept {
|
|
599
|
+
return ${actualType}::withValue(${type.result.canBePassedByReference ? 'value' : 'std::move(value)'});
|
|
600
|
+
}`.trim()
|
|
601
|
+
)
|
|
602
|
+
}
|
|
603
|
+
functions.push(
|
|
604
|
+
`
|
|
605
|
+
inline ${name} ${funcName}(const ${type.error.getCode('c++')}& error) noexcept {
|
|
606
|
+
return ${actualType}::withError(error);
|
|
607
|
+
}`.trim()
|
|
608
|
+
)
|
|
609
|
+
|
|
610
|
+
return {
|
|
611
|
+
cxxType: actualType,
|
|
612
|
+
specializationName: name,
|
|
613
|
+
funcName: funcName,
|
|
614
|
+
cxxHeader: {
|
|
615
|
+
code: `
|
|
616
|
+
using ${name} = ${actualType};
|
|
617
|
+
${functions.join('\n')}
|
|
618
|
+
`.trim(),
|
|
619
|
+
requiredIncludes: type.getRequiredImports('c++'),
|
|
620
|
+
},
|
|
621
|
+
dependencies: [],
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Creates a C++ `create_promise_T()` function that can be called from Swift to create a `std::shared_ptr<Promise<T>>`.
|
|
627
|
+
*/
|
|
628
|
+
function createCxxPromiseSwiftHelper(type: PromiseType): SwiftCxxHelper {
|
|
629
|
+
const resultingType = type.resultingType.getCode('c++')
|
|
630
|
+
const bridgedType = new SwiftCxxBridgedType(type)
|
|
631
|
+
const actualType = `std::shared_ptr<Promise<${resultingType}>>`
|
|
632
|
+
|
|
633
|
+
const resolverArgs: NamedType[] = []
|
|
634
|
+
if (type.resultingType.kind !== 'void') {
|
|
635
|
+
resolverArgs.push(new NamedWrappingType('result', type.resultingType))
|
|
636
|
+
}
|
|
637
|
+
const resolveFunction = new FunctionType(new VoidType(), resolverArgs)
|
|
638
|
+
const rejectFunction = new FunctionType(new VoidType(), [
|
|
639
|
+
new NamedWrappingType('error', new ErrorType()),
|
|
640
|
+
])
|
|
641
|
+
|
|
642
|
+
const name = escapeCppName(actualType)
|
|
643
|
+
return {
|
|
644
|
+
cxxType: actualType,
|
|
645
|
+
funcName: `create_${name}`,
|
|
646
|
+
specializationName: name,
|
|
647
|
+
cxxHeader: {
|
|
648
|
+
code: `
|
|
649
|
+
/**
|
|
650
|
+
* Specialized version of \`${escapeComments(actualType)}\`.
|
|
651
|
+
*/
|
|
652
|
+
using ${name} = ${actualType};
|
|
653
|
+
inline ${actualType} create_${name}() noexcept {
|
|
654
|
+
return Promise<${resultingType}>::create();
|
|
655
|
+
}
|
|
656
|
+
inline PromiseHolder<${resultingType}> wrap_${name}(${actualType} promise) noexcept {
|
|
657
|
+
return PromiseHolder<${resultingType}>(std::move(promise));
|
|
658
|
+
}
|
|
659
|
+
`.trim(),
|
|
660
|
+
requiredIncludes: [
|
|
661
|
+
{
|
|
662
|
+
name: 'NitroModules/PromiseHolder.hpp',
|
|
663
|
+
space: 'system',
|
|
664
|
+
language: 'c++',
|
|
665
|
+
},
|
|
666
|
+
...bridgedType.getRequiredImports('c++'),
|
|
667
|
+
],
|
|
668
|
+
},
|
|
669
|
+
dependencies: [
|
|
670
|
+
createCxxFunctionSwiftHelper(resolveFunction),
|
|
671
|
+
createCxxFunctionSwiftHelper(rejectFunction),
|
|
672
|
+
],
|
|
673
|
+
}
|
|
674
|
+
}
|