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,43 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js'
2
+ import { createFileMetadataString } from '../../syntax/helpers.js'
3
+ import type { SourceFile } from '../../syntax/SourceFile.js'
4
+
5
+ export interface GradleFile extends Omit<SourceFile, 'language'> {
6
+ language: 'gradle'
7
+ }
8
+
9
+ export function createGradleExtension(): GradleFile {
10
+ const name = NitroConfig.current.getAndroidCxxLibName()
11
+
12
+ const code = `
13
+ ${createFileMetadataString(`${name}+autolinking.gradle`)}
14
+
15
+ /// This is a Gradle file that adds all files generated by Nitrogen
16
+ /// to the current Gradle project.
17
+ ///
18
+ /// To use it, add this to your build.gradle:
19
+ /// \`\`\`gradle
20
+ /// apply from: '../nitrogen/generated/android/${name}+autolinking.gradle'
21
+ /// \`\`\`
22
+
23
+ logger.warn("[NitroModules] 🔥 ${name} is boosted by nitro!")
24
+
25
+ android {
26
+ sourceSets {
27
+ main {
28
+ java.srcDirs += [
29
+ // Nitrogen files
30
+ "\${project.projectDir}/../nitrogen/generated/android/kotlin"
31
+ ]
32
+ }
33
+ }
34
+ }
35
+ `.trim()
36
+ return {
37
+ content: code,
38
+ language: 'gradle',
39
+ name: `${name}+autolinking.gradle`,
40
+ platform: 'android',
41
+ subdirectory: [],
42
+ }
43
+ }
@@ -0,0 +1,174 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js'
2
+ import { createCppHybridObjectRegistration } from '../../syntax/c++/CppHybridObjectRegistration.js'
3
+ import { includeHeader } from '../../syntax/c++/includeNitroHeader.js'
4
+ import {
5
+ createFileMetadataString,
6
+ isNotDuplicate,
7
+ } from '../../syntax/helpers.js'
8
+ import { getJNINativeRegistrations } from '../../syntax/kotlin/JNINativeRegistrations.js'
9
+ import { createJNIHybridObjectRegistration } from '../../syntax/kotlin/KotlinHybridObjectRegistration.js'
10
+ import type { SourceFile, SourceImport } from '../../syntax/SourceFile.js'
11
+ import { indent } from '../../utils.js'
12
+ import { getBuildingWithGeneratedCmakeDefinition } from './createCMakeExtension.js'
13
+
14
+ export function createHybridObjectIntializer(): SourceFile[] {
15
+ const cxxNamespace = NitroConfig.current.getCxxNamespace('c++')
16
+ const cppLibName = NitroConfig.current.getAndroidCxxLibName()
17
+ const javaNamespace = NitroConfig.current.getAndroidPackage('java/kotlin')
18
+ const autolinkingClassName = `${NitroConfig.current.getAndroidCxxLibName()}OnLoad`
19
+
20
+ const jniRegistrations = getJNINativeRegistrations()
21
+ .map((r) => `${r.namespace}::${r.className}::registerNatives();`)
22
+ .filter(isNotDuplicate)
23
+
24
+ const autolinkedHybridObjects =
25
+ NitroConfig.current.getAutolinkedHybridObjects()
26
+
27
+ const cppHybridObjectImports: SourceImport[] = []
28
+ const cppRegistrations: string[] = []
29
+ for (const hybridObjectName of Object.keys(autolinkedHybridObjects)) {
30
+ const config = autolinkedHybridObjects[hybridObjectName]
31
+
32
+ if (config?.cpp != null) {
33
+ // Autolink a C++ HybridObject!
34
+ const { cppCode, requiredImports } = createCppHybridObjectRegistration({
35
+ hybridObjectName: hybridObjectName,
36
+ cppClassName: config.cpp,
37
+ })
38
+ cppHybridObjectImports.push(...requiredImports)
39
+ cppRegistrations.push(cppCode)
40
+ }
41
+ if (config?.kotlin != null) {
42
+ // Autolink a Kotlin HybridObject through JNI/C++!
43
+ const { cppCode, requiredImports } = createJNIHybridObjectRegistration({
44
+ hybridObjectName: hybridObjectName,
45
+ jniClassName: config.kotlin,
46
+ })
47
+ cppHybridObjectImports.push(...requiredImports)
48
+ cppRegistrations.push(cppCode)
49
+ }
50
+ }
51
+
52
+ const buildingWithDefinition = getBuildingWithGeneratedCmakeDefinition()
53
+
54
+ const includes = [
55
+ ...getJNINativeRegistrations().map((r) => includeHeader(r.import)),
56
+ ...cppHybridObjectImports.map((i) => includeHeader(i)),
57
+ ]
58
+ .filter(isNotDuplicate)
59
+ .join('\n')
60
+
61
+ const hppCode = `
62
+ ${createFileMetadataString(`${autolinkingClassName}.hpp`)}
63
+
64
+ #include <jni.h>
65
+ #include <NitroModules/NitroDefines.hpp>
66
+
67
+ namespace ${cxxNamespace} {
68
+
69
+ /**
70
+ * Initializes the native (C++) part of ${cppLibName}, and autolinks all Hybrid Objects.
71
+ * Call this in your \`JNI_OnLoad\` function (probably inside \`cpp-adapter.cpp\`).
72
+ * Example:
73
+ * \`\`\`cpp (cpp-adapter.cpp)
74
+ * JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
75
+ * return ${cxxNamespace}::initialize(vm);
76
+ * }
77
+ * \`\`\`
78
+ */
79
+ int initialize(JavaVM* vm);
80
+
81
+ } // namespace ${cxxNamespace}
82
+
83
+ `
84
+ const cppCode = `
85
+ ${createFileMetadataString(`${autolinkingClassName}.cpp`)}
86
+
87
+ #ifndef ${buildingWithDefinition}
88
+ #error ${autolinkingClassName}.cpp is not being built with the autogenerated CMakeLists.txt project. Is a different CMakeLists.txt building this?
89
+ #endif
90
+
91
+ #include "${autolinkingClassName}.hpp"
92
+
93
+ #include <jni.h>
94
+ #include <fbjni/fbjni.h>
95
+ #include <NitroModules/HybridObjectRegistry.hpp>
96
+
97
+ ${includes}
98
+
99
+ namespace ${cxxNamespace} {
100
+
101
+ int initialize(JavaVM* vm) {
102
+ using namespace margelo::nitro;
103
+ using namespace ${cxxNamespace};
104
+ using namespace facebook;
105
+
106
+ return facebook::jni::initialize(vm, [] {
107
+ // Register native JNI methods
108
+ ${indent(jniRegistrations.join('\n'), ' ')}
109
+
110
+ // Register Nitro Hybrid Objects
111
+ ${indent(cppRegistrations.join('\n'), ' ')}
112
+ });
113
+ }
114
+
115
+ } // namespace ${cxxNamespace}
116
+ `.trim()
117
+
118
+ const kotlinCode = `
119
+ ${createFileMetadataString(`${autolinkingClassName}.kt`)}
120
+
121
+ package ${javaNamespace}
122
+
123
+ import android.util.Log
124
+
125
+ internal class ${autolinkingClassName} {
126
+ companion object {
127
+ private const val TAG = "${autolinkingClassName}"
128
+ private var didLoad = false
129
+ /**
130
+ * Initializes the native part of "${cppLibName}".
131
+ * This method is idempotent and can be called more than once.
132
+ */
133
+ @JvmStatic
134
+ fun initializeNative() {
135
+ if (didLoad) return
136
+ try {
137
+ Log.i(TAG, "Loading ${cppLibName} C++ library...")
138
+ System.loadLibrary("${cppLibName}")
139
+ Log.i(TAG, "Successfully loaded ${cppLibName} C++ library!")
140
+ didLoad = true
141
+ } catch (e: Error) {
142
+ Log.e(TAG, "Failed to load ${cppLibName} C++ library! Is it properly installed and linked? " +
143
+ "Is the name correct? (see \`CMakeLists.txt\`, at \`add_library(...)\`)", e)
144
+ throw e
145
+ }
146
+ }
147
+ }
148
+ }
149
+ `.trim()
150
+
151
+ return [
152
+ {
153
+ content: hppCode,
154
+ language: 'c++',
155
+ name: `${autolinkingClassName}.hpp`,
156
+ platform: 'android',
157
+ subdirectory: [],
158
+ },
159
+ {
160
+ content: cppCode,
161
+ language: 'c++',
162
+ name: `${autolinkingClassName}.cpp`,
163
+ platform: 'android',
164
+ subdirectory: [],
165
+ },
166
+ {
167
+ content: kotlinCode,
168
+ language: 'kotlin',
169
+ name: `${autolinkingClassName}.kt`,
170
+ platform: 'android',
171
+ subdirectory: ['kotlin', ...javaNamespace.split('.')],
172
+ },
173
+ ]
174
+ }
@@ -0,0 +1,28 @@
1
+ import type { SourceFile, SourceImport } from '../syntax/SourceFile.js'
2
+ import { createCMakeExtension } from './android/createCMakeExtension.js'
3
+ import { createGradleExtension } from './android/createGradleExtension.js'
4
+ import { createHybridObjectIntializer } from './android/createHybridObjectInitializer.js'
5
+ import type { Autolinking } from './Autolinking.js'
6
+
7
+ interface JNIHybridRegistration {
8
+ sourceImport: SourceImport
9
+ registrationCode: string
10
+ }
11
+
12
+ interface AndroidAutolinking extends Autolinking {
13
+ jniHybridRegistrations: JNIHybridRegistration[]
14
+ }
15
+
16
+ export function createAndroidAutolinking(
17
+ allFiles: SourceFile[]
18
+ ): AndroidAutolinking {
19
+ const cmakeExtension = createCMakeExtension(allFiles)
20
+ const gradleExtension = createGradleExtension()
21
+ const hybridObjectInitializer = createHybridObjectIntializer()
22
+
23
+ return {
24
+ platform: 'android',
25
+ jniHybridRegistrations: [],
26
+ sourceFiles: [cmakeExtension, gradleExtension, ...hybridObjectInitializer],
27
+ }
28
+ }
@@ -0,0 +1,24 @@
1
+ import type { Autolinking } from './Autolinking.js'
2
+ import { createHybridObjectIntializer } from './ios/createHybridObjectInitializer.js'
3
+ import { createPodspecRubyExtension } from './ios/createPodspecRubyExtension.js'
4
+ import { createSwiftCxxBridge } from './ios/createSwiftCxxBridge.js'
5
+ import { createSwiftUmbrellaHeader } from './ios/createSwiftUmbrellaHeader.js'
6
+
7
+ interface IOSAutolinking extends Autolinking {}
8
+
9
+ export function createIOSAutolinking(): IOSAutolinking {
10
+ const podspecExtension = createPodspecRubyExtension()
11
+ const swiftCxxBridge = createSwiftCxxBridge()
12
+ const swiftUmbrellaHeader = createSwiftUmbrellaHeader()
13
+ const hybridObjectInitializer = createHybridObjectIntializer()
14
+
15
+ return {
16
+ platform: 'ios',
17
+ sourceFiles: [
18
+ podspecExtension,
19
+ ...swiftCxxBridge,
20
+ swiftUmbrellaHeader,
21
+ ...hybridObjectInitializer,
22
+ ],
23
+ }
24
+ }
@@ -0,0 +1,112 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js'
2
+ import { createCppHybridObjectRegistration } from '../../syntax/c++/CppHybridObjectRegistration.js'
3
+ import { includeHeader } from '../../syntax/c++/includeNitroHeader.js'
4
+ import { createFileMetadataString } from '../../syntax/helpers.js'
5
+ import type { SourceFile, SourceImport } from '../../syntax/SourceFile.js'
6
+ import { createSwiftHybridObjectRegistration } from '../../syntax/swift/SwiftHybridObjectRegistration.js'
7
+ import { indent } from '../../utils.js'
8
+ import { getUmbrellaHeaderName } from './createSwiftUmbrellaHeader.js'
9
+
10
+ type ObjcFile = Omit<SourceFile, 'language'> & { language: 'objective-c++' }
11
+ type SwiftFile = Omit<SourceFile, 'language'> & { language: 'swift' }
12
+
13
+ export function createHybridObjectIntializer(): [ObjcFile, SwiftFile] | [] {
14
+ const autolinkingClassName = `${NitroConfig.current.getIosModuleName()}Autolinking`
15
+ const umbrellaHeaderName = getUmbrellaHeaderName()
16
+ const bridgeNamespace = NitroConfig.current.getSwiftBridgeNamespace('swift')
17
+
18
+ const autolinkedHybridObjects =
19
+ NitroConfig.current.getAutolinkedHybridObjects()
20
+
21
+ const swiftFunctions: string[] = []
22
+ const cppRegistrations: string[] = []
23
+ const cppImports: SourceImport[] = []
24
+ let containsSwiftObjects = false
25
+ for (const hybridObjectName of Object.keys(autolinkedHybridObjects)) {
26
+ const config = autolinkedHybridObjects[hybridObjectName]
27
+
28
+ if (config?.cpp != null) {
29
+ // Autolink a C++ HybridObject!
30
+ const { cppCode, requiredImports } = createCppHybridObjectRegistration({
31
+ hybridObjectName: hybridObjectName,
32
+ cppClassName: config.cpp,
33
+ })
34
+ cppImports.push(...requiredImports)
35
+ cppRegistrations.push(cppCode)
36
+ }
37
+ if (config?.swift != null) {
38
+ // Autolink a Swift HybridObject!
39
+ containsSwiftObjects = true
40
+ const { cppCode, requiredImports, swiftFunction } =
41
+ createSwiftHybridObjectRegistration({
42
+ hybridObjectName: hybridObjectName,
43
+ swiftClassName: config.swift,
44
+ })
45
+ cppImports.push(...requiredImports)
46
+ cppRegistrations.push(cppCode)
47
+ swiftFunctions.push(swiftFunction)
48
+ }
49
+ }
50
+
51
+ if (cppRegistrations.length === 0) {
52
+ // Nothing to autolink!
53
+ return []
54
+ }
55
+
56
+ const umbrellaImport = containsSwiftObjects
57
+ ? `#import "${umbrellaHeaderName}"`
58
+ : ''
59
+ const imports = cppImports.map((i) => includeHeader(i, true)).join('\n')
60
+
61
+ const objcCode = `
62
+ ${createFileMetadataString(`${autolinkingClassName}.mm`)}
63
+
64
+ #import <Foundation/Foundation.h>
65
+ #import <NitroModules/HybridObjectRegistry.hpp>
66
+ ${umbrellaImport}
67
+ #import <type_traits>
68
+
69
+ ${imports}
70
+
71
+ @interface ${autolinkingClassName} : NSObject
72
+ @end
73
+
74
+ @implementation ${autolinkingClassName}
75
+
76
+ + (void) load {
77
+ using namespace margelo::nitro;
78
+ using namespace ${NitroConfig.current.getCxxNamespace('c++')};
79
+
80
+ ${indent(cppRegistrations.join('\n'), ' ')}
81
+ }
82
+
83
+ @end
84
+ `.trim()
85
+
86
+ const swiftCode = `
87
+ ${createFileMetadataString(`${autolinkingClassName}.swift`)}
88
+
89
+ public final class ${autolinkingClassName} {
90
+ public typealias bridge = ${bridgeNamespace}
91
+
92
+ ${indent(swiftFunctions.join('\n\n'), ' ')}
93
+ }
94
+ `.trim()
95
+
96
+ return [
97
+ {
98
+ content: objcCode,
99
+ language: 'objective-c++',
100
+ name: `${autolinkingClassName}.mm`,
101
+ platform: 'ios',
102
+ subdirectory: [],
103
+ },
104
+ {
105
+ content: swiftCode,
106
+ language: 'swift',
107
+ name: `${autolinkingClassName}.swift`,
108
+ platform: 'ios',
109
+ subdirectory: [],
110
+ },
111
+ ]
112
+ }
@@ -0,0 +1,76 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js'
2
+ import { createFileMetadataString } from '../../syntax/helpers.js'
3
+ import type { SourceFile } from '../../syntax/SourceFile.js'
4
+
5
+ export interface RubyFile extends Omit<SourceFile, 'language'> {
6
+ language: 'ruby'
7
+ }
8
+
9
+ export function createPodspecRubyExtension(): RubyFile {
10
+ const name = NitroConfig.current.getIosModuleName()
11
+
12
+ const code = `
13
+ ${createFileMetadataString(`${name}+autolinking.rb`, '#')}
14
+
15
+ # This is a Ruby script that adds all files generated by Nitrogen
16
+ # to the given podspec.
17
+ #
18
+ # To use it, add this to your .podspec:
19
+ # \`\`\`ruby
20
+ # Pod::Spec.new do |spec|
21
+ # # ...
22
+ #
23
+ # # Add all files generated by Nitrogen
24
+ # load 'nitrogen/generated/ios/${name}+autolinking.rb'
25
+ # add_nitrogen_files(spec)
26
+ # end
27
+ # \`\`\`
28
+
29
+ def add_nitrogen_files(spec)
30
+ Pod::UI.puts "[NitroModules] 🔥 ${name} is boosted by nitro!"
31
+
32
+ spec.dependency "NitroModules"
33
+
34
+ current_source_files = Array(spec.attributes_hash['source_files'])
35
+ spec.source_files = current_source_files + [
36
+ # Generated cross-platform specs
37
+ "nitrogen/generated/shared/**/*.{h,hpp,c,cpp,swift}",
38
+ # Generated bridges for the cross-platform specs
39
+ "nitrogen/generated/ios/**/*.{h,hpp,c,cpp,mm,swift}",
40
+ ]
41
+
42
+ current_public_header_files = Array(spec.attributes_hash['public_header_files'])
43
+ spec.public_header_files = current_public_header_files + [
44
+ # Generated specs
45
+ "nitrogen/generated/shared/**/*.{h,hpp}",
46
+ # Swift to C++ bridging helpers
47
+ "nitrogen/generated/ios/${name}-Swift-Cxx-Bridge.hpp"
48
+ ]
49
+
50
+ current_private_header_files = Array(spec.attributes_hash['private_header_files'])
51
+ spec.private_header_files = current_private_header_files + [
52
+ # iOS specific specs
53
+ "nitrogen/generated/ios/c++/**/*.{h,hpp}",
54
+ # Views are framework-specific and should be private
55
+ "nitrogen/generated/shared/**/views/**/*"
56
+ ]
57
+
58
+ current_pod_target_xcconfig = spec.attributes_hash['pod_target_xcconfig'] || {}
59
+ spec.pod_target_xcconfig = current_pod_target_xcconfig.merge({
60
+ # Use C++ 20
61
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
62
+ # Enables C++ <-> Swift interop (by default it's only C)
63
+ "SWIFT_OBJC_INTEROP_MODE" => "objcxx",
64
+ # Enables stricter modular headers
65
+ "DEFINES_MODULE" => "YES",
66
+ })
67
+ end
68
+ `.trim()
69
+ return {
70
+ content: code,
71
+ language: 'ruby',
72
+ name: `${name}+autolinking.rb`,
73
+ platform: 'ios',
74
+ subdirectory: [],
75
+ }
76
+ }
@@ -0,0 +1,137 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js'
2
+ import { includeHeader } from '../../syntax/c++/includeNitroHeader.js'
3
+ import { getAllKnownTypes } from '../../syntax/createType.js'
4
+ import {
5
+ createFileMetadataString,
6
+ isNotDuplicate,
7
+ } from '../../syntax/helpers.js'
8
+ import type { SourceFile } from '../../syntax/SourceFile.js'
9
+ import { getReferencedTypes } from '../../syntax/getReferencedTypes.js'
10
+ import { SwiftCxxBridgedType } from '../../syntax/swift/SwiftCxxBridgedType.js'
11
+ import { filterDuplicateHelperBridges, indent } from '../../utils.js'
12
+ import { getTypeAs } from '../../syntax/types/getTypeAs.js'
13
+ import { HybridObjectType } from '../../syntax/types/HybridObjectType.js'
14
+ import { getForwardDeclaration } from '../../syntax/c++/getForwardDeclaration.js'
15
+ import { getHybridObjectName } from '../../syntax/getHybridObjectName.js'
16
+
17
+ export function createSwiftCxxBridge(): SourceFile[] {
18
+ const bridgeName = NitroConfig.current.getSwiftBridgeHeaderName()
19
+ const bridgeNamespace = NitroConfig.current.getSwiftBridgeNamespace('c++')
20
+
21
+ const types = getAllKnownTypes('swift').map((t) => new SwiftCxxBridgedType(t))
22
+
23
+ const bridges = types
24
+ .flatMap((t) => {
25
+ const referenced = getReferencedTypes(t.type)
26
+ return referenced.map((r) => {
27
+ const bridge = new SwiftCxxBridgedType(r)
28
+ return bridge.getRequiredBridge()
29
+ })
30
+ })
31
+ .filter((b) => b != null)
32
+ .flatMap((b) => [b, ...b?.dependencies])
33
+ .filter(filterDuplicateHelperBridges)
34
+ const headerHelperFunctions = bridges
35
+ .map((b) => `// pragma MARK: ${b.cxxType}\n${b.cxxHeader.code}`)
36
+ .filter(isNotDuplicate)
37
+ .join('\n\n')
38
+ const implementationHelperFunctions = bridges
39
+ .map((b) => {
40
+ if (b.cxxImplementation == null) return undefined
41
+ else return `// pragma MARK: ${b.cxxType}\n${b.cxxImplementation.code}`
42
+ })
43
+ .filter((c) => c != null)
44
+ .filter(isNotDuplicate)
45
+ .join('\n\n')
46
+
47
+ const requiredImportsHeader = bridges.flatMap(
48
+ (b) => b.cxxHeader.requiredIncludes
49
+ )
50
+ const includesHeader = requiredImportsHeader
51
+ .map((i) => includeHeader(i, true))
52
+ .filter(isNotDuplicate)
53
+ const forwardDeclarationsHeader = requiredImportsHeader
54
+ .map((i) => i.forwardDeclaration)
55
+ .filter((f) => f != null)
56
+ .filter(isNotDuplicate)
57
+
58
+ const includesImplementation = bridges
59
+ .flatMap((b) => b.cxxImplementation?.requiredIncludes)
60
+ .filter((i) => i != null)
61
+ .map((i) => includeHeader(i, true))
62
+ .filter(isNotDuplicate)
63
+
64
+ const forwardDeclaredSwiftTypes = types
65
+ .filter((t) => t.type.kind === 'hybrid-object')
66
+ .map((t) => {
67
+ const hybridObject = getTypeAs(t.type, HybridObjectType)
68
+ const hybridObjectModuleName =
69
+ hybridObject.sourceConfig.getIosModuleName()
70
+ const { HybridTSpecCxx } = getHybridObjectName(
71
+ hybridObject.hybridObjectName
72
+ )
73
+ return getForwardDeclaration(
74
+ 'class',
75
+ HybridTSpecCxx,
76
+ hybridObjectModuleName
77
+ )
78
+ })
79
+ .filter(isNotDuplicate)
80
+
81
+ const header = `
82
+ ${createFileMetadataString(`${bridgeName}.hpp`)}
83
+
84
+ #pragma once
85
+
86
+ // Forward declarations of C++ defined types
87
+ ${forwardDeclarationsHeader.sort().join('\n')}
88
+
89
+ // Forward declarations of Swift defined types
90
+ ${forwardDeclaredSwiftTypes.sort().join('\n')}
91
+
92
+ // Include C++ defined types
93
+ ${includesHeader.sort().join('\n')}
94
+
95
+ /**
96
+ * Contains specialized versions of C++ templated types so they can be accessed from Swift,
97
+ * as well as helper functions to interact with those C++ types from Swift.
98
+ */
99
+ namespace ${bridgeNamespace} {
100
+
101
+ ${indent(headerHelperFunctions, ' ')}
102
+
103
+ } // namespace ${bridgeNamespace}
104
+ `
105
+
106
+ const source = `
107
+ ${createFileMetadataString(`${bridgeName}.cpp`)}
108
+
109
+ #include "${bridgeName}.hpp"
110
+
111
+ // Include C++ implementation defined types
112
+ ${includesImplementation.sort().join('\n')}
113
+
114
+ namespace ${bridgeNamespace} {
115
+
116
+ ${indent(implementationHelperFunctions, ' ')}
117
+
118
+ } // namespace ${bridgeNamespace}
119
+ `
120
+
121
+ const files: SourceFile[] = []
122
+ files.push({
123
+ content: header,
124
+ language: 'c++',
125
+ name: `${bridgeName}.hpp`,
126
+ platform: 'ios',
127
+ subdirectory: [],
128
+ })
129
+ files.push({
130
+ content: source,
131
+ language: 'c++',
132
+ name: `${bridgeName}.cpp`,
133
+ platform: 'ios',
134
+ subdirectory: [],
135
+ })
136
+ return files
137
+ }
@@ -0,0 +1,90 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js'
2
+ import { getForwardDeclaration } from '../../syntax/c++/getForwardDeclaration.js'
3
+ import { includeHeader } from '../../syntax/c++/includeNitroHeader.js'
4
+ import { getAllKnownTypes } from '../../syntax/createType.js'
5
+ import { getHybridObjectName } from '../../syntax/getHybridObjectName.js'
6
+ import {
7
+ createFileMetadataString,
8
+ isNotDuplicate,
9
+ } from '../../syntax/helpers.js'
10
+ import type { SourceFile } from '../../syntax/SourceFile.js'
11
+ import { getTypeAs } from '../../syntax/types/getTypeAs.js'
12
+ import { HybridObjectType } from '../../syntax/types/HybridObjectType.js'
13
+
14
+ export function getUmbrellaHeaderName(): string {
15
+ const moduleName = NitroConfig.current.getIosModuleName()
16
+ return `${moduleName}-Swift-Cxx-Umbrella.hpp`
17
+ }
18
+
19
+ export function createSwiftUmbrellaHeader(): SourceFile {
20
+ const moduleName = NitroConfig.current.getIosModuleName()
21
+ const filename = getUmbrellaHeaderName()
22
+
23
+ const types = getAllKnownTypes('swift')
24
+
25
+ const swiftForwardDeclares = types
26
+ .filter((t) => t.kind === 'hybrid-object')
27
+ .map((t) => {
28
+ const hybridObjectType = getTypeAs(t, HybridObjectType)
29
+ const hybridObjectModuleName =
30
+ hybridObjectType.sourceConfig.getIosModuleName()
31
+ const name = getHybridObjectName(hybridObjectType.hybridObjectName)
32
+ return getForwardDeclaration(
33
+ 'class',
34
+ name.HybridTSpecCxx,
35
+ hybridObjectModuleName
36
+ )
37
+ })
38
+ .filter(isNotDuplicate)
39
+
40
+ const imports = types.flatMap((t) => t.getRequiredImports('c++'))
41
+ const forwardDeclarations = imports
42
+ .map((i) => i.forwardDeclaration)
43
+ .filter((f) => f != null)
44
+ .filter(isNotDuplicate)
45
+ const includes = imports.map((i) => includeHeader(i)).filter(isNotDuplicate)
46
+
47
+ const code = `
48
+ ${createFileMetadataString(filename, '///')}
49
+
50
+ #pragma once
51
+
52
+ // Forward declarations of C++ defined types
53
+ ${forwardDeclarations.sort().join('\n')}
54
+
55
+ // Include C++ defined types
56
+ ${includes.sort().join('\n')}
57
+
58
+ // C++ helpers for Swift
59
+ #include "${moduleName}-Swift-Cxx-Bridge.hpp"
60
+
61
+ // Common C++ types used in Swift
62
+ #include <NitroModules/ArrayBufferHolder.hpp>
63
+ #include <NitroModules/AnyMapUtils.hpp>
64
+ #include <NitroModules/RuntimeError.hpp>
65
+ #include <NitroModules/DateToChronoDate.hpp>
66
+
67
+ // Forward declarations of Swift defined types
68
+ ${swiftForwardDeclares.sort().join('\n')}
69
+
70
+ // Include Swift defined types
71
+ #if __has_include("${moduleName}-Swift.h")
72
+ // This header is generated by Xcode/Swift on every app build.
73
+ // If it cannot be found, make sure the Swift module's name (= podspec name) is actually "${moduleName}".
74
+ #include "${moduleName}-Swift.h"
75
+ // Same as above, but used when building with frameworks (\`use_frameworks\`)
76
+ #elif __has_include(<${moduleName}/${moduleName}-Swift.h>)
77
+ #include <${moduleName}/${moduleName}-Swift.h>
78
+ #else
79
+ #error ${moduleName}'s autogenerated Swift header cannot be found! Make sure the Swift module's name (= podspec name) is actually "${moduleName}", and try building the app first.
80
+ #endif
81
+ `
82
+
83
+ return {
84
+ content: code,
85
+ language: 'c++',
86
+ name: filename,
87
+ platform: 'ios',
88
+ subdirectory: [],
89
+ }
90
+ }