nitrogen 0.2.24 → 0.29.5

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.
Files changed (241) hide show
  1. package/README.md +18 -108
  2. package/lib/Logger.js +56 -0
  3. package/lib/autolinking/Autolinking.js +1 -0
  4. package/lib/autolinking/android/createCMakeExtension.js +109 -0
  5. package/lib/autolinking/android/createGradleExtension.js +36 -0
  6. package/lib/autolinking/android/createHybridObjectInitializer.js +159 -0
  7. package/lib/autolinking/createAndroidAutolinking.js +13 -0
  8. package/lib/autolinking/createIOSAutolinking.js +19 -0
  9. package/lib/autolinking/ios/createHybridObjectInitializer.js +97 -0
  10. package/lib/autolinking/ios/createPodspecRubyExtension.js +69 -0
  11. package/lib/autolinking/ios/createSwiftCxxBridge.js +117 -0
  12. package/lib/autolinking/ios/createSwiftUmbrellaHeader.js +74 -0
  13. package/lib/config/NitroConfig.js +112 -0
  14. package/lib/config/NitroUserConfig.js +88 -0
  15. package/lib/config/getConfig.js +84 -0
  16. package/lib/createGitAttributes.js +11 -0
  17. package/lib/createPlatformSpec.js +127 -0
  18. package/lib/getFiles.js +28 -0
  19. package/lib/getPlatformSpecs.js +153 -0
  20. package/lib/index.js +113 -10
  21. package/lib/init.js +123 -0
  22. package/lib/nitrogen.js +165 -0
  23. package/lib/prettifyDirectory.js +27 -0
  24. package/lib/syntax/BridgedType.js +1 -0
  25. package/lib/syntax/CodeNode.js +1 -0
  26. package/lib/syntax/HybridObjectSpec.js +1 -0
  27. package/lib/syntax/Method.js +108 -0
  28. package/lib/syntax/Parameter.js +65 -0
  29. package/lib/syntax/Property.js +147 -0
  30. package/lib/syntax/SourceFile.js +7 -0
  31. package/lib/syntax/c++/CppEnum.js +110 -0
  32. package/lib/syntax/c++/CppHybridObject.js +146 -0
  33. package/lib/syntax/c++/CppHybridObjectRegistration.js +18 -0
  34. package/lib/syntax/c++/CppStruct.js +108 -0
  35. package/lib/syntax/c++/CppUnion.js +88 -0
  36. package/lib/syntax/c++/getForwardDeclaration.js +14 -0
  37. package/lib/syntax/c++/includeNitroHeader.js +34 -0
  38. package/lib/syntax/createType.js +303 -0
  39. package/lib/syntax/getAllTypes.js +11 -0
  40. package/lib/syntax/getCustomTypeConfig.js +53 -0
  41. package/lib/syntax/getHybridObjectName.d.ts +36 -0
  42. package/lib/syntax/getHybridObjectName.js +10 -0
  43. package/lib/syntax/getInterfaceProperties.js +9 -0
  44. package/lib/syntax/getReferencedTypes.js +47 -0
  45. package/lib/syntax/helpers.js +53 -0
  46. package/lib/syntax/isCoreType.js +47 -0
  47. package/lib/syntax/kotlin/FbjniHybridObject.js +261 -0
  48. package/lib/syntax/kotlin/JNINativeRegistrations.js +7 -0
  49. package/lib/syntax/kotlin/KotlinBoxedPrimitive.js +17 -0
  50. package/lib/syntax/kotlin/KotlinCxxBridgedType.js +893 -0
  51. package/lib/syntax/kotlin/KotlinEnum.js +113 -0
  52. package/lib/syntax/kotlin/KotlinFunction.js +256 -0
  53. package/lib/syntax/kotlin/KotlinHybridObject.js +177 -0
  54. package/lib/syntax/kotlin/KotlinHybridObjectRegistration.js +26 -0
  55. package/lib/syntax/kotlin/KotlinStruct.js +172 -0
  56. package/lib/syntax/kotlin/KotlinVariant.js +191 -0
  57. package/lib/syntax/swift/SwiftCxxBridgedType.js +819 -0
  58. package/lib/syntax/swift/SwiftCxxTypeHelper.js +613 -0
  59. package/lib/syntax/swift/SwiftEnum.js +52 -0
  60. package/lib/syntax/swift/SwiftFunction.js +83 -0
  61. package/lib/syntax/swift/SwiftHybridObject.js +103 -0
  62. package/lib/syntax/swift/SwiftHybridObjectBridge.js +451 -0
  63. package/lib/syntax/swift/SwiftHybridObjectRegistration.js +42 -0
  64. package/lib/syntax/swift/SwiftStruct.js +75 -0
  65. package/lib/syntax/swift/SwiftVariant.js +58 -0
  66. package/lib/syntax/types/ArrayBufferType.js +37 -0
  67. package/lib/syntax/types/ArrayType.d.ts +12 -0
  68. package/lib/syntax/types/ArrayType.js +52 -0
  69. package/lib/syntax/types/BigIntType.js +27 -0
  70. package/lib/syntax/types/BooleanType.js +27 -0
  71. package/lib/syntax/types/CustomType.d.ts +14 -0
  72. package/lib/syntax/types/CustomType.js +36 -0
  73. package/lib/syntax/types/DateType.js +35 -0
  74. package/lib/syntax/types/EnumType.js +101 -0
  75. package/lib/syntax/types/ErrorType.js +37 -0
  76. package/lib/syntax/types/FunctionType.js +147 -0
  77. package/lib/syntax/types/HybridObjectBaseType.js +38 -0
  78. package/lib/syntax/types/HybridObjectType.js +131 -0
  79. package/lib/syntax/types/MapType.js +37 -0
  80. package/lib/syntax/types/NamedWrappingType.js +27 -0
  81. package/lib/syntax/types/NullType.js +23 -0
  82. package/lib/syntax/types/NumberType.js +27 -0
  83. package/lib/syntax/types/OptionalType.js +59 -0
  84. package/lib/syntax/types/PromiseType.js +62 -0
  85. package/lib/syntax/types/RecordType.js +47 -0
  86. package/lib/syntax/types/ResultWrappingType.js +44 -0
  87. package/lib/syntax/types/StringType.js +35 -0
  88. package/lib/syntax/types/StructType.js +61 -0
  89. package/lib/syntax/types/TupleType.js +39 -0
  90. package/lib/syntax/types/Type.js +1 -0
  91. package/lib/syntax/types/VariantType.js +75 -0
  92. package/lib/syntax/types/VoidType.js +27 -0
  93. package/lib/syntax/types/getTypeAs.js +12 -0
  94. package/lib/utils.js +126 -0
  95. package/lib/views/CppHybridViewComponent.js +256 -0
  96. package/lib/views/createHostComponentJs.js +27 -0
  97. package/lib/views/kotlin/KotlinHybridViewManager.js +229 -0
  98. package/lib/views/swift/SwiftHybridViewManager.js +131 -0
  99. package/lib/writeFile.js +19 -0
  100. package/package.json +58 -29
  101. package/src/Logger.ts +63 -0
  102. package/src/autolinking/Autolinking.ts +9 -0
  103. package/src/autolinking/android/createCMakeExtension.ts +126 -0
  104. package/src/autolinking/android/createGradleExtension.ts +43 -0
  105. package/src/autolinking/android/createHybridObjectInitializer.ts +174 -0
  106. package/src/autolinking/createAndroidAutolinking.ts +28 -0
  107. package/src/autolinking/createIOSAutolinking.ts +24 -0
  108. package/src/autolinking/ios/createHybridObjectInitializer.ts +112 -0
  109. package/src/autolinking/ios/createPodspecRubyExtension.ts +76 -0
  110. package/src/autolinking/ios/createSwiftCxxBridge.ts +137 -0
  111. package/src/autolinking/ios/createSwiftUmbrellaHeader.ts +90 -0
  112. package/src/config/NitroConfig.ts +139 -0
  113. package/src/config/NitroUserConfig.ts +105 -0
  114. package/src/config/getConfig.ts +91 -0
  115. package/src/createGitAttributes.ts +15 -0
  116. package/src/createPlatformSpec.ts +176 -0
  117. package/src/getFiles.ts +31 -0
  118. package/src/getPlatformSpecs.ts +202 -0
  119. package/src/index.ts +146 -0
  120. package/src/init.ts +186 -0
  121. package/src/nitrogen.ts +246 -0
  122. package/src/prettifyDirectory.ts +32 -0
  123. package/src/syntax/BridgedType.ts +59 -0
  124. package/src/syntax/CodeNode.ts +24 -0
  125. package/src/syntax/HybridObjectSpec.ts +14 -0
  126. package/src/syntax/Method.ts +154 -0
  127. package/src/syntax/Parameter.ts +81 -0
  128. package/src/syntax/Property.ts +203 -0
  129. package/src/syntax/SourceFile.ts +80 -0
  130. package/src/syntax/c++/CppEnum.ts +128 -0
  131. package/src/syntax/c++/CppHybridObject.ts +165 -0
  132. package/src/syntax/c++/CppHybridObjectRegistration.ts +39 -0
  133. package/src/syntax/c++/CppStruct.ts +129 -0
  134. package/src/syntax/c++/CppUnion.ts +105 -0
  135. package/src/syntax/c++/getForwardDeclaration.ts +19 -0
  136. package/src/syntax/c++/includeNitroHeader.ts +40 -0
  137. package/src/syntax/createType.ts +365 -0
  138. package/src/syntax/getAllTypes.ts +18 -0
  139. package/src/syntax/getCustomTypeConfig.ts +71 -0
  140. package/src/syntax/getHybridObjectName.ts +48 -0
  141. package/src/syntax/getInterfaceProperties.ts +21 -0
  142. package/src/syntax/getReferencedTypes.ts +57 -0
  143. package/src/syntax/helpers.ts +79 -0
  144. package/src/syntax/isCoreType.ts +60 -0
  145. package/src/syntax/kotlin/FbjniHybridObject.ts +313 -0
  146. package/src/syntax/kotlin/JNINativeRegistrations.ts +19 -0
  147. package/src/syntax/kotlin/KotlinBoxedPrimitive.ts +19 -0
  148. package/src/syntax/kotlin/KotlinCxxBridgedType.ts +942 -0
  149. package/src/syntax/kotlin/KotlinEnum.ts +130 -0
  150. package/src/syntax/kotlin/KotlinFunction.ts +277 -0
  151. package/src/syntax/kotlin/KotlinHybridObject.ts +205 -0
  152. package/src/syntax/kotlin/KotlinHybridObjectRegistration.ts +51 -0
  153. package/src/syntax/kotlin/KotlinStruct.ts +198 -0
  154. package/src/syntax/kotlin/KotlinVariant.ts +212 -0
  155. package/src/syntax/swift/SwiftCxxBridgedType.ts +874 -0
  156. package/src/syntax/swift/SwiftCxxTypeHelper.ts +674 -0
  157. package/src/syntax/swift/SwiftEnum.ts +65 -0
  158. package/src/syntax/swift/SwiftFunction.ts +91 -0
  159. package/src/syntax/swift/SwiftHybridObject.ts +121 -0
  160. package/src/syntax/swift/SwiftHybridObjectBridge.ts +522 -0
  161. package/src/syntax/swift/SwiftHybridObjectRegistration.ts +75 -0
  162. package/src/syntax/swift/SwiftStruct.ts +85 -0
  163. package/src/syntax/swift/SwiftVariant.ts +67 -0
  164. package/src/syntax/types/ArrayBufferType.ts +49 -0
  165. package/src/syntax/types/ArrayType.ts +62 -0
  166. package/src/syntax/types/BigIntType.ts +35 -0
  167. package/src/syntax/types/BooleanType.ts +35 -0
  168. package/src/syntax/types/CustomType.ts +47 -0
  169. package/src/syntax/types/DateType.ts +43 -0
  170. package/src/syntax/types/EnumType.ts +130 -0
  171. package/src/syntax/types/ErrorType.ts +44 -0
  172. package/src/syntax/types/FunctionType.ts +167 -0
  173. package/src/syntax/types/HybridObjectBaseType.ts +54 -0
  174. package/src/syntax/types/HybridObjectType.ts +198 -0
  175. package/src/syntax/types/MapType.ts +49 -0
  176. package/src/syntax/types/NamedWrappingType.ts +33 -0
  177. package/src/syntax/types/NullType.ts +30 -0
  178. package/src/syntax/types/NumberType.ts +34 -0
  179. package/src/syntax/types/OptionalType.ts +66 -0
  180. package/src/syntax/types/PromiseType.ts +72 -0
  181. package/src/syntax/types/RecordType.ts +56 -0
  182. package/src/syntax/types/ResultWrappingType.ts +53 -0
  183. package/src/syntax/types/StringType.ts +44 -0
  184. package/src/syntax/types/StructType.ts +83 -0
  185. package/src/syntax/types/TupleType.ts +53 -0
  186. package/src/syntax/types/Type.ts +82 -0
  187. package/src/syntax/types/VariantType.ts +92 -0
  188. package/src/syntax/types/VoidType.ts +34 -0
  189. package/src/syntax/types/getTypeAs.ts +15 -0
  190. package/src/utils.ts +162 -0
  191. package/src/views/CppHybridViewComponent.ts +304 -0
  192. package/src/views/createHostComponentJs.ts +34 -0
  193. package/src/views/kotlin/KotlinHybridViewManager.ts +258 -0
  194. package/src/views/swift/SwiftHybridViewManager.ts +153 -0
  195. package/src/writeFile.ts +27 -0
  196. package/.jshintignore +0 -6
  197. package/.jshintrc +0 -3
  198. package/.npmignore +0 -3
  199. package/.travis.yml +0 -13
  200. package/LICENSE +0 -13
  201. package/browser/nitrogen-min.js +0 -3
  202. package/browser/nitrogen.js +0 -6369
  203. package/lib/apiKey.js +0 -67
  204. package/lib/blob.js +0 -57
  205. package/lib/commandManager.js +0 -350
  206. package/lib/device.js +0 -19
  207. package/lib/memoryStore.js +0 -24
  208. package/lib/message.js +0 -298
  209. package/lib/permission.js +0 -121
  210. package/lib/principal.js +0 -330
  211. package/lib/service.js +0 -347
  212. package/lib/session.js +0 -494
  213. package/lib/user.js +0 -20
  214. package/publish +0 -2
  215. package/scripts/build-documentation +0 -4
  216. package/scripts/build-module +0 -27
  217. package/scripts/module.js +0 -12
  218. package/scripts/postamble.js +0 -1
  219. package/scripts/preamble.js +0 -2
  220. package/scripts/run-test-server +0 -9
  221. package/test/config.js +0 -12
  222. package/test/fixtures/images/image.jpg +0 -0
  223. package/test/fixtures/images/motion0.jpg +0 -0
  224. package/test/fixtures/images/motion1.jpg +0 -0
  225. package/test/fixtures/images/motion2.jpg +0 -0
  226. package/test/fixtures/index.js +0 -76
  227. package/test/main.js +0 -5
  228. package/test/memoryStore.js +0 -22
  229. package/test/mocha.opts +0 -3
  230. package/test/units/apiKey.js +0 -46
  231. package/test/units/blob.js +0 -35
  232. package/test/units/commandManager.js +0 -67
  233. package/test/units/device.js +0 -26
  234. package/test/units/heartbeat.js +0 -28
  235. package/test/units/message.js +0 -79
  236. package/test/units/permissions.js +0 -43
  237. package/test/units/principal.js +0 -116
  238. package/test/units/service.js +0 -92
  239. package/test/units/session.js +0 -97
  240. package/test/units/user.js +0 -48
  241. package/yuidoc.json +0 -8
