nitrogen 0.2.24 → 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 -347
- 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,83 @@
|
|
|
1
|
+
import { NitroConfig } from '../../config/NitroConfig.js'
|
|
2
|
+
import type { Language } from '../../getPlatformSpecs.js'
|
|
3
|
+
import { createCppStruct } from '../c++/CppStruct.js'
|
|
4
|
+
import { getForwardDeclaration } from '../c++/getForwardDeclaration.js'
|
|
5
|
+
import {
|
|
6
|
+
type FileWithReferencedTypes,
|
|
7
|
+
type SourceFile,
|
|
8
|
+
type SourceImport,
|
|
9
|
+
} from '../SourceFile.js'
|
|
10
|
+
import type { GetCodeOptions, NamedType, Type, TypeKind } from './Type.js'
|
|
11
|
+
|
|
12
|
+
export class StructType implements Type {
|
|
13
|
+
readonly structName: string
|
|
14
|
+
readonly properties: NamedType[]
|
|
15
|
+
readonly declarationFile: FileWithReferencedTypes
|
|
16
|
+
|
|
17
|
+
constructor(structName: string, properties: NamedType[]) {
|
|
18
|
+
this.structName = structName
|
|
19
|
+
this.properties = properties
|
|
20
|
+
this.declarationFile = createCppStruct(structName, properties)
|
|
21
|
+
|
|
22
|
+
if (this.structName.startsWith('__')) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`Struct name cannot start with two underscores (__) as this is reserved syntax for Nitrogen! (In ${this.structName})`
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
if (this.properties.length === 0) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`Empty structs are not supported in Nitrogen! Add at least one property to ${this.structName}.`
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get canBePassedByReference(): boolean {
|
|
35
|
+
// It's a struct, heavy to copy.
|
|
36
|
+
return true
|
|
37
|
+
}
|
|
38
|
+
get kind(): TypeKind {
|
|
39
|
+
return 'struct'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getCode(language: Language, { fullyQualified }: GetCodeOptions = {}): string {
|
|
43
|
+
switch (language) {
|
|
44
|
+
case 'c++':
|
|
45
|
+
if (fullyQualified) {
|
|
46
|
+
return NitroConfig.current.getCxxNamespace('c++', this.structName)
|
|
47
|
+
} else {
|
|
48
|
+
return this.structName
|
|
49
|
+
}
|
|
50
|
+
case 'swift':
|
|
51
|
+
return this.structName
|
|
52
|
+
case 'kotlin':
|
|
53
|
+
return this.structName
|
|
54
|
+
default:
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Language ${language} is not yet supported for StructType!`
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
getExtraFiles(): SourceFile[] {
|
|
61
|
+
const referencedTypes = this.declarationFile.referencedTypes.flatMap((r) =>
|
|
62
|
+
r.getExtraFiles()
|
|
63
|
+
)
|
|
64
|
+
return [this.declarationFile, ...referencedTypes]
|
|
65
|
+
}
|
|
66
|
+
getRequiredImports(language: Language): SourceImport[] {
|
|
67
|
+
const imports: SourceImport[] = []
|
|
68
|
+
if (language === 'c++') {
|
|
69
|
+
const cxxNamespace = NitroConfig.current.getCxxNamespace('c++')
|
|
70
|
+
imports.push({
|
|
71
|
+
name: this.declarationFile.name,
|
|
72
|
+
language: this.declarationFile.language,
|
|
73
|
+
forwardDeclaration: getForwardDeclaration(
|
|
74
|
+
'struct',
|
|
75
|
+
this.structName,
|
|
76
|
+
cxxNamespace
|
|
77
|
+
),
|
|
78
|
+
space: 'user',
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
return imports
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Language } from '../../getPlatformSpecs.js'
|
|
2
|
+
import { type SourceFile, type SourceImport } from '../SourceFile.js'
|
|
3
|
+
import type { GetCodeOptions, Type, TypeKind } from './Type.js'
|
|
4
|
+
|
|
5
|
+
export class TupleType implements Type {
|
|
6
|
+
readonly itemTypes: Type[]
|
|
7
|
+
|
|
8
|
+
constructor(itemTypes: Type[]) {
|
|
9
|
+
this.itemTypes = itemTypes
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get canBePassedByReference(): boolean {
|
|
13
|
+
// It's a tuple<..> - heavy to copy
|
|
14
|
+
return true
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get kind(): TypeKind {
|
|
18
|
+
return 'tuple'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getCode(language: Language, options?: GetCodeOptions): string {
|
|
22
|
+
const types = this.itemTypes.map((t) => t.getCode(language, options))
|
|
23
|
+
|
|
24
|
+
switch (language) {
|
|
25
|
+
case 'c++':
|
|
26
|
+
return `std::tuple<${types.join(', ')}>`
|
|
27
|
+
case 'swift':
|
|
28
|
+
throw new Error(
|
|
29
|
+
`Tuple (${types.join(', ')}) is not yet supported in Swift due to a Swift bug! See https://github.com/swiftlang/swift/issues/75865`
|
|
30
|
+
)
|
|
31
|
+
default:
|
|
32
|
+
throw new Error(
|
|
33
|
+
`Language ${language} is not yet supported for TupleType!`
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
getExtraFiles(): SourceFile[] {
|
|
38
|
+
return this.itemTypes.flatMap((t) => t.getExtraFiles())
|
|
39
|
+
}
|
|
40
|
+
getRequiredImports(language: Language): SourceImport[] {
|
|
41
|
+
const imports = this.itemTypes.flatMap((t) =>
|
|
42
|
+
t.getRequiredImports(language)
|
|
43
|
+
)
|
|
44
|
+
if (language === 'c++') {
|
|
45
|
+
imports.push({
|
|
46
|
+
language: 'c++',
|
|
47
|
+
name: 'tuple',
|
|
48
|
+
space: 'system',
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
return imports
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Language } from '../../getPlatformSpecs.js'
|
|
2
|
+
import type { SourceFile, SourceImport } from '../SourceFile.js'
|
|
3
|
+
|
|
4
|
+
export type TypeKind =
|
|
5
|
+
| 'array-buffer'
|
|
6
|
+
| 'array'
|
|
7
|
+
| 'bigint'
|
|
8
|
+
| 'boolean'
|
|
9
|
+
| 'custom-type'
|
|
10
|
+
| 'enum'
|
|
11
|
+
| 'error'
|
|
12
|
+
| 'function'
|
|
13
|
+
| 'hybrid-object'
|
|
14
|
+
| 'hybrid-object-base'
|
|
15
|
+
| 'map'
|
|
16
|
+
| 'null'
|
|
17
|
+
| 'number'
|
|
18
|
+
| 'optional'
|
|
19
|
+
| 'promise'
|
|
20
|
+
| 'record'
|
|
21
|
+
| 'string'
|
|
22
|
+
| 'struct'
|
|
23
|
+
| 'tuple'
|
|
24
|
+
| 'variant'
|
|
25
|
+
| 'result-wrapper'
|
|
26
|
+
| 'date'
|
|
27
|
+
| 'void'
|
|
28
|
+
|
|
29
|
+
export interface GetCodeOptions {
|
|
30
|
+
/**
|
|
31
|
+
* Specifies whether the name (e.g. a C++ class name)
|
|
32
|
+
* should use the fully qualified namespace name prefix.
|
|
33
|
+
*/
|
|
34
|
+
fullyQualified?: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Represents a TypeScript Type that can be represented in a native language (C++, Swift, Kotlin)
|
|
39
|
+
*/
|
|
40
|
+
export interface Type {
|
|
41
|
+
/**
|
|
42
|
+
* Get whether this type can be passed by reference in C++ (`const T&` vs `T`)
|
|
43
|
+
*/
|
|
44
|
+
readonly canBePassedByReference: boolean
|
|
45
|
+
/**
|
|
46
|
+
* Get the kind of the type.
|
|
47
|
+
*/
|
|
48
|
+
readonly kind: TypeKind
|
|
49
|
+
/**
|
|
50
|
+
* Get the native code required to represent this type for the given language (C++, Swift, Kotlin).
|
|
51
|
+
*
|
|
52
|
+
* E.g. for a `number` type, this would return `'double'` in C++.
|
|
53
|
+
*
|
|
54
|
+
* The `options` parameter can specify custom options. Subclasses may have more options.
|
|
55
|
+
*/
|
|
56
|
+
getCode(language: Language, options?: GetCodeOptions): string
|
|
57
|
+
/**
|
|
58
|
+
* Get all required extra files that need to be **created** for this type to properly work.
|
|
59
|
+
*
|
|
60
|
+
* E.g. for `type Gender = 'male' | 'female'`, the enum `Gender` needs to be created first (as a separate file).
|
|
61
|
+
*/
|
|
62
|
+
getExtraFiles(): SourceFile[]
|
|
63
|
+
/**
|
|
64
|
+
* Get all required extra imports that need to be **imported** for this type to properly work.
|
|
65
|
+
*/
|
|
66
|
+
getRequiredImports(language: Language): SourceImport[]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface NamedType extends Type {
|
|
70
|
+
/**
|
|
71
|
+
* Get the name of the value if it is a member or parameter.
|
|
72
|
+
*
|
|
73
|
+
* E.g. for a class member `double _something`, this returns `'_something'`.
|
|
74
|
+
*/
|
|
75
|
+
readonly name: string
|
|
76
|
+
/**
|
|
77
|
+
* Get the name of the value escaped as a valid C++ variable name.
|
|
78
|
+
*
|
|
79
|
+
* E.g. for a class member `double some-value`, this returns `'some_value'`.
|
|
80
|
+
*/
|
|
81
|
+
readonly escapedName: string
|
|
82
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Language } from '../../getPlatformSpecs.js'
|
|
2
|
+
import { escapeCppName, isNotDuplicate } from '../helpers.js'
|
|
3
|
+
import { type SourceFile, type SourceImport } from '../SourceFile.js'
|
|
4
|
+
import type { GetCodeOptions, Type, TypeKind } from './Type.js'
|
|
5
|
+
|
|
6
|
+
export const VariantLabels = [
|
|
7
|
+
'first',
|
|
8
|
+
'second',
|
|
9
|
+
'third',
|
|
10
|
+
'fourth',
|
|
11
|
+
'fifth',
|
|
12
|
+
'sixth',
|
|
13
|
+
'seventh',
|
|
14
|
+
'eigth',
|
|
15
|
+
'ninth',
|
|
16
|
+
'tenth',
|
|
17
|
+
] as const
|
|
18
|
+
type VariantLabel = (typeof VariantLabels)[number]
|
|
19
|
+
|
|
20
|
+
export class VariantType implements Type {
|
|
21
|
+
readonly variants: Type[]
|
|
22
|
+
readonly aliasName?: string
|
|
23
|
+
|
|
24
|
+
constructor(variants: Type[], aliasName?: string) {
|
|
25
|
+
this.variants = variants
|
|
26
|
+
this.aliasName = aliasName
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get canBePassedByReference(): boolean {
|
|
30
|
+
// It's a variant<..> - heavy to copy
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get kind(): TypeKind {
|
|
35
|
+
return 'variant'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get jsType(): string {
|
|
39
|
+
return this.variants.map((v) => v.kind).join(' | ')
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get cases(): [VariantLabel, Type][] {
|
|
43
|
+
return this.variants.map((v, i) => {
|
|
44
|
+
const label = VariantLabels[i]
|
|
45
|
+
if (label == null)
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Variant<...> (\`${this.jsType}\`) does not support ${i} cases!`
|
|
48
|
+
)
|
|
49
|
+
return [label, v]
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getAliasName(language: Language, options?: GetCodeOptions): string {
|
|
54
|
+
if (this.aliasName == null) {
|
|
55
|
+
const variants = this.variants.map((v) => v.getCode(language, options))
|
|
56
|
+
return escapeCppName(`Variant_${variants.join('_')}`)
|
|
57
|
+
}
|
|
58
|
+
return this.aliasName
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getCode(language: Language, options?: GetCodeOptions): string {
|
|
62
|
+
const types = this.variants
|
|
63
|
+
.map((v) => v.getCode(language, options))
|
|
64
|
+
.filter(isNotDuplicate)
|
|
65
|
+
|
|
66
|
+
switch (language) {
|
|
67
|
+
case 'c++':
|
|
68
|
+
return `std::variant<${types.join(', ')}>`
|
|
69
|
+
case 'swift':
|
|
70
|
+
case 'kotlin':
|
|
71
|
+
return this.getAliasName(language, options)
|
|
72
|
+
default:
|
|
73
|
+
throw new Error(
|
|
74
|
+
`Language ${language} is not yet supported for VariantType!`
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
getExtraFiles(): SourceFile[] {
|
|
79
|
+
return this.variants.flatMap((v) => v.getExtraFiles())
|
|
80
|
+
}
|
|
81
|
+
getRequiredImports(language: Language): SourceImport[] {
|
|
82
|
+
const imports = this.variants.flatMap((v) => v.getRequiredImports(language))
|
|
83
|
+
if (language === 'c++') {
|
|
84
|
+
imports.push({
|
|
85
|
+
language: 'c++',
|
|
86
|
+
name: 'variant',
|
|
87
|
+
space: 'system',
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
return imports
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Language } from '../../getPlatformSpecs.js'
|
|
2
|
+
import type { SourceFile, SourceImport } from '../SourceFile.js'
|
|
3
|
+
import type { Type, TypeKind } from './Type.js'
|
|
4
|
+
|
|
5
|
+
export class VoidType implements Type {
|
|
6
|
+
get canBePassedByReference(): boolean {
|
|
7
|
+
// It's void.
|
|
8
|
+
return false
|
|
9
|
+
}
|
|
10
|
+
get kind(): TypeKind {
|
|
11
|
+
return 'void'
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
getCode(language: Language): string {
|
|
15
|
+
switch (language) {
|
|
16
|
+
case 'c++':
|
|
17
|
+
return 'void'
|
|
18
|
+
case 'swift':
|
|
19
|
+
return 'Void'
|
|
20
|
+
case 'kotlin':
|
|
21
|
+
return 'Unit'
|
|
22
|
+
default:
|
|
23
|
+
throw new Error(
|
|
24
|
+
`Language ${language} is not yet supported for VoidType!`
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
getExtraFiles(): SourceFile[] {
|
|
29
|
+
return []
|
|
30
|
+
}
|
|
31
|
+
getRequiredImports(): SourceImport[] {
|
|
32
|
+
return []
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NamedWrappingType } from './NamedWrappingType.js'
|
|
2
|
+
import type { Type } from './Type.js'
|
|
3
|
+
|
|
4
|
+
export function getTypeAs<T>(
|
|
5
|
+
type: Type,
|
|
6
|
+
classReference: new (...args: any[]) => T
|
|
7
|
+
): T {
|
|
8
|
+
if (type instanceof classReference) {
|
|
9
|
+
return type as unknown as T
|
|
10
|
+
} else if (type instanceof NamedWrappingType) {
|
|
11
|
+
return getTypeAs(type.type, classReference)
|
|
12
|
+
} else {
|
|
13
|
+
throw new Error(`Type of kind "${type.kind}" is not a ${classReference}!`)
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { SourceFile } from './syntax/SourceFile.js'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import fs from 'fs'
|
|
4
|
+
import type { SwiftCxxHelper } from './syntax/swift/SwiftCxxTypeHelper.js'
|
|
5
|
+
import type { Type } from 'ts-morph'
|
|
6
|
+
import { isNotDuplicate } from './syntax/helpers.js'
|
|
7
|
+
import { readUserConfig } from './config/getConfig.js'
|
|
8
|
+
import { NitroConfig } from './config/NitroConfig.js'
|
|
9
|
+
|
|
10
|
+
export const NITROGEN_VERSION = process.env.npm_package_version ?? '?.?.?'
|
|
11
|
+
|
|
12
|
+
export function capitalizeName(name: string): string {
|
|
13
|
+
if (name.length === 0) return name
|
|
14
|
+
return name.charAt(0).toUpperCase() + name.slice(1)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function createIndentation(spacesCount: number): string {
|
|
18
|
+
return Array.from(Array(spacesCount)).fill(' ').join('')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function indent(string: string, spacesCount: number): string
|
|
22
|
+
export function indent(string: string, indentation: string): string
|
|
23
|
+
export function indent(string: string, indentation: string | number): string {
|
|
24
|
+
let spaces: string
|
|
25
|
+
if (typeof indentation === 'number') {
|
|
26
|
+
spaces = createIndentation(indentation)
|
|
27
|
+
} else {
|
|
28
|
+
spaces = indentation
|
|
29
|
+
}
|
|
30
|
+
return string.replaceAll('\n', `\n${spaces}`)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function errorToString(error: unknown): string {
|
|
34
|
+
if (error == null) {
|
|
35
|
+
return `null`
|
|
36
|
+
}
|
|
37
|
+
if (typeof error !== 'object') {
|
|
38
|
+
return `${error}`
|
|
39
|
+
}
|
|
40
|
+
if (error instanceof Error) {
|
|
41
|
+
let message = `${error.name}: ${error.message}`
|
|
42
|
+
if (error.cause != null) {
|
|
43
|
+
message += ` (cause: ${JSON.stringify(error.cause)})`
|
|
44
|
+
}
|
|
45
|
+
return message
|
|
46
|
+
}
|
|
47
|
+
if ('toString' in error) {
|
|
48
|
+
return error.toString()
|
|
49
|
+
}
|
|
50
|
+
return JSON.stringify(error)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function escapeComments(string: string): string {
|
|
54
|
+
return string
|
|
55
|
+
.replace(/\/\*/g, '/ *') // Escape start of comment
|
|
56
|
+
.replace(/\*\//g, '* /') // Escape end of comment
|
|
57
|
+
.replace(/\/\//g, '/ /') // Escape single-line comment
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const HAS_UNIX_PATHS = path.join('a', 'b').includes('/')
|
|
61
|
+
export function toUnixPath(p: string): string {
|
|
62
|
+
if (HAS_UNIX_PATHS) return p
|
|
63
|
+
return p.replaceAll('\\', '/')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const sep = path.sep
|
|
67
|
+
export function unsafeFastJoin(...segments: string[]): string {
|
|
68
|
+
// this function should really not take any unsafe strings like `/` or `\`.
|
|
69
|
+
return segments.join(sep)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function getFullPath(file: SourceFile): string {
|
|
73
|
+
return unsafeFastJoin(
|
|
74
|
+
file.platform,
|
|
75
|
+
file.language,
|
|
76
|
+
...file.subdirectory,
|
|
77
|
+
file.name
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Deduplicates all files via their full path.
|
|
83
|
+
* If content differs, you are f*cked.
|
|
84
|
+
*/
|
|
85
|
+
export function deduplicateFiles(files: SourceFile[]): SourceFile[] {
|
|
86
|
+
const map = new Map<string, SourceFile>()
|
|
87
|
+
for (const file of files) {
|
|
88
|
+
const filePath = getFullPath(file)
|
|
89
|
+
|
|
90
|
+
if (!map.has(filePath)) {
|
|
91
|
+
map.set(filePath, file)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return [...map.values()]
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function filterDuplicateHelperBridges(
|
|
98
|
+
bridge: SwiftCxxHelper,
|
|
99
|
+
i: number,
|
|
100
|
+
array: SwiftCxxHelper[]
|
|
101
|
+
): boolean {
|
|
102
|
+
const otherIndex = array.findIndex(
|
|
103
|
+
(f2) => bridge.specializationName === f2.specializationName
|
|
104
|
+
)
|
|
105
|
+
return otherIndex === i
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function toLowerCamelCase(string: string): string {
|
|
109
|
+
const parts = string.split('_').filter((part) => part !== '')
|
|
110
|
+
|
|
111
|
+
if (parts.length === 0) return ''
|
|
112
|
+
|
|
113
|
+
const camelCaseString = parts[0]!.toLowerCase()
|
|
114
|
+
|
|
115
|
+
const camelCased = parts
|
|
116
|
+
.slice(1)
|
|
117
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
|
|
118
|
+
|
|
119
|
+
return camelCaseString + camelCased.join('')
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function getBaseTypes(type: Type): Type[] {
|
|
123
|
+
const baseTypes = type.getBaseTypes()
|
|
124
|
+
const symbol = type.getSymbol()
|
|
125
|
+
if (symbol != null) {
|
|
126
|
+
baseTypes.push(...symbol.getDeclaredType().getBaseTypes())
|
|
127
|
+
}
|
|
128
|
+
const recursive = baseTypes.flatMap((b) => [b, ...getBaseTypes(b)])
|
|
129
|
+
return recursive.filter(isNotDuplicate)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function getHybridObjectNitroModuleConfig(
|
|
133
|
+
type: Type
|
|
134
|
+
): NitroConfig | undefined {
|
|
135
|
+
const symbol = type.getSymbol() ?? type.getAliasSymbol()
|
|
136
|
+
if (!symbol) return undefined
|
|
137
|
+
|
|
138
|
+
const declarations =
|
|
139
|
+
symbol.getValueDeclaration() || symbol.getDeclarations()[0]
|
|
140
|
+
if (!declarations) return undefined
|
|
141
|
+
|
|
142
|
+
const sourceFile = declarations.getSourceFile()
|
|
143
|
+
let filePath: string = sourceFile.getFilePath()
|
|
144
|
+
|
|
145
|
+
while (true) {
|
|
146
|
+
// go up one dir
|
|
147
|
+
const newFilePath = path.resolve(path.join(filePath, '..'))
|
|
148
|
+
if (filePath === newFilePath) {
|
|
149
|
+
// going 'cd ..' in that path didn't change a thing - so we
|
|
150
|
+
// reached the root directory. we didn't find a nitro.json anywhere.
|
|
151
|
+
return undefined
|
|
152
|
+
}
|
|
153
|
+
filePath = newFilePath
|
|
154
|
+
|
|
155
|
+
const nitroJsonPath = path.join(filePath, 'nitro.json')
|
|
156
|
+
const hasNitroJson = fs.existsSync(nitroJsonPath)
|
|
157
|
+
if (hasNitroJson) {
|
|
158
|
+
const config = readUserConfig(nitroJsonPath)
|
|
159
|
+
return new NitroConfig(config)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|