react-native-nitro-modules 0.1.6 → 0.1.7
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/cpp/jsi/JSICache.cpp +1 -0
- package/cpp/jsi/JSICache.hpp +0 -1
- package/cpp/jsi/JSIConverter+AnyMap.hpp +22 -0
- package/cpp/jsi/JSIConverter+ArrayBuffer.hpp +9 -1
- package/cpp/jsi/JSIConverter+Function.hpp +8 -0
- package/cpp/jsi/JSIConverter+HybridObject.hpp +16 -0
- package/cpp/jsi/JSIConverter+Optional.hpp +9 -0
- package/cpp/jsi/JSIConverter+Promise.hpp +4 -0
- package/cpp/jsi/JSIConverter+Tuple.hpp +33 -0
- package/cpp/jsi/JSIConverter+UnorderedMap.hpp +20 -0
- package/cpp/jsi/JSIConverter+Variant.hpp +25 -53
- package/cpp/jsi/JSIConverter+Vector.hpp +21 -0
- package/cpp/jsi/JSIConverter.hpp +55 -7
- package/cpp/jsi/JSIHelpers.hpp +52 -0
- package/cpp/threading/Dispatcher.cpp +1 -1
- package/cpp/turbomodule/NativeNitroModules.cpp +0 -1
- package/ios/turbomodule/NitroModuleOnLoad.mm +0 -5
- package/lib/commonjs/index.js +0 -11
- package/lib/commonjs/index.js.map +1 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/lib/module/index.js +0 -1
- package/lib/module/index.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/typescript/AnyMap.d.ts +17 -0
- package/lib/typescript/AnyMap.d.ts.map +1 -0
- package/lib/typescript/HybridObject.d.ts +58 -0
- package/lib/typescript/HybridObject.d.ts.map +1 -0
- package/lib/typescript/ModuleNotFoundError.d.ts +7 -0
- package/lib/typescript/ModuleNotFoundError.d.ts.map +1 -0
- package/lib/typescript/NativeNitroModules.d.ts +13 -0
- package/lib/typescript/NativeNitroModules.d.ts.map +1 -0
- package/lib/typescript/NativeNitroModules.web.d.ts +5 -0
- package/lib/typescript/NativeNitroModules.web.d.ts.map +1 -0
- package/lib/typescript/NitroModules.d.ts +18 -0
- package/lib/typescript/NitroModules.d.ts.map +1 -0
- package/lib/typescript/__tests__/index.test.d.ts +1 -0
- package/lib/typescript/__tests__/index.test.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +4 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +0 -1
- package/cpp/test-object/TestHybridObject.cpp +0 -37
- package/cpp/test-object/TestHybridObject.hpp +0 -87
- package/cpp/utils/GetRuntimeID.hpp +0 -28
- package/lib/commonjs/createTestObject.js +0 -15
- package/lib/commonjs/createTestObject.js.map +0 -1
- package/lib/module/createTestObject.js +0 -8
- package/lib/module/createTestObject.js.map +0 -1
- package/src/createTestObject.ts +0 -40
package/cpp/jsi/JSICache.cpp
CHANGED
package/cpp/jsi/JSICache.hpp
CHANGED
|
@@ -17,6 +17,7 @@ struct JSIConverter;
|
|
|
17
17
|
#include "JSIConverter.hpp"
|
|
18
18
|
|
|
19
19
|
#include "AnyMap.hpp"
|
|
20
|
+
#include "JSIHelpers.hpp"
|
|
20
21
|
#include <jsi/jsi.h>
|
|
21
22
|
#include <memory>
|
|
22
23
|
|
|
@@ -33,6 +34,9 @@ struct JSIConverter<AnyValue> {
|
|
|
33
34
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const AnyValue& value) {
|
|
34
35
|
return JSIConverter<AnyValue::variant>::toJSI(runtime, value);
|
|
35
36
|
}
|
|
37
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
38
|
+
return JSIConverter<AnyValue::variant>::canConvert(runtime, value);
|
|
39
|
+
}
|
|
36
40
|
};
|
|
37
41
|
|
|
38
42
|
// AnyMap <> Record<K, V>
|
|
@@ -59,6 +63,24 @@ struct JSIConverter<std::shared_ptr<AnyMap>> {
|
|
|
59
63
|
}
|
|
60
64
|
return object;
|
|
61
65
|
}
|
|
66
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
67
|
+
if (!value.isObject()) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
jsi::Object object = value.getObject(runtime);
|
|
71
|
+
if (!isPlainObject(runtime, object)) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
jsi::Array properties = object.getPropertyNames(runtime);
|
|
75
|
+
size_t size = properties.size(runtime);
|
|
76
|
+
for (size_t i = 0; i < size; i++) {
|
|
77
|
+
bool canConvertProp = JSIConverter<AnyValue>::canConvert(runtime, properties.getValueAtIndex(runtime, i));
|
|
78
|
+
if (!canConvertProp) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
62
84
|
};
|
|
63
85
|
|
|
64
86
|
} // namespace margelo::nitro
|
|
@@ -32,7 +32,8 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::MutableBuffer
|
|
|
32
32
|
static inline std::shared_ptr<ArrayBuffer> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
33
33
|
jsi::Object object = arg.asObject(runtime);
|
|
34
34
|
if (!object.isArrayBuffer(runtime)) [[unlikely]] {
|
|
35
|
-
throw std::runtime_error("Object \"" + arg.toString(runtime).utf8(runtime) + "\" is not an ArrayBuffer!"
|
|
35
|
+
throw std::runtime_error("Object \"" + arg.toString(runtime).utf8(runtime) + "\" is not an ArrayBuffer! "
|
|
36
|
+
"Are you maybe passing a TypedArray (e.g. Uint8Array)? Try to pass it's `.buffer` value.");
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
JSICacheReference cache = JSICache::getOrCreateCache(runtime);
|
|
@@ -43,6 +44,13 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::MutableBuffer
|
|
|
43
44
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const std::shared_ptr<jsi::MutableBuffer>& buffer) {
|
|
44
45
|
return jsi::ArrayBuffer(runtime, buffer);
|
|
45
46
|
}
|
|
47
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
48
|
+
if (value.isObject()) {
|
|
49
|
+
jsi::Object object = value.getObject(runtime);
|
|
50
|
+
return object.isArrayBuffer(runtime);
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
46
54
|
};
|
|
47
55
|
|
|
48
56
|
} // namespace margelo::nitro
|
|
@@ -79,6 +79,14 @@ struct JSIConverter<std::function<ReturnType(Args...)>> {
|
|
|
79
79
|
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "hostFunction"), sizeof...(Args), jsFunction);
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
83
|
+
if (value.isObject()) {
|
|
84
|
+
jsi::Object object = value.getObject(runtime);
|
|
85
|
+
return object.isFunction(runtime);
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
82
90
|
private:
|
|
83
91
|
static inline ResultingType callJSFunction(jsi::Runtime& runtime, const OwningReference<jsi::Function>& function, const Args&... args) {
|
|
84
92
|
// Throw a lock on the OwningReference<T> so we can guarantee safe access (Hermes GC cannot delete it while `lock` is alive)
|
|
@@ -72,6 +72,14 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::HostObject>>>
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
76
|
+
if (value.isObject()) {
|
|
77
|
+
jsi::Object object = value.getObject(runtime);
|
|
78
|
+
return object.isHostObject<TPointee>(runtime);
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
75
83
|
private:
|
|
76
84
|
static inline std::string invalidTypeErrorMessage(const std::string& typeDescription, const std::string& reason) {
|
|
77
85
|
std::string typeName = TypeInfo::getFriendlyTypename<TPointee>();
|
|
@@ -120,6 +128,14 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::NativeState>>
|
|
|
120
128
|
return object;
|
|
121
129
|
}
|
|
122
130
|
|
|
131
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
132
|
+
if (value.isObject()) {
|
|
133
|
+
jsi::Object object = value.getObject(runtime);
|
|
134
|
+
return object.hasNativeState<TPointee>(runtime);
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
123
139
|
private:
|
|
124
140
|
static inline std::string invalidTypeErrorMessage(const std::string& typeDescription, const std::string& reason) {
|
|
125
141
|
std::string typeName = TypeInfo::getFriendlyTypename<TPointee>();
|
|
@@ -36,6 +36,15 @@ struct JSIConverter<std::optional<TInner>> {
|
|
|
36
36
|
return JSIConverter<TInner>::toJSI(runtime, arg.value());
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
40
|
+
if (value.isUndefined() || value.isNull()) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
if (JSIConverter<TInner>::canConvert(runtime, value)) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
39
48
|
};
|
|
40
49
|
|
|
41
50
|
} // namespace margelo::nitro
|
|
@@ -82,6 +82,10 @@ struct JSIConverter<std::future<TResult>> {
|
|
|
82
82
|
});
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
|
+
|
|
86
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
87
|
+
throw std::runtime_error("jsi::Value of type Promise cannot be converted to std::future yet!");
|
|
88
|
+
}
|
|
85
89
|
};
|
|
86
90
|
|
|
87
91
|
} // namespace margelo::nitro
|
|
@@ -42,6 +42,23 @@ struct JSIConverter<std::tuple<Types...>> {
|
|
|
42
42
|
return array;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
46
|
+
if (!value.isObject()) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
jsi::Object object = value.getObject(runtime);
|
|
50
|
+
if (!object.isArray(runtime)) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
jsi::Array array = object.getArray(runtime);
|
|
54
|
+
size_t size = array.size(runtime);
|
|
55
|
+
if (size != sizeof...(Types)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return canConvertRecursive<Types...>(runtime, array, 0);
|
|
60
|
+
}
|
|
61
|
+
|
|
45
62
|
private:
|
|
46
63
|
template <std::size_t... Is>
|
|
47
64
|
static inline std::tuple<Types...> copyArrayItemsToTuple(jsi::Runtime& runtime, const jsi::Array& array, std::index_sequence<Is...>) {
|
|
@@ -55,6 +72,22 @@ private:
|
|
|
55
72
|
JSIConverter<std::tuple_element_t<Is, std::tuple<Types...>>>::toJSI(runtime, std::get<Is>(tuple)))),
|
|
56
73
|
...);
|
|
57
74
|
}
|
|
75
|
+
|
|
76
|
+
template <typename T>
|
|
77
|
+
static bool canConvertElement(jsi::Runtime& runtime, const jsi::Array& array, size_t index) {
|
|
78
|
+
return JSIConverter<T>::canConvert(runtime, array.getValueAtIndex(runtime, index));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
template <typename First, typename... Rest>
|
|
82
|
+
static bool canConvertRecursive(jsi::Runtime& runtime, const jsi::Array& array, size_t index) {
|
|
83
|
+
if (!canConvertElement<First>(runtime, array, index)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if constexpr (sizeof...(Rest) > 0) {
|
|
87
|
+
return canConvertRecursive<Rest...>(runtime, array, index + 1);
|
|
88
|
+
}
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
58
91
|
};
|
|
59
92
|
|
|
60
93
|
} // namespace margelo::nitro
|
|
@@ -13,6 +13,7 @@ struct JSIConverter;
|
|
|
13
13
|
#include "JSIConverter.hpp"
|
|
14
14
|
|
|
15
15
|
#include "AnyMap.hpp"
|
|
16
|
+
#include "JSIHelpers.hpp"
|
|
16
17
|
#include <jsi/jsi.h>
|
|
17
18
|
#include <unordered_map>
|
|
18
19
|
|
|
@@ -45,6 +46,25 @@ struct JSIConverter<std::unordered_map<std::string, ValueType>> {
|
|
|
45
46
|
}
|
|
46
47
|
return object;
|
|
47
48
|
}
|
|
49
|
+
|
|
50
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
51
|
+
if (!value.isObject()) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
jsi::Object object = value.getObject(runtime);
|
|
55
|
+
if (!isPlainObject(runtime, object)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
jsi::Array properties = object.getPropertyNames(runtime);
|
|
59
|
+
size_t size = properties.size(runtime);
|
|
60
|
+
for (size_t i = 0; i < size; i++) {
|
|
61
|
+
bool canConvertProp = JSIConverter<ValueType>::canConvert(runtime, properties.getValueAtIndex(runtime, i));
|
|
62
|
+
if (!canConvertProp) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
48
68
|
};
|
|
49
69
|
|
|
50
70
|
} // namespace margelo::nitro
|
|
@@ -31,66 +31,38 @@ using namespace facebook;
|
|
|
31
31
|
// std::variant<A, B, C> <> A | B | C
|
|
32
32
|
template <typename... Types>
|
|
33
33
|
struct JSIConverter<std::variant<Types...>> {
|
|
34
|
+
|
|
35
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
36
|
+
// Check each type in `Types...` to make sure we can convert `jsi::Value` to one of those.
|
|
37
|
+
return (JSIConverter<Types>::canConvert(runtime, value) || ...);
|
|
38
|
+
}
|
|
39
|
+
|
|
34
40
|
static inline std::variant<Types...> fromJSI(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
35
|
-
|
|
36
|
-
if constexpr (is_in_pack_v<std::monostate, Types...>) {
|
|
37
|
-
return std::monostate();
|
|
38
|
-
} else {
|
|
39
|
-
throw typeNotSupportedError("null");
|
|
40
|
-
}
|
|
41
|
-
} else if (value.isBool()) {
|
|
42
|
-
if constexpr (is_in_pack_v<bool, Types...>) {
|
|
43
|
-
return JSIConverter<bool>::fromJSI(runtime, value);
|
|
44
|
-
} else {
|
|
45
|
-
throw typeNotSupportedError("boolean");
|
|
46
|
-
}
|
|
47
|
-
} else if (value.isNumber()) {
|
|
48
|
-
if constexpr (is_in_pack_v<double, Types...>) {
|
|
49
|
-
return JSIConverter<double>::fromJSI(runtime, value);
|
|
50
|
-
} else {
|
|
51
|
-
throw typeNotSupportedError("number");
|
|
52
|
-
}
|
|
53
|
-
} else if (value.isString()) {
|
|
54
|
-
if constexpr (is_in_pack_v<std::string, Types...>) {
|
|
55
|
-
return JSIConverter<std::string>::fromJSI(runtime, value);
|
|
56
|
-
} else {
|
|
57
|
-
throw typeNotSupportedError("string");
|
|
58
|
-
}
|
|
59
|
-
} else if (value.isBigInt()) {
|
|
60
|
-
if constexpr (is_in_pack_v<int64_t, Types...>) {
|
|
61
|
-
return JSIConverter<int64_t>::fromJSI(runtime, value);
|
|
62
|
-
} else {
|
|
63
|
-
throw typeNotSupportedError("bigint");
|
|
64
|
-
}
|
|
65
|
-
} else if (value.isObject()) {
|
|
66
|
-
jsi::Object valueObj = value.getObject(runtime);
|
|
67
|
-
if (valueObj.isArray(runtime)) {
|
|
68
|
-
if constexpr (is_in_pack_v<std::vector<AnyValue>, Types...>) {
|
|
69
|
-
return JSIConverter<std::vector<AnyValue>>::fromJSI(runtime, value);
|
|
70
|
-
} else {
|
|
71
|
-
throw typeNotSupportedError("array[]");
|
|
72
|
-
}
|
|
73
|
-
} else {
|
|
74
|
-
if constexpr (is_in_pack_v<std::unordered_map<std::string, AnyValue>, Types...>) {
|
|
75
|
-
return JSIConverter<std::unordered_map<std::string, AnyValue>>::fromJSI(runtime, value);
|
|
76
|
-
} else {
|
|
77
|
-
throw typeNotSupportedError("object{}");
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
std::string stringRepresentation = value.toString(runtime).utf8(runtime);
|
|
82
|
-
throw std::runtime_error("Cannot convert \"" + stringRepresentation + "\" to std::variant<...>!");
|
|
83
|
-
}
|
|
41
|
+
return fromJSIRecursive<Types...>(runtime, value);
|
|
84
42
|
}
|
|
85
43
|
|
|
86
44
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const std::variant<Types...>& variant) {
|
|
87
|
-
return std::visit(
|
|
45
|
+
return std::visit(
|
|
46
|
+
[&runtime](const auto& val) {
|
|
47
|
+
// Try to convert each type
|
|
48
|
+
return JSIConverter<std::decay_t<decltype(val)>>::toJSI(runtime, val);
|
|
49
|
+
},
|
|
50
|
+
variant);
|
|
88
51
|
}
|
|
89
52
|
|
|
90
53
|
private:
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
54
|
+
template <typename First, typename... Rest>
|
|
55
|
+
static inline std::variant<Types...> fromJSIRecursive(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
56
|
+
if (JSIConverter<First>::canConvert(runtime, value)) {
|
|
57
|
+
return JSIConverter<First>::fromJSI(runtime, value);
|
|
58
|
+
}
|
|
59
|
+
if constexpr (sizeof...(Rest) == 0) {
|
|
60
|
+
std::string string = value.toString(runtime).utf8(runtime);
|
|
61
|
+
std::string types = TypeInfo::getFriendlyTypenames<Types...>();
|
|
62
|
+
throw std::runtime_error("Cannot convert \"" + string + "\" to any type in variant<" + types + ">!");
|
|
63
|
+
} else {
|
|
64
|
+
return fromJSIRecursive<Rest...>(runtime, value);
|
|
65
|
+
}
|
|
94
66
|
}
|
|
95
67
|
};
|
|
96
68
|
|
|
@@ -43,6 +43,27 @@ struct JSIConverter<std::vector<ElementType>> {
|
|
|
43
43
|
}
|
|
44
44
|
return array;
|
|
45
45
|
}
|
|
46
|
+
|
|
47
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
48
|
+
if (!value.isObject()) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
jsi::Object object = value.getObject(runtime);
|
|
52
|
+
if (!object.isArray(runtime)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
jsi::Array array = object.getArray(runtime);
|
|
56
|
+
if (array.size(runtime) == 0) {
|
|
57
|
+
// it is an empty array, so it _theoretically_ doesn't matter what type it holds. Just say true.
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
// Check the type of the first element in the array.
|
|
61
|
+
// Technically the array can also have different types for each item,
|
|
62
|
+
// and to be absolutely sure that we can convert the entire array, we have to check each item in the array.
|
|
63
|
+
// But we don't want to do that for performance reasons - let's just assume the user doesn't make this mistake.
|
|
64
|
+
jsi::Value firstElement = array.getValueAtIndex(runtime, 0);
|
|
65
|
+
return JSIConverter<ElementType>::canConvert(runtime, firstElement);
|
|
66
|
+
}
|
|
46
67
|
};
|
|
47
68
|
|
|
48
69
|
} // namespace margelo::nitro
|
package/cpp/jsi/JSIConverter.hpp
CHANGED
|
@@ -25,18 +25,34 @@ using namespace facebook;
|
|
|
25
25
|
* Value types, custom types (HostObjects), and even functions with any number of arguments/types are supported.
|
|
26
26
|
* This type can be extended by just creating a new template for JSIConverter in a header.
|
|
27
27
|
*/
|
|
28
|
-
template <typename
|
|
28
|
+
template <typename T, typename Enable = void>
|
|
29
29
|
struct JSIConverter final {
|
|
30
30
|
JSIConverter() = delete;
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
static inline
|
|
37
|
-
static_assert(always_false<
|
|
32
|
+
/**
|
|
33
|
+
* Converts the given `jsi::Value` to type `T`.
|
|
34
|
+
* By default, this static-asserts.
|
|
35
|
+
*/
|
|
36
|
+
static inline T fromJSI(jsi::Runtime&, const jsi::Value&) {
|
|
37
|
+
static_assert(always_false<T>::value, "This type is not supported by the JSIConverter!");
|
|
38
|
+
return T();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Converts `T` to a `jsi::Value`.
|
|
42
|
+
* By default, this static-asserts.
|
|
43
|
+
*/
|
|
44
|
+
static inline jsi::Value toJSI(jsi::Runtime&, T) {
|
|
45
|
+
static_assert(always_false<T>::value, "This type is not supported by the JSIConverter!");
|
|
38
46
|
return jsi::Value::undefined();
|
|
39
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns whether the given `jsi::Value` can be converted to `T`.
|
|
50
|
+
* This involves runtime type-checks.
|
|
51
|
+
* By default, this returns `false`.
|
|
52
|
+
*/
|
|
53
|
+
static inline bool canConvert(jsi::Runtime&, const jsi::Value&) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
40
56
|
|
|
41
57
|
private:
|
|
42
58
|
template <typename>
|
|
@@ -52,6 +68,9 @@ struct JSIConverter<int> {
|
|
|
52
68
|
static inline jsi::Value toJSI(jsi::Runtime&, int arg) {
|
|
53
69
|
return jsi::Value(arg);
|
|
54
70
|
}
|
|
71
|
+
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
|
|
72
|
+
return value.isNumber();
|
|
73
|
+
}
|
|
55
74
|
};
|
|
56
75
|
|
|
57
76
|
// std::monostate <> null
|
|
@@ -63,6 +82,9 @@ struct JSIConverter<std::monostate> {
|
|
|
63
82
|
static inline jsi::Value toJSI(jsi::Runtime&, std::monostate arg) {
|
|
64
83
|
return jsi::Value::null();
|
|
65
84
|
}
|
|
85
|
+
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
|
|
86
|
+
return value.isNull() || value.isUndefined();
|
|
87
|
+
}
|
|
66
88
|
};
|
|
67
89
|
|
|
68
90
|
// double <> number
|
|
@@ -74,6 +96,9 @@ struct JSIConverter<double> {
|
|
|
74
96
|
static inline jsi::Value toJSI(jsi::Runtime&, double arg) {
|
|
75
97
|
return jsi::Value(arg);
|
|
76
98
|
}
|
|
99
|
+
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
|
|
100
|
+
return value.isNumber();
|
|
101
|
+
}
|
|
77
102
|
};
|
|
78
103
|
|
|
79
104
|
// float <> number
|
|
@@ -85,6 +110,9 @@ struct JSIConverter<float> {
|
|
|
85
110
|
static inline jsi::Value toJSI(jsi::Runtime&, float arg) {
|
|
86
111
|
return jsi::Value(static_cast<double>(arg));
|
|
87
112
|
}
|
|
113
|
+
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
|
|
114
|
+
return value.isNumber();
|
|
115
|
+
}
|
|
88
116
|
};
|
|
89
117
|
|
|
90
118
|
// int64_t <> BigInt
|
|
@@ -96,6 +124,13 @@ struct JSIConverter<int64_t> {
|
|
|
96
124
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, int64_t arg) {
|
|
97
125
|
return jsi::BigInt::fromInt64(runtime, arg);
|
|
98
126
|
}
|
|
127
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
128
|
+
if (value.isBigInt()) {
|
|
129
|
+
jsi::BigInt bigint = value.getBigInt(runtime);
|
|
130
|
+
return bigint.isInt64(runtime);
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
99
134
|
};
|
|
100
135
|
|
|
101
136
|
// uint64_t <> BigInt
|
|
@@ -107,6 +142,13 @@ struct JSIConverter<uint64_t> {
|
|
|
107
142
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, uint64_t arg) {
|
|
108
143
|
return jsi::BigInt::fromUint64(runtime, arg);
|
|
109
144
|
}
|
|
145
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
146
|
+
if (value.isBigInt()) {
|
|
147
|
+
jsi::BigInt bigint = value.getBigInt(runtime);
|
|
148
|
+
return bigint.isUint64(runtime);
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
110
152
|
};
|
|
111
153
|
|
|
112
154
|
// bool <> boolean
|
|
@@ -118,6 +160,9 @@ struct JSIConverter<bool> {
|
|
|
118
160
|
static inline jsi::Value toJSI(jsi::Runtime&, bool arg) {
|
|
119
161
|
return jsi::Value(arg);
|
|
120
162
|
}
|
|
163
|
+
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
|
|
164
|
+
return value.isBool();
|
|
165
|
+
}
|
|
121
166
|
};
|
|
122
167
|
|
|
123
168
|
// std::string <> string
|
|
@@ -129,6 +174,9 @@ struct JSIConverter<std::string> {
|
|
|
129
174
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const std::string& arg) {
|
|
130
175
|
return jsi::String::createFromUtf8(runtime, arg);
|
|
131
176
|
}
|
|
177
|
+
static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
|
|
178
|
+
return value.isString();
|
|
179
|
+
}
|
|
132
180
|
};
|
|
133
181
|
|
|
134
182
|
} // namespace margelo::nitro
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//
|
|
2
|
+
// JSIHelpers.hpp
|
|
3
|
+
// Pods
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 07.08.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include "ThreadUtils.hpp"
|
|
11
|
+
#include <jsi/jsi.h>
|
|
12
|
+
|
|
13
|
+
namespace margelo::nitro {
|
|
14
|
+
|
|
15
|
+
using namespace facebook;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns whether the given `jsi::Object` is a plain-JS object, or not.
|
|
19
|
+
* If it is not a plain-JS object, it could be an Array, ArrayBuffer, Function,
|
|
20
|
+
* HostObject or NativeState.
|
|
21
|
+
*/
|
|
22
|
+
static inline bool isPlainObject(jsi::Runtime& runtime, const jsi::Object& object) {
|
|
23
|
+
if (object.isArray(runtime)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (object.isArrayBuffer(runtime)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
if (object.isFunction(runtime)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (object.isHostObject(runtime)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (object.hasNativeState(runtime)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get an ID for the given Runtime.
|
|
43
|
+
*
|
|
44
|
+
* The ID usually consists of a Runtime description (e.g. "HermesRuntime"),
|
|
45
|
+
* and it's Thread (e.g. "com.facebook.react.runtime.JavaScript")
|
|
46
|
+
*/
|
|
47
|
+
static inline std::string getRuntimeId(jsi::Runtime& runtime) {
|
|
48
|
+
std::string threadName = ThreadUtils::getThreadName();
|
|
49
|
+
return runtime.description() + std::string(" (") + threadName + std::string(")");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
} // namespace margelo::nitro
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
|
|
8
8
|
#import "HybridObjectRegistry.hpp"
|
|
9
9
|
#import "RegisterNativeNitroModules.hpp"
|
|
10
|
-
#import "TestHybridObject.hpp"
|
|
11
10
|
#import <Foundation/Foundation.h>
|
|
12
11
|
|
|
13
12
|
@interface NitroModulesOnLoad : NSObject
|
|
@@ -22,10 +21,6 @@ using namespace margelo::nitro;
|
|
|
22
21
|
// We need Objective-C here because these things do not get compiled out - meaning this will always be
|
|
23
22
|
// called when the app starts.
|
|
24
23
|
RegisterNativeNitroModules::registerNativeNitroModules();
|
|
25
|
-
|
|
26
|
-
// Register Test HybridObject so it can be created from JS.
|
|
27
|
-
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
28
|
-
"TestHybridObject", []() -> std::shared_ptr<TestHybridObject> { return std::make_shared<TestHybridObject>(); });
|
|
29
24
|
}
|
|
30
25
|
|
|
31
26
|
@end
|
package/lib/commonjs/index.js
CHANGED
|
@@ -3,17 +3,6 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
var _createTestObject = require("./createTestObject");
|
|
7
|
-
Object.keys(_createTestObject).forEach(function (key) {
|
|
8
|
-
if (key === "default" || key === "__esModule") return;
|
|
9
|
-
if (key in exports && exports[key] === _createTestObject[key]) return;
|
|
10
|
-
Object.defineProperty(exports, key, {
|
|
11
|
-
enumerable: true,
|
|
12
|
-
get: function () {
|
|
13
|
-
return _createTestObject[key];
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
6
|
var _HybridObject = require("./HybridObject");
|
|
18
7
|
Object.keys(_HybridObject).forEach(function (key) {
|
|
19
8
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["_HybridObject","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_NitroModules","_AnyMap"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;AAAA,IAAAA,aAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,aAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,aAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,aAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,aAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,aAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,aAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,aAAA,CAAAL,GAAA;IAAA;EAAA;AAAA;AACA,IAAAM,OAAA,GAAAV,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAQ,OAAA,EAAAP,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAM,OAAA,CAAAN,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAE,OAAA,CAAAN,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/lib/module/index.js
CHANGED
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,cAAc,gBAAgB;AAC9B,cAAc,gBAAgB;AAC9B,cAAc,UAAU","ignoreList":[]}
|