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,130 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js'
2
+ import { capitalizeName, indent } from '../../utils.js'
3
+ import { createFileMetadataString } from '../helpers.js'
4
+ import type { SourceFile } from '../SourceFile.js'
5
+ import { EnumType } from '../types/EnumType.js'
6
+
7
+ export function createKotlinEnum(enumType: EnumType): SourceFile[] {
8
+ const members = enumType.enumMembers.map(
9
+ (m) => `${m.name.toUpperCase()}(${m.value})`
10
+ )
11
+ const packageName = NitroConfig.current.getAndroidPackage('java/kotlin')
12
+ const code = `
13
+ ${createFileMetadataString(`${enumType.enumName}.kt`)}
14
+
15
+ package ${packageName}
16
+
17
+ import androidx.annotation.Keep
18
+ import com.facebook.proguard.annotations.DoNotStrip
19
+
20
+ /**
21
+ * Represents the JavaScript enum/union "${enumType.enumName}".
22
+ */
23
+ @DoNotStrip
24
+ @Keep
25
+ enum class ${enumType.enumName}(@DoNotStrip @Keep val value: Int) {
26
+ ${indent(members.join(',\n'), ' ')};
27
+ }
28
+ `.trim()
29
+
30
+ const cxxNamespace = NitroConfig.current.getCxxNamespace('c++')
31
+ const jniClassDescriptor = NitroConfig.current.getAndroidPackage(
32
+ 'c++/jni',
33
+ enumType.enumName
34
+ )
35
+ const cppToJniConverterCode = getCppToJniConverterCode('value', enumType)
36
+
37
+ const fbjniCode = `
38
+ ${createFileMetadataString(`J${enumType.enumName}.hpp`)}
39
+
40
+ #pragma once
41
+
42
+ #include <fbjni/fbjni.h>
43
+ #include "${enumType.declarationFile.name}"
44
+
45
+ namespace ${cxxNamespace} {
46
+
47
+ using namespace facebook;
48
+
49
+ /**
50
+ * The C++ JNI bridge between the C++ enum "${enumType.enumName}" and the the Kotlin enum "${enumType.enumName}".
51
+ */
52
+ struct J${enumType.enumName} final: public jni::JavaClass<J${enumType.enumName}> {
53
+ public:
54
+ static auto constexpr kJavaDescriptor = "L${jniClassDescriptor};";
55
+
56
+ public:
57
+ /**
58
+ * Convert this Java/Kotlin-based enum to the C++ enum ${enumType.enumName}.
59
+ */
60
+ [[maybe_unused]]
61
+ [[nodiscard]]
62
+ ${enumType.enumName} toCpp() const {
63
+ static const auto clazz = javaClassStatic();
64
+ static const auto fieldOrdinal = clazz->getField<int>("value");
65
+ int ordinal = this->getFieldValue(fieldOrdinal);
66
+ return static_cast<${enumType.enumName}>(ordinal);
67
+ }
68
+
69
+ public:
70
+ /**
71
+ * Create a Java/Kotlin-based enum with the given C++ enum's value.
72
+ */
73
+ [[maybe_unused]]
74
+ static jni::alias_ref<J${enumType.enumName}> fromCpp(${enumType.enumName} value) {
75
+ ${indent(cppToJniConverterCode, ' ')}
76
+ }
77
+ };
78
+
79
+ } // namespace ${cxxNamespace}
80
+ `.trim()
81
+
82
+ const files: SourceFile[] = []
83
+ files.push({
84
+ content: code,
85
+ language: 'kotlin',
86
+ name: `${enumType.enumName}.kt`,
87
+ subdirectory: NitroConfig.current.getAndroidPackageDirectory(),
88
+ platform: 'android',
89
+ })
90
+ files.push({
91
+ content: fbjniCode,
92
+ language: 'c++',
93
+ name: `J${enumType.enumName}.hpp`,
94
+ subdirectory: [],
95
+ platform: 'android',
96
+ })
97
+ return files
98
+ }
99
+
100
+ function getCppToJniConverterCode(
101
+ cppValueName: string,
102
+ enumType: EnumType
103
+ ): string {
104
+ const jniEnumName = `J${enumType.enumName}`
105
+
106
+ const fields = enumType.enumMembers.map((m) => {
107
+ const fieldName = `field${capitalizeName(m.name)}`
108
+ return `static const auto ${fieldName} = clazz->getStaticField<${jniEnumName}>("${m.name}");`
109
+ })
110
+
111
+ const cases = enumType.enumMembers.map((m) => {
112
+ const fieldName = `field${capitalizeName(m.name)}`
113
+ return `
114
+ case ${enumType.enumName}::${m.name}:
115
+ return clazz->getStaticFieldValue(${fieldName});
116
+ `.trim()
117
+ })
118
+
119
+ return `
120
+ static const auto clazz = javaClassStatic();
121
+ ${fields.join('\n')}
122
+
123
+ switch (${cppValueName}) {
124
+ ${indent(cases.join('\n'), ' ')}
125
+ default:
126
+ std::string stringValue = std::to_string(static_cast<int>(${cppValueName}));
127
+ throw std::invalid_argument("Invalid enum value (" + stringValue + "!");
128
+ }
129
+ `.trim()
130
+ }
@@ -0,0 +1,277 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js'
2
+ import { indent } from '../../utils.js'
3
+ import { includeHeader } from '../c++/includeNitroHeader.js'
4
+ import { createFileMetadataString, isNotDuplicate } from '../helpers.js'
5
+ import type { SourceFile } from '../SourceFile.js'
6
+ import type { FunctionType } from '../types/FunctionType.js'
7
+ import { addJNINativeRegistration } from './JNINativeRegistrations.js'
8
+ import { KotlinCxxBridgedType } from './KotlinCxxBridgedType.js'
9
+
10
+ export function createKotlinFunction(functionType: FunctionType): SourceFile[] {
11
+ const name = functionType.specializationName
12
+ const packageName = NitroConfig.current.getAndroidPackage('java/kotlin')
13
+ const kotlinReturnType = functionType.returnType.getCode('kotlin')
14
+ const kotlinParams = functionType.parameters.map(
15
+ (p) => `${p.escapedName}: ${p.getCode('kotlin')}`
16
+ )
17
+ const kotlinParamTypes = functionType.parameters.map((p) =>
18
+ p.getCode('kotlin')
19
+ )
20
+ const kotlinParamsForward = functionType.parameters.map((p) => p.escapedName)
21
+ const lambdaSignature = `(${kotlinParamTypes.join(', ')}) -> ${kotlinReturnType}`
22
+
23
+ const extraImports = functionType
24
+ .getRequiredImports('kotlin')
25
+ .map((i) => `import ${i.name}`)
26
+
27
+ const kotlinCode = `
28
+ ${createFileMetadataString(`${name}.kt`)}
29
+
30
+ package ${packageName}
31
+
32
+ import androidx.annotation.Keep
33
+ import com.facebook.jni.HybridData
34
+ import com.facebook.proguard.annotations.DoNotStrip
35
+ import com.margelo.nitro.core.*
36
+ import dalvik.annotation.optimization.FastNative
37
+ ${extraImports.join('\n')}
38
+
39
+ /**
40
+ * Represents the JavaScript callback \`${functionType.jsName}\`.
41
+ * This can be either implemented in C++ (in which case it might be a callback coming from JS),
42
+ * or in Kotlin/Java (in which case it is a native callback).
43
+ */
44
+ @DoNotStrip
45
+ @Keep
46
+ @Suppress("ClassName", "RedundantUnitReturnType")
47
+ fun interface ${name}: ${lambdaSignature} {
48
+ /**
49
+ * Call the given JS callback.
50
+ * @throws Throwable if the JS function itself throws an error, or if the JS function/runtime has already been deleted.
51
+ */
52
+ @DoNotStrip
53
+ @Keep
54
+ override fun invoke(${kotlinParams.join(', ')}): ${kotlinReturnType}
55
+ }
56
+
57
+ /**
58
+ * Represents the JavaScript callback \`${functionType.jsName}\`.
59
+ * This is implemented in C++, via a \`std::function<...>\`.
60
+ * The callback might be coming from JS.
61
+ */
62
+ @DoNotStrip
63
+ @Keep
64
+ @Suppress(
65
+ "KotlinJniMissingFunction", "unused",
66
+ "RedundantSuppression", "RedundantUnitReturnType", "FunctionName",
67
+ "ConvertSecondaryConstructorToPrimary", "ClassName", "LocalVariableName",
68
+ )
69
+ class ${name}_cxx: ${name} {
70
+ @DoNotStrip
71
+ @Keep
72
+ private val mHybridData: HybridData
73
+
74
+ @DoNotStrip
75
+ @Keep
76
+ private constructor(hybridData: HybridData) {
77
+ mHybridData = hybridData
78
+ }
79
+
80
+ @DoNotStrip
81
+ @Keep
82
+ override fun invoke(${kotlinParams.join(', ')}): ${kotlinReturnType}
83
+ = invoke_cxx(${kotlinParamsForward.join(',')})
84
+
85
+ @FastNative
86
+ private external fun invoke_cxx(${kotlinParams.join(', ')}): ${kotlinReturnType}
87
+ }
88
+
89
+ /**
90
+ * Represents the JavaScript callback \`${functionType.jsName}\`.
91
+ * This is implemented in Java/Kotlin, via a \`${lambdaSignature}\`.
92
+ * The callback is always coming from native.
93
+ */
94
+ @DoNotStrip
95
+ @Keep
96
+ @Suppress("ClassName", "RedundantUnitReturnType", "unused")
97
+ class ${name}_java(private val function: ${lambdaSignature}): ${name} {
98
+ @DoNotStrip
99
+ @Keep
100
+ override fun invoke(${kotlinParams.join(', ')}): ${kotlinReturnType} {
101
+ return this.function(${kotlinParamsForward.join(', ')})
102
+ }
103
+ }
104
+ `.trim()
105
+
106
+ const jniInterfaceDescriptor = NitroConfig.current.getAndroidPackage(
107
+ 'c++/jni',
108
+ name
109
+ )
110
+ const jniClassDescriptor = NitroConfig.current.getAndroidPackage(
111
+ 'c++/jni',
112
+ `${name}_cxx`
113
+ )
114
+ const bridgedReturn = new KotlinCxxBridgedType(functionType.returnType)
115
+ const cxxNamespace = NitroConfig.current.getCxxNamespace('c++')
116
+ const typename = functionType.getCode('c++')
117
+
118
+ // call() Java -> C++
119
+ const cppParams = functionType.parameters.map((p) => {
120
+ const bridge = new KotlinCxxBridgedType(p)
121
+ const type = bridge.asJniReferenceType('alias')
122
+ return `${type} ${p.escapedName}`
123
+ })
124
+ const paramsForward = functionType.parameters.map((p) => {
125
+ const bridge = new KotlinCxxBridgedType(p)
126
+ return bridge.parseFromKotlinToCpp(p.escapedName, 'c++', false)
127
+ })
128
+
129
+ // call() C++ -> Java
130
+ const jniParams = functionType.parameters.map((p) => {
131
+ if (p.canBePassedByReference) {
132
+ return `const ${p.getCode('c++')}& ${p.escapedName}`
133
+ } else {
134
+ return `${p.getCode('c++')} ${p.escapedName}`
135
+ }
136
+ })
137
+ const jniParamsForward = [
138
+ 'self()',
139
+ ...functionType.parameters.map((p) => {
140
+ const bridge = new KotlinCxxBridgedType(p)
141
+ return bridge.parseFromCppToKotlin(p.escapedName, 'c++', false)
142
+ }),
143
+ ]
144
+ const jniSignature = `${bridgedReturn.asJniReferenceType('local')}(${functionType.parameters
145
+ .map((p) => {
146
+ const bridge = new KotlinCxxBridgedType(p)
147
+ return `${bridge.asJniReferenceType('alias')} /* ${p.escapedName} */`
148
+ })
149
+ .join(', ')})`
150
+
151
+ let cppCallBody: string
152
+ let jniCallBody: string
153
+ if (functionType.returnType.kind === 'void') {
154
+ // It returns void
155
+ cppCallBody = `_func(${indent(paramsForward.join(', '), ' ')});`
156
+ jniCallBody = `
157
+ static const auto method = javaClassStatic()->getMethod<${jniSignature}>("invoke");
158
+ method(${jniParamsForward.join(', ')});
159
+ `.trim()
160
+ } else {
161
+ // It returns a type!
162
+ cppCallBody = `
163
+ ${functionType.returnType.getCode('c++')} __result = _func(${indent(paramsForward.join(', '), ' ')});
164
+ return ${bridgedReturn.parseFromCppToKotlin('__result', 'c++')};
165
+ `.trim()
166
+ jniCallBody = `
167
+ static const auto method = javaClassStatic()->getMethod<${jniSignature}>("invoke");
168
+ auto __result = method(${jniParamsForward.join(', ')});
169
+ return ${bridgedReturn.parseFromKotlinToCpp('__result', 'c++', false)};
170
+ `.trim()
171
+ }
172
+
173
+ const bridged = new KotlinCxxBridgedType(functionType)
174
+ const imports = bridged
175
+ .getRequiredImports('c++')
176
+ .filter((i) => i.name !== `J${name}.hpp`)
177
+ const includes = imports.map((i) => includeHeader(i)).filter(isNotDuplicate)
178
+
179
+ const fbjniCode = `
180
+ ${createFileMetadataString(`J${name}.hpp`)}
181
+
182
+ #pragma once
183
+
184
+ #include <fbjni/fbjni.h>
185
+ #include <functional>
186
+
187
+ ${includes.join('\n')}
188
+
189
+ namespace ${cxxNamespace} {
190
+
191
+ using namespace facebook;
192
+
193
+ /**
194
+ * Represents the Java/Kotlin callback \`${functionType.getCode('kotlin')}\`.
195
+ * This can be passed around between C++ and Java/Kotlin.
196
+ */
197
+ struct J${name}: public jni::JavaClass<J${name}> {
198
+ public:
199
+ static auto constexpr kJavaDescriptor = "L${jniInterfaceDescriptor};";
200
+
201
+ public:
202
+ /**
203
+ * Invokes the function this \`J${name}\` instance holds through JNI.
204
+ */
205
+ ${functionType.returnType.getCode('c++')} invoke(${jniParams.join(', ')}) const {
206
+ ${indent(jniCallBody, ' ')}
207
+ }
208
+ };
209
+
210
+ /**
211
+ * An implementation of ${name} that is backed by a C++ implementation (using \`std::function<...>\`)
212
+ */
213
+ struct J${name}_cxx final: public jni::HybridClass<J${name}_cxx, J${name}> {
214
+ public:
215
+ static jni::local_ref<J${name}::javaobject> fromCpp(const ${typename}& func) {
216
+ return J${name}_cxx::newObjectCxxArgs(func);
217
+ }
218
+
219
+ public:
220
+ /**
221
+ * Invokes the C++ \`std::function<...>\` this \`J${name}_cxx\` instance holds.
222
+ */
223
+ ${bridgedReturn.asJniReferenceType('local')} invoke_cxx(${cppParams.join(', ')}) {
224
+ ${indent(cppCallBody, ' ')}
225
+ }
226
+
227
+ public:
228
+ [[nodiscard]]
229
+ inline const ${typename}& getFunction() const {
230
+ return _func;
231
+ }
232
+
233
+ public:
234
+ static auto constexpr kJavaDescriptor = "L${jniClassDescriptor};";
235
+ static void registerNatives() {
236
+ registerHybrid({makeNativeMethod("invoke_cxx", J${name}_cxx::invoke_cxx)});
237
+ }
238
+
239
+ private:
240
+ explicit J${name}_cxx(const ${typename}& func): _func(func) { }
241
+
242
+ private:
243
+ friend HybridBase;
244
+ ${typename} _func;
245
+ };
246
+
247
+ } // namespace ${cxxNamespace}
248
+ `.trim()
249
+
250
+ // Make sure we register all native JNI methods on app startup
251
+ addJNINativeRegistration({
252
+ namespace: cxxNamespace,
253
+ className: `J${name}_cxx`,
254
+ import: {
255
+ name: `J${name}.hpp`,
256
+ space: 'user',
257
+ language: 'c++',
258
+ },
259
+ })
260
+
261
+ const files: SourceFile[] = []
262
+ files.push({
263
+ content: kotlinCode,
264
+ language: 'kotlin',
265
+ name: `${name}.kt`,
266
+ subdirectory: NitroConfig.current.getAndroidPackageDirectory(),
267
+ platform: 'android',
268
+ })
269
+ files.push({
270
+ content: fbjniCode,
271
+ language: 'c++',
272
+ name: `J${name}.hpp`,
273
+ subdirectory: [],
274
+ platform: 'android',
275
+ })
276
+ return files
277
+ }
@@ -0,0 +1,205 @@
1
+ import { indent } from '../../utils.js'
2
+ import { createKotlinHybridViewManager } from '../../views/kotlin/KotlinHybridViewManager.js'
3
+ import { getAllTypes } from '../getAllTypes.js'
4
+ import { getHybridObjectName } from '../getHybridObjectName.js'
5
+ import { createFileMetadataString, isNotDuplicate } from '../helpers.js'
6
+ import type { HybridObjectSpec } from '../HybridObjectSpec.js'
7
+ import { Method } from '../Method.js'
8
+ import { Property } from '../Property.js'
9
+ import type { SourceFile } from '../SourceFile.js'
10
+ import { HybridObjectType } from '../types/HybridObjectType.js'
11
+ import { createFbjniHybridObject } from './FbjniHybridObject.js'
12
+ import { KotlinCxxBridgedType } from './KotlinCxxBridgedType.js'
13
+
14
+ export function createKotlinHybridObject(spec: HybridObjectSpec): SourceFile[] {
15
+ const name = getHybridObjectName(spec.name)
16
+ const properties = spec.properties
17
+ .map((p) => getPropertyForwardImplementation(p))
18
+ .join('\n\n')
19
+ const methods = spec.methods
20
+ .map((m) => getMethodForwardImplementation(m))
21
+ .join('\n\n')
22
+
23
+ const extraImports = [
24
+ ...spec.properties.flatMap((p) => p.getRequiredImports('kotlin')),
25
+ ...spec.methods.flatMap((m) => m.getRequiredImports('kotlin')),
26
+ ...spec.baseTypes.flatMap((b) =>
27
+ new HybridObjectType(b).getRequiredImports('kotlin')
28
+ ),
29
+ ]
30
+
31
+ let kotlinBase = spec.isHybridView ? 'HybridView' : 'HybridObject'
32
+ if (spec.baseTypes.length > 0) {
33
+ if (spec.baseTypes.length > 1) {
34
+ throw new Error(
35
+ `${name.T}: Inheriting from multiple HybridObject bases is not yet supported in Kotlin!`
36
+ )
37
+ }
38
+ const base = spec.baseTypes[0]!
39
+ const baseHybrid = new HybridObjectType(base)
40
+ kotlinBase = baseHybrid.getCode('kotlin')
41
+ }
42
+
43
+ const imports: string[] = []
44
+ imports.push('import com.margelo.nitro.core.*')
45
+ if (spec.isHybridView) {
46
+ imports.push('import com.margelo.nitro.views.*')
47
+ }
48
+ imports.push(
49
+ ...extraImports.map((i) => `import ${i.name}`).filter(isNotDuplicate)
50
+ )
51
+
52
+ const javaPackage = spec.config.getAndroidPackage('java/kotlin')
53
+
54
+ // 1. Create Kotlin abstract class definition
55
+ const abstractClassCode = `
56
+ ${createFileMetadataString(`${name.HybridTSpec}.kt`)}
57
+
58
+ package ${javaPackage}
59
+
60
+ import androidx.annotation.Keep
61
+ import com.facebook.jni.HybridData
62
+ import com.facebook.proguard.annotations.DoNotStrip
63
+ ${imports.join('\n')}
64
+
65
+ /**
66
+ * A Kotlin class representing the ${spec.name} HybridObject.
67
+ * Implement this abstract class to create Kotlin-based instances of ${spec.name}.
68
+ */
69
+ @DoNotStrip
70
+ @Keep
71
+ @Suppress(
72
+ "KotlinJniMissingFunction", "unused",
73
+ "RedundantSuppression", "RedundantUnitReturnType", "SimpleRedundantLet",
74
+ "LocalVariableName", "PropertyName", "PrivatePropertyName", "FunctionName"
75
+ )
76
+ abstract class ${name.HybridTSpec}: ${kotlinBase}() {
77
+ @DoNotStrip
78
+ private var mHybridData: HybridData = initHybrid()
79
+
80
+ init {
81
+ super.updateNative(mHybridData)
82
+ }
83
+
84
+ override fun updateNative(hybridData: HybridData) {
85
+ mHybridData = hybridData
86
+ super.updateNative(hybridData)
87
+ }
88
+
89
+ // Properties
90
+ ${indent(properties, ' ')}
91
+
92
+ // Methods
93
+ ${indent(methods, ' ')}
94
+
95
+ private external fun initHybrid(): HybridData
96
+
97
+ companion object {
98
+ private const val TAG = "${name.HybridTSpec}"
99
+ }
100
+ }
101
+ `.trim()
102
+
103
+ // 2. Create C++ (fbjni) bindings
104
+ const cppFiles = createFbjniHybridObject(spec)
105
+
106
+ // 3. Create enums or structs in Kotlin
107
+ const extraFiles = getAllTypes(spec)
108
+ .map((t) => new KotlinCxxBridgedType(t))
109
+ .flatMap((b) => b.getExtraFiles())
110
+
111
+ const files: SourceFile[] = []
112
+ files.push({
113
+ content: abstractClassCode,
114
+ language: 'kotlin',
115
+ name: `${name.HybridTSpec}.kt`,
116
+ subdirectory: spec.config.getAndroidPackageDirectory(),
117
+ platform: 'android',
118
+ })
119
+ files.push(...cppFiles)
120
+ files.push(...extraFiles)
121
+
122
+ if (spec.isHybridView) {
123
+ const viewFiles = createKotlinHybridViewManager(spec)
124
+ files.push(...viewFiles)
125
+ }
126
+
127
+ return files
128
+ }
129
+
130
+ function getMethodForwardImplementation(method: Method): string {
131
+ const bridgedReturn = new KotlinCxxBridgedType(method.returnType)
132
+ const requiresBridge =
133
+ bridgedReturn.needsSpecialHandling ||
134
+ method.parameters.some((p) => {
135
+ const bridged = new KotlinCxxBridgedType(p.type)
136
+ return bridged.needsSpecialHandling
137
+ })
138
+
139
+ if (requiresBridge) {
140
+ const paramsSignature = method.parameters.map((p) => {
141
+ const bridge = new KotlinCxxBridgedType(p.type)
142
+ return `${p.name}: ${bridge.getTypeCode('kotlin')}`
143
+ })
144
+ const paramsForward = method.parameters.map((p) => {
145
+ const bridge = new KotlinCxxBridgedType(p.type)
146
+ return bridge.parseFromCppToKotlin(p.name, 'kotlin')
147
+ })
148
+ const returnForward = bridgedReturn.parseFromKotlinToCpp(
149
+ '__result',
150
+ 'kotlin'
151
+ )
152
+ const code = method.getCode('kotlin', { virtual: true })
153
+ return `
154
+ ${code}
155
+
156
+ @DoNotStrip
157
+ @Keep
158
+ private fun ${method.name}_cxx(${paramsSignature.join(', ')}): ${bridgedReturn.getTypeCode('kotlin')} {
159
+ val __result = ${method.name}(${paramsForward.join(', ')})
160
+ return ${returnForward}
161
+ }
162
+ `.trim()
163
+ } else {
164
+ const code = method.getCode('kotlin', { doNotStrip: true, virtual: true })
165
+ return code
166
+ }
167
+ }
168
+
169
+ function getPropertyForwardImplementation(property: Property): string {
170
+ const bridged = new KotlinCxxBridgedType(property.type)
171
+ if (bridged.needsSpecialHandling) {
172
+ let keyword = property.isReadonly ? 'val' : 'var'
173
+ let lines: string[] = []
174
+ lines.push(
175
+ `
176
+ @Keep
177
+ @DoNotStrip
178
+ get() {
179
+ return ${indent(bridged.parseFromKotlinToCpp(property.name, 'kotlin'), ' ')}
180
+ }
181
+ `.trim()
182
+ )
183
+ if (!property.isReadonly) {
184
+ lines.push(
185
+ `
186
+ @Keep
187
+ @DoNotStrip
188
+ set(value) {
189
+ ${property.name} = ${indent(bridged.parseFromCppToKotlin('value', 'kotlin'), ' ')}
190
+ }
191
+ `.trim()
192
+ )
193
+ }
194
+ const code = property.getCode('kotlin', { virtual: true })
195
+ return `
196
+ ${code}
197
+
198
+ private ${keyword} ${property.name}_cxx: ${bridged.getTypeCode('kotlin')}
199
+ ${indent(lines.join('\n'), ' ')}
200
+ `.trim()
201
+ } else {
202
+ const code = property.getCode('kotlin', { doNotStrip: true, virtual: true })
203
+ return code
204
+ }
205
+ }
@@ -0,0 +1,51 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js'
2
+ import { getHybridObjectName } from '../getHybridObjectName.js'
3
+ import type { SourceImport } from '../SourceFile.js'
4
+
5
+ interface Props {
6
+ /**
7
+ * The name of the Hybrid Object under which it should be registered and exposed to JS to.
8
+ */
9
+ hybridObjectName: string
10
+ /**
11
+ * The name of the Kotlin/Java class that will be default-constructed
12
+ */
13
+ jniClassName: string
14
+ }
15
+
16
+ interface JNIHybridObjectRegistration {
17
+ cppCode: string
18
+ requiredImports: SourceImport[]
19
+ }
20
+
21
+ export function createJNIHybridObjectRegistration({
22
+ hybridObjectName,
23
+ jniClassName,
24
+ }: Props): JNIHybridObjectRegistration {
25
+ const { JHybridTSpec } = getHybridObjectName(hybridObjectName)
26
+ const jniNamespace = NitroConfig.current.getAndroidPackage(
27
+ 'c++/jni',
28
+ jniClassName
29
+ )
30
+
31
+ return {
32
+ requiredImports: [
33
+ { name: `${JHybridTSpec}.hpp`, language: 'c++', space: 'user' },
34
+ {
35
+ name: 'NitroModules/DefaultConstructableObject.hpp',
36
+ language: 'c++',
37
+ space: 'system',
38
+ },
39
+ ],
40
+ cppCode: `
41
+ HybridObjectRegistry::registerHybridObjectConstructor(
42
+ "${hybridObjectName}",
43
+ []() -> std::shared_ptr<HybridObject> {
44
+ static DefaultConstructableObject<${JHybridTSpec}::javaobject> object("${jniNamespace}");
45
+ auto instance = object.create();
46
+ return instance->cthis()->shared();
47
+ }
48
+ );
49
+ `.trim(),
50
+ }
51
+ }