react-native-nitro-modules 0.0.2 → 0.0.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/NitroModules.podspec +49 -0
- package/android/CMakeLists.txt +44 -7
- package/android/build.gradle +28 -24
- package/cpp/core/AnyMap.cpp +181 -0
- package/cpp/core/AnyMap.hpp +191 -0
- package/cpp/core/HybridContext.hpp +51 -0
- package/cpp/core/HybridObject.cpp +220 -0
- package/cpp/core/HybridObject.hpp +241 -0
- package/cpp/core/PointerHolder.hpp +93 -0
- package/cpp/jsi/ArrayBuffer.hpp +79 -0
- package/cpp/jsi/JSICache.hpp +145 -0
- package/cpp/jsi/JSIConverter.hpp +610 -0
- package/cpp/jsi/Promise.cpp +54 -0
- package/cpp/jsi/Promise.hpp +54 -0
- package/cpp/platform/ThreadUtils.hpp +23 -0
- package/cpp/registry/HybridObjectRegistry.cpp +57 -0
- package/cpp/registry/HybridObjectRegistry.hpp +44 -0
- package/cpp/test-object/TestHybridObject.cpp +37 -0
- package/cpp/test-object/TestHybridObject.hpp +87 -0
- package/cpp/threading/CallInvokerDispatcher.hpp +33 -0
- package/cpp/threading/Dispatcher.cpp +56 -0
- package/cpp/threading/Dispatcher.hpp +82 -0
- package/cpp/turbomodule/NativeNitroModules.cpp +70 -0
- package/cpp/turbomodule/NativeNitroModules.h +7 -0
- package/cpp/turbomodule/NativeNitroModules.hpp +35 -0
- package/cpp/turbomodule/RegisterNativeNitroModules.cpp +33 -0
- package/cpp/turbomodule/RegisterNativeNitroModules.hpp +21 -0
- package/cpp/utils/BorrowingReference+Owning.hpp +34 -0
- package/cpp/utils/BorrowingReference.hpp +115 -0
- package/cpp/utils/DoesClassExist.hpp +23 -0
- package/cpp/utils/GetRuntimeID.hpp +28 -0
- package/cpp/utils/NitroDefines.hpp +32 -0
- package/cpp/utils/NitroHash.hpp +42 -0
- package/cpp/utils/NitroLogger.hpp +55 -0
- package/cpp/utils/OwningLock.hpp +54 -0
- package/cpp/utils/OwningReference.hpp +214 -0
- package/cpp/utils/TypeInfo.hpp +81 -0
- package/ios/core/HybridObjectSpec.swift +52 -0
- package/ios/core/RuntimeError.swift +17 -0
- package/ios/platform/ThreadUtils.cpp +28 -0
- package/ios/turbomodule/NitroModuleOnLoad.mm +31 -0
- package/lib/AnyMap.d.ts +16 -0
- package/lib/AnyMap.js +1 -0
- package/lib/HybridObject.d.ts +57 -0
- package/lib/HybridObject.js +1 -0
- package/lib/ModuleNotFoundError.d.ts +6 -0
- package/lib/ModuleNotFoundError.js +61 -0
- package/lib/NativeNitro.d.ts +8 -0
- package/lib/NativeNitro.js +3 -0
- package/lib/NativeNitroModules.d.ts +7 -0
- package/lib/NativeNitroModules.js +17 -0
- package/lib/NitroModules.d.ts +17 -0
- package/lib/NitroModules.js +21 -0
- package/lib/__tests__/index.test.d.ts +0 -0
- package/lib/__tests__/index.test.js +2 -0
- package/lib/commonjs/AnyMap.js +2 -0
- package/lib/commonjs/AnyMap.js.map +1 -0
- package/lib/commonjs/HybridObject.js +2 -0
- package/lib/commonjs/HybridObject.js.map +1 -0
- package/lib/commonjs/ModuleNotFoundError.js +72 -0
- package/lib/commonjs/ModuleNotFoundError.js.map +1 -0
- package/lib/commonjs/NativeNitroModules.js +24 -0
- package/lib/commonjs/NativeNitroModules.js.map +1 -0
- package/lib/commonjs/NitroModules.js +32 -0
- package/lib/commonjs/NitroModules.js.map +1 -0
- package/lib/commonjs/createTestObject.js +15 -0
- package/lib/commonjs/createTestObject.js.map +1 -0
- package/lib/commonjs/index.js +44 -5
- package/lib/commonjs/index.js.map +1 -1
- package/lib/createTestObject.d.ts +22 -0
- package/lib/createTestObject.js +7 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +4 -0
- package/lib/module/AnyMap.js +2 -0
- package/lib/module/AnyMap.js.map +1 -0
- package/lib/module/HybridObject.js +2 -0
- package/lib/module/HybridObject.js.map +1 -0
- package/lib/module/ModuleNotFoundError.js +65 -0
- package/lib/module/ModuleNotFoundError.js.map +1 -0
- package/lib/module/NativeNitroModules.js +18 -0
- package/lib/module/NativeNitroModules.js.map +1 -0
- package/lib/module/NitroModules.js +27 -0
- package/lib/module/NitroModules.js.map +1 -0
- package/lib/module/createTestObject.js +8 -0
- package/lib/module/createTestObject.js.map +1 -0
- package/lib/module/index.js +4 -4
- package/lib/module/index.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +76 -49
- package/react-native.config.js +16 -0
- package/src/AnyMap.ts +22 -0
- package/src/HybridObject.ts +58 -0
- package/src/ModuleNotFoundError.ts +90 -0
- package/src/NativeNitroModules.ts +26 -0
- package/src/NitroModules.ts +30 -0
- package/src/__tests__/index.test.tsx +1 -0
- package/src/createTestObject.ts +40 -0
- package/src/index.ts +4 -0
- package/LICENSE +0 -20
- package/README.md +0 -32
- package/android/cpp-adapter.cpp +0 -8
- package/android/src/main/AndroidManifest.xml +0 -3
- package/android/src/main/AndroidManifestNew.xml +0 -2
- package/android/src/main/java/com/nitro/NitroModule.java +0 -34
- package/android/src/main/java/com/nitro/NitroPackage.java +0 -44
- package/cpp/react-native-nitro.cpp +0 -7
- package/cpp/react-native-nitro.h +0 -8
- package/ios/Nitro.h +0 -15
- package/ios/Nitro.mm +0 -21
- package/lib/commonjs/NativeNitro.js +0 -9
- package/lib/commonjs/NativeNitro.js.map +0 -1
- package/lib/module/NativeNitro.js +0 -3
- package/lib/module/NativeNitro.js.map +0 -1
- package/lib/typescript/src/NativeNitro.d.ts +0 -7
- package/lib/typescript/src/NativeNitro.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts +0 -2
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/react-native-nitro.podspec +0 -41
- package/src/NativeNitro.ts +0 -8
- package/src/index.tsx +0 -5
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::UI.puts "[NitroModules] Your app is boosted by nitro modules! 🔥"
|
|
6
|
+
|
|
7
|
+
Pod::Spec.new do |s|
|
|
8
|
+
s.name = "NitroModules"
|
|
9
|
+
s.version = package["version"]
|
|
10
|
+
s.summary = package["description"]
|
|
11
|
+
s.homepage = package["homepage"]
|
|
12
|
+
s.license = package["license"]
|
|
13
|
+
s.authors = package["author"]
|
|
14
|
+
|
|
15
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
16
|
+
s.source = { :git => "https://github.com/mrousavy/react-native-nitro.git", :tag => "#{s.version}" }
|
|
17
|
+
|
|
18
|
+
# VisionCamera Core C++ bindings
|
|
19
|
+
s.source_files = [
|
|
20
|
+
# Shared C++ codebase
|
|
21
|
+
"cpp/**/*.{h,hpp}",
|
|
22
|
+
"cpp/**/*.{c,cpp}",
|
|
23
|
+
# iOS codebase
|
|
24
|
+
"ios/**/*.{h,hpp}",
|
|
25
|
+
"ios/**/*.{c,cpp}",
|
|
26
|
+
"ios/**/*.{m,mm}",
|
|
27
|
+
"ios/**/*.swift",
|
|
28
|
+
]
|
|
29
|
+
s.public_header_files = [
|
|
30
|
+
# Public C++ headers will be exposed in modulemap (for Swift)
|
|
31
|
+
"cpp/core/HybridObject.hpp",
|
|
32
|
+
"cpp/core/HybridContext.hpp",
|
|
33
|
+
"cpp/registry/HybridObjectRegistry.hpp",
|
|
34
|
+
"cpp/jsi/JSIConverter.hpp",
|
|
35
|
+
"cpp/jsi/ArrayBuffer.hpp",
|
|
36
|
+
"cpp/threading/Dispatcher.hpp",
|
|
37
|
+
"cpp/utils/NitroHash.hpp",
|
|
38
|
+
"cpp/utils/NitroDefines.hpp",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
s.pod_target_xcconfig = {
|
|
42
|
+
# Use C++ 20
|
|
43
|
+
"CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
|
|
44
|
+
# Enables C++ <-> Swift interop (by default it's only C)
|
|
45
|
+
"SWIFT_OBJC_INTEROP_MODE" => "objcxx",
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
install_modules_dependencies(s)
|
|
49
|
+
end
|
package/android/CMakeLists.txt
CHANGED
|
@@ -1,15 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
project(NitroModules)
|
|
2
|
+
cmake_minimum_required(VERSION 3.9.0)
|
|
3
3
|
|
|
4
4
|
set (CMAKE_VERBOSE_MAKEFILE ON)
|
|
5
|
-
set (CMAKE_CXX_STANDARD
|
|
5
|
+
set (CMAKE_CXX_STANDARD 20)
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
# Find all C++ files (shared and platform specifics)
|
|
8
|
+
file(GLOB shared_files
|
|
9
|
+
"../cpp/**/*.cpp"
|
|
10
|
+
)
|
|
11
|
+
file(GLOB android_files
|
|
12
|
+
"src/main/cpp/**/*.cpp"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
# Create library "NitroModules" and add all C++ files to it
|
|
16
|
+
add_library(NitroModules SHARED
|
|
17
|
+
${shared_files}
|
|
18
|
+
${android_files}
|
|
10
19
|
)
|
|
11
20
|
|
|
12
21
|
# Specifies a path to native header files.
|
|
13
22
|
include_directories(
|
|
14
|
-
|
|
23
|
+
# Shared C++ includes
|
|
24
|
+
../cpp/core
|
|
25
|
+
../cpp/jsi
|
|
26
|
+
../cpp/platform
|
|
27
|
+
../cpp/registry
|
|
28
|
+
../cpp/test-object
|
|
29
|
+
../cpp/threading
|
|
30
|
+
../cpp/turbomodule
|
|
31
|
+
../cpp/utils
|
|
32
|
+
# Android-specific C++ includes
|
|
33
|
+
src/main/cpp/core
|
|
34
|
+
src/main/cpp/registry
|
|
35
|
+
src/main/cpp/platform
|
|
36
|
+
src/main/cpp/utils
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# Find required libraries
|
|
40
|
+
find_library(LOG_LIB log)
|
|
41
|
+
find_package(fbjni REQUIRED NitroConfig)
|
|
42
|
+
find_package(ReactAndroid REQUIRED NitroConfig)
|
|
43
|
+
|
|
44
|
+
# Link all libraries together
|
|
45
|
+
target_link_libraries(
|
|
46
|
+
NitroModules
|
|
47
|
+
${LOG_LIB} # <-- Logcat logger
|
|
48
|
+
android # <-- Android JNI core
|
|
49
|
+
fbjni::fbjni # <-- Facebook C++ JNI helpers
|
|
50
|
+
ReactAndroid::jsi # <-- RN: JSI
|
|
51
|
+
ReactAndroid::react_nativemodule_core # <-- RN: TurboModules Core
|
|
15
52
|
)
|
package/android/build.gradle
CHANGED
|
@@ -19,6 +19,7 @@ def isNewArchitectureEnabled() {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
apply plugin: "com.android.library"
|
|
22
|
+
apply plugin: 'org.jetbrains.kotlin.android'
|
|
22
23
|
|
|
23
24
|
if (isNewArchitectureEnabled()) {
|
|
24
25
|
apply plugin: "com.facebook.react"
|
|
@@ -32,25 +33,8 @@ def getExtOrIntegerDefault(name) {
|
|
|
32
33
|
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Nitro_" + name]).toInteger()
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
def supportsNamespace() {
|
|
36
|
-
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
|
|
37
|
-
def major = parsed[0].toInteger()
|
|
38
|
-
def minor = parsed[1].toInteger()
|
|
39
|
-
|
|
40
|
-
// Namespace support was added in 7.3.0
|
|
41
|
-
return (major == 7 && minor >= 3) || major >= 8
|
|
42
|
-
}
|
|
43
|
-
|
|
44
36
|
android {
|
|
45
|
-
|
|
46
|
-
namespace "com.nitro"
|
|
47
|
-
|
|
48
|
-
sourceSets {
|
|
49
|
-
main {
|
|
50
|
-
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
37
|
+
namespace "com.margelo.nitro"
|
|
54
38
|
|
|
55
39
|
ndkVersion getExtOrDefault("ndkVersion")
|
|
56
40
|
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
@@ -63,6 +47,7 @@ android {
|
|
|
63
47
|
externalNativeBuild {
|
|
64
48
|
cmake {
|
|
65
49
|
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
|
|
50
|
+
arguments "-DANDROID_STL=c++_shared"
|
|
66
51
|
abiFilters (*reactNativeArchitectures())
|
|
67
52
|
}
|
|
68
53
|
}
|
|
@@ -76,6 +61,14 @@ android {
|
|
|
76
61
|
|
|
77
62
|
buildFeatures {
|
|
78
63
|
buildConfig true
|
|
64
|
+
prefab true
|
|
65
|
+
prefabPublishing true
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
prefab {
|
|
69
|
+
NitroModules {
|
|
70
|
+
headers "${project.buildDir}/headers/nitromodules/"
|
|
71
|
+
}
|
|
79
72
|
}
|
|
80
73
|
|
|
81
74
|
buildTypes {
|
|
@@ -96,10 +89,10 @@ android {
|
|
|
96
89
|
sourceSets {
|
|
97
90
|
main {
|
|
98
91
|
if (isNewArchitectureEnabled()) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
92
|
+
java.srcDirs += [
|
|
93
|
+
// React Codegen files
|
|
94
|
+
"${project.buildDir}/generated/source/codegen/java"
|
|
95
|
+
]
|
|
103
96
|
}
|
|
104
97
|
}
|
|
105
98
|
}
|
|
@@ -121,7 +114,18 @@ dependencies {
|
|
|
121
114
|
if (isNewArchitectureEnabled()) {
|
|
122
115
|
react {
|
|
123
116
|
jsRootDir = file("../src/")
|
|
124
|
-
libraryName = "
|
|
125
|
-
codegenJavaPackageName = "com.nitro"
|
|
117
|
+
libraryName = "NitroModules"
|
|
118
|
+
codegenJavaPackageName = "com.margelo.nitro"
|
|
126
119
|
}
|
|
127
120
|
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
task prepareHeaders(type: Copy) {
|
|
124
|
+
from fileTree('./src/main/cpp').filter { it.isFile() }
|
|
125
|
+
from fileTree('../cpp/').filter { it.isFile() }
|
|
126
|
+
include "*.hpp"
|
|
127
|
+
into "${project.buildDir}/headers/nitromodules/NitroModules/"
|
|
128
|
+
includeEmptyDirs = false
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
preBuild.dependsOn(prepareHeaders)
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Created by Marc Rousavy on 30.07.24.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#include "AnyMap.hpp"
|
|
6
|
+
|
|
7
|
+
namespace margelo::nitro {
|
|
8
|
+
|
|
9
|
+
// Contains
|
|
10
|
+
bool AnyMap::contains(const std::string& key) const {
|
|
11
|
+
return _map.contains(key);
|
|
12
|
+
}
|
|
13
|
+
void AnyMap::remove(const std::string& key) {
|
|
14
|
+
_map.erase(key);
|
|
15
|
+
}
|
|
16
|
+
void AnyMap::clear() noexcept {
|
|
17
|
+
_map.clear();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Is
|
|
21
|
+
bool AnyMap::isNull(const std::string& key) const {
|
|
22
|
+
auto found = _map.find(key);
|
|
23
|
+
if (found == _map.end()) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return std::holds_alternative<std::monostate>(found->second);
|
|
27
|
+
}
|
|
28
|
+
bool AnyMap::isDouble(const std::string& key) const {
|
|
29
|
+
auto found = _map.find(key);
|
|
30
|
+
if (found == _map.end()) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return std::holds_alternative<double>(found->second);
|
|
34
|
+
}
|
|
35
|
+
bool AnyMap::isBoolean(const std::string& key) const {
|
|
36
|
+
auto found = _map.find(key);
|
|
37
|
+
if (found == _map.end()) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return std::holds_alternative<bool>(found->second);
|
|
41
|
+
}
|
|
42
|
+
bool AnyMap::isBigInt(const std::string& key) const {
|
|
43
|
+
auto found = _map.find(key);
|
|
44
|
+
if (found == _map.end()) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return std::holds_alternative<int64_t>(found->second);
|
|
48
|
+
}
|
|
49
|
+
bool AnyMap::isString(const std::string& key) const {
|
|
50
|
+
auto found = _map.find(key);
|
|
51
|
+
if (found == _map.end()) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
return std::holds_alternative<std::string>(found->second);
|
|
55
|
+
}
|
|
56
|
+
bool AnyMap::isArray(const std::string& key) const {
|
|
57
|
+
auto found = _map.find(key);
|
|
58
|
+
if (found == _map.end()) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return std::holds_alternative<AnyArray>(found->second);
|
|
62
|
+
}
|
|
63
|
+
bool AnyMap::isObject(const std::string& key) const {
|
|
64
|
+
auto found = _map.find(key);
|
|
65
|
+
if (found == _map.end()) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return std::holds_alternative<AnyObject>(found->second);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Get
|
|
72
|
+
std::monostate AnyMap::getNull(const std::string& key) const {
|
|
73
|
+
auto found = _map.find(key);
|
|
74
|
+
if (found == _map.end()) {
|
|
75
|
+
throw std::runtime_error("The key \"" + key + "\" does not exist in this Map!");
|
|
76
|
+
}
|
|
77
|
+
if (auto result = std::get_if<std::monostate>(&found->second)) {
|
|
78
|
+
return *result;
|
|
79
|
+
} else {
|
|
80
|
+
throw std::runtime_error("The value at key \"" + key + "\" is not a null!");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
double AnyMap::getDouble(const std::string& key) const {
|
|
84
|
+
auto found = _map.find(key);
|
|
85
|
+
if (found == _map.end()) {
|
|
86
|
+
throw std::runtime_error("The key \"" + key + "\" does not exist in this Map!");
|
|
87
|
+
}
|
|
88
|
+
if (auto result = std::get_if<double>(&found->second)) {
|
|
89
|
+
return *result;
|
|
90
|
+
} else {
|
|
91
|
+
throw std::runtime_error("The value at key \"" + key + "\" is not a double!");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
bool AnyMap::getBoolean(const std::string& key) const {
|
|
95
|
+
auto found = _map.find(key);
|
|
96
|
+
if (found == _map.end()) {
|
|
97
|
+
throw std::runtime_error("The key \"" + key + "\" does not exist in this Map!");
|
|
98
|
+
}
|
|
99
|
+
if (auto result = std::get_if<bool>(&found->second)) {
|
|
100
|
+
return *result;
|
|
101
|
+
} else {
|
|
102
|
+
throw std::runtime_error("The value at key \"" + key + "\" is not a bool!");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
int64_t AnyMap::getBigInt(const std::string& key) const {
|
|
106
|
+
auto found = _map.find(key);
|
|
107
|
+
if (found == _map.end()) {
|
|
108
|
+
throw std::runtime_error("The key \"" + key + "\" does not exist in this Map!");
|
|
109
|
+
}
|
|
110
|
+
if (auto result = std::get_if<int64_t>(&found->second)) {
|
|
111
|
+
return *result;
|
|
112
|
+
} else {
|
|
113
|
+
throw std::runtime_error("The value at key \"" + key + "\" is not a bigint!");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
std::string AnyMap::getString(const std::string& key) const {
|
|
117
|
+
auto found = _map.find(key);
|
|
118
|
+
if (found == _map.end()) {
|
|
119
|
+
throw std::runtime_error("The key \"" + key + "\" does not exist in this Map!");
|
|
120
|
+
}
|
|
121
|
+
if (auto result = std::get_if<std::string>(&found->second)) {
|
|
122
|
+
return *result;
|
|
123
|
+
} else {
|
|
124
|
+
throw std::runtime_error("The value at key \"" + key + "\" is not a string!");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
AnyArray AnyMap::getArray(const std::string& key) const {
|
|
128
|
+
auto found = _map.find(key);
|
|
129
|
+
if (found == _map.end()) {
|
|
130
|
+
throw std::runtime_error("The key \"" + key + "\" does not exist in this Map!");
|
|
131
|
+
}
|
|
132
|
+
if (auto result = std::get_if<AnyArray>(&found->second)) {
|
|
133
|
+
return *result;
|
|
134
|
+
} else {
|
|
135
|
+
throw std::runtime_error("The value at key \"" + key + "\" is not an array!");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
AnyObject AnyMap::getObject(const std::string& key) const {
|
|
139
|
+
auto found = _map.find(key);
|
|
140
|
+
if (found == _map.end()) {
|
|
141
|
+
throw std::runtime_error("The key \"" + key + "\" does not exist in this Map!");
|
|
142
|
+
}
|
|
143
|
+
if (auto result = std::get_if<AnyObject>(&found->second)) {
|
|
144
|
+
return *result;
|
|
145
|
+
} else {
|
|
146
|
+
throw std::runtime_error("The value at key \"" + key + "\" is not an object!");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Set
|
|
151
|
+
void AnyMap::setNull(const std::string& key) {
|
|
152
|
+
_map.emplace(key, std::monostate());
|
|
153
|
+
}
|
|
154
|
+
void AnyMap::setDouble(const std::string& key, double value) {
|
|
155
|
+
_map.emplace(key, value);
|
|
156
|
+
}
|
|
157
|
+
void AnyMap::setBoolean(const std::string& key, bool value) {
|
|
158
|
+
_map.emplace(key, value);
|
|
159
|
+
}
|
|
160
|
+
void AnyMap::setBigInt(const std::string& key, int64_t value) {
|
|
161
|
+
_map.emplace(key, value);
|
|
162
|
+
}
|
|
163
|
+
void AnyMap::setString(const std::string& key, const std::string& value) {
|
|
164
|
+
_map.emplace(key, value);
|
|
165
|
+
}
|
|
166
|
+
void AnyMap::setArray(const std::string& key, const AnyArray& value) {
|
|
167
|
+
_map.emplace(key, value);
|
|
168
|
+
}
|
|
169
|
+
void AnyMap::setObject(const std::string& key, const AnyObject& value) {
|
|
170
|
+
_map.emplace(key, value);
|
|
171
|
+
}
|
|
172
|
+
void AnyMap::setAny(const std::string& key, const AnyValue& value) {
|
|
173
|
+
_map.emplace(key, value);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// C++ getter
|
|
177
|
+
const std::unordered_map<std::string, AnyValue>& AnyMap::getMap() const {
|
|
178
|
+
return _map;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
} // namespace margelo::nitro
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Created by Marc Rousavy on 30.07.24.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#pragma once
|
|
6
|
+
|
|
7
|
+
#include <map>
|
|
8
|
+
#include <memory>
|
|
9
|
+
#include <string>
|
|
10
|
+
#include <unordered_map>
|
|
11
|
+
#include <variant>
|
|
12
|
+
|
|
13
|
+
namespace margelo::nitro {
|
|
14
|
+
|
|
15
|
+
struct AnyValue;
|
|
16
|
+
using AnyArray = std::vector<AnyValue>;
|
|
17
|
+
using AnyObject = std::unordered_map<std::string, AnyValue>;
|
|
18
|
+
|
|
19
|
+
using VariantType = std::variant<std::monostate, bool, double, int64_t, std::string, AnyArray, AnyObject>;
|
|
20
|
+
struct AnyValue : VariantType {
|
|
21
|
+
using VariantType::variant;
|
|
22
|
+
|
|
23
|
+
AnyValue(const VariantType& variant) : VariantType(variant) {}
|
|
24
|
+
AnyValue(VariantType&& variant) : VariantType(std::move(variant)) {}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Represents a JS map-like object (`Record<K, V>`).
|
|
29
|
+
* This is essentially a wrapper around `std::unordered_map<string, variant<...>>`.
|
|
30
|
+
*
|
|
31
|
+
* Supported values are:
|
|
32
|
+
* 1. Primitives
|
|
33
|
+
* 2. Arrays of primitives
|
|
34
|
+
* 3. Objects of primitives
|
|
35
|
+
*/
|
|
36
|
+
class AnyMap {
|
|
37
|
+
public:
|
|
38
|
+
/**
|
|
39
|
+
* Create a new instance of AnyMap.
|
|
40
|
+
*/
|
|
41
|
+
explicit AnyMap() {}
|
|
42
|
+
|
|
43
|
+
public:
|
|
44
|
+
/**
|
|
45
|
+
* Create a new shared_ptr instance of AnyMap.
|
|
46
|
+
*/
|
|
47
|
+
static std::shared_ptr<AnyMap> make() {
|
|
48
|
+
return std::make_shared<AnyMap>();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public:
|
|
52
|
+
/**
|
|
53
|
+
* Returns whether the map contains the given key, or not.
|
|
54
|
+
*/
|
|
55
|
+
bool contains(const std::string& key) const;
|
|
56
|
+
/**
|
|
57
|
+
* Removes the given key from the map, leaving no value.
|
|
58
|
+
*/
|
|
59
|
+
void remove(const std::string& key);
|
|
60
|
+
/**
|
|
61
|
+
* Deletes all keys and values inside the map.
|
|
62
|
+
*/
|
|
63
|
+
void clear() noexcept;
|
|
64
|
+
|
|
65
|
+
public:
|
|
66
|
+
/**
|
|
67
|
+
* Returns whether the value under the given key is a `null`.
|
|
68
|
+
* If the value is not a `null` (or there is no value at the given `key`), this returns `false`.
|
|
69
|
+
*/
|
|
70
|
+
bool isNull(const std::string& key) const;
|
|
71
|
+
/**
|
|
72
|
+
* Returns whether the value under the given key is a `double`.
|
|
73
|
+
* If the value is not a `double` (or there is no value at the given `key`), this returns `false`.
|
|
74
|
+
*/
|
|
75
|
+
bool isDouble(const std::string& key) const;
|
|
76
|
+
/**
|
|
77
|
+
* Returns whether the value under the given key is a `boolean`.
|
|
78
|
+
* If the value is not a `boolean` (or there is no value at the given `key`), this returns `false`.
|
|
79
|
+
*/
|
|
80
|
+
bool isBoolean(const std::string& key) const;
|
|
81
|
+
/**
|
|
82
|
+
* Returns whether the value under the given key is a `bigint`.
|
|
83
|
+
* If the value is not a `bigint` (or there is no value at the given `key`), this returns `false`.
|
|
84
|
+
*/
|
|
85
|
+
bool isBigInt(const std::string& key) const;
|
|
86
|
+
/**
|
|
87
|
+
* Returns whether the value under the given key is a `string`.
|
|
88
|
+
* If the value is not a `string` (or there is no value at the given `key`), this returns `false`.
|
|
89
|
+
*/
|
|
90
|
+
bool isString(const std::string& key) const;
|
|
91
|
+
/**
|
|
92
|
+
* Returns whether the value under the given key is an array.
|
|
93
|
+
* If the value is not an array (or there is no value at the given `key`), this returns `false`.
|
|
94
|
+
*/
|
|
95
|
+
bool isArray(const std::string& key) const;
|
|
96
|
+
/**
|
|
97
|
+
* Returns whether the value under the given key is an object.
|
|
98
|
+
* If the value is not an object (or there is no value at the given `key`), this returns `false`.
|
|
99
|
+
*/
|
|
100
|
+
bool isObject(const std::string& key) const;
|
|
101
|
+
|
|
102
|
+
public:
|
|
103
|
+
/**
|
|
104
|
+
* Returns the null value at the given `key`.
|
|
105
|
+
* If no `null` value exists at the given `key`, this method will throw.
|
|
106
|
+
*/
|
|
107
|
+
std::monostate getNull(const std::string& key) const;
|
|
108
|
+
/**
|
|
109
|
+
* Returns the double value at the given `key`.
|
|
110
|
+
* If no `double` value exists at the given `key`, this method will throw.
|
|
111
|
+
*/
|
|
112
|
+
double getDouble(const std::string& key) const;
|
|
113
|
+
/**
|
|
114
|
+
* Returns the boolean value at the given `key`.
|
|
115
|
+
* If no `boolean` value exists at the given `key`, this method will throw.
|
|
116
|
+
*/
|
|
117
|
+
bool getBoolean(const std::string& key) const;
|
|
118
|
+
/**
|
|
119
|
+
* Returns the bigint value at the given `key`.
|
|
120
|
+
* If no `bigint` value exists at the given `key`, this method will throw.
|
|
121
|
+
*/
|
|
122
|
+
int64_t getBigInt(const std::string& key) const;
|
|
123
|
+
/**
|
|
124
|
+
* Returns the string value at the given `key`.
|
|
125
|
+
* If no `string` value exists at the given `key`, this method will throw.
|
|
126
|
+
*/
|
|
127
|
+
std::string getString(const std::string& key) const;
|
|
128
|
+
/**
|
|
129
|
+
* Returns the array value at the given `key`.
|
|
130
|
+
* If no array value exists at the given `key`, this method will throw.
|
|
131
|
+
*/
|
|
132
|
+
AnyArray getArray(const std::string& key) const;
|
|
133
|
+
/**
|
|
134
|
+
* Returns the object value at the given `key`.
|
|
135
|
+
* If no object value exists at the given `key`, this method will throw.
|
|
136
|
+
*/
|
|
137
|
+
AnyObject getObject(const std::string& key) const;
|
|
138
|
+
|
|
139
|
+
public:
|
|
140
|
+
/**
|
|
141
|
+
* Set the value at the given key to `null`.
|
|
142
|
+
* If the key already exists, this will overwrite the value at that `key`.
|
|
143
|
+
*/
|
|
144
|
+
void setNull(const std::string& key);
|
|
145
|
+
/**
|
|
146
|
+
* Set the value at the given key to the given `double`.
|
|
147
|
+
* If the key already exists, this will overwrite the value at that `key`.
|
|
148
|
+
*/
|
|
149
|
+
void setDouble(const std::string& key, double value);
|
|
150
|
+
/**
|
|
151
|
+
* Set the value at the given key to the given `boolean`.
|
|
152
|
+
* If the key already exists, this will overwrite the value at that `key`.
|
|
153
|
+
*/
|
|
154
|
+
void setBoolean(const std::string& key, bool value);
|
|
155
|
+
/**
|
|
156
|
+
* Set the value at the given key to the given `bigint`.
|
|
157
|
+
* If the key already exists, this will overwrite the value at that `key`.
|
|
158
|
+
*/
|
|
159
|
+
void setBigInt(const std::string& key, int64_t value);
|
|
160
|
+
/**
|
|
161
|
+
* Set the value at the given key to the given `string`.
|
|
162
|
+
* If the key already exists, this will overwrite the value at that `key`.
|
|
163
|
+
*/
|
|
164
|
+
void setString(const std::string& key, const std::string& value);
|
|
165
|
+
/**
|
|
166
|
+
* Set the value at the given key to the given array.
|
|
167
|
+
* If the key already exists, this will overwrite the value at that `key`.
|
|
168
|
+
*/
|
|
169
|
+
void setArray(const std::string& key, const AnyArray& value);
|
|
170
|
+
/**
|
|
171
|
+
* Set the value at the given key to the given object.
|
|
172
|
+
* If the key already exists, this will overwrite the value at that `key`.
|
|
173
|
+
*/
|
|
174
|
+
void setObject(const std::string& key, const AnyObject& value);
|
|
175
|
+
/**
|
|
176
|
+
* Set the value at the given key to the given `AnyValue`.
|
|
177
|
+
* If the key already exists, this will overwrite the value at that `key`.
|
|
178
|
+
*/
|
|
179
|
+
void setAny(const std::string& key, const AnyValue& value);
|
|
180
|
+
|
|
181
|
+
public:
|
|
182
|
+
/**
|
|
183
|
+
* Get the actual C++ map that holds all keys and variant values.
|
|
184
|
+
*/
|
|
185
|
+
const std::unordered_map<std::string, AnyValue>& getMap() const;
|
|
186
|
+
|
|
187
|
+
private:
|
|
188
|
+
std::unordered_map<std::string, AnyValue> _map;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
} // namespace margelo::nitro
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//
|
|
2
|
+
// HybridContext.hpp
|
|
3
|
+
// Pods
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 17.07.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include "NitroLogger.hpp"
|
|
11
|
+
#include "TypeInfo.hpp"
|
|
12
|
+
#include <memory>
|
|
13
|
+
|
|
14
|
+
namespace margelo::nitro {
|
|
15
|
+
|
|
16
|
+
class HybridObject;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Represents contextual state for a `HybridObject`.
|
|
20
|
+
*
|
|
21
|
+
* This can be used in remote implementations, e.g. in a Swift implementation
|
|
22
|
+
* to properly (weak-)reference the `HybridObject` instead of re-creating it each time.
|
|
23
|
+
*/
|
|
24
|
+
struct HybridContext final {
|
|
25
|
+
public:
|
|
26
|
+
std::weak_ptr<HybridObject> cppPart;
|
|
27
|
+
|
|
28
|
+
public:
|
|
29
|
+
template <typename THybridObject, typename TSwiftPart>
|
|
30
|
+
static inline std::shared_ptr<THybridObject> getOrCreate(TSwiftPart swiftPart) noexcept {
|
|
31
|
+
auto hybridContext = swiftPart.getHybridContext();
|
|
32
|
+
auto hybridObject = std::static_pointer_cast<THybridObject>(hybridContext.cppPart.lock());
|
|
33
|
+
if (hybridObject != nullptr) [[likely]] {
|
|
34
|
+
// Fast path - an existing HybridObject is still in cache! (HybridContext)
|
|
35
|
+
return hybridObject;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Slow path - we need to create a new HybridObject that wraps our Swift implementation.
|
|
39
|
+
Logger::log(TAG, "Creating new HybridObject<%s> for %s...", TypeInfo::getFriendlyTypename<THybridObject>(),
|
|
40
|
+
TypeInfo::getFriendlyTypename<TSwiftPart>());
|
|
41
|
+
hybridObject = std::make_shared<THybridObject>(std::forward<decltype(swiftPart)>(swiftPart));
|
|
42
|
+
hybridContext.cppPart = hybridObject;
|
|
43
|
+
swiftPart.setHybridContext(hybridContext);
|
|
44
|
+
return hybridObject;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private:
|
|
48
|
+
static constexpr auto TAG = "HybridContext";
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
} // namespace margelo::nitro
|