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,54 @@
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 HybridObjectBaseType implements Type {
7
+ constructor() {}
8
+
9
+ get canBePassedByReference(): boolean {
10
+ // It's a shared_ptr<..>, no copy.
11
+ return true
12
+ }
13
+
14
+ get kind(): TypeKind {
15
+ return 'hybrid-object-base'
16
+ }
17
+
18
+ getCode(language: Language): string {
19
+ switch (language) {
20
+ case 'c++':
21
+ return `std::shared_ptr<HybridObject>`
22
+ default:
23
+ throw new Error(
24
+ `The base type \`HybridObject\` cannot be used directly in ${language} yet. Use a specific derived class of \`HybridObject\` instead!`
25
+ )
26
+ }
27
+ }
28
+ getExtraFiles(): SourceFile[] {
29
+ return []
30
+ }
31
+ getRequiredImports(language: Language): SourceImport[] {
32
+ const imports: SourceImport[] = []
33
+ if (language === 'c++') {
34
+ imports.push(
35
+ {
36
+ language: 'c++',
37
+ name: 'memory',
38
+ space: 'system',
39
+ },
40
+ {
41
+ name: `NitroModules/HybridObject.hpp`,
42
+ forwardDeclaration: getForwardDeclaration(
43
+ 'class',
44
+ 'HybridObject',
45
+ 'margelo::nitro'
46
+ ),
47
+ language: 'c++',
48
+ space: 'system',
49
+ }
50
+ )
51
+ }
52
+ return imports
53
+ }
54
+ }
@@ -0,0 +1,198 @@
1
+ import { type NitroConfig } from '../../config/NitroConfig.js'
2
+ import type { Language } from '../../getPlatformSpecs.js'
3
+ import { getForwardDeclaration } from '../c++/getForwardDeclaration.js'
4
+ import { getHybridObjectName } from '../getHybridObjectName.js'
5
+ import type { HybridObjectSpec } from '../HybridObjectSpec.js'
6
+ import type { SourceFile, SourceImport } from '../SourceFile.js'
7
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js'
8
+
9
+ interface GetHybridObjectCodeOptions extends GetCodeOptions {
10
+ mode?: 'strong' | 'weak'
11
+ }
12
+
13
+ export class HybridObjectType implements Type {
14
+ readonly hybridObjectName: string
15
+ readonly implementationLanguage: Language
16
+ readonly baseTypes: HybridObjectType[]
17
+ readonly sourceConfig: NitroConfig
18
+
19
+ constructor(
20
+ hybridObjectName: string,
21
+ implementationLanguage: Language,
22
+ baseTypes: HybridObjectType[],
23
+ sourceConfig: NitroConfig
24
+ )
25
+ constructor(spec: HybridObjectSpec)
26
+ constructor(
27
+ ...args:
28
+ | [
29
+ hybridObjectName: string,
30
+ implementationLanguage: Language,
31
+ baseTypes: HybridObjectType[],
32
+ sourceConfig: NitroConfig,
33
+ ]
34
+ | [HybridObjectSpec]
35
+ ) {
36
+ if (args.length === 1) {
37
+ const [spec] = args
38
+
39
+ this.hybridObjectName = spec.name
40
+ this.implementationLanguage = spec.language
41
+ this.sourceConfig = spec.config
42
+ this.baseTypes = spec.baseTypes.map((b) => new HybridObjectType(b))
43
+ } else {
44
+ const [
45
+ hybridObjectName,
46
+ implementationLanguage,
47
+ baseTypes,
48
+ sourceConfig,
49
+ ] = args
50
+
51
+ this.hybridObjectName = hybridObjectName
52
+ this.implementationLanguage = implementationLanguage
53
+ this.baseTypes = baseTypes
54
+ this.sourceConfig = sourceConfig
55
+ }
56
+
57
+ if (this.hybridObjectName.startsWith('__')) {
58
+ throw new Error(
59
+ `HybridObject name cannot start with two underscores (__)! (In ${this.hybridObjectName})`
60
+ )
61
+ }
62
+ }
63
+
64
+ get canBePassedByReference(): boolean {
65
+ // It's a shared_ptr<..>, no copy.
66
+ return true
67
+ }
68
+
69
+ get kind(): TypeKind {
70
+ return 'hybrid-object'
71
+ }
72
+
73
+ getCode(
74
+ language: Language,
75
+ options: GetHybridObjectCodeOptions = {}
76
+ ): string {
77
+ const name = getHybridObjectName(this.hybridObjectName)
78
+ const mode = options.mode ?? 'strong'
79
+ const fullyQualified =
80
+ options.fullyQualified ?? this.sourceConfig.isExternalConfig
81
+
82
+ switch (language) {
83
+ case 'c++': {
84
+ const ptrType = mode === 'strong' ? 'std::shared_ptr' : 'std::weak_ptr'
85
+ if (fullyQualified) {
86
+ const fullName = this.sourceConfig.getCxxNamespace(
87
+ 'c++',
88
+ name.HybridTSpec
89
+ )
90
+ return `${ptrType}<${fullName}>`
91
+ } else {
92
+ return `${ptrType}<${name.HybridTSpec}>`
93
+ }
94
+ }
95
+ case 'swift': {
96
+ return `(any ${name.HybridTSpec})`
97
+ }
98
+ case 'kotlin': {
99
+ if (fullyQualified) {
100
+ // full qualified name: "com.margelo.nitro.image.Image"
101
+ return this.sourceConfig.getAndroidPackage(
102
+ 'java/kotlin',
103
+ name.HybridTSpec
104
+ )
105
+ } else {
106
+ // "Image"
107
+ return name.HybridTSpec
108
+ }
109
+ }
110
+ default:
111
+ throw new Error(
112
+ `Language ${language} is not yet supported for HybridObjectType!`
113
+ )
114
+ }
115
+ }
116
+ getExtraFiles(): SourceFile[] {
117
+ return []
118
+ }
119
+
120
+ private getExternalCxxImportName(): string {
121
+ // TODO: We currently don't have a xplat way of handling import paths, therefore iosModuleName and androidCxxLibName need to have the same value.
122
+ if (
123
+ this.sourceConfig.getIosModuleName() !==
124
+ this.sourceConfig.getAndroidCxxLibName()
125
+ ) {
126
+ throw new Error(
127
+ `Cannot import external HybridObject "${this.hybridObjectName}" if it's nitro.json's iosModuleName and androidCxxLibName are not the same value!`
128
+ )
129
+ }
130
+ return this.sourceConfig.getIosModuleName()
131
+ }
132
+
133
+ getRequiredImports(language: Language): SourceImport[] {
134
+ const name = getHybridObjectName(this.hybridObjectName)
135
+ const cxxNamespace = this.sourceConfig.getCxxNamespace('c++')
136
+ const imports: SourceImport[] = []
137
+
138
+ switch (language) {
139
+ case 'c++': {
140
+ imports.push({
141
+ language: 'c++',
142
+ name: 'memory',
143
+ space: 'system',
144
+ })
145
+ if (this.sourceConfig.isExternalConfig) {
146
+ const cxxImport = this.getExternalCxxImportName()
147
+ imports.push({
148
+ name: `${cxxImport}/${name.HybridTSpec}.hpp`,
149
+ forwardDeclaration: getForwardDeclaration(
150
+ 'class',
151
+ name.HybridTSpec,
152
+ cxxNamespace
153
+ ),
154
+ language: 'c++',
155
+ space: 'system',
156
+ })
157
+ } else {
158
+ imports.push({
159
+ name: `${name.HybridTSpec}.hpp`,
160
+ forwardDeclaration: getForwardDeclaration(
161
+ 'class',
162
+ name.HybridTSpec,
163
+ cxxNamespace
164
+ ),
165
+ language: 'c++',
166
+ space: 'user',
167
+ })
168
+ }
169
+ break
170
+ }
171
+ case 'kotlin': {
172
+ if (this.sourceConfig.isExternalConfig) {
173
+ imports.push({
174
+ language: 'kotlin',
175
+ name: this.sourceConfig.getAndroidPackage(
176
+ 'java/kotlin',
177
+ name.HybridTSpec
178
+ ),
179
+ space: 'system',
180
+ })
181
+ }
182
+ break
183
+ }
184
+ case 'swift': {
185
+ if (this.sourceConfig.isExternalConfig) {
186
+ imports.push({
187
+ language: 'swift',
188
+ name: this.sourceConfig.getIosModuleName(),
189
+ space: 'system',
190
+ })
191
+ }
192
+ break
193
+ }
194
+ }
195
+
196
+ return imports
197
+ }
198
+ }
@@ -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 MapType implements Type {
7
+ get canBePassedByReference(): boolean {
8
+ // It's a shared_ptr<..>, no ref.
9
+ return true
10
+ }
11
+
12
+ get kind(): TypeKind {
13
+ return 'map'
14
+ }
15
+
16
+ getCode(language: Language): string {
17
+ switch (language) {
18
+ case 'c++':
19
+ return 'std::shared_ptr<AnyMap>'
20
+ case 'swift':
21
+ return 'AnyMap'
22
+ case 'kotlin':
23
+ return 'AnyMap'
24
+ default:
25
+ throw new Error(
26
+ `Language ${language} is not yet supported for MapType!`
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/AnyMap.hpp',
38
+ forwardDeclaration: getForwardDeclaration(
39
+ 'class',
40
+ 'AnyMap',
41
+ 'NitroModules'
42
+ ),
43
+ language: 'c++',
44
+ space: 'system',
45
+ })
46
+ }
47
+ return imports
48
+ }
49
+ }
@@ -0,0 +1,33 @@
1
+ import type { Language } from '../../getPlatformSpecs.js'
2
+ import { escapeCppName } from '../helpers.js'
3
+ import type { SourceFile, SourceImport } from '../SourceFile.js'
4
+ import type { GetCodeOptions, NamedType, Type, TypeKind } from './Type.js'
5
+
6
+ export class NamedWrappingType<T extends Type> implements NamedType {
7
+ readonly type: T
8
+ readonly name: string
9
+
10
+ constructor(name: string, type: T) {
11
+ this.name = name
12
+ this.type = type
13
+ }
14
+
15
+ get escapedName(): string {
16
+ return escapeCppName(this.name)
17
+ }
18
+ get kind(): TypeKind {
19
+ return this.type.kind
20
+ }
21
+ get canBePassedByReference(): boolean {
22
+ return this.type.canBePassedByReference
23
+ }
24
+ getCode(language: Language, options?: GetCodeOptions): string {
25
+ return this.type.getCode(language, options)
26
+ }
27
+ getExtraFiles(): SourceFile[] {
28
+ return this.type.getExtraFiles()
29
+ }
30
+ getRequiredImports(language: Language): SourceImport[] {
31
+ return this.type.getRequiredImports(language)
32
+ }
33
+ }
@@ -0,0 +1,30 @@
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 NullType implements Type {
6
+ get canBePassedByReference(): boolean {
7
+ // It's a primitive.
8
+ return false
9
+ }
10
+ get kind(): TypeKind {
11
+ return 'null'
12
+ }
13
+
14
+ getCode(language: Language): string {
15
+ switch (language) {
16
+ case 'c++':
17
+ return 'std::nullptr_t'
18
+ default:
19
+ throw new Error(
20
+ `Language ${language} is not yet supported for NullType!`
21
+ )
22
+ }
23
+ }
24
+ getExtraFiles(): SourceFile[] {
25
+ return []
26
+ }
27
+ getRequiredImports(): SourceImport[] {
28
+ return []
29
+ }
30
+ }
@@ -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 NumberType implements Type {
6
+ get canBePassedByReference(): boolean {
7
+ // It's a primitive
8
+ return false
9
+ }
10
+ get kind(): TypeKind {
11
+ return 'number'
12
+ }
13
+
14
+ getCode(language: Language): string {
15
+ switch (language) {
16
+ case 'c++':
17
+ return 'double'
18
+ case 'swift':
19
+ return 'Double'
20
+ case 'kotlin':
21
+ return 'Double'
22
+ default:
23
+ throw new Error(
24
+ `Language ${language} is not yet supported for NumberType!`
25
+ )
26
+ }
27
+ }
28
+ getExtraFiles(): SourceFile[] {
29
+ return []
30
+ }
31
+ getRequiredImports(): SourceImport[] {
32
+ return []
33
+ }
34
+ }
@@ -0,0 +1,66 @@
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 OptionalType implements Type {
6
+ readonly wrappingType: Type
7
+
8
+ constructor(wrappingType: Type) {
9
+ this.wrappingType = wrappingType
10
+ }
11
+
12
+ get canBePassedByReference(): boolean {
13
+ // depends whether the wrapping type is heavy to copy or not.
14
+ return this.wrappingType.canBePassedByReference
15
+ }
16
+ get kind(): TypeKind {
17
+ return 'optional'
18
+ }
19
+ get needsBraces(): boolean {
20
+ switch (this.wrappingType.kind) {
21
+ case 'function':
22
+ return true
23
+ default:
24
+ return false
25
+ }
26
+ }
27
+
28
+ getCode(language: Language, options?: GetCodeOptions): string {
29
+ const wrapping = this.wrappingType.getCode(language, options)
30
+ switch (language) {
31
+ case 'c++':
32
+ return `std::optional<${wrapping}>`
33
+ case 'swift':
34
+ if (this.needsBraces) {
35
+ return `(${wrapping})?`
36
+ } else {
37
+ return `${wrapping}?`
38
+ }
39
+ case 'kotlin':
40
+ if (this.needsBraces) {
41
+ return `(${wrapping})?`
42
+ } else {
43
+ return `${wrapping}?`
44
+ }
45
+ default:
46
+ throw new Error(
47
+ `Language ${language} is not yet supported for OptionalType!`
48
+ )
49
+ }
50
+ }
51
+ getExtraFiles(): SourceFile[] {
52
+ return this.wrappingType.getExtraFiles()
53
+ }
54
+ getRequiredImports(language: Language): SourceImport[] {
55
+ const imports: SourceImport[] =
56
+ this.wrappingType.getRequiredImports(language)
57
+ if (language === 'c++') {
58
+ imports.push({
59
+ language: 'c++',
60
+ name: 'optional',
61
+ space: 'system',
62
+ })
63
+ }
64
+ return imports
65
+ }
66
+ }
@@ -0,0 +1,72 @@
1
+ import type { Language } from '../../getPlatformSpecs.js'
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js'
3
+ import { ErrorType } from './ErrorType.js'
4
+ import { FunctionType } from './FunctionType.js'
5
+ import { NamedWrappingType } from './NamedWrappingType.js'
6
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js'
7
+ import { VoidType } from './VoidType.js'
8
+
9
+ export class PromiseType implements Type {
10
+ readonly resultingType: Type
11
+ readonly errorType: Type
12
+
13
+ constructor(resultingType: Type) {
14
+ this.resultingType = resultingType
15
+ this.errorType = new ErrorType()
16
+ }
17
+
18
+ get canBePassedByReference(): boolean {
19
+ // It's a future<..>, it cannot be copied.
20
+ return true
21
+ }
22
+ get kind(): TypeKind {
23
+ return 'promise'
24
+ }
25
+
26
+ get resolverFunction(): FunctionType {
27
+ if (this.resultingType.kind === 'void') {
28
+ return new FunctionType(new VoidType(), [])
29
+ } else {
30
+ return new FunctionType(new VoidType(), [
31
+ new NamedWrappingType('value', this.resultingType),
32
+ ])
33
+ }
34
+ }
35
+
36
+ get rejecterFunction(): FunctionType {
37
+ return new FunctionType(new VoidType(), [
38
+ new NamedWrappingType('error', this.errorType),
39
+ ])
40
+ }
41
+
42
+ getCode(language: Language, options?: GetCodeOptions): string {
43
+ const resultingCode = this.resultingType.getCode(language, options)
44
+ switch (language) {
45
+ case 'c++':
46
+ return `std::shared_ptr<Promise<${resultingCode}>>`
47
+ case 'swift':
48
+ return `Promise<${resultingCode}>`
49
+ case 'kotlin':
50
+ return `Promise<${resultingCode}>`
51
+ default:
52
+ throw new Error(
53
+ `Language ${language} is not yet supported for PromiseType!`
54
+ )
55
+ }
56
+ }
57
+ getExtraFiles(): SourceFile[] {
58
+ return this.resultingType.getExtraFiles()
59
+ }
60
+ getRequiredImports(language: Language): SourceImport[] {
61
+ const imports: SourceImport[] =
62
+ this.resultingType.getRequiredImports(language)
63
+ if (language === 'c++') {
64
+ imports.push({
65
+ language: 'c++',
66
+ name: 'NitroModules/Promise.hpp',
67
+ space: 'system',
68
+ })
69
+ }
70
+ return imports
71
+ }
72
+ }
@@ -0,0 +1,56 @@
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 RecordType implements Type {
6
+ readonly keyType: Type
7
+ readonly valueType: Type
8
+
9
+ constructor(keyType: Type, valueType: Type) {
10
+ this.keyType = keyType
11
+ this.valueType = valueType
12
+ }
13
+
14
+ get canBePassedByReference(): boolean {
15
+ // It's a unordered_map<..>, heavy to copy.
16
+ return true
17
+ }
18
+ get kind(): TypeKind {
19
+ return 'record'
20
+ }
21
+
22
+ getCode(language: Language, options?: GetCodeOptions): string {
23
+ const keyCode = this.keyType.getCode(language, options)
24
+ const valueCode = this.valueType.getCode(language, options)
25
+
26
+ switch (language) {
27
+ case 'c++':
28
+ return `std::unordered_map<${keyCode}, ${valueCode}>`
29
+ case 'swift':
30
+ return `Dictionary<${keyCode}, ${valueCode}>`
31
+ case 'kotlin':
32
+ return `Map<${keyCode}, ${valueCode}>`
33
+ default:
34
+ throw new Error(
35
+ `Language ${language} is not yet supported for RecordType!`
36
+ )
37
+ }
38
+ }
39
+ getExtraFiles(): SourceFile[] {
40
+ return [...this.keyType.getExtraFiles(), ...this.valueType.getExtraFiles()]
41
+ }
42
+ getRequiredImports(language: Language): SourceImport[] {
43
+ const imports: SourceImport[] = [
44
+ ...this.keyType.getRequiredImports(language),
45
+ ...this.valueType.getRequiredImports(language),
46
+ ]
47
+ if (language === 'c++') {
48
+ imports.push({
49
+ language: 'c++',
50
+ name: 'unordered_map',
51
+ space: 'system',
52
+ })
53
+ }
54
+ return imports
55
+ }
56
+ }
@@ -0,0 +1,53 @@
1
+ import type { Language } from '../../getPlatformSpecs.js'
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js'
3
+ import { ErrorType } from './ErrorType.js'
4
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js'
5
+
6
+ export class ResultWrappingType implements Type {
7
+ readonly result: Type
8
+ readonly error: Type
9
+
10
+ constructor(result: Type) {
11
+ this.result = result
12
+ this.error = new ErrorType()
13
+ }
14
+
15
+ get canBePassedByReference(): boolean {
16
+ return this.result.canBePassedByReference
17
+ }
18
+
19
+ get kind(): TypeKind {
20
+ return 'result-wrapper'
21
+ }
22
+
23
+ getCode(language: Language, options?: GetCodeOptions): string {
24
+ const type = this.result.getCode(language, options)
25
+ switch (language) {
26
+ case 'c++':
27
+ return `Result<${type}>`
28
+ case 'swift':
29
+ return type
30
+ default:
31
+ throw new Error(
32
+ `Language ${language} is not yet supported for VariantType!`
33
+ )
34
+ }
35
+ }
36
+ getExtraFiles(): SourceFile[] {
37
+ return [...this.result.getExtraFiles(), ...this.error.getExtraFiles()]
38
+ }
39
+ getRequiredImports(language: Language): SourceImport[] {
40
+ const imports: SourceImport[] = [
41
+ ...this.result.getRequiredImports(language),
42
+ ...this.error.getRequiredImports(language),
43
+ ]
44
+ if (language === 'c++') {
45
+ imports.push({
46
+ language: 'c++',
47
+ name: 'NitroModules/Result.hpp',
48
+ space: 'system',
49
+ })
50
+ }
51
+ return imports
52
+ }
53
+ }
@@ -0,0 +1,44 @@
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 StringType implements Type {
6
+ get canBePassedByReference(): boolean {
7
+ // It's a string<..>, heavy to copy.
8
+ return true
9
+ }
10
+ get kind(): TypeKind {
11
+ return 'string'
12
+ }
13
+
14
+ getCode(language: Language): string {
15
+ switch (language) {
16
+ case 'c++':
17
+ return 'std::string'
18
+ case 'swift':
19
+ return 'String'
20
+ case 'kotlin':
21
+ return 'String'
22
+ default:
23
+ throw new Error(
24
+ `Language ${language} is not yet supported for StringType!`
25
+ )
26
+ }
27
+ }
28
+
29
+ getExtraFiles(): SourceFile[] {
30
+ return []
31
+ }
32
+
33
+ getRequiredImports(language: Language): SourceImport[] {
34
+ const imports: SourceImport[] = []
35
+ if (language === 'c++') {
36
+ imports.push({
37
+ language: 'c++',
38
+ name: 'string',
39
+ space: 'system',
40
+ })
41
+ }
42
+ return imports
43
+ }
44
+ }