react-native-nitro-modules 0.26.2 → 0.26.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 +13 -4
- package/README.md +2 -2
- package/android/build.gradle +1 -1
- package/android/gradle.properties +1 -1
- package/android/src/main/cpp/JNIOnLoad.cpp +2 -0
- package/android/src/main/cpp/core/HardwareBufferArrayBuffer.hpp +93 -0
- package/android/src/main/cpp/core/JArrayBuffer.hpp +72 -8
- package/android/src/main/cpp/utils/JHardwareBufferUtils.cpp +98 -0
- package/android/src/main/cpp/utils/JHardwareBufferUtils.hpp +39 -0
- package/android/src/main/java/com/margelo/nitro/core/ArrayBuffer.kt +68 -2
- package/android/src/main/java/com/margelo/nitro/utils/HardwareBuffer+updateFrom.kt +15 -0
- package/android/src/main/java/com/margelo/nitro/utils/HardwareBufferUtils.kt +46 -0
- package/cpp/core/ArrayBuffer.cpp +4 -0
- package/cpp/core/ArrayBuffer.hpp +9 -0
- package/cpp/core/HybridFunction.hpp +3 -13
- package/cpp/jsi/JSIConverter+ArrayBuffer.hpp +29 -1
- package/cpp/jsi/JSIConverter+Exception.hpp +0 -9
- package/cpp/platform/NitroLogger.hpp +2 -1
- package/cpp/utils/NitroDefines.hpp +1 -1
- package/ios/core/{AnyMapHolder.swift → AnyMap.swift} +32 -27
- package/ios/core/{ArrayBufferHolder.swift → ArrayBuffer.swift} +31 -28
- package/ios/core/ArrayBufferHolder.hpp +1 -1
- package/ios/utils/AnyMapHolder.hpp +13 -0
- package/ios/utils/AnyMapUtils.hpp +107 -0
- package/ios/utils/Date+fromChrono.swift +2 -2
- package/lib/tsconfig.build.tsbuildinfo +1 -1
- package/nitro_pod_utils.rb +27 -0
- package/package.json +3 -3
- package/ios/core/AnyMapHolder.hpp +0 -99
package/cpp/core/ArrayBuffer.hpp
CHANGED
|
@@ -63,6 +63,10 @@ public:
|
|
|
63
63
|
* Create a new `NativeArrayBuffer` that copies the given `std::vector`.
|
|
64
64
|
*/
|
|
65
65
|
static std::shared_ptr<ArrayBuffer> copy(const std::vector<uint8_t>& data);
|
|
66
|
+
/**
|
|
67
|
+
* Create a new `NativeArrayBuffer` that copies the given `std::shared_ptr<ArrayBuffer>`.
|
|
68
|
+
*/
|
|
69
|
+
static std::shared_ptr<ArrayBuffer> copy(const std::shared_ptr<ArrayBuffer>& buffer);
|
|
66
70
|
/**
|
|
67
71
|
* Create a new `NativeArrayBuffer` that allocates a new buffer of the given size.
|
|
68
72
|
*/
|
|
@@ -134,6 +138,11 @@ public:
|
|
|
134
138
|
*/
|
|
135
139
|
bool isOwner() const noexcept override;
|
|
136
140
|
|
|
141
|
+
public:
|
|
142
|
+
BorrowingReference<jsi::ArrayBuffer> getJSReference() const noexcept {
|
|
143
|
+
return _jsReference;
|
|
144
|
+
}
|
|
145
|
+
|
|
137
146
|
private:
|
|
138
147
|
jsi::Runtime& _runtime;
|
|
139
148
|
BorrowingReference<jsi::ArrayBuffer> _jsReference;
|
|
@@ -109,17 +109,6 @@ public:
|
|
|
109
109
|
std::string funcName = getHybridFuncFullName<THybrid>(kind, name, hybridInstance.get());
|
|
110
110
|
std::string message = exception.what();
|
|
111
111
|
throw jsi::JSError(runtime, funcName + ": " + message);
|
|
112
|
-
#ifdef ANDROID
|
|
113
|
-
#pragma clang diagnostic push
|
|
114
|
-
#pragma clang diagnostic ignored "-Wexceptions"
|
|
115
|
-
// Workaround for https://github.com/mrousavy/nitro/issues/382
|
|
116
|
-
} catch (const std::runtime_error& exception) {
|
|
117
|
-
// Some exception was thrown - add method name information and re-throw as `JSError`.
|
|
118
|
-
std::string funcName = getHybridFuncFullName<THybrid>(kind, name, hybridInstance.get());
|
|
119
|
-
std::string message = exception.what();
|
|
120
|
-
throw jsi::JSError(runtime, funcName + ": " + message);
|
|
121
|
-
#pragma clang diagnostic pop
|
|
122
|
-
#endif
|
|
123
112
|
} catch (...) {
|
|
124
113
|
// Some unknown exception was thrown - add method name information and re-throw as `JSError`.
|
|
125
114
|
std::string funcName = getHybridFuncFullName<THybrid>(kind, name, hybridInstance.get());
|
|
@@ -181,8 +170,9 @@ private:
|
|
|
181
170
|
* Get the `NativeState` of the given `value`.
|
|
182
171
|
*/
|
|
183
172
|
template <typename THybrid>
|
|
184
|
-
static inline std::shared_ptr<THybrid> getHybridObjectNativeState(jsi::Runtime& runtime, const jsi::Value& value,
|
|
185
|
-
|
|
173
|
+
static inline std::shared_ptr<THybrid> getHybridObjectNativeState(jsi::Runtime& runtime, const jsi::Value& value,
|
|
174
|
+
[[maybe_unused]] FunctionKind funcKind,
|
|
175
|
+
[[maybe_unused]] const std::string& funcName) {
|
|
186
176
|
// 1. Convert jsi::Value to jsi::Object
|
|
187
177
|
#ifdef NITRO_DEBUG
|
|
188
178
|
if (!value.isObject()) [[unlikely]] {
|
|
@@ -27,6 +27,12 @@ namespace margelo::nitro {
|
|
|
27
27
|
|
|
28
28
|
using namespace facebook;
|
|
29
29
|
|
|
30
|
+
struct MutableBufferNativeState final : public jsi::NativeState {
|
|
31
|
+
public:
|
|
32
|
+
explicit MutableBufferNativeState(const std::shared_ptr<jsi::MutableBuffer>& buffer) : buffer(buffer) {}
|
|
33
|
+
std::shared_ptr<jsi::MutableBuffer> buffer;
|
|
34
|
+
};
|
|
35
|
+
|
|
30
36
|
// MutableBuffer <> ArrayBuffer
|
|
31
37
|
template <typename T>
|
|
32
38
|
struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::MutableBuffer>>> final {
|
|
@@ -47,6 +53,14 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::MutableBuffer
|
|
|
47
53
|
"Are you maybe passing a TypedArray (e.g. Uint8Array)? Try to pass it's `.buffer` value.");
|
|
48
54
|
}
|
|
49
55
|
#endif
|
|
56
|
+
if (object.hasNativeState<MutableBufferNativeState>(runtime)) {
|
|
57
|
+
// It already is a NativeBuffer! Let's get the jsi::MutableBuffer from the jsi::NativeState...
|
|
58
|
+
auto mutableBufferHolder = object.getNativeState<MutableBufferNativeState>(runtime);
|
|
59
|
+
auto mutableBuffer = mutableBufferHolder->buffer;
|
|
60
|
+
if (auto arrayBuffer = std::dynamic_pointer_cast<ArrayBuffer>(mutableBuffer)) [[likely]] {
|
|
61
|
+
return arrayBuffer;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
50
64
|
|
|
51
65
|
JSICacheReference cache = JSICache::getOrCreateCache(runtime);
|
|
52
66
|
auto borrowingArrayBuffer = cache.makeShared(object.getArrayBuffer(runtime));
|
|
@@ -54,7 +68,21 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::MutableBuffer
|
|
|
54
68
|
return std::make_shared<JSArrayBuffer>(runtime, borrowingArrayBuffer);
|
|
55
69
|
}
|
|
56
70
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const std::shared_ptr<jsi::MutableBuffer>& buffer) {
|
|
57
|
-
|
|
71
|
+
if (auto jsBuffer = std::dynamic_pointer_cast<JSArrayBuffer>(buffer)) {
|
|
72
|
+
// It already is a JSBuffer! Let's try to just get it's existing jsi::Value...
|
|
73
|
+
auto jsValue = jsBuffer->getJSReference();
|
|
74
|
+
if (jsValue != nullptr) [[likely]] {
|
|
75
|
+
return jsi::Value(runtime, *jsValue);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 1. Create jsi::ArrayBuffer
|
|
80
|
+
jsi::ArrayBuffer arrayBuffer(runtime, buffer);
|
|
81
|
+
// 2. Wrap jsi::MutableBuffer in jsi::NativeState holder & attach it
|
|
82
|
+
auto mutableBufferHolder = std::make_shared<MutableBufferNativeState>(buffer);
|
|
83
|
+
arrayBuffer.setNativeState(runtime, mutableBufferHolder);
|
|
84
|
+
// 3. Return jsi::ArrayBuffer (with jsi::NativeState) to JS
|
|
85
|
+
return arrayBuffer;
|
|
58
86
|
}
|
|
59
87
|
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
60
88
|
if (value.isObject()) {
|
|
@@ -39,15 +39,6 @@ struct JSIConverter<std::exception_ptr> final {
|
|
|
39
39
|
} catch (const std::exception& e) {
|
|
40
40
|
jsi::JSError error(runtime, e.what());
|
|
41
41
|
return jsi::Value(runtime, error.value());
|
|
42
|
-
#ifdef ANDROID
|
|
43
|
-
#pragma clang diagnostic push
|
|
44
|
-
#pragma clang diagnostic ignored "-Wexceptions"
|
|
45
|
-
// Workaround for https://github.com/mrousavy/nitro/issues/382
|
|
46
|
-
} catch (const std::runtime_error& e) {
|
|
47
|
-
jsi::JSError error(runtime, e.what());
|
|
48
|
-
return jsi::Value(runtime, error.value());
|
|
49
|
-
#pragma clang diagnostic pop
|
|
50
|
-
#endif
|
|
51
42
|
} catch (...) {
|
|
52
43
|
// Some unknown exception was thrown - maybe an Objective-C error?
|
|
53
44
|
std::string errorName = TypeInfo::getCurrentExceptionName();
|
|
@@ -22,7 +22,8 @@ private:
|
|
|
22
22
|
|
|
23
23
|
public:
|
|
24
24
|
template <typename... Args>
|
|
25
|
-
static void log(LogLevel level, const char* tag, const char* format,
|
|
25
|
+
static void log([[maybe_unused]] LogLevel level, [[maybe_unused]] const char* tag, [[maybe_unused]] const char* format,
|
|
26
|
+
[[maybe_unused]] Args... args) {
|
|
26
27
|
#ifdef NITRO_DEBUG
|
|
27
28
|
// 1. Make sure args can be passed to sprintf(..)
|
|
28
29
|
static_assert(all_are_trivially_copyable<Args...>(), "All arguments passed to Logger::log(..) must be trivially copyable! "
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//
|
|
2
|
-
//
|
|
2
|
+
// AnyMap.swift
|
|
3
3
|
// NitroModules
|
|
4
4
|
//
|
|
5
5
|
// Created by Marc Rousavy on 20.08.24.
|
|
@@ -21,20 +21,20 @@ public indirect enum AnyValue {
|
|
|
21
21
|
case object(Dictionary<String, AnyValue>)
|
|
22
22
|
|
|
23
23
|
static func create(_ value: margelo.nitro.AnyValue) -> AnyValue {
|
|
24
|
-
if margelo.nitro.is_AnyValue_null(value) {
|
|
24
|
+
if margelo.nitro.AnyMapUtils.is_AnyValue_null(value) {
|
|
25
25
|
return .null
|
|
26
|
-
} else if margelo.nitro.is_AnyValue_bool(value) {
|
|
27
|
-
return .bool(margelo.nitro.get_AnyValue_bool(value))
|
|
28
|
-
} else if margelo.nitro.is_AnyValue_number(value) {
|
|
29
|
-
return .number(margelo.nitro.get_AnyValue_number(value))
|
|
30
|
-
} else if margelo.nitro.is_AnyValue_bigint(value) {
|
|
31
|
-
return .bigint(margelo.nitro.get_AnyValue_bigint(value))
|
|
32
|
-
} else if margelo.nitro.is_AnyValue_string(value) {
|
|
33
|
-
return .string(margelo.nitro.get_AnyValue_string(value).toSwift())
|
|
34
|
-
} else if margelo.nitro.is_AnyValue_AnyArray(value) {
|
|
35
|
-
return .array(margelo.nitro.get_AnyValue_AnyArray(value).toSwift())
|
|
36
|
-
} else if margelo.nitro.is_AnyValue_AnyObject(value) {
|
|
37
|
-
return .object(margelo.nitro.get_AnyValue_AnyObject(value).toSwift())
|
|
26
|
+
} else if margelo.nitro.AnyMapUtils.is_AnyValue_bool(value) {
|
|
27
|
+
return .bool(margelo.nitro.AnyMapUtils.get_AnyValue_bool(value))
|
|
28
|
+
} else if margelo.nitro.AnyMapUtils.is_AnyValue_number(value) {
|
|
29
|
+
return .number(margelo.nitro.AnyMapUtils.get_AnyValue_number(value))
|
|
30
|
+
} else if margelo.nitro.AnyMapUtils.is_AnyValue_bigint(value) {
|
|
31
|
+
return .bigint(margelo.nitro.AnyMapUtils.get_AnyValue_bigint(value))
|
|
32
|
+
} else if margelo.nitro.AnyMapUtils.is_AnyValue_string(value) {
|
|
33
|
+
return .string(margelo.nitro.AnyMapUtils.get_AnyValue_string(value).toSwift())
|
|
34
|
+
} else if margelo.nitro.AnyMapUtils.is_AnyValue_AnyArray(value) {
|
|
35
|
+
return .array(margelo.nitro.AnyMapUtils.get_AnyValue_AnyArray(value).toSwift())
|
|
36
|
+
} else if margelo.nitro.AnyMapUtils.is_AnyValue_AnyObject(value) {
|
|
37
|
+
return .object(margelo.nitro.AnyMapUtils.get_AnyValue_AnyObject(value).toSwift())
|
|
38
38
|
} else {
|
|
39
39
|
fatalError("AnyValue has unknown type!")
|
|
40
40
|
}
|
|
@@ -42,10 +42,11 @@ public indirect enum AnyValue {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
* Represents an `AnyMap
|
|
45
|
+
* Represents an `AnyMap`- an untyped map instance.
|
|
46
|
+
* See C++ `AnyMap.hpp` for more information.
|
|
46
47
|
*/
|
|
47
|
-
public final class
|
|
48
|
-
public let cppPart: margelo.nitro.
|
|
48
|
+
public final class AnyMap {
|
|
49
|
+
public let cppPart: margelo.nitro.SharedAnyMap
|
|
49
50
|
|
|
50
51
|
public init() {
|
|
51
52
|
cppPart = margelo.nitro.AnyMap.make()
|
|
@@ -55,7 +56,7 @@ public final class AnyMapHolder {
|
|
|
55
56
|
cppPart = margelo.nitro.AnyMap.make(size)
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
public init(withCppPart otherCppPart: margelo.nitro.
|
|
59
|
+
public init(withCppPart otherCppPart: margelo.nitro.SharedAnyMap) {
|
|
59
60
|
cppPart = otherCppPart
|
|
60
61
|
}
|
|
61
62
|
|
|
@@ -279,25 +280,25 @@ extension margelo.nitro.AnyValue {
|
|
|
279
280
|
}
|
|
280
281
|
}
|
|
281
282
|
static func create() -> margelo.nitro.AnyValue {
|
|
282
|
-
return margelo.nitro.create_AnyValue()
|
|
283
|
+
return margelo.nitro.AnyMapUtils.create_AnyValue()
|
|
283
284
|
}
|
|
284
285
|
static func create(_ value: Bool) -> margelo.nitro.AnyValue {
|
|
285
|
-
return margelo.nitro.create_AnyValue(value)
|
|
286
|
+
return margelo.nitro.AnyMapUtils.create_AnyValue(value)
|
|
286
287
|
}
|
|
287
288
|
static func create(_ value: Double) -> margelo.nitro.AnyValue {
|
|
288
|
-
return margelo.nitro.create_AnyValue(value)
|
|
289
|
+
return margelo.nitro.AnyMapUtils.create_AnyValue(value)
|
|
289
290
|
}
|
|
290
291
|
static func create(_ value: Int64) -> margelo.nitro.AnyValue {
|
|
291
|
-
return margelo.nitro.create_AnyValue(value)
|
|
292
|
+
return margelo.nitro.AnyMapUtils.create_AnyValue(value)
|
|
292
293
|
}
|
|
293
294
|
static func create(_ value: String) -> margelo.nitro.AnyValue {
|
|
294
|
-
return margelo.nitro.create_AnyValue(std.string(value))
|
|
295
|
+
return margelo.nitro.AnyMapUtils.create_AnyValue(std.string(value))
|
|
295
296
|
}
|
|
296
297
|
static func create(_ value: [AnyValue]) -> margelo.nitro.AnyValue {
|
|
297
|
-
return margelo.nitro.create_AnyValue(margelo.nitro.AnyArray.create(value))
|
|
298
|
+
return margelo.nitro.AnyMapUtils.create_AnyValue(margelo.nitro.AnyArray.create(value))
|
|
298
299
|
}
|
|
299
300
|
static func create(_ value: Dictionary<String, AnyValue>) -> margelo.nitro.AnyValue {
|
|
300
|
-
return margelo.nitro.create_AnyValue(margelo.nitro.AnyObject.create(value))
|
|
301
|
+
return margelo.nitro.AnyMapUtils.create_AnyValue(margelo.nitro.AnyObject.create(value))
|
|
301
302
|
}
|
|
302
303
|
}
|
|
303
304
|
|
|
@@ -344,12 +345,16 @@ extension margelo.nitro.AnyObject {
|
|
|
344
345
|
}
|
|
345
346
|
|
|
346
347
|
func toSwift() -> Dictionary<String, AnyValue> {
|
|
347
|
-
let keys = margelo.nitro.getAnyObjectKeys(self)
|
|
348
|
+
let keys = margelo.nitro.AnyMapUtils.getAnyObjectKeys(self)
|
|
348
349
|
var dictionary = Dictionary<String, AnyValue>(minimumCapacity: keys.size())
|
|
349
350
|
for key in keys {
|
|
350
|
-
let value = margelo.nitro.getAnyObjectValue(self, key)
|
|
351
|
+
let value = margelo.nitro.AnyMapUtils.getAnyObjectValue(self, key)
|
|
351
352
|
dictionary[String(key)] = AnyValue.create(value)
|
|
352
353
|
}
|
|
353
354
|
return dictionary
|
|
354
355
|
}
|
|
355
356
|
}
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
@available(*, deprecated, renamed: "AnyMap")
|
|
360
|
+
public typealias AnyMapHolder = AnyMap
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//
|
|
2
|
-
//
|
|
2
|
+
// ArrayBuffer.swift
|
|
3
3
|
// NitroModules
|
|
4
4
|
//
|
|
5
5
|
// Created by Marc Rousavy on 17.07.24.
|
|
@@ -13,36 +13,39 @@ import Foundation
|
|
|
13
13
|
*
|
|
14
14
|
* See `data`, `size` and `isOwning`.
|
|
15
15
|
*/
|
|
16
|
-
public typealias
|
|
16
|
+
public typealias ArrayBuffer = margelo.nitro.ArrayBufferHolder
|
|
17
|
+
|
|
18
|
+
@available(*, deprecated, renamed: "ArrayBuffer")
|
|
19
|
+
public typealias ArrayBufferHolder = ArrayBuffer
|
|
17
20
|
|
|
18
21
|
// pragma MARK: Wrap
|
|
19
22
|
|
|
20
|
-
public extension
|
|
23
|
+
public extension ArrayBuffer {
|
|
21
24
|
/**
|
|
22
|
-
* Create a new `
|
|
25
|
+
* Create a new `ArrayBuffer` that wraps the given `data` of the given `size`
|
|
23
26
|
* without performing a copy.
|
|
24
27
|
* When the `ArrayBuffer` is no longer used, `onDelete` will be called, in which
|
|
25
28
|
* you as a caller are responsible for deleting `data`.
|
|
26
29
|
*/
|
|
27
30
|
static func wrap(dataWithoutCopy data: UnsafeMutablePointer<UInt8>,
|
|
28
31
|
size: Int,
|
|
29
|
-
onDelete delete: @escaping () -> Void) ->
|
|
32
|
+
onDelete delete: @escaping () -> Void) -> ArrayBuffer {
|
|
30
33
|
// Convert escaping Swift closure to a `void*`
|
|
31
34
|
let swiftClosure = SwiftClosure(wrappingClosure: delete)
|
|
32
|
-
// Create
|
|
33
|
-
return
|
|
35
|
+
// Create ArrayBuffer with our wrapped Swift closure to make it callable as a C-function pointer
|
|
36
|
+
return ArrayBuffer.wrap(data, size, swiftClosure)
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
/**
|
|
37
|
-
* Create a new `
|
|
40
|
+
* Create a new `ArrayBuffer` that wraps the given `data` of the given `size`
|
|
38
41
|
* without performing a copy.
|
|
39
42
|
* When the `ArrayBuffer` is no longer used, `onDelete` will be called, in which
|
|
40
43
|
* you as a caller are responsible for deleting `data`.
|
|
41
44
|
*/
|
|
42
45
|
static func wrap(dataWithoutCopy data: UnsafeMutableRawPointer,
|
|
43
46
|
size: Int,
|
|
44
|
-
onDelete delete: @escaping () -> Void) ->
|
|
45
|
-
return
|
|
47
|
+
onDelete delete: @escaping () -> Void) -> ArrayBuffer {
|
|
48
|
+
return ArrayBuffer.wrap(dataWithoutCopy: data.assumingMemoryBound(to: UInt8.self),
|
|
46
49
|
size: size,
|
|
47
50
|
onDelete: delete)
|
|
48
51
|
}
|
|
@@ -50,12 +53,12 @@ public extension ArrayBufferHolder {
|
|
|
50
53
|
|
|
51
54
|
// pragma MARK: Allocate
|
|
52
55
|
|
|
53
|
-
public extension
|
|
56
|
+
public extension ArrayBuffer {
|
|
54
57
|
/**
|
|
55
58
|
* Allocate a new buffer of the given `size`.
|
|
56
59
|
* If `initializeToZero` is `true`, all bytes are set to `0`, otherwise they are left untouched.
|
|
57
60
|
*/
|
|
58
|
-
static func allocate(size: Int, initializeToZero: Bool = false) ->
|
|
61
|
+
static func allocate(size: Int, initializeToZero: Bool = false) -> ArrayBuffer {
|
|
59
62
|
let data = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
|
|
60
63
|
if initializeToZero {
|
|
61
64
|
data.initialize(repeating: 0, count: size)
|
|
@@ -63,18 +66,18 @@ public extension ArrayBufferHolder {
|
|
|
63
66
|
let deleteFunc = SwiftClosure {
|
|
64
67
|
data.deallocate()
|
|
65
68
|
}
|
|
66
|
-
return
|
|
69
|
+
return ArrayBuffer.wrap(data, size, deleteFunc)
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
// pragma MARK: Copy
|
|
71
74
|
|
|
72
|
-
public extension
|
|
75
|
+
public extension ArrayBuffer {
|
|
73
76
|
/**
|
|
74
|
-
* Copy the given `UnsafeMutablePointer<UInt8>` into a new **owning** `
|
|
77
|
+
* Copy the given `UnsafeMutablePointer<UInt8>` into a new **owning** `ArrayBuffer`.
|
|
75
78
|
*/
|
|
76
79
|
static func copy(of other: UnsafeMutablePointer<UInt8>,
|
|
77
|
-
size: Int) ->
|
|
80
|
+
size: Int) -> ArrayBuffer {
|
|
78
81
|
// 1. Create new `UnsafeMutablePointer<UInt8>`
|
|
79
82
|
let copy = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
|
|
80
83
|
// 2. Copy over data
|
|
@@ -83,32 +86,32 @@ public extension ArrayBufferHolder {
|
|
|
83
86
|
let deleteFunc = SwiftClosure {
|
|
84
87
|
copy.deallocate()
|
|
85
88
|
}
|
|
86
|
-
return
|
|
89
|
+
return ArrayBuffer.wrap(copy, size, deleteFunc)
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
/**
|
|
90
|
-
* Copy the given `UnsafeMutableRawPointer` into a new **owning** `
|
|
93
|
+
* Copy the given `UnsafeMutableRawPointer` into a new **owning** `ArrayBuffer`.
|
|
91
94
|
*/
|
|
92
95
|
static func copy(of other: UnsafeMutableRawPointer,
|
|
93
|
-
size: Int) ->
|
|
94
|
-
return
|
|
96
|
+
size: Int) -> ArrayBuffer {
|
|
97
|
+
return ArrayBuffer.copy(of: other.assumingMemoryBound(to: UInt8.self),
|
|
95
98
|
size: size)
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
/**
|
|
99
|
-
* Copy the given `
|
|
102
|
+
* Copy the given `ArrayBuffer` into a new **owning** `ArrayBuffer`.
|
|
100
103
|
*/
|
|
101
|
-
static func copy(of other:
|
|
102
|
-
return
|
|
104
|
+
static func copy(of other: ArrayBuffer) -> ArrayBuffer {
|
|
105
|
+
return ArrayBuffer.copy(of: other.data, size: other.size)
|
|
103
106
|
}
|
|
104
107
|
|
|
105
108
|
/**
|
|
106
|
-
* Copy the given `Data` into a new **owning** `
|
|
109
|
+
* Copy the given `Data` into a new **owning** `ArrayBuffer`.
|
|
107
110
|
*/
|
|
108
|
-
static func copy(data: Data) throws ->
|
|
111
|
+
static func copy(data: Data) throws -> ArrayBuffer {
|
|
109
112
|
// 1. Create new `ArrayBuffer` of same size
|
|
110
113
|
let size = data.count
|
|
111
|
-
let arrayBuffer =
|
|
114
|
+
let arrayBuffer = ArrayBuffer.allocate(size: size)
|
|
112
115
|
// 2. Copy all bytes from `Data` into our new `ArrayBuffer`
|
|
113
116
|
try data.withUnsafeBytes { rawPointer in
|
|
114
117
|
guard let baseAddress = rawPointer.baseAddress else {
|
|
@@ -123,9 +126,9 @@ public extension ArrayBufferHolder {
|
|
|
123
126
|
|
|
124
127
|
// pragma MARK: Data
|
|
125
128
|
|
|
126
|
-
public extension
|
|
129
|
+
public extension ArrayBuffer {
|
|
127
130
|
/**
|
|
128
|
-
* Wrap this `
|
|
131
|
+
* Wrap this `ArrayBuffer` in a `Data` instance, without performing a copy.
|
|
129
132
|
* - `copyIfNeeded`: If this `ArrayBuffer` is **non-owning**, the foreign
|
|
130
133
|
* data may needs to be copied to be safely used outside of the scope of the caller function.
|
|
131
134
|
* This flag controls that.
|
|
@@ -24,7 +24,7 @@ using namespace facebook;
|
|
|
24
24
|
* Passing around instances of `ArrayBufferHolder` (or `std::shared_ptr<ArrayBuffer>`)
|
|
25
25
|
* does not involve any data copies and is almost zero-overhead - even when passed to JS.
|
|
26
26
|
*/
|
|
27
|
-
class ArrayBufferHolder {
|
|
27
|
+
class ArrayBufferHolder final {
|
|
28
28
|
public:
|
|
29
29
|
ArrayBufferHolder(const std::shared_ptr<ArrayBuffer>& arrayBuffer) : _arrayBuffer(arrayBuffer) {}
|
|
30
30
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//
|
|
2
|
+
// AnyMapHolder.hpp
|
|
3
|
+
// NitroModules
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 20.08.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include "AnyMapUtils.hpp"
|
|
11
|
+
|
|
12
|
+
// THIS HEADER IS DEPRECATED! Update to latest Nitro
|
|
13
|
+
#warning "AnyMapHolder.hpp is deprecated. Update Nitro to the latest version to use AnyMapUtils.hpp instead!"
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
//
|
|
2
|
+
// AnyMapUtils.hpp
|
|
3
|
+
// NitroModules
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 20.08.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include "AnyMap.hpp"
|
|
11
|
+
|
|
12
|
+
namespace margelo::nitro {
|
|
13
|
+
|
|
14
|
+
using SharedAnyMap = std::shared_ptr<AnyMap>;
|
|
15
|
+
|
|
16
|
+
// TODO: Remove TSharedMap.
|
|
17
|
+
/// @deprecated
|
|
18
|
+
using TSharedMap = SharedAnyMap;
|
|
19
|
+
|
|
20
|
+
namespace AnyMapUtils {
|
|
21
|
+
|
|
22
|
+
inline AnyValue create_AnyValue() {
|
|
23
|
+
return AnyValue{std::monostate{}};
|
|
24
|
+
}
|
|
25
|
+
inline AnyValue create_AnyValue(bool boolValue) {
|
|
26
|
+
return AnyValue{boolValue};
|
|
27
|
+
}
|
|
28
|
+
inline AnyValue create_AnyValue(double doubleValue) {
|
|
29
|
+
return AnyValue{doubleValue};
|
|
30
|
+
}
|
|
31
|
+
inline AnyValue create_AnyValue(int64_t bigintValue) {
|
|
32
|
+
return AnyValue{bigintValue};
|
|
33
|
+
}
|
|
34
|
+
inline AnyValue create_AnyValue(const std::string& stringValue) {
|
|
35
|
+
return AnyValue{stringValue};
|
|
36
|
+
}
|
|
37
|
+
inline AnyValue create_AnyValue(const AnyArray& arrayValue) {
|
|
38
|
+
return AnyValue{arrayValue};
|
|
39
|
+
}
|
|
40
|
+
inline AnyValue create_AnyValue(const AnyObject& objectValue) {
|
|
41
|
+
return AnyValue{objectValue};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
inline bool is_AnyValue_null(const AnyValue& value) {
|
|
45
|
+
return std::holds_alternative<std::monostate>(value);
|
|
46
|
+
}
|
|
47
|
+
inline bool is_AnyValue_bool(const AnyValue& value) {
|
|
48
|
+
return std::holds_alternative<bool>(value);
|
|
49
|
+
}
|
|
50
|
+
inline bool is_AnyValue_number(const AnyValue& value) {
|
|
51
|
+
return std::holds_alternative<double>(value);
|
|
52
|
+
}
|
|
53
|
+
inline bool is_AnyValue_bigint(const AnyValue& value) {
|
|
54
|
+
return std::holds_alternative<int64_t>(value);
|
|
55
|
+
}
|
|
56
|
+
inline bool is_AnyValue_string(const AnyValue& value) {
|
|
57
|
+
return std::holds_alternative<std::string>(value);
|
|
58
|
+
}
|
|
59
|
+
inline bool is_AnyValue_AnyArray(const AnyValue& value) {
|
|
60
|
+
return std::holds_alternative<AnyArray>(value);
|
|
61
|
+
}
|
|
62
|
+
inline bool is_AnyValue_AnyObject(const AnyValue& value) {
|
|
63
|
+
return std::holds_alternative<AnyObject>(value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
inline std::monostate get_AnyValue_null(const AnyValue& value) {
|
|
67
|
+
return std::get<std::monostate>(value);
|
|
68
|
+
}
|
|
69
|
+
inline bool get_AnyValue_bool(const AnyValue& value) {
|
|
70
|
+
return std::get<bool>(value);
|
|
71
|
+
}
|
|
72
|
+
inline double get_AnyValue_number(const AnyValue& value) {
|
|
73
|
+
return std::get<double>(value);
|
|
74
|
+
}
|
|
75
|
+
inline int64_t get_AnyValue_bigint(const AnyValue& value) {
|
|
76
|
+
return std::get<int64_t>(value);
|
|
77
|
+
}
|
|
78
|
+
inline std::string get_AnyValue_string(const AnyValue& value) {
|
|
79
|
+
return std::get<std::string>(value);
|
|
80
|
+
}
|
|
81
|
+
inline AnyArray get_AnyValue_AnyArray(const AnyValue& value) {
|
|
82
|
+
return std::get<AnyArray>(value);
|
|
83
|
+
}
|
|
84
|
+
inline AnyObject get_AnyValue_AnyObject(const AnyValue& value) {
|
|
85
|
+
return std::get<AnyObject>(value);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
inline std::vector<std::string> getAnyObjectKeys(const AnyObject& object) {
|
|
89
|
+
std::vector<std::string> keys;
|
|
90
|
+
keys.reserve(object.size());
|
|
91
|
+
for (const auto& entry : object) {
|
|
92
|
+
keys.push_back(entry.first);
|
|
93
|
+
}
|
|
94
|
+
return keys;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
inline AnyValue getAnyObjectValue(const AnyObject& object, const std::string& key) {
|
|
98
|
+
auto item = object.find(key);
|
|
99
|
+
if (item == object.end()) {
|
|
100
|
+
throw std::runtime_error("Couldn't find " + key + " in AnyObject!");
|
|
101
|
+
}
|
|
102
|
+
return item->second;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
}; // namespace AnyMapUtils
|
|
106
|
+
|
|
107
|
+
} // namespace margelo::nitro
|
|
@@ -11,7 +11,7 @@ public extension Date {
|
|
|
11
11
|
/**
|
|
12
12
|
* Create a new `Date` object from the given `std::chrono::system_clock::time_point` value.
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
init(fromChrono date: margelo.nitro.chrono_time) {
|
|
15
15
|
let millisecondsSinceEpoch = margelo.nitro.millisecondsSinceEpochFromChronoDate(date)
|
|
16
16
|
self = .init(timeIntervalSince1970: millisecondsSinceEpoch / 1_000)
|
|
17
17
|
}
|
|
@@ -19,7 +19,7 @@ public extension Date {
|
|
|
19
19
|
/**
|
|
20
20
|
* Converts this `Date` object to a `std::chrono::system_clock::time_point` value.
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
func toCpp() -> margelo.nitro.chrono_time {
|
|
23
23
|
let secondsSinceEpoch = self.timeIntervalSince1970
|
|
24
24
|
let millisecondsSinceEpoch = secondsSinceEpoch * 1_000
|
|
25
25
|
return margelo.nitro.chronoDateFromMillisecondsSinceEpoch(millisecondsSinceEpoch)
|