@@ -0,0 +1,67 @@
1
+ import { indent } from '../../utils.js'
2
+ import { createFileMetadataString } from '../helpers.js'
3
+ import type { SourceFile } from '../SourceFile.js'
4
+ import { getTypeAs } from '../types/getTypeAs.js'
5
+ import { OptionalType } from '../types/OptionalType.js'
6
+ import type { Type } from '../types/Type.js'
7
+ import type { VariantType } from '../types/VariantType.js'
8
+
9
+ function isPrimitive(type: Type): boolean {
10
+ switch (type.kind) {
11
+ case 'bigint':
12
+ case 'boolean':
13
+ case 'number':
14
+ case 'string':
15
+ case 'void':
16
+ case 'result-wrapper':
17
+ case 'null':
18
+ case 'error':
19
+ return true
20
+ case 'optional':
21
+ const optional = getTypeAs(type, OptionalType)
22
+ return isPrimitive(optional.wrappingType)
23
+ default:
24
+ return false
25
+ }
26
+ }
27
+
28
+ export function createSwiftVariant(variant: VariantType): SourceFile {
29
+ const typename = variant.getAliasName('swift')
30
+ const cases = variant.cases
31
+ .map(([label, v]) => {
32
+ const type = v.getCode('swift')
33
+ return `case ${label}(${type})`
34
+ })
35
+ .join('\n')
36
+ const jsSignature = variant.variants.map((t) => t.kind).join(' | ')
37
+
38
+ const allPrimitives = variant.variants.every((v) => isPrimitive(v))
39
+ const enumDeclaration = allPrimitives ? 'enum' : 'indirect enum'
40
+
41
+ const extraImports = variant.variants
42
+ .flatMap((t) => t.getRequiredImports('swift'))
43
+ .map((i) => `import ${i.name}`)
44
+
45
+ const code = `
46
+ ${createFileMetadataString(`${typename}.swift`)}
47
+
48
+ ${extraImports.join('\n')}
49
+
50
+ /**
51
+ * An Swift enum with associated values representing a Variant/Union type.
52
+ * JS type: \`${jsSignature}\`
53
+ */
54
+ @frozen
55
+ public ${enumDeclaration} ${typename} {
56
+ ${indent(cases, ' ')}
57
+ }
58
+ `.trim()
59
+
60
+ return {
61
+ content: code,
62
+ language: 'swift',
63
+ name: `${typename}.swift`,
64
+ platform: 'ios',
65
+ subdirectory: [],
66
+ }
67
+ }
@@ -0,0 +1,49 @@
1
+ import type { Language } from '../../getPlatformSpecs.js'
2
+ import { getForwardDeclaration } from '../c++/getForwardDeclaration.js'
3
+ import type { SourceFile, SourceImport } from '../SourceFile.js'
4
+ import type { Type, TypeKind } from './Type.js'
5
+
6
+ export class ArrayBufferType implements Type {
7
+ get canBePassedByReference(): boolean {
8
+ // It's a shared_ptr.
9
+ return true
10
+ }
11
+
12
+ get kind(): TypeKind {
13
+ return 'array-buffer'
14
+ }
15
+
16
+ getCode(language: Language): string {
17
+ switch (language) {
18
+ case 'c++':
19
+ return 'std::shared_ptr<ArrayBuffer>'
20
+ case 'swift':
21
+ return 'ArrayBuffer'
22
+ case 'kotlin':
23
+ return 'ArrayBuffer'
24
+ default:
25
+ throw new Error(
26
+ `Language ${language} is not yet supported for ArrayBufferType!`
27
+ )
28
+ }
29
+ }
30
+ getExtraFiles(): SourceFile[] {
31
+ return []
32
+ }
33
+ getRequiredImports(language: Language): SourceImport[] {
34
+ const imports: SourceImport[] = []
35
+ if (language === 'c++') {
36
+ imports.push({
37
+ name: 'NitroModules/ArrayBuffer.hpp',
38
+ forwardDeclaration: getForwardDeclaration(
39
+ 'class',
40
+ 'ArrayBuffer',
41
+ 'NitroModules'
42
+ ),
43
+ language: 'c++',
44
+ space: 'system',
45
+ })
46
+ }
47
+ return imports
48
+ }
49
+ }
@@ -0,0 +1,62 @@
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 ArrayType implements Type {
6
+ readonly itemType: Type
7
+
8
+ constructor(itemType: Type) {
9
+ this.itemType = itemType
10
+ }
11
+
12
+ get canBePassedByReference(): boolean {
13
+ // It's a vector<..>, heavy to copy
14
+ return true
15
+ }
16
+
17
+ get kind(): TypeKind {
18
+ return 'array'
19
+ }
20
+
21
+ getCode(language: Language, options?: GetCodeOptions): string {
22
+ const itemCode = this.itemType.getCode(language, options)
23
+
24
+ switch (language) {
25
+ case 'c++':
26
+ return `std::vector<${itemCode}>`
27
+ case 'swift':
28
+ return `[${itemCode}]`
29
+ case 'kotlin':
30
+ switch (this.itemType.kind) {
31
+ case 'number':
32
+ return 'DoubleArray'
33
+ case 'boolean':
34
+ return 'BooleanArray'
35
+ case 'bigint':
36
+ return 'LongArray'
37
+ default:
38
+ return `Array<${itemCode}>`
39
+ }
40
+ default:
41
+ throw new Error(
42
+ `Language ${language} is not yet supported for ArrayType!`
43
+ )
44
+ }
45
+ }
46
+ getExtraFiles(): SourceFile[] {
47
+ return this.itemType.getExtraFiles()
48
+ }
49
+ getRequiredImports(language: Language): SourceImport[] {
50
+ const imports: SourceImport[] = [
51
+ ...this.itemType.getRequiredImports(language),
52
+ ]
53
+ if (language === 'c++') {
54
+ imports.push({
55
+ name: 'vector',
56
+ language: 'c++',
57
+ space: 'system',
58
+ })
59
+ }
60
+ return imports
61
+ }
62
+ }
@@ -0,0 +1,35 @@
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 BigIntType implements Type {
6
+ get canBePassedByReference(): boolean {
7
+ // It's a primitive.
8
+ return false
9
+ }
10
+
11
+ get kind(): TypeKind {
12
+ return 'bigint'
13
+ }
14
+
15
+ getCode(language: Language): string {
16
+ switch (language) {
17
+ case 'c++':
18
+ return 'int64_t'
19
+ case 'swift':
20
+ return 'Int64'
21
+ case 'kotlin':
22
+ return 'Long'
23
+ default:
24
+ throw new Error(
25
+ `Language ${language} is not yet supported for BigIntType!`
26
+ )
27
+ }
28
+ }
29
+ getExtraFiles(): SourceFile[] {
30
+ return []
31
+ }
32
+ getRequiredImports(): SourceImport[] {
33
+ return []
34
+ }
35
+ }
@@ -0,0 +1,35 @@
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 BooleanType implements Type {
6
+ get canBePassedByReference(): boolean {
7
+ // It's a primitive.
8
+ return false
9
+ }
10
+
11
+ get kind(): TypeKind {
12
+ return 'boolean'
13
+ }
14
+
15
+ getCode(language: Language): string {
16
+ switch (language) {
17
+ case 'c++':
18
+ return 'bool'
19
+ case 'swift':
20
+ return 'Bool'
21
+ case 'kotlin':
22
+ return 'Boolean'
23
+ default:
24
+ throw new Error(
25
+ `Language ${language} is not yet supported for BooleanType!`
26
+ )
27
+ }
28
+ }
29
+ getExtraFiles(): SourceFile[] {
30
+ return []
31
+ }
32
+ getRequiredImports(): SourceImport[] {
33
+ return []
34
+ }
35
+ }
@@ -0,0 +1,47 @@
1
+ import type { CustomTypeConfig } from 'react-native-nitro-modules'
2
+ import type { Language } from '../../getPlatformSpecs.js'
3
+ import type { SourceFile, SourceImport } from '../SourceFile.js'
4
+ import type { Type, TypeKind } from './Type.js'
5
+
6
+ export class CustomType implements Type {
7
+ typeConfig: CustomTypeConfig
8
+ typeName: string
9
+
10
+ constructor(typeName: string, typeConfig: CustomTypeConfig) {
11
+ this.typeName = typeName
12
+ this.typeConfig = typeConfig
13
+ }
14
+
15
+ get canBePassedByReference() {
16
+ return this.typeConfig.canBePassedByReference ?? false
17
+ }
18
+
19
+ get kind(): TypeKind {
20
+ return 'custom-type'
21
+ }
22
+
23
+ getCode(language: Language): string {
24
+ switch (language) {
25
+ case 'c++':
26
+ return this.typeName
27
+ default:
28
+ throw new Error(
29
+ `Language ${language} is not yet supported for CustomType "${this.typeName}"!`
30
+ )
31
+ }
32
+ }
33
+ getExtraFiles(): SourceFile[] {
34
+ return []
35
+ }
36
+ getRequiredImports(language: Language): SourceImport[] {
37
+ const imports: SourceImport[] = []
38
+ if (language === 'c++') {
39
+ imports.push({
40
+ name: this.typeConfig.include,
41
+ language: 'c++',
42
+ space: 'user',
43
+ })
44
+ }
45
+ return imports
46
+ }
47
+ }
@@ -0,0 +1,43 @@
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 DateType implements Type {
6
+ get canBePassedByReference(): boolean {
7
+ // simple chrono value type
8
+ return false
9
+ }
10
+
11
+ get kind(): TypeKind {
12
+ return 'date'
13
+ }
14
+
15
+ getCode(language: Language): string {
16
+ switch (language) {
17
+ case 'c++':
18
+ return 'std::chrono::system_clock::time_point'
19
+ case 'swift':
20
+ return 'Date'
21
+ case 'kotlin':
22
+ return 'java.time.Instant'
23
+ default:
24
+ throw new Error(
25
+ `Language ${language} is not yet supported for DateType!`
26
+ )
27
+ }
28
+ }
29
+ getExtraFiles(): SourceFile[] {
30
+ return []
31
+ }
32
+ getRequiredImports(language: Language): SourceImport[] {
33
+ const imports: SourceImport[] = []
34
+ if (language === 'c++') {
35
+ imports.push({
36
+ name: 'chrono',
37
+ language: 'c++',
38
+ space: 'system',
39
+ })
40
+ }
41
+ return imports
42
+ }
43
+ }
@@ -0,0 +1,130 @@
1
+ import { EnumDeclaration } from 'ts-morph'
2
+ import { Type as TSMorphType, type ts } from 'ts-morph'
3
+ import type { Language } from '../../getPlatformSpecs.js'
4
+ import { getForwardDeclaration } from '../c++/getForwardDeclaration.js'
5
+ import { type SourceFile, type SourceImport } from '../SourceFile.js'
6
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js'
7
+ import { createCppEnum } from '../c++/CppEnum.js'
8
+ import { escapeCppName } from '../helpers.js'
9
+ import { createCppUnion } from '../c++/CppUnion.js'
10
+ import { NitroConfig } from '../../config/NitroConfig.js'
11
+
12
+ export interface EnumMember {
13
+ name: string
14
+ value: number
15
+ stringValue: string
16
+ }
17
+
18
+ export class EnumType implements Type {
19
+ readonly enumName: string
20
+ readonly enumMembers: EnumMember[]
21
+ readonly jsType: 'enum' | 'union'
22
+ readonly declarationFile: SourceFile
23
+
24
+ constructor(enumName: string, enumDeclaration: EnumDeclaration)
25
+ constructor(enumName: string, union: TSMorphType<ts.UnionType>)
26
+ constructor(
27
+ enumName: string,
28
+ declaration: EnumDeclaration | TSMorphType<ts.UnionType>
29
+ ) {
30
+ this.enumName = enumName
31
+ if (declaration instanceof EnumDeclaration) {
32
+ // It's a JS enum { ... }
33
+ this.jsType = 'enum'
34
+ this.enumMembers = declaration.getMembers().map<EnumMember>((m) => {
35
+ const name = m.getSymbolOrThrow().getEscapedName()
36
+ const value = m.getValue()
37
+ if (typeof value !== 'number') {
38
+ throw new Error(
39
+ `Enum member ${enumName}.${name} is ${value} (${typeof value}), which cannot be represented in C++ enums.\n` +
40
+ `Each enum member must be a number! If you want to use strings, use TypeScript unions ("a" | "b") instead!`
41
+ )
42
+ }
43
+ return {
44
+ name: escapeCppName(name).toUpperCase(),
45
+ value: value,
46
+ stringValue: name,
47
+ }
48
+ })
49
+ this.declarationFile = createCppEnum(enumName, this.enumMembers)
50
+ } else {
51
+ // It's a TS union '..' | '..'
52
+ this.jsType = 'union'
53
+ this.enumMembers = declaration
54
+ .getNonNullableType()
55
+ .getUnionTypes()
56
+ .map((t, i) => {
57
+ if (t.isStringLiteral()) {
58
+ const literalValue = t.getLiteralValueOrThrow()
59
+ if (typeof literalValue !== 'string')
60
+ throw new Error(
61
+ `${enumName}: Value "${literalValue}" is not a string - it is ${typeof literalValue}!`
62
+ )
63
+ return {
64
+ name: escapeCppName(literalValue).toUpperCase(),
65
+ value: i,
66
+ stringValue: literalValue,
67
+ }
68
+ } else {
69
+ throw new Error(
70
+ `${enumName}: Value "${t.getText()}" is not a string literal - it cannot be represented in a C++ enum!`
71
+ )
72
+ }
73
+ })
74
+ this.declarationFile = createCppUnion(enumName, this.enumMembers)
75
+ }
76
+ if (this.enumName.startsWith('__')) {
77
+ throw new Error(
78
+ `Enum name cannot start with two underscores (__) as this is reserved syntax for Nitrogen! (In ${this.enumName}: ${this.enumMembers.map((m) => m.name).join(' | ')})`
79
+ )
80
+ }
81
+ }
82
+
83
+ get canBePassedByReference(): boolean {
84
+ // It's a primitive.
85
+ return false
86
+ }
87
+
88
+ get kind(): TypeKind {
89
+ return 'enum'
90
+ }
91
+
92
+ getCode(language: Language, { fullyQualified }: GetCodeOptions = {}): string {
93
+ switch (language) {
94
+ case 'c++':
95
+ if (fullyQualified) {
96
+ return NitroConfig.current.getCxxNamespace('c++', this.enumName)
97
+ } else {
98
+ return this.enumName
99
+ }
100
+ case 'swift':
101
+ return this.enumName
102
+ case 'kotlin':
103
+ return this.enumName
104
+ default:
105
+ throw new Error(
106
+ `Language ${language} is not yet supported for NumberType!`
107
+ )
108
+ }
109
+ }
110
+ getExtraFiles(): SourceFile[] {
111
+ return [this.declarationFile]
112
+ }
113
+ getRequiredImports(language: Language): SourceImport[] {
114
+ const imports: SourceImport[] = []
115
+ if (language === 'c++') {
116
+ const cxxNamespace = NitroConfig.current.getCxxNamespace('c++')
117
+ imports.push({
118
+ name: this.declarationFile.name,
119
+ language: this.declarationFile.language,
120
+ forwardDeclaration: getForwardDeclaration(
121
+ 'enum class',
122
+ this.enumName,
123
+ cxxNamespace
124
+ ),
125
+ space: 'user',
126
+ })
127
+ }
128
+ return imports
129
+ }
130
+ }
@@ -0,0 +1,44 @@
1
+ import type { Language } from '../../getPlatformSpecs.js'
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js'
3
+ import type { Type, TypeKind } from './Type.js'
4
+
5
+ export class ErrorType implements Type {
6
+ constructor() {}
7
+
8
+ get canBePassedByReference(): boolean {
9
+ // It's a exception<..>, pass by reference.
10
+ return true
11
+ }
12
+ get kind(): TypeKind {
13
+ return 'error'
14
+ }
15
+
16
+ getCode(language: Language): string {
17
+ switch (language) {
18
+ case 'c++':
19
+ return `std::exception_ptr`
20
+ case 'swift':
21
+ return `Error`
22
+ case 'kotlin':
23
+ return `Throwable`
24
+ default:
25
+ throw new Error(
26
+ `Language ${language} is not yet supported for ThrowableType!`
27
+ )
28
+ }
29
+ }
30
+ getExtraFiles(): SourceFile[] {
31
+ return []
32
+ }
33
+ getRequiredImports(language: Language): SourceImport[] {
34
+ const imports: SourceImport[] = []
35
+ if (language === 'c++') {
36
+ imports.push({
37
+ language: 'c++',
38
+ name: 'exception',
39
+ space: 'system',
40
+ })
41
+ }
42
+ return imports
43
+ }
44
+ }
@@ -0,0 +1,167 @@
1
+ import type { Language } from '../../getPlatformSpecs.js'
2
+ import { escapeCppName, toReferenceType } from '../helpers.js'
3
+ import { Parameter } from '../Parameter.js'
4
+ import { type SourceFile, type SourceImport } from '../SourceFile.js'
5
+ import { PromiseType } from './PromiseType.js'
6
+ import type { GetCodeOptions, NamedType, Type, TypeKind } from './Type.js'
7
+
8
+ export interface GetFunctionCodeOptions extends GetCodeOptions {
9
+ includeNameInfo?: boolean
10
+ }
11
+
12
+ export class FunctionType implements Type {
13
+ readonly returnType: Type
14
+ readonly parameters: NamedType[]
15
+
16
+ constructor(returnType: Type, parameters: NamedType[], isSync = false) {
17
+ if (returnType.kind === 'void' || isSync) {
18
+ // void callbacks are async, but we don't care about the result.
19
+ this.returnType = returnType
20
+ } else {
21
+ // non-void callbacks are async and need to be awaited to get the result from JS.
22
+ this.returnType = new PromiseType(returnType)
23
+ }
24
+ this.parameters = parameters
25
+
26
+ if (isSync && returnType.kind === 'void') {
27
+ throw new Error(
28
+ `Function \`${this.jsName}\` cannot be sync (\`Sync<...>\`) AND return \`void\`, as this is ambiguous. ` +
29
+ `Either return a value (even if it's just a \`boolean\`) to keep it sync, or make it async.`
30
+ )
31
+ }
32
+ }
33
+
34
+ get specializationName(): string {
35
+ return (
36
+ 'Func_' +
37
+ [this.returnType, ...this.parameters]
38
+ .map((p) => escapeCppName(p.getCode('c++')))
39
+ .join('_')
40
+ )
41
+ }
42
+
43
+ get jsName(): string {
44
+ const paramsJs = this.parameters
45
+ .map((p) => `${p.name}: ${p.kind}`)
46
+ .join(', ')
47
+ const returnType = this.returnType.getCode('c++')
48
+ return `(${paramsJs}) => ${returnType}`
49
+ }
50
+
51
+ get canBePassedByReference(): boolean {
52
+ // It's a function<..>, heavy to copy.
53
+ return true
54
+ }
55
+
56
+ get kind(): TypeKind {
57
+ return 'function'
58
+ }
59
+
60
+ /**
61
+ * For a function, get the forward recreation of it:
62
+ * If variable is called `func`, this would return:
63
+ * ```cpp
64
+ * [func = std::move(func)](Params... params) -> ReturnType {
65
+ * return func(params...);
66
+ * }
67
+ * ```
68
+ */
69
+ getForwardRecreationCode(variableName: string, language: Language): string {
70
+ const returnType = this.returnType.getCode(language)
71
+ const parameters = this.parameters
72
+ .map((p) => new Parameter(p.name, p))
73
+ .map((p) => p.getCode('c++'))
74
+ const forwardedParameters = this.parameters.map(
75
+ (p) => `std::forward<decltype(${p.name})>(${p.name})`
76
+ )
77
+
78
+ switch (language) {
79
+ case 'c++':
80
+ const closure = `[${variableName} = std::move(${variableName})]`
81
+ const signature = `(${parameters.join(', ')}) -> ${returnType}`
82
+ const body = `{ return ${variableName}(${forwardedParameters.join(', ')}); }`
83
+ return `${closure} ${signature} ${body}`
84
+ default:
85
+ throw new Error(
86
+ `Language ${language} is not yet supported for function forward recreations!`
87
+ )
88
+ }
89
+ }
90
+
91
+ getCppFunctionPointerType(name: string, includeNameInfo = true): string {
92
+ const params = this.parameters
93
+ .map((p) => {
94
+ const type = p.getCode('c++')
95
+ const code = p.canBePassedByReference ? toReferenceType(type) : type
96
+ if (includeNameInfo) return `${code} /* ${p.name} */`
97
+ else return code
98
+ })
99
+ .join(', ')
100
+ const returnType = this.returnType.getCode('c++')
101
+ return `${returnType}(*${name})(${params})`
102
+ }
103
+
104
+ getCode(language: Language, options: GetFunctionCodeOptions = {}): string {
105
+ const includeNameInfo = options.includeNameInfo ?? true
106
+ switch (language) {
107
+ case 'c++': {
108
+ const params = this.parameters
109
+ .map((p) => {
110
+ const type = p.getCode('c++', options)
111
+ const code = p.canBePassedByReference ? toReferenceType(type) : type
112
+ if (includeNameInfo) return `${code} /* ${p.name} */`
113
+ else return code
114
+ })
115
+ .join(', ')
116
+ const returnType = this.returnType.getCode(language, options)
117
+ return `std::function<${returnType}(${params})>`
118
+ }
119
+ case 'swift': {
120
+ const params = this.parameters
121
+ .map((p) => {
122
+ if (includeNameInfo)
123
+ return `_ ${p.escapedName}: ${p.getCode(language, options)}`
124
+ else return p.getCode(language, options)
125
+ })
126
+ .join(', ')
127
+ const returnType = this.returnType.getCode(language, options)
128
+ return `(${params}) -> ${returnType}`
129
+ }
130
+ case 'kotlin': {
131
+ const params = this.parameters
132
+ .map((p) => {
133
+ if (includeNameInfo)
134
+ return `${p.escapedName}: ${p.getCode(language, options)}`
135
+ else return p.getCode(language, options)
136
+ })
137
+ .join(', ')
138
+ const returnType = this.returnType.getCode(language, options)
139
+ return `(${params}) -> ${returnType}`
140
+ }
141
+ default:
142
+ throw new Error(
143
+ `Language ${language} is not yet supported for FunctionType!`
144
+ )
145
+ }
146
+ }
147
+ getExtraFiles(): SourceFile[] {
148
+ return [
149
+ ...this.returnType.getExtraFiles(),
150
+ ...this.parameters.flatMap((p) => p.getExtraFiles()),
151
+ ]
152
+ }
153
+ getRequiredImports(language: Language): SourceImport[] {
154
+ const imports: SourceImport[] = [
155
+ ...this.returnType.getRequiredImports(language),
156
+ ...this.parameters.flatMap((p) => p.getRequiredImports(language)),
157
+ ]
158
+ if (language === 'c++') {
159
+ imports.push({
160
+ language: 'c++',
161
+ name: 'functional',
162
+ space: 'system',
163
+ })
164
+ }
165
+ return imports
166
+ }
167
+ }