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,874 @@
|
|
|
1
|
+
import { NitroConfig } from '../../config/NitroConfig.js'
|
|
2
|
+
import { indent } from '../../utils.js'
|
|
3
|
+
import type { BridgedType } from '../BridgedType.js'
|
|
4
|
+
import { getForwardDeclaration } from '../c++/getForwardDeclaration.js'
|
|
5
|
+
import {
|
|
6
|
+
getHybridObjectName,
|
|
7
|
+
type HybridObjectName,
|
|
8
|
+
} from '../getHybridObjectName.js'
|
|
9
|
+
import type { SourceFile, SourceImport } from '../SourceFile.js'
|
|
10
|
+
import { ArrayType } from '../types/ArrayType.js'
|
|
11
|
+
import { EnumType } from '../types/EnumType.js'
|
|
12
|
+
import { FunctionType } from '../types/FunctionType.js'
|
|
13
|
+
import { getTypeAs } from '../types/getTypeAs.js'
|
|
14
|
+
import { HybridObjectType } from '../types/HybridObjectType.js'
|
|
15
|
+
import { OptionalType } from '../types/OptionalType.js'
|
|
16
|
+
import { PromiseType } from '../types/PromiseType.js'
|
|
17
|
+
import { RecordType } from '../types/RecordType.js'
|
|
18
|
+
import { StructType } from '../types/StructType.js'
|
|
19
|
+
import { TupleType } from '../types/TupleType.js'
|
|
20
|
+
import type { Type } from '../types/Type.js'
|
|
21
|
+
import { VariantType } from '../types/VariantType.js'
|
|
22
|
+
import { getReferencedTypes } from '../getReferencedTypes.js'
|
|
23
|
+
import {
|
|
24
|
+
createSwiftCxxHelpers,
|
|
25
|
+
type SwiftCxxHelper,
|
|
26
|
+
} from './SwiftCxxTypeHelper.js'
|
|
27
|
+
import { createSwiftEnumBridge } from './SwiftEnum.js'
|
|
28
|
+
import { createSwiftStructBridge } from './SwiftStruct.js'
|
|
29
|
+
import { createSwiftVariant } from './SwiftVariant.js'
|
|
30
|
+
import { VoidType } from '../types/VoidType.js'
|
|
31
|
+
import { NamedWrappingType } from '../types/NamedWrappingType.js'
|
|
32
|
+
import { ErrorType } from '../types/ErrorType.js'
|
|
33
|
+
import { createSwiftFunctionBridge } from './SwiftFunction.js'
|
|
34
|
+
import type { Language } from '../../getPlatformSpecs.js'
|
|
35
|
+
|
|
36
|
+
// TODO: Remove enum bridge once Swift fixes bidirectional enums crashing the `-Swift.h` header.
|
|
37
|
+
|
|
38
|
+
export class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
|
|
39
|
+
readonly type: Type
|
|
40
|
+
private readonly isBridgingToDirectCppTarget: boolean
|
|
41
|
+
|
|
42
|
+
constructor(type: Type, isBridgingToDirectCppTarget: boolean = false) {
|
|
43
|
+
this.type = type
|
|
44
|
+
this.isBridgingToDirectCppTarget = isBridgingToDirectCppTarget
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get hasType(): boolean {
|
|
48
|
+
return this.type.kind !== 'void' && this.type.kind !== 'null'
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get canBePassedByReference(): boolean {
|
|
52
|
+
return this.type.canBePassedByReference
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get needsSpecialHandling(): boolean {
|
|
56
|
+
switch (this.type.kind) {
|
|
57
|
+
case 'enum':
|
|
58
|
+
// Enums cannot be referenced from C++ <-> Swift bi-directionally,
|
|
59
|
+
// so we just pass the underlying raw value (int32), and cast from Int <-> Enum.
|
|
60
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
61
|
+
// ...unless we bridge directly to a C++ target. Then we don't need special conversion.
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
return true
|
|
65
|
+
case 'hybrid-object':
|
|
66
|
+
// Swift HybridObjects need to be wrapped in our own *Cxx Swift classes.
|
|
67
|
+
// We wrap/unwrap them if needed.
|
|
68
|
+
return true
|
|
69
|
+
case 'optional':
|
|
70
|
+
// swift::Optional<T> <> std::optional<T>
|
|
71
|
+
return true
|
|
72
|
+
case 'string':
|
|
73
|
+
// swift::String <> std::string
|
|
74
|
+
return true
|
|
75
|
+
case 'array':
|
|
76
|
+
// swift::Array<T> <> std::vector<T>
|
|
77
|
+
return true
|
|
78
|
+
case 'record':
|
|
79
|
+
// Dictionary<K, V> <> std::unordered_map<K, V>
|
|
80
|
+
return true
|
|
81
|
+
case 'variant':
|
|
82
|
+
// Variant_A_B_C <> std::variant<A, B, C>
|
|
83
|
+
return true
|
|
84
|
+
case 'tuple':
|
|
85
|
+
// (A, B) <> std::tuple<A, B>
|
|
86
|
+
return true
|
|
87
|
+
case 'struct':
|
|
88
|
+
// SomeStruct (Swift extension) <> SomeStruct (C++)
|
|
89
|
+
return true
|
|
90
|
+
case 'function':
|
|
91
|
+
// (@ecaping () -> Void) <> std::function<...>
|
|
92
|
+
return true
|
|
93
|
+
case 'array-buffer':
|
|
94
|
+
// ArrayBufferHolder <> std::shared_ptr<ArrayBuffer>
|
|
95
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
return true
|
|
99
|
+
case 'date':
|
|
100
|
+
// Date <> double
|
|
101
|
+
return true
|
|
102
|
+
case 'promise':
|
|
103
|
+
// Promise<T> <> std::shared_ptr<Promise<T>>
|
|
104
|
+
return true
|
|
105
|
+
case 'error':
|
|
106
|
+
// Error <> std.exception_ptr
|
|
107
|
+
return true
|
|
108
|
+
case 'map':
|
|
109
|
+
// AnyMapHolder <> AnyMap
|
|
110
|
+
return true
|
|
111
|
+
case 'result-wrapper':
|
|
112
|
+
// Result<T> <> T
|
|
113
|
+
return true
|
|
114
|
+
default:
|
|
115
|
+
return false
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
getRequiredBridge(): SwiftCxxHelper | undefined {
|
|
120
|
+
// Since Swift doesn't support C++ templates, we need to create helper
|
|
121
|
+
// functions that create those types (specialized) for us.
|
|
122
|
+
return createSwiftCxxHelpers(this.type)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private getBridgeOrThrow(): SwiftCxxHelper {
|
|
126
|
+
const bridge = this.getRequiredBridge()
|
|
127
|
+
if (bridge == null)
|
|
128
|
+
throw new Error(
|
|
129
|
+
`Type ${this.type.kind} requires a bridged specialization!`
|
|
130
|
+
)
|
|
131
|
+
return bridge
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
getRequiredImports(language: Language): SourceImport[] {
|
|
135
|
+
const imports = this.type.getRequiredImports(language)
|
|
136
|
+
|
|
137
|
+
if (language === 'c++') {
|
|
138
|
+
if (this.type.kind === 'array-buffer') {
|
|
139
|
+
imports.push({
|
|
140
|
+
name: 'NitroModules/ArrayBufferHolder.hpp',
|
|
141
|
+
forwardDeclaration: getForwardDeclaration(
|
|
142
|
+
'class',
|
|
143
|
+
'ArrayBufferHolder',
|
|
144
|
+
'NitroModules'
|
|
145
|
+
),
|
|
146
|
+
language: 'c++',
|
|
147
|
+
space: 'system',
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Recursively look into referenced types (e.g. the `T` of a `optional<T>`, or `T` of a `T[]`)
|
|
153
|
+
const referencedTypes = getReferencedTypes(this.type)
|
|
154
|
+
referencedTypes.forEach((t) => {
|
|
155
|
+
if (t === this.type) {
|
|
156
|
+
// break a recursion - we already know this type
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
const bridged = new SwiftCxxBridgedType(t)
|
|
160
|
+
imports.push(...bridged.getRequiredImports(language))
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
return imports
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
getExtraFiles(): SourceFile[] {
|
|
167
|
+
const files: SourceFile[] = []
|
|
168
|
+
|
|
169
|
+
switch (this.type.kind) {
|
|
170
|
+
case 'struct': {
|
|
171
|
+
const struct = getTypeAs(this.type, StructType)
|
|
172
|
+
const extensionFile = createSwiftStructBridge(struct)
|
|
173
|
+
files.push(extensionFile)
|
|
174
|
+
extensionFile.referencedTypes.forEach((t) => {
|
|
175
|
+
const bridge = new SwiftCxxBridgedType(t)
|
|
176
|
+
files.push(...bridge.getExtraFiles())
|
|
177
|
+
})
|
|
178
|
+
break
|
|
179
|
+
}
|
|
180
|
+
case 'enum': {
|
|
181
|
+
const enumType = getTypeAs(this.type, EnumType)
|
|
182
|
+
const extensionFile = createSwiftEnumBridge(enumType)
|
|
183
|
+
files.push(extensionFile)
|
|
184
|
+
break
|
|
185
|
+
}
|
|
186
|
+
case 'function': {
|
|
187
|
+
const functionType = getTypeAs(this.type, FunctionType)
|
|
188
|
+
const extensionFile = createSwiftFunctionBridge(functionType)
|
|
189
|
+
files.push(extensionFile)
|
|
190
|
+
break
|
|
191
|
+
}
|
|
192
|
+
case 'promise': {
|
|
193
|
+
// Promise needs resolver and rejecter funcs in Swift
|
|
194
|
+
const promiseType = getTypeAs(this.type, PromiseType)
|
|
195
|
+
files.push(createSwiftFunctionBridge(promiseType.resolverFunction))
|
|
196
|
+
files.push(createSwiftFunctionBridge(promiseType.rejecterFunction))
|
|
197
|
+
break
|
|
198
|
+
}
|
|
199
|
+
case 'variant': {
|
|
200
|
+
const variant = getTypeAs(this.type, VariantType)
|
|
201
|
+
const file = createSwiftVariant(variant)
|
|
202
|
+
files.push(file)
|
|
203
|
+
break
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Recursively look into referenced types (e.g. the `T` of a `optional<T>`, or `T` of a `T[]`)
|
|
208
|
+
const referencedTypes = getReferencedTypes(this.type)
|
|
209
|
+
referencedTypes.forEach((t) => {
|
|
210
|
+
if (t === this.type) {
|
|
211
|
+
// break a recursion - we already know this type
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
const bridged = new SwiftCxxBridgedType(t)
|
|
215
|
+
files.push(...bridged.getExtraFiles())
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
return files
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
getTypeCode(language: 'swift' | 'c++'): string {
|
|
222
|
+
switch (this.type.kind) {
|
|
223
|
+
case 'enum':
|
|
224
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
225
|
+
return this.type.getCode('swift')
|
|
226
|
+
}
|
|
227
|
+
switch (language) {
|
|
228
|
+
case 'c++':
|
|
229
|
+
return 'int'
|
|
230
|
+
case 'swift':
|
|
231
|
+
return 'Int32'
|
|
232
|
+
default:
|
|
233
|
+
throw new Error(`Invalid language! ${language}`)
|
|
234
|
+
}
|
|
235
|
+
case 'map': {
|
|
236
|
+
switch (language) {
|
|
237
|
+
case 'swift':
|
|
238
|
+
return 'margelo.nitro.SharedAnyMap'
|
|
239
|
+
default:
|
|
240
|
+
return this.type.getCode(language)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
case 'hybrid-object':
|
|
244
|
+
case 'optional':
|
|
245
|
+
case 'array':
|
|
246
|
+
case 'function':
|
|
247
|
+
case 'variant':
|
|
248
|
+
case 'tuple':
|
|
249
|
+
case 'record':
|
|
250
|
+
case 'result-wrapper':
|
|
251
|
+
case 'promise': {
|
|
252
|
+
const bridge = this.getBridgeOrThrow()
|
|
253
|
+
switch (language) {
|
|
254
|
+
case 'swift':
|
|
255
|
+
return `bridge.${bridge.specializationName}`
|
|
256
|
+
case 'c++':
|
|
257
|
+
return bridge.cxxType
|
|
258
|
+
default:
|
|
259
|
+
return this.type.getCode(language)
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
case 'array-buffer':
|
|
263
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
264
|
+
return this.type.getCode(language)
|
|
265
|
+
} else {
|
|
266
|
+
switch (language) {
|
|
267
|
+
case 'swift':
|
|
268
|
+
return 'ArrayBuffer'
|
|
269
|
+
case 'c++':
|
|
270
|
+
return `ArrayBufferHolder`
|
|
271
|
+
default:
|
|
272
|
+
throw new Error(`Invalid language! ${language}`)
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
case 'string': {
|
|
276
|
+
switch (language) {
|
|
277
|
+
case 'c++':
|
|
278
|
+
return `std::string`
|
|
279
|
+
case 'swift':
|
|
280
|
+
return 'std.string'
|
|
281
|
+
default:
|
|
282
|
+
throw new Error(`Invalid language! ${language}`)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
case 'date':
|
|
286
|
+
switch (language) {
|
|
287
|
+
case 'swift':
|
|
288
|
+
return `margelo.nitro.chrono_time`
|
|
289
|
+
default:
|
|
290
|
+
return this.type.getCode(language)
|
|
291
|
+
}
|
|
292
|
+
case 'error':
|
|
293
|
+
switch (language) {
|
|
294
|
+
case 'c++':
|
|
295
|
+
return 'std::exception_ptr'
|
|
296
|
+
case 'swift':
|
|
297
|
+
return 'std.exception_ptr'
|
|
298
|
+
default:
|
|
299
|
+
throw new Error(`Invalid language! ${language}`)
|
|
300
|
+
}
|
|
301
|
+
default:
|
|
302
|
+
// No workaround - just return normal type
|
|
303
|
+
return this.type.getCode(language)
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
parse(
|
|
308
|
+
parameterName: string,
|
|
309
|
+
from: 'c++' | 'swift',
|
|
310
|
+
to: 'swift' | 'c++',
|
|
311
|
+
inLanguage: 'swift' | 'c++'
|
|
312
|
+
): string {
|
|
313
|
+
if (from === 'c++') {
|
|
314
|
+
return this.parseFromCppToSwift(parameterName, inLanguage)
|
|
315
|
+
} else if (from === 'swift') {
|
|
316
|
+
return this.parseFromSwiftToCpp(parameterName, inLanguage)
|
|
317
|
+
} else {
|
|
318
|
+
throw new Error(`Cannot parse from ${from} to ${to}!`)
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
parseFromCppToSwift(
|
|
323
|
+
cppParameterName: string,
|
|
324
|
+
language: 'swift' | 'c++'
|
|
325
|
+
): string {
|
|
326
|
+
switch (this.type.kind) {
|
|
327
|
+
case 'enum': {
|
|
328
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
329
|
+
return cppParameterName
|
|
330
|
+
}
|
|
331
|
+
const enumType = getTypeAs(this.type, EnumType)
|
|
332
|
+
switch (language) {
|
|
333
|
+
case 'c++':
|
|
334
|
+
return `static_cast<int>(${cppParameterName})`
|
|
335
|
+
case 'swift':
|
|
336
|
+
const fullName = NitroConfig.current.getCxxNamespace(
|
|
337
|
+
'swift',
|
|
338
|
+
enumType.enumName
|
|
339
|
+
)
|
|
340
|
+
return `${fullName}(rawValue: ${cppParameterName})!`
|
|
341
|
+
default:
|
|
342
|
+
throw new Error(`Invalid language! ${language}`)
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
case 'hybrid-object': {
|
|
346
|
+
const bridge = this.getBridgeOrThrow()
|
|
347
|
+
const getFunc = `bridge.get_${bridge.specializationName}`
|
|
348
|
+
const name = getTypeHybridObjectName(this.type)
|
|
349
|
+
switch (language) {
|
|
350
|
+
case 'swift':
|
|
351
|
+
return `
|
|
352
|
+
{ () -> ${name.HybridTSpec} in
|
|
353
|
+
let __unsafePointer = ${getFunc}(${cppParameterName})
|
|
354
|
+
let __instance = ${name.HybridTSpecCxx}.fromUnsafe(__unsafePointer)
|
|
355
|
+
return __instance.get${name.HybridTSpec}()
|
|
356
|
+
}()`.trim()
|
|
357
|
+
default:
|
|
358
|
+
return cppParameterName
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
case 'array-buffer': {
|
|
362
|
+
switch (language) {
|
|
363
|
+
case 'swift':
|
|
364
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
365
|
+
return `ArrayBuffer(${cppParameterName})`
|
|
366
|
+
} else {
|
|
367
|
+
return cppParameterName
|
|
368
|
+
}
|
|
369
|
+
case 'c++':
|
|
370
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
371
|
+
return cppParameterName
|
|
372
|
+
} else {
|
|
373
|
+
return `ArrayBufferHolder(${cppParameterName})`
|
|
374
|
+
}
|
|
375
|
+
default:
|
|
376
|
+
return cppParameterName
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
case 'promise': {
|
|
380
|
+
const promise = getTypeAs(this.type, PromiseType)
|
|
381
|
+
switch (language) {
|
|
382
|
+
case 'swift': {
|
|
383
|
+
const bridge = this.getBridgeOrThrow()
|
|
384
|
+
if (promise.resultingType.kind === 'void') {
|
|
385
|
+
// It's void - resolve()
|
|
386
|
+
const resolverFunc = new FunctionType(new VoidType(), [])
|
|
387
|
+
const rejecterFunc = new FunctionType(new VoidType(), [
|
|
388
|
+
new NamedWrappingType('error', new ErrorType()),
|
|
389
|
+
])
|
|
390
|
+
const resolverFuncBridge = new SwiftCxxBridgedType(resolverFunc)
|
|
391
|
+
const rejecterFuncBridge = new SwiftCxxBridgedType(rejecterFunc)
|
|
392
|
+
return `
|
|
393
|
+
{ () -> ${promise.getCode('swift')} in
|
|
394
|
+
let __promise = ${promise.getCode('swift')}()
|
|
395
|
+
let __resolver = { __promise.resolve(withResult: ()) }
|
|
396
|
+
let __rejecter = { (__error: Error) in
|
|
397
|
+
__promise.reject(withError: __error)
|
|
398
|
+
}
|
|
399
|
+
let __resolverCpp = ${indent(resolverFuncBridge.parseFromSwiftToCpp('__resolver', 'swift'), ' ')}
|
|
400
|
+
let __rejecterCpp = ${indent(rejecterFuncBridge.parseFromSwiftToCpp('__rejecter', 'swift'), ' ')}
|
|
401
|
+
let __promiseHolder = bridge.wrap_${bridge.specializationName}(${cppParameterName})
|
|
402
|
+
__promiseHolder.addOnResolvedListener(__resolverCpp)
|
|
403
|
+
__promiseHolder.addOnRejectedListener(__rejecterCpp)
|
|
404
|
+
return __promise
|
|
405
|
+
}()`.trim()
|
|
406
|
+
} else {
|
|
407
|
+
// It's resolving to a type - resolve(T)
|
|
408
|
+
const resolverFunc = new FunctionType(new VoidType(), [
|
|
409
|
+
new NamedWrappingType('result', promise.resultingType),
|
|
410
|
+
])
|
|
411
|
+
const rejecterFunc = new FunctionType(new VoidType(), [
|
|
412
|
+
new NamedWrappingType('error', new ErrorType()),
|
|
413
|
+
])
|
|
414
|
+
const resolverFuncBridge = new SwiftCxxBridgedType(resolverFunc)
|
|
415
|
+
const rejecterFuncBridge = new SwiftCxxBridgedType(rejecterFunc)
|
|
416
|
+
const resolverFuncName = promise.resultingType
|
|
417
|
+
.canBePassedByReference
|
|
418
|
+
? 'addOnResolvedListener'
|
|
419
|
+
: 'addOnResolvedListenerCopy'
|
|
420
|
+
return `
|
|
421
|
+
{ () -> ${promise.getCode('swift')} in
|
|
422
|
+
let __promise = ${promise.getCode('swift')}()
|
|
423
|
+
let __resolver = { (__result: ${promise.resultingType.getCode('swift')}) in
|
|
424
|
+
__promise.resolve(withResult: __result)
|
|
425
|
+
}
|
|
426
|
+
let __rejecter = { (__error: Error) in
|
|
427
|
+
__promise.reject(withError: __error)
|
|
428
|
+
}
|
|
429
|
+
let __resolverCpp = ${indent(resolverFuncBridge.parseFromSwiftToCpp('__resolver', 'swift'), ' ')}
|
|
430
|
+
let __rejecterCpp = ${indent(rejecterFuncBridge.parseFromSwiftToCpp('__rejecter', 'swift'), ' ')}
|
|
431
|
+
let __promiseHolder = bridge.wrap_${bridge.specializationName}(${cppParameterName})
|
|
432
|
+
__promiseHolder.${resolverFuncName}(__resolverCpp)
|
|
433
|
+
__promiseHolder.addOnRejectedListener(__rejecterCpp)
|
|
434
|
+
return __promise
|
|
435
|
+
}()`.trim()
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
default:
|
|
439
|
+
return cppParameterName
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
case 'optional': {
|
|
443
|
+
const optional = getTypeAs(this.type, OptionalType)
|
|
444
|
+
const wrapping = new SwiftCxxBridgedType(optional.wrappingType, true)
|
|
445
|
+
const bridge = this.getBridgeOrThrow()
|
|
446
|
+
switch (language) {
|
|
447
|
+
case 'swift':
|
|
448
|
+
if (wrapping.type.kind === 'enum') {
|
|
449
|
+
const enumType = getTypeAs(wrapping.type, EnumType)
|
|
450
|
+
if (enumType.jsType === 'enum') {
|
|
451
|
+
// TODO: Remove this hack once Swift fixes this shit.
|
|
452
|
+
// A JS enum is implemented as a number/int based enum.
|
|
453
|
+
// For some reason, those break in Swift. I have no idea why.
|
|
454
|
+
return `${cppParameterName}.has_value() ? ${cppParameterName}.pointee : nil`
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (!wrapping.needsSpecialHandling) {
|
|
458
|
+
return `${cppParameterName}.value`
|
|
459
|
+
}
|
|
460
|
+
return `
|
|
461
|
+
{ () -> ${optional.getCode('swift')} in
|
|
462
|
+
if bridge.has_value_${bridge.specializationName}(${cppParameterName}) {
|
|
463
|
+
let __unwrapped = bridge.get_${bridge.specializationName}(${cppParameterName})
|
|
464
|
+
return ${indent(wrapping.parseFromCppToSwift('__unwrapped', language), ' ')}
|
|
465
|
+
} else {
|
|
466
|
+
return nil
|
|
467
|
+
}
|
|
468
|
+
}()
|
|
469
|
+
`.trim()
|
|
470
|
+
default:
|
|
471
|
+
return cppParameterName
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
case 'string': {
|
|
475
|
+
switch (language) {
|
|
476
|
+
case 'swift':
|
|
477
|
+
return `String(${cppParameterName})`
|
|
478
|
+
default:
|
|
479
|
+
return cppParameterName
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
case 'date': {
|
|
483
|
+
switch (language) {
|
|
484
|
+
case 'swift':
|
|
485
|
+
return `Date(fromChrono: ${cppParameterName})`.trim()
|
|
486
|
+
default:
|
|
487
|
+
return cppParameterName
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
case 'array': {
|
|
491
|
+
const array = getTypeAs(this.type, ArrayType)
|
|
492
|
+
const wrapping = new SwiftCxxBridgedType(array.itemType, true)
|
|
493
|
+
switch (language) {
|
|
494
|
+
case 'swift':
|
|
495
|
+
return `${cppParameterName}.map({ __item in ${wrapping.parseFromCppToSwift('__item', 'swift')} })`.trim()
|
|
496
|
+
default:
|
|
497
|
+
return cppParameterName
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
case 'map': {
|
|
501
|
+
switch (language) {
|
|
502
|
+
case 'swift':
|
|
503
|
+
return `AnyMap(withCppPart: ${cppParameterName})`
|
|
504
|
+
default:
|
|
505
|
+
return cppParameterName
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
case 'record': {
|
|
509
|
+
const bridge = this.getBridgeOrThrow()
|
|
510
|
+
const getKeysFunc = `bridge.get_${bridge.specializationName}_keys`
|
|
511
|
+
const getValueFunc = `bridge.get_${bridge.specializationName}_value`
|
|
512
|
+
const record = getTypeAs(this.type, RecordType)
|
|
513
|
+
const wrappingKey = new SwiftCxxBridgedType(record.keyType, true)
|
|
514
|
+
const wrappingValue = new SwiftCxxBridgedType(record.valueType, true)
|
|
515
|
+
switch (language) {
|
|
516
|
+
case 'swift':
|
|
517
|
+
return `
|
|
518
|
+
{ () -> ${record.getCode('swift')} in
|
|
519
|
+
var __dictionary = ${record.getCode('swift')}(minimumCapacity: ${cppParameterName}.size())
|
|
520
|
+
let __keys = ${getKeysFunc}(${cppParameterName})
|
|
521
|
+
for __key in __keys {
|
|
522
|
+
let __value = ${getValueFunc}(${cppParameterName}, __key)
|
|
523
|
+
__dictionary[${indent(wrappingKey.parseFromCppToSwift('__key', 'swift'), ' ')}] = ${indent(wrappingValue.parseFromCppToSwift('__value', 'swift'), ' ')}
|
|
524
|
+
}
|
|
525
|
+
return __dictionary
|
|
526
|
+
}()`.trim()
|
|
527
|
+
default:
|
|
528
|
+
return cppParameterName
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
case 'tuple': {
|
|
532
|
+
switch (language) {
|
|
533
|
+
case 'swift':
|
|
534
|
+
return `${cppParameterName}.arg0`
|
|
535
|
+
default:
|
|
536
|
+
return cppParameterName
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
case 'variant': {
|
|
540
|
+
const bridge = this.getBridgeOrThrow()
|
|
541
|
+
const variant = getTypeAs(this.type, VariantType)
|
|
542
|
+
const valueInitialization = this.isBridgingToDirectCppTarget
|
|
543
|
+
? `bridge.${bridge.specializationName}(${cppParameterName})`
|
|
544
|
+
: cppParameterName
|
|
545
|
+
const cases = variant.cases
|
|
546
|
+
.map(([label, v], i) => {
|
|
547
|
+
const wrapping = new SwiftCxxBridgedType(v, true)
|
|
548
|
+
const parse = wrapping.parseFromCppToSwift('__actual', 'swift')
|
|
549
|
+
return `
|
|
550
|
+
case ${i}:
|
|
551
|
+
let __actual = __variant.get_${i}()
|
|
552
|
+
return .${label}(${indent(parse, ' ')})`.trim()
|
|
553
|
+
})
|
|
554
|
+
.join('\n')
|
|
555
|
+
switch (language) {
|
|
556
|
+
case 'swift':
|
|
557
|
+
return `
|
|
558
|
+
{ () -> ${variant.getCode('swift')} in
|
|
559
|
+
let __variant = ${valueInitialization}
|
|
560
|
+
switch __variant.index() {
|
|
561
|
+
${indent(cases, ' ')}
|
|
562
|
+
default:
|
|
563
|
+
fatalError("Variant can never have index \\(__variant.index())!")
|
|
564
|
+
}
|
|
565
|
+
}()`.trim()
|
|
566
|
+
default:
|
|
567
|
+
return cppParameterName
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
case 'function': {
|
|
571
|
+
const funcType = getTypeAs(this.type, FunctionType)
|
|
572
|
+
switch (language) {
|
|
573
|
+
case 'swift':
|
|
574
|
+
const swiftClosureType = funcType.getCode('swift', {
|
|
575
|
+
includeNameInfo: false,
|
|
576
|
+
})
|
|
577
|
+
const bridge = this.getBridgeOrThrow()
|
|
578
|
+
const paramsSignature = funcType.parameters.map(
|
|
579
|
+
(p) => `__${p.escapedName}: ${p.getCode('swift')}`
|
|
580
|
+
)
|
|
581
|
+
const returnType = funcType.returnType.getCode('swift')
|
|
582
|
+
const signature = `(${paramsSignature.join(', ')}) -> ${returnType}`
|
|
583
|
+
const paramsForward = funcType.parameters.map((p) => {
|
|
584
|
+
const bridged = new SwiftCxxBridgedType(p)
|
|
585
|
+
return bridged.parseFromSwiftToCpp(`__${p.escapedName}`, 'swift')
|
|
586
|
+
})
|
|
587
|
+
|
|
588
|
+
if (funcType.returnType.kind === 'void') {
|
|
589
|
+
return `
|
|
590
|
+
{ () -> ${swiftClosureType} in
|
|
591
|
+
let __wrappedFunction = bridge.wrap_${bridge.specializationName}(${cppParameterName})
|
|
592
|
+
return { ${signature} in
|
|
593
|
+
__wrappedFunction.call(${indent(paramsForward.join(', '), ' ')})
|
|
594
|
+
}
|
|
595
|
+
}()`.trim()
|
|
596
|
+
} else {
|
|
597
|
+
const resultBridged = new SwiftCxxBridgedType(
|
|
598
|
+
funcType.returnType,
|
|
599
|
+
true
|
|
600
|
+
)
|
|
601
|
+
return `
|
|
602
|
+
{ () -> ${swiftClosureType} in
|
|
603
|
+
let __wrappedFunction = bridge.wrap_${bridge.specializationName}(${cppParameterName})
|
|
604
|
+
return { ${signature} in
|
|
605
|
+
let __result = __wrappedFunction.call(${indent(paramsForward.join(', '), ' ')})
|
|
606
|
+
return ${indent(resultBridged.parseFromCppToSwift('__result', 'swift'), ' ')}
|
|
607
|
+
}
|
|
608
|
+
}()`.trim()
|
|
609
|
+
}
|
|
610
|
+
default:
|
|
611
|
+
return cppParameterName
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
case 'error':
|
|
615
|
+
switch (language) {
|
|
616
|
+
case 'swift':
|
|
617
|
+
return `RuntimeError.from(cppError: ${cppParameterName})`
|
|
618
|
+
default:
|
|
619
|
+
return cppParameterName
|
|
620
|
+
}
|
|
621
|
+
case 'void':
|
|
622
|
+
// When type is void, don't return anything
|
|
623
|
+
return ''
|
|
624
|
+
default:
|
|
625
|
+
// No workaround - we can just use the value we get from C++
|
|
626
|
+
return cppParameterName
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
parseFromSwiftToCpp(
|
|
631
|
+
swiftParameterName: string,
|
|
632
|
+
language: 'swift' | 'c++'
|
|
633
|
+
): string {
|
|
634
|
+
switch (this.type.kind) {
|
|
635
|
+
case 'enum':
|
|
636
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
637
|
+
return swiftParameterName
|
|
638
|
+
}
|
|
639
|
+
// TODO: Remove the int casting once https://github.com/swiftlang/swift/issues/75330 is fixed.
|
|
640
|
+
switch (language) {
|
|
641
|
+
case 'c++':
|
|
642
|
+
return `static_cast<${this.type.getCode('c++')}>(${swiftParameterName})`
|
|
643
|
+
case 'swift':
|
|
644
|
+
return `${swiftParameterName}.rawValue`
|
|
645
|
+
default:
|
|
646
|
+
throw new Error(`Invalid language! ${language}`)
|
|
647
|
+
}
|
|
648
|
+
case 'hybrid-object': {
|
|
649
|
+
const bridge = this.getBridgeOrThrow()
|
|
650
|
+
switch (language) {
|
|
651
|
+
case 'swift':
|
|
652
|
+
return `
|
|
653
|
+
{ () -> bridge.${bridge.specializationName} in
|
|
654
|
+
let __cxxWrapped = ${swiftParameterName}.getCxxWrapper()
|
|
655
|
+
return __cxxWrapped.getCxxPart()
|
|
656
|
+
}()`.trim()
|
|
657
|
+
default:
|
|
658
|
+
return swiftParameterName
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
case 'optional': {
|
|
662
|
+
const optional = getTypeAs(this.type, OptionalType)
|
|
663
|
+
const wrapping = new SwiftCxxBridgedType(optional.wrappingType, true)
|
|
664
|
+
const bridge = this.getBridgeOrThrow()
|
|
665
|
+
const makeFunc = `bridge.${bridge.funcName}`
|
|
666
|
+
switch (language) {
|
|
667
|
+
case 'swift':
|
|
668
|
+
return `
|
|
669
|
+
{ () -> bridge.${bridge.specializationName} in
|
|
670
|
+
if let __unwrappedValue = ${swiftParameterName} {
|
|
671
|
+
return ${makeFunc}(${indent(wrapping.parseFromSwiftToCpp('__unwrappedValue', language), ' ')})
|
|
672
|
+
} else {
|
|
673
|
+
return .init()
|
|
674
|
+
}
|
|
675
|
+
}()
|
|
676
|
+
`.trim()
|
|
677
|
+
default:
|
|
678
|
+
return swiftParameterName
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
case 'string': {
|
|
682
|
+
switch (language) {
|
|
683
|
+
case 'swift':
|
|
684
|
+
return `std.string(${swiftParameterName})`
|
|
685
|
+
default:
|
|
686
|
+
return swiftParameterName
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
case 'date': {
|
|
690
|
+
switch (language) {
|
|
691
|
+
case 'swift':
|
|
692
|
+
return `${swiftParameterName}.toCpp()`
|
|
693
|
+
default:
|
|
694
|
+
return swiftParameterName
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
case 'array-buffer': {
|
|
698
|
+
switch (language) {
|
|
699
|
+
case 'swift':
|
|
700
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
701
|
+
return `${swiftParameterName}.getArrayBuffer()`
|
|
702
|
+
} else {
|
|
703
|
+
return swiftParameterName
|
|
704
|
+
}
|
|
705
|
+
case 'c++':
|
|
706
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
707
|
+
return swiftParameterName
|
|
708
|
+
} else {
|
|
709
|
+
return `${swiftParameterName}.getArrayBuffer()`
|
|
710
|
+
}
|
|
711
|
+
default:
|
|
712
|
+
return swiftParameterName
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
case 'map': {
|
|
716
|
+
switch (language) {
|
|
717
|
+
case 'swift':
|
|
718
|
+
return `${swiftParameterName}.cppPart`
|
|
719
|
+
default:
|
|
720
|
+
return swiftParameterName
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
case 'promise': {
|
|
724
|
+
const bridge = this.getBridgeOrThrow()
|
|
725
|
+
const makePromise = `bridge.${bridge.funcName}`
|
|
726
|
+
const promise = getTypeAs(this.type, PromiseType)
|
|
727
|
+
const resolvingType = new SwiftCxxBridgedType(
|
|
728
|
+
promise.resultingType,
|
|
729
|
+
true
|
|
730
|
+
)
|
|
731
|
+
switch (language) {
|
|
732
|
+
case 'swift':
|
|
733
|
+
const arg =
|
|
734
|
+
promise.resultingType.kind === 'void'
|
|
735
|
+
? ''
|
|
736
|
+
: resolvingType.parseFromSwiftToCpp('__result', 'swift')
|
|
737
|
+
return `
|
|
738
|
+
{ () -> bridge.${bridge.specializationName} in
|
|
739
|
+
let __promise = ${makePromise}()
|
|
740
|
+
let __promiseHolder = bridge.wrap_${bridge.specializationName}(__promise)
|
|
741
|
+
${swiftParameterName}
|
|
742
|
+
.then({ __result in __promiseHolder.resolve(${indent(arg, ' ')}) })
|
|
743
|
+
.catch({ __error in __promiseHolder.reject(__error.toCpp()) })
|
|
744
|
+
return __promise
|
|
745
|
+
}()`.trim()
|
|
746
|
+
default:
|
|
747
|
+
return swiftParameterName
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
case 'array': {
|
|
751
|
+
const bridge = this.getBridgeOrThrow()
|
|
752
|
+
const makeFunc = `bridge.${bridge.funcName}`
|
|
753
|
+
const array = getTypeAs(this.type, ArrayType)
|
|
754
|
+
const wrapping = new SwiftCxxBridgedType(array.itemType, true)
|
|
755
|
+
switch (language) {
|
|
756
|
+
case 'swift':
|
|
757
|
+
return `
|
|
758
|
+
{ () -> bridge.${bridge.specializationName} in
|
|
759
|
+
var __vector = ${makeFunc}(${swiftParameterName}.count)
|
|
760
|
+
for __item in ${swiftParameterName} {
|
|
761
|
+
__vector.push_back(${indent(wrapping.parseFromSwiftToCpp('__item', language), ' ')})
|
|
762
|
+
}
|
|
763
|
+
return __vector
|
|
764
|
+
}()`.trim()
|
|
765
|
+
default:
|
|
766
|
+
return swiftParameterName
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
case 'tuple': {
|
|
770
|
+
const tuple = getTypeAs(this.type, TupleType)
|
|
771
|
+
const bridge = this.getBridgeOrThrow()
|
|
772
|
+
const makeFunc = NitroConfig.current.getCxxNamespace(
|
|
773
|
+
language,
|
|
774
|
+
bridge.funcName
|
|
775
|
+
)
|
|
776
|
+
switch (language) {
|
|
777
|
+
case 'swift':
|
|
778
|
+
const typesForward = tuple.itemTypes
|
|
779
|
+
.map((t, i) => {
|
|
780
|
+
const bridged = new SwiftCxxBridgedType(t)
|
|
781
|
+
return `${bridged.parseFromSwiftToCpp(`${swiftParameterName}.${i}`, language)}`
|
|
782
|
+
})
|
|
783
|
+
.join(', ')
|
|
784
|
+
return `${makeFunc}(${typesForward})`
|
|
785
|
+
default:
|
|
786
|
+
return swiftParameterName
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
case 'variant': {
|
|
790
|
+
const bridge = this.getBridgeOrThrow()
|
|
791
|
+
const variant = getTypeAs(this.type, VariantType)
|
|
792
|
+
switch (language) {
|
|
793
|
+
case 'swift':
|
|
794
|
+
const cases = variant.cases
|
|
795
|
+
.map(([label, v]) => {
|
|
796
|
+
const wrapping = new SwiftCxxBridgedType(v, true)
|
|
797
|
+
const parse = wrapping.parseFromSwiftToCpp('__value', 'swift')
|
|
798
|
+
return `
|
|
799
|
+
case .${label}(let __value):
|
|
800
|
+
return bridge.${bridge.funcName}(${indent(parse, ' ')})`.trim()
|
|
801
|
+
})
|
|
802
|
+
.join('\n')
|
|
803
|
+
let code = `
|
|
804
|
+
{ () -> bridge.${bridge.specializationName} in
|
|
805
|
+
switch ${swiftParameterName} {
|
|
806
|
+
${indent(cases, ' ')}
|
|
807
|
+
}
|
|
808
|
+
}()`.trim()
|
|
809
|
+
if (this.isBridgingToDirectCppTarget) {
|
|
810
|
+
// If we bridge directly to a C++ variant, we need to return the .variant of our wrapper type.
|
|
811
|
+
code += `.variant`
|
|
812
|
+
}
|
|
813
|
+
return code
|
|
814
|
+
default:
|
|
815
|
+
return swiftParameterName
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
case 'record': {
|
|
819
|
+
const bridge = this.getBridgeOrThrow()
|
|
820
|
+
const createMap = `bridge.${bridge.funcName}`
|
|
821
|
+
const record = getTypeAs(this.type, RecordType)
|
|
822
|
+
const wrappingKey = new SwiftCxxBridgedType(record.keyType, true)
|
|
823
|
+
const wrappingValue = new SwiftCxxBridgedType(record.valueType, true)
|
|
824
|
+
switch (language) {
|
|
825
|
+
case 'swift':
|
|
826
|
+
return `
|
|
827
|
+
{ () -> bridge.${bridge.specializationName} in
|
|
828
|
+
var __map = ${createMap}(${swiftParameterName}.count)
|
|
829
|
+
for (__k, __v) in ${swiftParameterName} {
|
|
830
|
+
bridge.emplace_${bridge.specializationName}(&__map, ${indent(wrappingKey.parseFromSwiftToCpp('__k', 'swift'), ' ')}, ${indent(wrappingValue.parseFromSwiftToCpp('__v', 'swift'), ' ')})
|
|
831
|
+
}
|
|
832
|
+
return __map
|
|
833
|
+
}()`.trim()
|
|
834
|
+
default:
|
|
835
|
+
return swiftParameterName
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
case 'function': {
|
|
839
|
+
switch (language) {
|
|
840
|
+
case 'swift': {
|
|
841
|
+
const bridge = this.getBridgeOrThrow()
|
|
842
|
+
const createFunc = `bridge.${bridge.funcName}`
|
|
843
|
+
return `
|
|
844
|
+
{ () -> bridge.${bridge.specializationName} in
|
|
845
|
+
let __closureWrapper = ${bridge.specializationName}(${swiftParameterName})
|
|
846
|
+
return ${createFunc}(__closureWrapper.toUnsafe())
|
|
847
|
+
}()
|
|
848
|
+
`.trim()
|
|
849
|
+
}
|
|
850
|
+
default:
|
|
851
|
+
return swiftParameterName
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
case 'error':
|
|
855
|
+
switch (language) {
|
|
856
|
+
case 'swift':
|
|
857
|
+
return `${swiftParameterName}.toCpp()`
|
|
858
|
+
default:
|
|
859
|
+
return swiftParameterName
|
|
860
|
+
}
|
|
861
|
+
case 'void':
|
|
862
|
+
// When type is void, don't return anything
|
|
863
|
+
return ''
|
|
864
|
+
default:
|
|
865
|
+
// No workaround - we can just use the value we get from C++
|
|
866
|
+
return swiftParameterName
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
function getTypeHybridObjectName(type: Type): HybridObjectName {
|
|
872
|
+
const hybridObject = getTypeAs(type, HybridObjectType)
|
|
873
|
+
return getHybridObjectName(hybridObject.hybridObjectName)
|
|
874
|
+
}
|