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,65 @@
|
|
|
1
|
+
import { NitroConfig } from '../../config/NitroConfig.js'
|
|
2
|
+
import { indent, toLowerCamelCase } from '../../utils.js'
|
|
3
|
+
import { createFileMetadataString } from '../helpers.js'
|
|
4
|
+
import type { SourceFile } from '../SourceFile.js'
|
|
5
|
+
import type { EnumType } from '../types/EnumType.js'
|
|
6
|
+
|
|
7
|
+
export function createSwiftEnumBridge(enumType: EnumType): SourceFile {
|
|
8
|
+
const fullName = NitroConfig.current.getCxxNamespace(
|
|
9
|
+
'swift',
|
|
10
|
+
enumType.enumName
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
const initializeCases = enumType.enumMembers.map((m) =>
|
|
14
|
+
`
|
|
15
|
+
case "${m.stringValue}":
|
|
16
|
+
self = .${toLowerCamelCase(m.name)}
|
|
17
|
+
`.trim()
|
|
18
|
+
)
|
|
19
|
+
const toStringCases = enumType.enumMembers.map((m) =>
|
|
20
|
+
`
|
|
21
|
+
case .${toLowerCamelCase(m.name)}:
|
|
22
|
+
return "${m.stringValue}"
|
|
23
|
+
`.trim()
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
const code = `
|
|
27
|
+
${createFileMetadataString(`${enumType.enumName}.swift`)}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Represents the JS ${enumType.jsType} \`${enumType.enumName}\`, backed by a C++ enum.
|
|
31
|
+
*/
|
|
32
|
+
public typealias ${enumType.enumName} = ${fullName}
|
|
33
|
+
|
|
34
|
+
public extension ${enumType.enumName} {
|
|
35
|
+
/**
|
|
36
|
+
* Get a ${enumType.enumName} for the given String value, or
|
|
37
|
+
* return \`nil\` if the given value was invalid/unknown.
|
|
38
|
+
*/
|
|
39
|
+
init?(fromString string: String) {
|
|
40
|
+
switch string {
|
|
41
|
+
${indent(initializeCases.join('\n'), ' ')}
|
|
42
|
+
default:
|
|
43
|
+
return nil
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get the String value this ${enumType.enumName} represents.
|
|
49
|
+
*/
|
|
50
|
+
var stringValue: String {
|
|
51
|
+
switch self {
|
|
52
|
+
${indent(toStringCases.join('\n'), ' ')}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
`.trim()
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
content: code,
|
|
60
|
+
language: 'swift',
|
|
61
|
+
name: `${enumType.enumName}.swift`,
|
|
62
|
+
platform: 'ios',
|
|
63
|
+
subdirectory: [],
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { NitroConfig } from '../../config/NitroConfig.js'
|
|
2
|
+
import { indent } from '../../utils.js'
|
|
3
|
+
import { createFileMetadataString } from '../helpers.js'
|
|
4
|
+
import type { SourceFile } from '../SourceFile.js'
|
|
5
|
+
import type { FunctionType } from '../types/FunctionType.js'
|
|
6
|
+
import { SwiftCxxBridgedType } from './SwiftCxxBridgedType.js'
|
|
7
|
+
|
|
8
|
+
export function createSwiftFunctionBridge(
|
|
9
|
+
functionType: FunctionType
|
|
10
|
+
): SourceFile {
|
|
11
|
+
const swiftClassName = functionType.specializationName
|
|
12
|
+
const bridgeNamespace = NitroConfig.current.getSwiftBridgeNamespace('swift')
|
|
13
|
+
const argsTypes = functionType.parameters.map((p) => {
|
|
14
|
+
const bridged = new SwiftCxxBridgedType(p)
|
|
15
|
+
return `${p.escapedName}: ${bridged.getTypeCode('swift')}`
|
|
16
|
+
})
|
|
17
|
+
const returnType = new SwiftCxxBridgedType(functionType.returnType)
|
|
18
|
+
const argsForward = functionType.parameters.map((p) => {
|
|
19
|
+
const bridged = new SwiftCxxBridgedType(p)
|
|
20
|
+
return bridged.parseFromCppToSwift(p.escapedName, 'swift')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
let body: string
|
|
24
|
+
if (functionType.returnType.kind === 'void') {
|
|
25
|
+
body = `
|
|
26
|
+
self.closure(${argsForward.join(', ')})
|
|
27
|
+
`.trim()
|
|
28
|
+
} else {
|
|
29
|
+
body = `
|
|
30
|
+
let __result: ${functionType.returnType.getCode('swift')} = self.closure(${argsForward.join(', ')})
|
|
31
|
+
return ${returnType.parseFromSwiftToCpp('__result', 'swift')}
|
|
32
|
+
`.trim()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const extraImports = functionType
|
|
36
|
+
.getRequiredImports('swift')
|
|
37
|
+
.map((i) => `import ${i.name}`)
|
|
38
|
+
|
|
39
|
+
const code = `
|
|
40
|
+
${createFileMetadataString(`${swiftClassName}.swift`)}
|
|
41
|
+
|
|
42
|
+
import NitroModules
|
|
43
|
+
${extraImports.join('\n')}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Wraps a Swift \`${functionType.getCode('swift')}\` as a class.
|
|
47
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a \`std::function\`.
|
|
48
|
+
*/
|
|
49
|
+
public final class ${swiftClassName} {
|
|
50
|
+
public typealias bridge = ${bridgeNamespace}
|
|
51
|
+
|
|
52
|
+
private let closure: ${functionType.getCode('swift')}
|
|
53
|
+
|
|
54
|
+
public init(_ closure: @escaping ${functionType.getCode('swift')}) {
|
|
55
|
+
self.closure = closure
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@inline(__always)
|
|
59
|
+
public func call(${argsTypes.join(', ')}) -> ${returnType.getTypeCode('swift')} {
|
|
60
|
+
${indent(body, ' ')}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
65
|
+
* This acquires one additional strong reference on the object!
|
|
66
|
+
*/
|
|
67
|
+
@inline(__always)
|
|
68
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
69
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Casts an unsafe pointer to a \`${swiftClassName}\`.
|
|
74
|
+
* The pointer has to be a retained opaque \`Unmanaged<${swiftClassName}>\`.
|
|
75
|
+
* This removes one strong reference from the object!
|
|
76
|
+
*/
|
|
77
|
+
@inline(__always)
|
|
78
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> ${swiftClassName} {
|
|
79
|
+
return Unmanaged<${swiftClassName}>.fromOpaque(pointer).takeRetainedValue()
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
`.trim()
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
content: code,
|
|
86
|
+
language: 'swift',
|
|
87
|
+
name: `${swiftClassName}.swift`,
|
|
88
|
+
platform: 'ios',
|
|
89
|
+
subdirectory: [],
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { indent } from '../../utils.js'
|
|
2
|
+
import { createSwiftHybridViewManager } from '../../views/swift/SwiftHybridViewManager.js'
|
|
3
|
+
import { getHybridObjectName } from '../getHybridObjectName.js'
|
|
4
|
+
import { createFileMetadataString, isNotDuplicate } from '../helpers.js'
|
|
5
|
+
import type { HybridObjectSpec } from '../HybridObjectSpec.js'
|
|
6
|
+
import type { SourceFile } from '../SourceFile.js'
|
|
7
|
+
import { HybridObjectType } from '../types/HybridObjectType.js'
|
|
8
|
+
import { createSwiftHybridObjectCxxBridge } from './SwiftHybridObjectBridge.js'
|
|
9
|
+
|
|
10
|
+
export function createSwiftHybridObject(spec: HybridObjectSpec): SourceFile[] {
|
|
11
|
+
const name = getHybridObjectName(spec.name)
|
|
12
|
+
const protocolName = name.HybridTSpec
|
|
13
|
+
const properties = spec.properties.map((p) => p.getCode('swift')).join('\n')
|
|
14
|
+
const methods = spec.methods.map((p) => p.getCode('swift')).join('\n')
|
|
15
|
+
const extraImports = [
|
|
16
|
+
...spec.properties.flatMap((p) => p.getRequiredImports('swift')),
|
|
17
|
+
...spec.methods.flatMap((m) => m.getRequiredImports('swift')),
|
|
18
|
+
...spec.baseTypes.flatMap((b) =>
|
|
19
|
+
new HybridObjectType(b).getRequiredImports('swift')
|
|
20
|
+
),
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
const protocolBaseClasses = ['HybridObject']
|
|
24
|
+
const classBaseClasses: string[] = []
|
|
25
|
+
if (spec.baseTypes.length > 0) {
|
|
26
|
+
if (spec.baseTypes.length > 1) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`${name.T}: Inheriting from multiple HybridObject bases is not yet supported in Swift!`
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
const base = spec.baseTypes[0]!
|
|
32
|
+
const baseName = getHybridObjectName(base.name)
|
|
33
|
+
protocolBaseClasses.push(`${baseName.HybridTSpec}_protocol`)
|
|
34
|
+
classBaseClasses.push(`${baseName.HybridTSpec}_base`)
|
|
35
|
+
}
|
|
36
|
+
if (spec.isHybridView) {
|
|
37
|
+
protocolBaseClasses.push('HybridView')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const hasBaseClass = classBaseClasses.length > 0
|
|
41
|
+
const baseMembers: string[] = []
|
|
42
|
+
baseMembers.push(`private weak var cxxWrapper: ${name.HybridTSpecCxx}? = nil`)
|
|
43
|
+
if (hasBaseClass) {
|
|
44
|
+
baseMembers.push(`public override init() { super.init() }`)
|
|
45
|
+
} else {
|
|
46
|
+
baseMembers.push(`public init() { }`)
|
|
47
|
+
}
|
|
48
|
+
baseMembers.push(
|
|
49
|
+
`
|
|
50
|
+
public ${hasBaseClass ? 'override func' : 'func'} getCxxWrapper() -> ${name.HybridTSpecCxx} {
|
|
51
|
+
#if DEBUG
|
|
52
|
+
guard self is ${name.HybridTSpec} else {
|
|
53
|
+
fatalError("\`self\` is not a \`${name.HybridTSpec}\`! Did you accidentally inherit from \`${name.HybridTSpec}_base\` instead of \`${name.HybridTSpec}\`?")
|
|
54
|
+
}
|
|
55
|
+
#endif
|
|
56
|
+
if let cxxWrapper = self.cxxWrapper {
|
|
57
|
+
return cxxWrapper
|
|
58
|
+
} else {
|
|
59
|
+
let cxxWrapper = ${name.HybridTSpecCxx}(self as! ${name.HybridTSpec})
|
|
60
|
+
self.cxxWrapper = cxxWrapper
|
|
61
|
+
return cxxWrapper
|
|
62
|
+
}
|
|
63
|
+
}`.trim()
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
const imports = ['import NitroModules']
|
|
67
|
+
imports.push(
|
|
68
|
+
...extraImports.map((i) => `import ${i.name}`).filter(isNotDuplicate)
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
const protocolCode = `
|
|
72
|
+
${createFileMetadataString(`${protocolName}.swift`)}
|
|
73
|
+
|
|
74
|
+
import Foundation
|
|
75
|
+
${imports.join('\n')}
|
|
76
|
+
|
|
77
|
+
/// See \`\`${protocolName}\`\`
|
|
78
|
+
public protocol ${protocolName}_protocol: ${protocolBaseClasses.join(', ')} {
|
|
79
|
+
// Properties
|
|
80
|
+
${indent(properties, ' ')}
|
|
81
|
+
|
|
82
|
+
// Methods
|
|
83
|
+
${indent(methods, ' ')}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/// See \`\`${protocolName}\`\`
|
|
87
|
+
open class ${protocolName}_base${classBaseClasses.length > 0 ? `: ${classBaseClasses.join(',')}` : ''} {
|
|
88
|
+
${indent(baseMembers.join('\n'), ' ')}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A Swift base-protocol representing the ${spec.name} HybridObject.
|
|
93
|
+
* Implement this protocol to create Swift-based instances of ${spec.name}.
|
|
94
|
+
* \`\`\`swift
|
|
95
|
+
* class ${name.HybridT} : ${protocolName} {
|
|
96
|
+
* // ...
|
|
97
|
+
* }
|
|
98
|
+
* \`\`\`
|
|
99
|
+
*/
|
|
100
|
+
public typealias ${protocolName} = ${protocolName}_protocol & ${protocolName}_base
|
|
101
|
+
`
|
|
102
|
+
|
|
103
|
+
const swiftBridge = createSwiftHybridObjectCxxBridge(spec)
|
|
104
|
+
|
|
105
|
+
const files: SourceFile[] = []
|
|
106
|
+
files.push({
|
|
107
|
+
content: protocolCode,
|
|
108
|
+
language: 'swift',
|
|
109
|
+
name: `${protocolName}.swift`,
|
|
110
|
+
subdirectory: [],
|
|
111
|
+
platform: 'ios',
|
|
112
|
+
})
|
|
113
|
+
files.push(...swiftBridge)
|
|
114
|
+
|
|
115
|
+
if (spec.isHybridView) {
|
|
116
|
+
const viewFiles = createSwiftHybridViewManager(spec)
|
|
117
|
+
files.push(...viewFiles)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return files
|
|
121
|
+
}
|