nitrogen 0.2.24 → 0.29.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -109
- package/lib/Logger.d.ts +10 -0
- package/lib/Logger.js +56 -0
- package/lib/autolinking/Autolinking.d.ts +8 -0
- package/lib/autolinking/Autolinking.js +1 -0
- package/lib/autolinking/android/createCMakeExtension.d.ts +6 -0
- package/lib/autolinking/android/createCMakeExtension.js +106 -0
- package/lib/autolinking/android/createGradleExtension.d.ts +5 -0
- package/lib/autolinking/android/createGradleExtension.js +36 -0
- package/lib/autolinking/android/createHybridObjectInitializer.d.ts +2 -0
- package/lib/autolinking/android/createHybridObjectInitializer.js +159 -0
- package/lib/autolinking/createAndroidAutolinking.d.ts +11 -0
- package/lib/autolinking/createAndroidAutolinking.js +13 -0
- package/lib/autolinking/createIOSAutolinking.d.ts +5 -0
- package/lib/autolinking/createIOSAutolinking.js +19 -0
- package/lib/autolinking/ios/createHybridObjectInitializer.d.ts +9 -0
- package/lib/autolinking/ios/createHybridObjectInitializer.js +97 -0
- package/lib/autolinking/ios/createPodspecRubyExtension.d.ts +5 -0
- package/lib/autolinking/ios/createPodspecRubyExtension.js +69 -0
- package/lib/autolinking/ios/createSwiftCxxBridge.d.ts +2 -0
- package/lib/autolinking/ios/createSwiftCxxBridge.js +117 -0
- package/lib/autolinking/ios/createSwiftUmbrellaHeader.d.ts +3 -0
- package/lib/autolinking/ios/createSwiftUmbrellaHeader.js +74 -0
- package/lib/config/NitroConfig.d.ts +52 -0
- package/lib/config/NitroConfig.js +112 -0
- package/lib/config/NitroUserConfig.d.ts +22 -0
- package/lib/config/NitroUserConfig.js +88 -0
- package/lib/config/getConfig.d.ts +2 -0
- package/lib/config/getConfig.js +84 -0
- package/lib/createGitAttributes.d.ts +1 -0
- package/lib/createGitAttributes.js +11 -0
- package/lib/createPlatformSpec.d.ts +4 -0
- package/lib/createPlatformSpec.js +127 -0
- package/lib/getFiles.d.ts +1 -0
- package/lib/getFiles.js +28 -0
- package/lib/getPlatformSpecs.d.ts +17 -0
- package/lib/getPlatformSpecs.js +153 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +113 -10
- package/lib/init.d.ts +1 -0
- package/lib/init.js +123 -0
- package/lib/nitrogen.d.ts +11 -0
- package/lib/nitrogen.js +165 -0
- package/lib/prettifyDirectory.d.ts +1 -0
- package/lib/prettifyDirectory.js +27 -0
- package/lib/syntax/BridgedType.d.ts +41 -0
- package/lib/syntax/BridgedType.js +1 -0
- package/lib/syntax/CodeNode.d.ts +23 -0
- package/lib/syntax/CodeNode.js +1 -0
- package/lib/syntax/HybridObjectSpec.d.ts +13 -0
- package/lib/syntax/HybridObjectSpec.js +1 -0
- package/lib/syntax/Method.d.ts +41 -0
- package/lib/syntax/Method.js +108 -0
- package/lib/syntax/Parameter.d.ts +15 -0
- package/lib/syntax/Parameter.js +65 -0
- package/lib/syntax/Property.d.ts +50 -0
- package/lib/syntax/Property.js +147 -0
- package/lib/syntax/SourceFile.d.ts +70 -0
- package/lib/syntax/SourceFile.js +7 -0
- package/lib/syntax/c++/CppEnum.d.ts +6 -0
- package/lib/syntax/c++/CppEnum.js +110 -0
- package/lib/syntax/c++/CppHybridObject.d.ts +3 -0
- package/lib/syntax/c++/CppHybridObject.js +146 -0
- package/lib/syntax/c++/CppHybridObjectRegistration.d.ts +17 -0
- package/lib/syntax/c++/CppHybridObjectRegistration.js +18 -0
- package/lib/syntax/c++/CppStruct.d.ts +3 -0
- package/lib/syntax/c++/CppStruct.js +108 -0
- package/lib/syntax/c++/CppUnion.d.ts +6 -0
- package/lib/syntax/c++/CppUnion.js +88 -0
- package/lib/syntax/c++/getForwardDeclaration.d.ts +3 -0
- package/lib/syntax/c++/getForwardDeclaration.js +14 -0
- package/lib/syntax/c++/includeNitroHeader.d.ts +7 -0
- package/lib/syntax/c++/includeNitroHeader.js +34 -0
- package/lib/syntax/createType.d.ts +15 -0
- package/lib/syntax/createType.js +303 -0
- package/lib/syntax/getAllTypes.d.ts +3 -0
- package/lib/syntax/getAllTypes.js +11 -0
- package/lib/syntax/getCustomTypeConfig.d.ts +8 -0
- package/lib/syntax/getCustomTypeConfig.js +53 -0
- package/lib/syntax/getHybridObjectName.d.ts +36 -0
- package/lib/syntax/getHybridObjectName.js +10 -0
- package/lib/syntax/getInterfaceProperties.d.ts +4 -0
- package/lib/syntax/getInterfaceProperties.js +9 -0
- package/lib/syntax/getReferencedTypes.d.ts +2 -0
- package/lib/syntax/getReferencedTypes.js +47 -0
- package/lib/syntax/helpers.d.ts +13 -0
- package/lib/syntax/helpers.js +53 -0
- package/lib/syntax/isCoreType.d.ts +9 -0
- package/lib/syntax/isCoreType.js +47 -0
- package/lib/syntax/kotlin/FbjniHybridObject.d.ts +3 -0
- package/lib/syntax/kotlin/FbjniHybridObject.js +261 -0
- package/lib/syntax/kotlin/JNINativeRegistrations.d.ts +8 -0
- package/lib/syntax/kotlin/JNINativeRegistrations.js +7 -0
- package/lib/syntax/kotlin/KotlinBoxedPrimitive.d.ts +7 -0
- package/lib/syntax/kotlin/KotlinBoxedPrimitive.js +17 -0
- package/lib/syntax/kotlin/KotlinCxxBridgedType.d.ts +19 -0
- package/lib/syntax/kotlin/KotlinCxxBridgedType.js +893 -0
- package/lib/syntax/kotlin/KotlinEnum.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinEnum.js +113 -0
- package/lib/syntax/kotlin/KotlinFunction.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinFunction.js +256 -0
- package/lib/syntax/kotlin/KotlinHybridObject.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinHybridObject.js +177 -0
- package/lib/syntax/kotlin/KotlinHybridObjectRegistration.d.ts +17 -0
- package/lib/syntax/kotlin/KotlinHybridObjectRegistration.js +26 -0
- package/lib/syntax/kotlin/KotlinStruct.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinStruct.js +172 -0
- package/lib/syntax/kotlin/KotlinVariant.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinVariant.js +191 -0
- package/lib/syntax/swift/SwiftCxxBridgedType.d.ts +21 -0
- package/lib/syntax/swift/SwiftCxxBridgedType.js +819 -0
- package/lib/syntax/swift/SwiftCxxTypeHelper.d.ts +17 -0
- package/lib/syntax/swift/SwiftCxxTypeHelper.js +613 -0
- package/lib/syntax/swift/SwiftEnum.d.ts +3 -0
- package/lib/syntax/swift/SwiftEnum.js +52 -0
- package/lib/syntax/swift/SwiftFunction.d.ts +3 -0
- package/lib/syntax/swift/SwiftFunction.js +83 -0
- package/lib/syntax/swift/SwiftHybridObject.d.ts +3 -0
- package/lib/syntax/swift/SwiftHybridObject.js +103 -0
- package/lib/syntax/swift/SwiftHybridObjectBridge.d.ts +11 -0
- package/lib/syntax/swift/SwiftHybridObjectBridge.js +451 -0
- package/lib/syntax/swift/SwiftHybridObjectRegistration.d.ts +19 -0
- package/lib/syntax/swift/SwiftHybridObjectRegistration.js +42 -0
- package/lib/syntax/swift/SwiftStruct.d.ts +3 -0
- package/lib/syntax/swift/SwiftStruct.js +75 -0
- package/lib/syntax/swift/SwiftVariant.d.ts +3 -0
- package/lib/syntax/swift/SwiftVariant.js +58 -0
- package/lib/syntax/types/ArrayBufferType.d.ts +10 -0
- package/lib/syntax/types/ArrayBufferType.js +37 -0
- package/lib/syntax/types/ArrayType.d.ts +12 -0
- package/lib/syntax/types/ArrayType.js +52 -0
- package/lib/syntax/types/BigIntType.d.ts +10 -0
- package/lib/syntax/types/BigIntType.js +27 -0
- package/lib/syntax/types/BooleanType.d.ts +10 -0
- package/lib/syntax/types/BooleanType.js +27 -0
- package/lib/syntax/types/CustomType.d.ts +14 -0
- package/lib/syntax/types/CustomType.js +36 -0
- package/lib/syntax/types/DateType.d.ts +10 -0
- package/lib/syntax/types/DateType.js +35 -0
- package/lib/syntax/types/EnumType.d.ts +23 -0
- package/lib/syntax/types/EnumType.js +101 -0
- package/lib/syntax/types/ErrorType.d.ts +11 -0
- package/lib/syntax/types/ErrorType.js +37 -0
- package/lib/syntax/types/FunctionType.d.ts +29 -0
- package/lib/syntax/types/FunctionType.js +147 -0
- package/lib/syntax/types/HybridObjectBaseType.d.ts +11 -0
- package/lib/syntax/types/HybridObjectBaseType.js +38 -0
- package/lib/syntax/types/HybridObjectType.d.ts +23 -0
- package/lib/syntax/types/HybridObjectType.js +131 -0
- package/lib/syntax/types/MapType.d.ts +10 -0
- package/lib/syntax/types/MapType.js +37 -0
- package/lib/syntax/types/NamedWrappingType.d.ts +14 -0
- package/lib/syntax/types/NamedWrappingType.js +27 -0
- package/lib/syntax/types/NullType.d.ts +10 -0
- package/lib/syntax/types/NullType.js +23 -0
- package/lib/syntax/types/NumberType.d.ts +10 -0
- package/lib/syntax/types/NumberType.js +27 -0
- package/lib/syntax/types/OptionalType.d.ts +13 -0
- package/lib/syntax/types/OptionalType.js +59 -0
- package/lib/syntax/types/PromiseType.d.ts +16 -0
- package/lib/syntax/types/PromiseType.js +62 -0
- package/lib/syntax/types/RecordType.d.ts +13 -0
- package/lib/syntax/types/RecordType.js +47 -0
- package/lib/syntax/types/ResultWrappingType.d.ts +13 -0
- package/lib/syntax/types/ResultWrappingType.js +44 -0
- package/lib/syntax/types/StringType.d.ts +10 -0
- package/lib/syntax/types/StringType.js +35 -0
- package/lib/syntax/types/StructType.d.ts +14 -0
- package/lib/syntax/types/StructType.js +61 -0
- package/lib/syntax/types/TupleType.d.ts +12 -0
- package/lib/syntax/types/TupleType.js +39 -0
- package/lib/syntax/types/Type.d.ts +55 -0
- package/lib/syntax/types/Type.js +1 -0
- package/lib/syntax/types/VariantType.d.ts +19 -0
- package/lib/syntax/types/VariantType.js +75 -0
- package/lib/syntax/types/VoidType.d.ts +10 -0
- package/lib/syntax/types/VoidType.js +27 -0
- package/lib/syntax/types/getTypeAs.d.ts +2 -0
- package/lib/syntax/types/getTypeAs.js +12 -0
- package/lib/utils.d.ts +22 -0
- package/lib/utils.js +126 -0
- package/lib/views/CppHybridViewComponent.d.ts +14 -0
- package/lib/views/CppHybridViewComponent.js +254 -0
- package/lib/views/createHostComponentJs.d.ts +3 -0
- package/lib/views/createHostComponentJs.js +27 -0
- package/lib/views/kotlin/KotlinHybridViewManager.d.ts +3 -0
- package/lib/views/kotlin/KotlinHybridViewManager.js +229 -0
- package/lib/views/swift/SwiftHybridViewManager.d.ts +3 -0
- package/lib/views/swift/SwiftHybridViewManager.js +131 -0
- package/lib/writeFile.d.ts +5 -0
- package/lib/writeFile.js +19 -0
- package/package.json +58 -29
- package/src/Logger.ts +63 -0
- package/src/autolinking/Autolinking.ts +9 -0
- package/src/autolinking/android/createCMakeExtension.ts +123 -0
- package/src/autolinking/android/createGradleExtension.ts +43 -0
- package/src/autolinking/android/createHybridObjectInitializer.ts +174 -0
- package/src/autolinking/createAndroidAutolinking.ts +28 -0
- package/src/autolinking/createIOSAutolinking.ts +24 -0
- package/src/autolinking/ios/createHybridObjectInitializer.ts +112 -0
- package/src/autolinking/ios/createPodspecRubyExtension.ts +76 -0
- package/src/autolinking/ios/createSwiftCxxBridge.ts +137 -0
- package/src/autolinking/ios/createSwiftUmbrellaHeader.ts +90 -0
- package/src/config/NitroConfig.ts +139 -0
- package/src/config/NitroUserConfig.ts +105 -0
- package/src/config/getConfig.ts +91 -0
- package/src/createGitAttributes.ts +15 -0
- package/src/createPlatformSpec.ts +176 -0
- package/src/getFiles.ts +31 -0
- package/src/getPlatformSpecs.ts +202 -0
- package/src/index.ts +146 -0
- package/src/init.ts +186 -0
- package/src/nitrogen.ts +246 -0
- package/src/prettifyDirectory.ts +32 -0
- package/src/syntax/BridgedType.ts +59 -0
- package/src/syntax/CodeNode.ts +24 -0
- package/src/syntax/HybridObjectSpec.ts +14 -0
- package/src/syntax/Method.ts +154 -0
- package/src/syntax/Parameter.ts +81 -0
- package/src/syntax/Property.ts +203 -0
- package/src/syntax/SourceFile.ts +80 -0
- package/src/syntax/c++/CppEnum.ts +128 -0
- package/src/syntax/c++/CppHybridObject.ts +165 -0
- package/src/syntax/c++/CppHybridObjectRegistration.ts +39 -0
- package/src/syntax/c++/CppStruct.ts +129 -0
- package/src/syntax/c++/CppUnion.ts +105 -0
- package/src/syntax/c++/getForwardDeclaration.ts +19 -0
- package/src/syntax/c++/includeNitroHeader.ts +40 -0
- package/src/syntax/createType.ts +365 -0
- package/src/syntax/getAllTypes.ts +18 -0
- package/src/syntax/getCustomTypeConfig.ts +71 -0
- package/src/syntax/getHybridObjectName.ts +48 -0
- package/src/syntax/getInterfaceProperties.ts +21 -0
- package/src/syntax/getReferencedTypes.ts +57 -0
- package/src/syntax/helpers.ts +79 -0
- package/src/syntax/isCoreType.ts +60 -0
- package/src/syntax/kotlin/FbjniHybridObject.ts +313 -0
- package/src/syntax/kotlin/JNINativeRegistrations.ts +19 -0
- package/src/syntax/kotlin/KotlinBoxedPrimitive.ts +19 -0
- package/src/syntax/kotlin/KotlinCxxBridgedType.ts +942 -0
- package/src/syntax/kotlin/KotlinEnum.ts +130 -0
- package/src/syntax/kotlin/KotlinFunction.ts +277 -0
- package/src/syntax/kotlin/KotlinHybridObject.ts +205 -0
- package/src/syntax/kotlin/KotlinHybridObjectRegistration.ts +51 -0
- package/src/syntax/kotlin/KotlinStruct.ts +198 -0
- package/src/syntax/kotlin/KotlinVariant.ts +212 -0
- package/src/syntax/swift/SwiftCxxBridgedType.ts +874 -0
- package/src/syntax/swift/SwiftCxxTypeHelper.ts +674 -0
- package/src/syntax/swift/SwiftEnum.ts +65 -0
- package/src/syntax/swift/SwiftFunction.ts +91 -0
- package/src/syntax/swift/SwiftHybridObject.ts +121 -0
- package/src/syntax/swift/SwiftHybridObjectBridge.ts +522 -0
- package/src/syntax/swift/SwiftHybridObjectRegistration.ts +75 -0
- package/src/syntax/swift/SwiftStruct.ts +85 -0
- package/src/syntax/swift/SwiftVariant.ts +67 -0
- package/src/syntax/types/ArrayBufferType.ts +49 -0
- package/src/syntax/types/ArrayType.ts +62 -0
- package/src/syntax/types/BigIntType.ts +35 -0
- package/src/syntax/types/BooleanType.ts +35 -0
- package/src/syntax/types/CustomType.ts +47 -0
- package/src/syntax/types/DateType.ts +43 -0
- package/src/syntax/types/EnumType.ts +130 -0
- package/src/syntax/types/ErrorType.ts +44 -0
- package/src/syntax/types/FunctionType.ts +167 -0
- package/src/syntax/types/HybridObjectBaseType.ts +54 -0
- package/src/syntax/types/HybridObjectType.ts +198 -0
- package/src/syntax/types/MapType.ts +49 -0
- package/src/syntax/types/NamedWrappingType.ts +33 -0
- package/src/syntax/types/NullType.ts +30 -0
- package/src/syntax/types/NumberType.ts +34 -0
- package/src/syntax/types/OptionalType.ts +66 -0
- package/src/syntax/types/PromiseType.ts +72 -0
- package/src/syntax/types/RecordType.ts +56 -0
- package/src/syntax/types/ResultWrappingType.ts +53 -0
- package/src/syntax/types/StringType.ts +44 -0
- package/src/syntax/types/StructType.ts +83 -0
- package/src/syntax/types/TupleType.ts +53 -0
- package/src/syntax/types/Type.ts +82 -0
- package/src/syntax/types/VariantType.ts +92 -0
- package/src/syntax/types/VoidType.ts +34 -0
- package/src/syntax/types/getTypeAs.ts +15 -0
- package/src/utils.ts +162 -0
- package/src/views/CppHybridViewComponent.ts +301 -0
- package/src/views/createHostComponentJs.ts +34 -0
- package/src/views/kotlin/KotlinHybridViewManager.ts +258 -0
- package/src/views/swift/SwiftHybridViewManager.ts +153 -0
- package/src/writeFile.ts +27 -0
- package/.jshintignore +0 -6
- package/.jshintrc +0 -3
- package/.npmignore +0 -3
- package/.travis.yml +0 -13
- package/LICENSE +0 -13
- package/browser/nitrogen-min.js +0 -3
- package/browser/nitrogen.js +0 -6369
- package/lib/apiKey.js +0 -67
- package/lib/blob.js +0 -57
- package/lib/commandManager.js +0 -350
- package/lib/device.js +0 -19
- package/lib/memoryStore.js +0 -24
- package/lib/message.js +0 -298
- package/lib/permission.js +0 -121
- package/lib/principal.js +0 -330
- package/lib/service.js +0 -347
- package/lib/session.js +0 -494
- package/lib/user.js +0 -20
- package/publish +0 -2
- package/scripts/build-documentation +0 -4
- package/scripts/build-module +0 -27
- package/scripts/module.js +0 -12
- package/scripts/postamble.js +0 -1
- package/scripts/preamble.js +0 -2
- package/scripts/run-test-server +0 -9
- package/test/config.js +0 -12
- package/test/fixtures/images/image.jpg +0 -0
- package/test/fixtures/images/motion0.jpg +0 -0
- package/test/fixtures/images/motion1.jpg +0 -0
- package/test/fixtures/images/motion2.jpg +0 -0
- package/test/fixtures/index.js +0 -76
- package/test/main.js +0 -5
- package/test/memoryStore.js +0 -22
- package/test/mocha.opts +0 -3
- package/test/units/apiKey.js +0 -46
- package/test/units/blob.js +0 -35
- package/test/units/commandManager.js +0 -67
- package/test/units/device.js +0 -26
- package/test/units/heartbeat.js +0 -28
- package/test/units/message.js +0 -79
- package/test/units/permissions.js +0 -43
- package/test/units/principal.js +0 -116
- package/test/units/service.js +0 -92
- package/test/units/session.js +0 -97
- package/test/units/user.js +0 -48
- package/yuidoc.json +0 -8
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { NitroConfig } from '../../config/NitroConfig.js';
|
|
2
|
+
import { capitalizeName, indent } from '../../utils.js';
|
|
3
|
+
import { createFileMetadataString } from '../helpers.js';
|
|
4
|
+
import { EnumType } from '../types/EnumType.js';
|
|
5
|
+
export function createKotlinEnum(enumType) {
|
|
6
|
+
const members = enumType.enumMembers.map((m) => `${m.name.toUpperCase()}(${m.value})`);
|
|
7
|
+
const packageName = NitroConfig.current.getAndroidPackage('java/kotlin');
|
|
8
|
+
const code = `
|
|
9
|
+
${createFileMetadataString(`${enumType.enumName}.kt`)}
|
|
10
|
+
|
|
11
|
+
package ${packageName}
|
|
12
|
+
|
|
13
|
+
import androidx.annotation.Keep
|
|
14
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Represents the JavaScript enum/union "${enumType.enumName}".
|
|
18
|
+
*/
|
|
19
|
+
@DoNotStrip
|
|
20
|
+
@Keep
|
|
21
|
+
enum class ${enumType.enumName}(@DoNotStrip @Keep val value: Int) {
|
|
22
|
+
${indent(members.join(',\n'), ' ')};
|
|
23
|
+
}
|
|
24
|
+
`.trim();
|
|
25
|
+
const cxxNamespace = NitroConfig.current.getCxxNamespace('c++');
|
|
26
|
+
const jniClassDescriptor = NitroConfig.current.getAndroidPackage('c++/jni', enumType.enumName);
|
|
27
|
+
const cppToJniConverterCode = getCppToJniConverterCode('value', enumType);
|
|
28
|
+
const fbjniCode = `
|
|
29
|
+
${createFileMetadataString(`J${enumType.enumName}.hpp`)}
|
|
30
|
+
|
|
31
|
+
#pragma once
|
|
32
|
+
|
|
33
|
+
#include <fbjni/fbjni.h>
|
|
34
|
+
#include "${enumType.declarationFile.name}"
|
|
35
|
+
|
|
36
|
+
namespace ${cxxNamespace} {
|
|
37
|
+
|
|
38
|
+
using namespace facebook;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The C++ JNI bridge between the C++ enum "${enumType.enumName}" and the the Kotlin enum "${enumType.enumName}".
|
|
42
|
+
*/
|
|
43
|
+
struct J${enumType.enumName} final: public jni::JavaClass<J${enumType.enumName}> {
|
|
44
|
+
public:
|
|
45
|
+
static auto constexpr kJavaDescriptor = "L${jniClassDescriptor};";
|
|
46
|
+
|
|
47
|
+
public:
|
|
48
|
+
/**
|
|
49
|
+
* Convert this Java/Kotlin-based enum to the C++ enum ${enumType.enumName}.
|
|
50
|
+
*/
|
|
51
|
+
[[maybe_unused]]
|
|
52
|
+
[[nodiscard]]
|
|
53
|
+
${enumType.enumName} toCpp() const {
|
|
54
|
+
static const auto clazz = javaClassStatic();
|
|
55
|
+
static const auto fieldOrdinal = clazz->getField<int>("value");
|
|
56
|
+
int ordinal = this->getFieldValue(fieldOrdinal);
|
|
57
|
+
return static_cast<${enumType.enumName}>(ordinal);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public:
|
|
61
|
+
/**
|
|
62
|
+
* Create a Java/Kotlin-based enum with the given C++ enum's value.
|
|
63
|
+
*/
|
|
64
|
+
[[maybe_unused]]
|
|
65
|
+
static jni::alias_ref<J${enumType.enumName}> fromCpp(${enumType.enumName} value) {
|
|
66
|
+
${indent(cppToJniConverterCode, ' ')}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
} // namespace ${cxxNamespace}
|
|
71
|
+
`.trim();
|
|
72
|
+
const files = [];
|
|
73
|
+
files.push({
|
|
74
|
+
content: code,
|
|
75
|
+
language: 'kotlin',
|
|
76
|
+
name: `${enumType.enumName}.kt`,
|
|
77
|
+
subdirectory: NitroConfig.current.getAndroidPackageDirectory(),
|
|
78
|
+
platform: 'android',
|
|
79
|
+
});
|
|
80
|
+
files.push({
|
|
81
|
+
content: fbjniCode,
|
|
82
|
+
language: 'c++',
|
|
83
|
+
name: `J${enumType.enumName}.hpp`,
|
|
84
|
+
subdirectory: [],
|
|
85
|
+
platform: 'android',
|
|
86
|
+
});
|
|
87
|
+
return files;
|
|
88
|
+
}
|
|
89
|
+
function getCppToJniConverterCode(cppValueName, enumType) {
|
|
90
|
+
const jniEnumName = `J${enumType.enumName}`;
|
|
91
|
+
const fields = enumType.enumMembers.map((m) => {
|
|
92
|
+
const fieldName = `field${capitalizeName(m.name)}`;
|
|
93
|
+
return `static const auto ${fieldName} = clazz->getStaticField<${jniEnumName}>("${m.name}");`;
|
|
94
|
+
});
|
|
95
|
+
const cases = enumType.enumMembers.map((m) => {
|
|
96
|
+
const fieldName = `field${capitalizeName(m.name)}`;
|
|
97
|
+
return `
|
|
98
|
+
case ${enumType.enumName}::${m.name}:
|
|
99
|
+
return clazz->getStaticFieldValue(${fieldName});
|
|
100
|
+
`.trim();
|
|
101
|
+
});
|
|
102
|
+
return `
|
|
103
|
+
static const auto clazz = javaClassStatic();
|
|
104
|
+
${fields.join('\n')}
|
|
105
|
+
|
|
106
|
+
switch (${cppValueName}) {
|
|
107
|
+
${indent(cases.join('\n'), ' ')}
|
|
108
|
+
default:
|
|
109
|
+
std::string stringValue = std::to_string(static_cast<int>(${cppValueName}));
|
|
110
|
+
throw std::invalid_argument("Invalid enum value (" + stringValue + "!");
|
|
111
|
+
}
|
|
112
|
+
`.trim();
|
|
113
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
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 { addJNINativeRegistration } from './JNINativeRegistrations.js';
|
|
6
|
+
import { KotlinCxxBridgedType } from './KotlinCxxBridgedType.js';
|
|
7
|
+
export function createKotlinFunction(functionType) {
|
|
8
|
+
const name = functionType.specializationName;
|
|
9
|
+
const packageName = NitroConfig.current.getAndroidPackage('java/kotlin');
|
|
10
|
+
const kotlinReturnType = functionType.returnType.getCode('kotlin');
|
|
11
|
+
const kotlinParams = functionType.parameters.map((p) => `${p.escapedName}: ${p.getCode('kotlin')}`);
|
|
12
|
+
const kotlinParamTypes = functionType.parameters.map((p) => p.getCode('kotlin'));
|
|
13
|
+
const kotlinParamsForward = functionType.parameters.map((p) => p.escapedName);
|
|
14
|
+
const lambdaSignature = `(${kotlinParamTypes.join(', ')}) -> ${kotlinReturnType}`;
|
|
15
|
+
const extraImports = functionType
|
|
16
|
+
.getRequiredImports('kotlin')
|
|
17
|
+
.map((i) => `import ${i.name}`);
|
|
18
|
+
const kotlinCode = `
|
|
19
|
+
${createFileMetadataString(`${name}.kt`)}
|
|
20
|
+
|
|
21
|
+
package ${packageName}
|
|
22
|
+
|
|
23
|
+
import androidx.annotation.Keep
|
|
24
|
+
import com.facebook.jni.HybridData
|
|
25
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
26
|
+
import com.margelo.nitro.core.*
|
|
27
|
+
import dalvik.annotation.optimization.FastNative
|
|
28
|
+
${extraImports.join('\n')}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Represents the JavaScript callback \`${functionType.jsName}\`.
|
|
32
|
+
* This can be either implemented in C++ (in which case it might be a callback coming from JS),
|
|
33
|
+
* or in Kotlin/Java (in which case it is a native callback).
|
|
34
|
+
*/
|
|
35
|
+
@DoNotStrip
|
|
36
|
+
@Keep
|
|
37
|
+
@Suppress("ClassName", "RedundantUnitReturnType")
|
|
38
|
+
fun interface ${name}: ${lambdaSignature} {
|
|
39
|
+
/**
|
|
40
|
+
* Call the given JS callback.
|
|
41
|
+
* @throws Throwable if the JS function itself throws an error, or if the JS function/runtime has already been deleted.
|
|
42
|
+
*/
|
|
43
|
+
@DoNotStrip
|
|
44
|
+
@Keep
|
|
45
|
+
override fun invoke(${kotlinParams.join(', ')}): ${kotlinReturnType}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Represents the JavaScript callback \`${functionType.jsName}\`.
|
|
50
|
+
* This is implemented in C++, via a \`std::function<...>\`.
|
|
51
|
+
* The callback might be coming from JS.
|
|
52
|
+
*/
|
|
53
|
+
@DoNotStrip
|
|
54
|
+
@Keep
|
|
55
|
+
@Suppress(
|
|
56
|
+
"KotlinJniMissingFunction", "unused",
|
|
57
|
+
"RedundantSuppression", "RedundantUnitReturnType", "FunctionName",
|
|
58
|
+
"ConvertSecondaryConstructorToPrimary", "ClassName", "LocalVariableName",
|
|
59
|
+
)
|
|
60
|
+
class ${name}_cxx: ${name} {
|
|
61
|
+
@DoNotStrip
|
|
62
|
+
@Keep
|
|
63
|
+
private val mHybridData: HybridData
|
|
64
|
+
|
|
65
|
+
@DoNotStrip
|
|
66
|
+
@Keep
|
|
67
|
+
private constructor(hybridData: HybridData) {
|
|
68
|
+
mHybridData = hybridData
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@DoNotStrip
|
|
72
|
+
@Keep
|
|
73
|
+
override fun invoke(${kotlinParams.join(', ')}): ${kotlinReturnType}
|
|
74
|
+
= invoke_cxx(${kotlinParamsForward.join(',')})
|
|
75
|
+
|
|
76
|
+
@FastNative
|
|
77
|
+
private external fun invoke_cxx(${kotlinParams.join(', ')}): ${kotlinReturnType}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Represents the JavaScript callback \`${functionType.jsName}\`.
|
|
82
|
+
* This is implemented in Java/Kotlin, via a \`${lambdaSignature}\`.
|
|
83
|
+
* The callback is always coming from native.
|
|
84
|
+
*/
|
|
85
|
+
@DoNotStrip
|
|
86
|
+
@Keep
|
|
87
|
+
@Suppress("ClassName", "RedundantUnitReturnType", "unused")
|
|
88
|
+
class ${name}_java(private val function: ${lambdaSignature}): ${name} {
|
|
89
|
+
@DoNotStrip
|
|
90
|
+
@Keep
|
|
91
|
+
override fun invoke(${kotlinParams.join(', ')}): ${kotlinReturnType} {
|
|
92
|
+
return this.function(${kotlinParamsForward.join(', ')})
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
`.trim();
|
|
96
|
+
const jniInterfaceDescriptor = NitroConfig.current.getAndroidPackage('c++/jni', name);
|
|
97
|
+
const jniClassDescriptor = NitroConfig.current.getAndroidPackage('c++/jni', `${name}_cxx`);
|
|
98
|
+
const bridgedReturn = new KotlinCxxBridgedType(functionType.returnType);
|
|
99
|
+
const cxxNamespace = NitroConfig.current.getCxxNamespace('c++');
|
|
100
|
+
const typename = functionType.getCode('c++');
|
|
101
|
+
// call() Java -> C++
|
|
102
|
+
const cppParams = functionType.parameters.map((p) => {
|
|
103
|
+
const bridge = new KotlinCxxBridgedType(p);
|
|
104
|
+
const type = bridge.asJniReferenceType('alias');
|
|
105
|
+
return `${type} ${p.escapedName}`;
|
|
106
|
+
});
|
|
107
|
+
const paramsForward = functionType.parameters.map((p) => {
|
|
108
|
+
const bridge = new KotlinCxxBridgedType(p);
|
|
109
|
+
return bridge.parseFromKotlinToCpp(p.escapedName, 'c++', false);
|
|
110
|
+
});
|
|
111
|
+
// call() C++ -> Java
|
|
112
|
+
const jniParams = functionType.parameters.map((p) => {
|
|
113
|
+
if (p.canBePassedByReference) {
|
|
114
|
+
return `const ${p.getCode('c++')}& ${p.escapedName}`;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
return `${p.getCode('c++')} ${p.escapedName}`;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
const jniParamsForward = [
|
|
121
|
+
'self()',
|
|
122
|
+
...functionType.parameters.map((p) => {
|
|
123
|
+
const bridge = new KotlinCxxBridgedType(p);
|
|
124
|
+
return bridge.parseFromCppToKotlin(p.escapedName, 'c++', false);
|
|
125
|
+
}),
|
|
126
|
+
];
|
|
127
|
+
const jniSignature = `${bridgedReturn.asJniReferenceType('local')}(${functionType.parameters
|
|
128
|
+
.map((p) => {
|
|
129
|
+
const bridge = new KotlinCxxBridgedType(p);
|
|
130
|
+
return `${bridge.asJniReferenceType('alias')} /* ${p.escapedName} */`;
|
|
131
|
+
})
|
|
132
|
+
.join(', ')})`;
|
|
133
|
+
let cppCallBody;
|
|
134
|
+
let jniCallBody;
|
|
135
|
+
if (functionType.returnType.kind === 'void') {
|
|
136
|
+
// It returns void
|
|
137
|
+
cppCallBody = `_func(${indent(paramsForward.join(', '), ' ')});`;
|
|
138
|
+
jniCallBody = `
|
|
139
|
+
static const auto method = javaClassStatic()->getMethod<${jniSignature}>("invoke");
|
|
140
|
+
method(${jniParamsForward.join(', ')});
|
|
141
|
+
`.trim();
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
// It returns a type!
|
|
145
|
+
cppCallBody = `
|
|
146
|
+
${functionType.returnType.getCode('c++')} __result = _func(${indent(paramsForward.join(', '), ' ')});
|
|
147
|
+
return ${bridgedReturn.parseFromCppToKotlin('__result', 'c++')};
|
|
148
|
+
`.trim();
|
|
149
|
+
jniCallBody = `
|
|
150
|
+
static const auto method = javaClassStatic()->getMethod<${jniSignature}>("invoke");
|
|
151
|
+
auto __result = method(${jniParamsForward.join(', ')});
|
|
152
|
+
return ${bridgedReturn.parseFromKotlinToCpp('__result', 'c++', false)};
|
|
153
|
+
`.trim();
|
|
154
|
+
}
|
|
155
|
+
const bridged = new KotlinCxxBridgedType(functionType);
|
|
156
|
+
const imports = bridged
|
|
157
|
+
.getRequiredImports('c++')
|
|
158
|
+
.filter((i) => i.name !== `J${name}.hpp`);
|
|
159
|
+
const includes = imports.map((i) => includeHeader(i)).filter(isNotDuplicate);
|
|
160
|
+
const fbjniCode = `
|
|
161
|
+
${createFileMetadataString(`J${name}.hpp`)}
|
|
162
|
+
|
|
163
|
+
#pragma once
|
|
164
|
+
|
|
165
|
+
#include <fbjni/fbjni.h>
|
|
166
|
+
#include <functional>
|
|
167
|
+
|
|
168
|
+
${includes.join('\n')}
|
|
169
|
+
|
|
170
|
+
namespace ${cxxNamespace} {
|
|
171
|
+
|
|
172
|
+
using namespace facebook;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Represents the Java/Kotlin callback \`${functionType.getCode('kotlin')}\`.
|
|
176
|
+
* This can be passed around between C++ and Java/Kotlin.
|
|
177
|
+
*/
|
|
178
|
+
struct J${name}: public jni::JavaClass<J${name}> {
|
|
179
|
+
public:
|
|
180
|
+
static auto constexpr kJavaDescriptor = "L${jniInterfaceDescriptor};";
|
|
181
|
+
|
|
182
|
+
public:
|
|
183
|
+
/**
|
|
184
|
+
* Invokes the function this \`J${name}\` instance holds through JNI.
|
|
185
|
+
*/
|
|
186
|
+
${functionType.returnType.getCode('c++')} invoke(${jniParams.join(', ')}) const {
|
|
187
|
+
${indent(jniCallBody, ' ')}
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* An implementation of ${name} that is backed by a C++ implementation (using \`std::function<...>\`)
|
|
193
|
+
*/
|
|
194
|
+
struct J${name}_cxx final: public jni::HybridClass<J${name}_cxx, J${name}> {
|
|
195
|
+
public:
|
|
196
|
+
static jni::local_ref<J${name}::javaobject> fromCpp(const ${typename}& func) {
|
|
197
|
+
return J${name}_cxx::newObjectCxxArgs(func);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
public:
|
|
201
|
+
/**
|
|
202
|
+
* Invokes the C++ \`std::function<...>\` this \`J${name}_cxx\` instance holds.
|
|
203
|
+
*/
|
|
204
|
+
${bridgedReturn.asJniReferenceType('local')} invoke_cxx(${cppParams.join(', ')}) {
|
|
205
|
+
${indent(cppCallBody, ' ')}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
public:
|
|
209
|
+
[[nodiscard]]
|
|
210
|
+
inline const ${typename}& getFunction() const {
|
|
211
|
+
return _func;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
public:
|
|
215
|
+
static auto constexpr kJavaDescriptor = "L${jniClassDescriptor};";
|
|
216
|
+
static void registerNatives() {
|
|
217
|
+
registerHybrid({makeNativeMethod("invoke_cxx", J${name}_cxx::invoke_cxx)});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private:
|
|
221
|
+
explicit J${name}_cxx(const ${typename}& func): _func(func) { }
|
|
222
|
+
|
|
223
|
+
private:
|
|
224
|
+
friend HybridBase;
|
|
225
|
+
${typename} _func;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
} // namespace ${cxxNamespace}
|
|
229
|
+
`.trim();
|
|
230
|
+
// Make sure we register all native JNI methods on app startup
|
|
231
|
+
addJNINativeRegistration({
|
|
232
|
+
namespace: cxxNamespace,
|
|
233
|
+
className: `J${name}_cxx`,
|
|
234
|
+
import: {
|
|
235
|
+
name: `J${name}.hpp`,
|
|
236
|
+
space: 'user',
|
|
237
|
+
language: 'c++',
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
const files = [];
|
|
241
|
+
files.push({
|
|
242
|
+
content: kotlinCode,
|
|
243
|
+
language: 'kotlin',
|
|
244
|
+
name: `${name}.kt`,
|
|
245
|
+
subdirectory: NitroConfig.current.getAndroidPackageDirectory(),
|
|
246
|
+
platform: 'android',
|
|
247
|
+
});
|
|
248
|
+
files.push({
|
|
249
|
+
content: fbjniCode,
|
|
250
|
+
language: 'c++',
|
|
251
|
+
name: `J${name}.hpp`,
|
|
252
|
+
subdirectory: [],
|
|
253
|
+
platform: 'android',
|
|
254
|
+
});
|
|
255
|
+
return files;
|
|
256
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
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 { Method } from '../Method.js';
|
|
7
|
+
import { Property } from '../Property.js';
|
|
8
|
+
import { HybridObjectType } from '../types/HybridObjectType.js';
|
|
9
|
+
import { createFbjniHybridObject } from './FbjniHybridObject.js';
|
|
10
|
+
import { KotlinCxxBridgedType } from './KotlinCxxBridgedType.js';
|
|
11
|
+
export function createKotlinHybridObject(spec) {
|
|
12
|
+
const name = getHybridObjectName(spec.name);
|
|
13
|
+
const properties = spec.properties
|
|
14
|
+
.map((p) => getPropertyForwardImplementation(p))
|
|
15
|
+
.join('\n\n');
|
|
16
|
+
const methods = spec.methods
|
|
17
|
+
.map((m) => getMethodForwardImplementation(m))
|
|
18
|
+
.join('\n\n');
|
|
19
|
+
const extraImports = [
|
|
20
|
+
...spec.properties.flatMap((p) => p.getRequiredImports('kotlin')),
|
|
21
|
+
...spec.methods.flatMap((m) => m.getRequiredImports('kotlin')),
|
|
22
|
+
...spec.baseTypes.flatMap((b) => new HybridObjectType(b).getRequiredImports('kotlin')),
|
|
23
|
+
];
|
|
24
|
+
let kotlinBase = spec.isHybridView ? 'HybridView' : 'HybridObject';
|
|
25
|
+
if (spec.baseTypes.length > 0) {
|
|
26
|
+
if (spec.baseTypes.length > 1) {
|
|
27
|
+
throw new Error(`${name.T}: Inheriting from multiple HybridObject bases is not yet supported in Kotlin!`);
|
|
28
|
+
}
|
|
29
|
+
const base = spec.baseTypes[0];
|
|
30
|
+
const baseHybrid = new HybridObjectType(base);
|
|
31
|
+
kotlinBase = baseHybrid.getCode('kotlin');
|
|
32
|
+
}
|
|
33
|
+
const imports = [];
|
|
34
|
+
imports.push('import com.margelo.nitro.core.*');
|
|
35
|
+
if (spec.isHybridView) {
|
|
36
|
+
imports.push('import com.margelo.nitro.views.*');
|
|
37
|
+
}
|
|
38
|
+
imports.push(...extraImports.map((i) => `import ${i.name}`).filter(isNotDuplicate));
|
|
39
|
+
const javaPackage = spec.config.getAndroidPackage('java/kotlin');
|
|
40
|
+
// 1. Create Kotlin abstract class definition
|
|
41
|
+
const abstractClassCode = `
|
|
42
|
+
${createFileMetadataString(`${name.HybridTSpec}.kt`)}
|
|
43
|
+
|
|
44
|
+
package ${javaPackage}
|
|
45
|
+
|
|
46
|
+
import androidx.annotation.Keep
|
|
47
|
+
import com.facebook.jni.HybridData
|
|
48
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
49
|
+
${imports.join('\n')}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A Kotlin class representing the ${spec.name} HybridObject.
|
|
53
|
+
* Implement this abstract class to create Kotlin-based instances of ${spec.name}.
|
|
54
|
+
*/
|
|
55
|
+
@DoNotStrip
|
|
56
|
+
@Keep
|
|
57
|
+
@Suppress(
|
|
58
|
+
"KotlinJniMissingFunction", "unused",
|
|
59
|
+
"RedundantSuppression", "RedundantUnitReturnType", "SimpleRedundantLet",
|
|
60
|
+
"LocalVariableName", "PropertyName", "PrivatePropertyName", "FunctionName"
|
|
61
|
+
)
|
|
62
|
+
abstract class ${name.HybridTSpec}: ${kotlinBase}() {
|
|
63
|
+
@DoNotStrip
|
|
64
|
+
private var mHybridData: HybridData = initHybrid()
|
|
65
|
+
|
|
66
|
+
init {
|
|
67
|
+
super.updateNative(mHybridData)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
override fun updateNative(hybridData: HybridData) {
|
|
71
|
+
mHybridData = hybridData
|
|
72
|
+
super.updateNative(hybridData)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Properties
|
|
76
|
+
${indent(properties, ' ')}
|
|
77
|
+
|
|
78
|
+
// Methods
|
|
79
|
+
${indent(methods, ' ')}
|
|
80
|
+
|
|
81
|
+
private external fun initHybrid(): HybridData
|
|
82
|
+
|
|
83
|
+
companion object {
|
|
84
|
+
private const val TAG = "${name.HybridTSpec}"
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
`.trim();
|
|
88
|
+
// 2. Create C++ (fbjni) bindings
|
|
89
|
+
const cppFiles = createFbjniHybridObject(spec);
|
|
90
|
+
// 3. Create enums or structs in Kotlin
|
|
91
|
+
const extraFiles = getAllTypes(spec)
|
|
92
|
+
.map((t) => new KotlinCxxBridgedType(t))
|
|
93
|
+
.flatMap((b) => b.getExtraFiles());
|
|
94
|
+
const files = [];
|
|
95
|
+
files.push({
|
|
96
|
+
content: abstractClassCode,
|
|
97
|
+
language: 'kotlin',
|
|
98
|
+
name: `${name.HybridTSpec}.kt`,
|
|
99
|
+
subdirectory: spec.config.getAndroidPackageDirectory(),
|
|
100
|
+
platform: 'android',
|
|
101
|
+
});
|
|
102
|
+
files.push(...cppFiles);
|
|
103
|
+
files.push(...extraFiles);
|
|
104
|
+
if (spec.isHybridView) {
|
|
105
|
+
const viewFiles = createKotlinHybridViewManager(spec);
|
|
106
|
+
files.push(...viewFiles);
|
|
107
|
+
}
|
|
108
|
+
return files;
|
|
109
|
+
}
|
|
110
|
+
function getMethodForwardImplementation(method) {
|
|
111
|
+
const bridgedReturn = new KotlinCxxBridgedType(method.returnType);
|
|
112
|
+
const requiresBridge = bridgedReturn.needsSpecialHandling ||
|
|
113
|
+
method.parameters.some((p) => {
|
|
114
|
+
const bridged = new KotlinCxxBridgedType(p.type);
|
|
115
|
+
return bridged.needsSpecialHandling;
|
|
116
|
+
});
|
|
117
|
+
if (requiresBridge) {
|
|
118
|
+
const paramsSignature = method.parameters.map((p) => {
|
|
119
|
+
const bridge = new KotlinCxxBridgedType(p.type);
|
|
120
|
+
return `${p.name}: ${bridge.getTypeCode('kotlin')}`;
|
|
121
|
+
});
|
|
122
|
+
const paramsForward = method.parameters.map((p) => {
|
|
123
|
+
const bridge = new KotlinCxxBridgedType(p.type);
|
|
124
|
+
return bridge.parseFromCppToKotlin(p.name, 'kotlin');
|
|
125
|
+
});
|
|
126
|
+
const returnForward = bridgedReturn.parseFromKotlinToCpp('__result', 'kotlin');
|
|
127
|
+
const code = method.getCode('kotlin', { virtual: true });
|
|
128
|
+
return `
|
|
129
|
+
${code}
|
|
130
|
+
|
|
131
|
+
@DoNotStrip
|
|
132
|
+
@Keep
|
|
133
|
+
private fun ${method.name}_cxx(${paramsSignature.join(', ')}): ${bridgedReturn.getTypeCode('kotlin')} {
|
|
134
|
+
val __result = ${method.name}(${paramsForward.join(', ')})
|
|
135
|
+
return ${returnForward}
|
|
136
|
+
}
|
|
137
|
+
`.trim();
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
const code = method.getCode('kotlin', { doNotStrip: true, virtual: true });
|
|
141
|
+
return code;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function getPropertyForwardImplementation(property) {
|
|
145
|
+
const bridged = new KotlinCxxBridgedType(property.type);
|
|
146
|
+
if (bridged.needsSpecialHandling) {
|
|
147
|
+
let keyword = property.isReadonly ? 'val' : 'var';
|
|
148
|
+
let lines = [];
|
|
149
|
+
lines.push(`
|
|
150
|
+
@Keep
|
|
151
|
+
@DoNotStrip
|
|
152
|
+
get() {
|
|
153
|
+
return ${indent(bridged.parseFromKotlinToCpp(property.name, 'kotlin'), ' ')}
|
|
154
|
+
}
|
|
155
|
+
`.trim());
|
|
156
|
+
if (!property.isReadonly) {
|
|
157
|
+
lines.push(`
|
|
158
|
+
@Keep
|
|
159
|
+
@DoNotStrip
|
|
160
|
+
set(value) {
|
|
161
|
+
${property.name} = ${indent(bridged.parseFromCppToKotlin('value', 'kotlin'), ' ')}
|
|
162
|
+
}
|
|
163
|
+
`.trim());
|
|
164
|
+
}
|
|
165
|
+
const code = property.getCode('kotlin', { virtual: true });
|
|
166
|
+
return `
|
|
167
|
+
${code}
|
|
168
|
+
|
|
169
|
+
private ${keyword} ${property.name}_cxx: ${bridged.getTypeCode('kotlin')}
|
|
170
|
+
${indent(lines.join('\n'), ' ')}
|
|
171
|
+
`.trim();
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
const code = property.getCode('kotlin', { doNotStrip: true, virtual: true });
|
|
175
|
+
return code;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { SourceImport } from '../SourceFile.js';
|
|
2
|
+
interface Props {
|
|
3
|
+
/**
|
|
4
|
+
* The name of the Hybrid Object under which it should be registered and exposed to JS to.
|
|
5
|
+
*/
|
|
6
|
+
hybridObjectName: string;
|
|
7
|
+
/**
|
|
8
|
+
* The name of the Kotlin/Java class that will be default-constructed
|
|
9
|
+
*/
|
|
10
|
+
jniClassName: string;
|
|
11
|
+
}
|
|
12
|
+
interface JNIHybridObjectRegistration {
|
|
13
|
+
cppCode: string;
|
|
14
|
+
requiredImports: SourceImport[];
|
|
15
|
+
}
|
|
16
|
+
export declare function createJNIHybridObjectRegistration({ hybridObjectName, jniClassName, }: Props): JNIHybridObjectRegistration;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { NitroConfig } from '../../config/NitroConfig.js';
|
|
2
|
+
import { getHybridObjectName } from '../getHybridObjectName.js';
|
|
3
|
+
export function createJNIHybridObjectRegistration({ hybridObjectName, jniClassName, }) {
|
|
4
|
+
const { JHybridTSpec } = getHybridObjectName(hybridObjectName);
|
|
5
|
+
const jniNamespace = NitroConfig.current.getAndroidPackage('c++/jni', jniClassName);
|
|
6
|
+
return {
|
|
7
|
+
requiredImports: [
|
|
8
|
+
{ name: `${JHybridTSpec}.hpp`, language: 'c++', space: 'user' },
|
|
9
|
+
{
|
|
10
|
+
name: 'NitroModules/DefaultConstructableObject.hpp',
|
|
11
|
+
language: 'c++',
|
|
12
|
+
space: 'system',
|
|
13
|
+
},
|
|
14
|
+
],
|
|
15
|
+
cppCode: `
|
|
16
|
+
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
17
|
+
"${hybridObjectName}",
|
|
18
|
+
[]() -> std::shared_ptr<HybridObject> {
|
|
19
|
+
static DefaultConstructableObject<${JHybridTSpec}::javaobject> object("${jniNamespace}");
|
|
20
|
+
auto instance = object.create();
|
|
21
|
+
return instance->cthis()->shared();
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
`.trim(),
|
|
25
|
+
};
|
|
26
|
+
}
|