react-native-mmkv 2.12.2 → 3.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/LICENSE +1 -200
  2. package/MMKV/Core/Core.xcodeproj/project.pbxproj +4 -2
  3. package/MMKV/Core/MMKVPredef.h +1 -1
  4. package/MMKV/Core/MMKV_IO.cpp +5 -1
  5. package/MMKV/Core/MemoryFile.cpp +1 -0
  6. package/MMKV/Core/MemoryFile_OSX.cpp +2 -1
  7. package/MMKV/README.md +8 -8
  8. package/README.md +7 -1
  9. package/android/CMakeLists.txt +20 -28
  10. package/android/build.gradle +59 -89
  11. package/android/gradle.properties +4 -5
  12. package/android/src/main/AndroidManifest.xml +2 -2
  13. package/android/src/{hasNamespace/AndroidManifest.xml → main/AndroidManifestNew.xml} +0 -1
  14. package/android/src/main/cpp/AndroidLogger.cpp +16 -0
  15. package/android/src/main/cpp/cpp-adapter.cpp +4 -80
  16. package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java +44 -0
  17. package/android/src/main/java/com/mrousavy/mmkv/MmkvPlatformContextModule.java +17 -0
  18. package/cpp/Logger.h +35 -0
  19. package/cpp/MMKVManagedBuffer.h +37 -0
  20. package/{android/src/main/cpp → cpp}/MmkvHostObject.cpp +126 -63
  21. package/{android/src/main/cpp → cpp}/MmkvHostObject.h +14 -3
  22. package/cpp/NativeMmkvModule.cpp +40 -0
  23. package/cpp/NativeMmkvModule.h +37 -0
  24. package/ios/AppleLogger.mm +16 -0
  25. package/ios/MmkvOnLoad.mm +25 -0
  26. package/ios/MmkvPlatformContext.h +19 -0
  27. package/ios/MmkvPlatformContextModule.mm +37 -0
  28. package/lib/commonjs/MMKV.js +34 -4
  29. package/lib/commonjs/MMKV.js.map +1 -1
  30. package/lib/commonjs/NativeMmkv.js +66 -0
  31. package/lib/commonjs/NativeMmkv.js.map +1 -0
  32. package/lib/commonjs/NativeMmkvPlatformContext.js +9 -0
  33. package/lib/commonjs/NativeMmkvPlatformContext.js.map +1 -0
  34. package/lib/commonjs/PlatformChecker.js.map +1 -1
  35. package/lib/commonjs/createMMKV.js +7 -43
  36. package/lib/commonjs/createMMKV.js.map +1 -1
  37. package/lib/commonjs/createMMKV.mock.js +5 -1
  38. package/lib/commonjs/createMMKV.mock.js.map +1 -1
  39. package/lib/commonjs/createMMKV.web.js +8 -4
  40. package/lib/commonjs/createMMKV.web.js.map +1 -1
  41. package/lib/commonjs/createTextEncoder.js.map +1 -1
  42. package/lib/commonjs/hooks.js +11 -10
  43. package/lib/commonjs/hooks.js.map +1 -1
  44. package/lib/commonjs/index.js.map +1 -1
  45. package/lib/module/MMKV.js +23 -4
  46. package/lib/module/MMKV.js.map +1 -1
  47. package/lib/module/NativeMmkv.js +63 -0
  48. package/lib/module/NativeMmkv.js.map +1 -0
  49. package/lib/module/NativeMmkvPlatformContext.js +3 -0
  50. package/lib/module/NativeMmkvPlatformContext.js.map +1 -0
  51. package/lib/module/PlatformChecker.js.map +1 -1
  52. package/lib/module/createMMKV.js +7 -43
  53. package/lib/module/createMMKV.js.map +1 -1
  54. package/lib/module/createMMKV.mock.js +5 -1
  55. package/lib/module/createMMKV.mock.js.map +1 -1
  56. package/lib/module/createMMKV.web.js +6 -4
  57. package/lib/module/createMMKV.web.js.map +1 -1
  58. package/lib/module/createTextEncoder.js.map +1 -1
  59. package/lib/module/hooks.js +7 -2
  60. package/lib/module/hooks.js.map +1 -1
  61. package/lib/module/index.js.map +1 -1
  62. package/lib/typescript/{MMKV.d.ts → src/MMKV.d.ts} +26 -49
  63. package/lib/typescript/src/MMKV.d.ts.map +1 -0
  64. package/lib/typescript/src/NativeMmkv.d.ts +79 -0
  65. package/lib/typescript/src/NativeMmkv.d.ts.map +1 -0
  66. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts +9 -0
  67. package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +1 -0
  68. package/lib/typescript/src/PlatformChecker.d.ts.map +1 -0
  69. package/lib/typescript/src/__tests__/hooks.test.d.ts +2 -0
  70. package/lib/typescript/src/__tests__/hooks.test.d.ts.map +1 -0
  71. package/lib/typescript/src/createMMKV.d.ts +3 -0
  72. package/lib/typescript/src/createMMKV.d.ts.map +1 -0
  73. package/lib/typescript/src/createMMKV.mock.d.ts +3 -0
  74. package/lib/typescript/src/createMMKV.mock.d.ts.map +1 -0
  75. package/lib/typescript/src/createMMKV.web.d.ts +3 -0
  76. package/lib/typescript/src/createMMKV.web.d.ts.map +1 -0
  77. package/lib/typescript/src/createTextEncoder.d.ts.map +1 -0
  78. package/lib/typescript/{hooks.d.ts → src/hooks.d.ts} +7 -4
  79. package/lib/typescript/src/hooks.d.ts.map +1 -0
  80. package/lib/typescript/src/index.d.ts.map +1 -0
  81. package/package.json +49 -40
  82. package/react-native-mmkv.podspec +17 -18
  83. package/src/MMKV.ts +40 -62
  84. package/src/NativeMmkv.ts +132 -0
  85. package/src/NativeMmkvPlatformContext.ts +12 -0
  86. package/src/__tests__/hooks.test.tsx +88 -0
  87. package/src/createMMKV.mock.ts +7 -3
  88. package/src/createMMKV.ts +9 -63
  89. package/src/createMMKV.web.ts +6 -2
  90. package/src/hooks.ts +17 -11
  91. package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +0 -49
  92. package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +0 -26
  93. package/cpp/TypedArray.cpp +0 -322
  94. package/cpp/TypedArray.h +0 -153
  95. package/ios/JSIUtils.h +0 -38
  96. package/ios/JSIUtils.mm +0 -167
  97. package/ios/Mmkv.xcodeproj/project.pbxproj +0 -291
  98. package/ios/MmkvHostObject.h +0 -27
  99. package/ios/MmkvHostObject.mm +0 -295
  100. package/ios/MmkvModule.h +0 -5
  101. package/ios/MmkvModule.mm +0 -101
  102. package/lib/typescript/MMKV.d.ts.map +0 -1
  103. package/lib/typescript/PlatformChecker.d.ts.map +0 -1
  104. package/lib/typescript/createMMKV.d.ts +0 -7
  105. package/lib/typescript/createMMKV.d.ts.map +0 -1
  106. package/lib/typescript/createMMKV.mock.d.ts +0 -3
  107. package/lib/typescript/createMMKV.mock.d.ts.map +0 -1
  108. package/lib/typescript/createMMKV.web.d.ts +0 -3
  109. package/lib/typescript/createMMKV.web.d.ts.map +0 -1
  110. package/lib/typescript/createTextEncoder.d.ts.map +0 -1
  111. package/lib/typescript/hooks.d.ts.map +0 -1
  112. package/lib/typescript/index.d.ts.map +0 -1
  113. /package/lib/typescript/{PlatformChecker.d.ts → src/PlatformChecker.d.ts} +0 -0
  114. /package/lib/typescript/{createTextEncoder.d.ts → src/createTextEncoder.d.ts} +0 -0
  115. /package/lib/typescript/{index.d.ts → src/index.d.ts} +0 -0
  116. /package/src/{index.ts → index.tsx} +0 -0
@@ -1,322 +0,0 @@
1
- //
2
- // TypedArray.cpp
3
- // react-native-mmkv
4
- //
5
- // Created by Marc Rousavy on 11.10.2022.
6
- // Originally created by Expo (expo-gl)
7
- //
8
-
9
- #include "TypedArray.h"
10
-
11
- #include <algorithm>
12
- #include <memory>
13
- #include <string>
14
- #include <unordered_map>
15
- #include <utility>
16
- #include <vector>
17
-
18
- template <TypedArrayKind T> using ContentType = typename typedArrayTypeMap<T>::type;
19
-
20
- enum class Prop {
21
- Buffer, // "buffer"
22
- Constructor, // "constructor"
23
- Name, // "name"
24
- Proto, // "__proto__"
25
- Length, // "length"
26
- ByteLength, // "byteLength"
27
- ByteOffset, // "offset"
28
- IsView, // "isView"
29
- ArrayBuffer, // "ArrayBuffer"
30
- Int8Array, // "Int8Array"
31
- Int16Array, // "Int16Array"
32
- Int32Array, // "Int32Array"
33
- Uint8Array, // "Uint8Array"
34
- Uint8ClampedArray, // "Uint8ClampedArray"
35
- Uint16Array, // "Uint16Array"
36
- Uint32Array, // "Uint32Array"
37
- Float32Array, // "Float32Array"
38
- Float64Array, // "Float64Array"
39
- };
40
-
41
- class PropNameIDCache {
42
- public:
43
- const jsi::PropNameID& get(jsi::Runtime& runtime, Prop prop) {
44
- auto key = reinterpret_cast<uintptr_t>(&runtime);
45
- if (this->props.find(key) == this->props.end()) {
46
- this->props[key] = std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>>();
47
- }
48
- if (!this->props[key][prop]) {
49
- this->props[key][prop] = std::make_unique<jsi::PropNameID>(createProp(runtime, prop));
50
- }
51
- return *(this->props[key][prop]);
52
- }
53
-
54
- const jsi::PropNameID& getConstructorNameProp(jsi::Runtime& runtime, TypedArrayKind kind);
55
-
56
- void invalidate(uintptr_t key) {
57
- if (props.find(key) != props.end()) {
58
- props[key].clear();
59
- }
60
- }
61
-
62
- private:
63
- std::unordered_map<uintptr_t, std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>>> props;
64
-
65
- jsi::PropNameID createProp(jsi::Runtime& runtime, Prop prop);
66
- };
67
-
68
- PropNameIDCache propNameIDCache;
69
-
70
- InvalidateCacheOnDestroy::InvalidateCacheOnDestroy(jsi::Runtime& runtime) {
71
- key = reinterpret_cast<uintptr_t>(&runtime);
72
- }
73
- InvalidateCacheOnDestroy::~InvalidateCacheOnDestroy() {
74
- propNameIDCache.invalidate(key);
75
- }
76
-
77
- TypedArrayKind getTypedArrayKindForName(const std::string& name);
78
-
79
- TypedArrayBase::TypedArrayBase(jsi::Runtime& runtime, size_t size, TypedArrayKind kind)
80
- : TypedArrayBase(
81
- runtime, runtime.global()
82
- .getProperty(runtime, propNameIDCache.getConstructorNameProp(runtime, kind))
83
- .asObject(runtime)
84
- .asFunction(runtime)
85
- .callAsConstructor(runtime, {static_cast<double>(size)})
86
- .asObject(runtime)) {}
87
-
88
- TypedArrayBase::TypedArrayBase(jsi::Runtime& runtime, const jsi::Object& obj)
89
- : jsi::Object(jsi::Value(runtime, obj).asObject(runtime)) {}
90
-
91
- TypedArrayKind TypedArrayBase::getKind(jsi::Runtime& runtime) const {
92
- auto constructorName = this->getProperty(runtime, propNameIDCache.get(runtime, Prop::Constructor))
93
- .asObject(runtime)
94
- .getProperty(runtime, propNameIDCache.get(runtime, Prop::Name))
95
- .asString(runtime)
96
- .utf8(runtime);
97
- return getTypedArrayKindForName(constructorName);
98
- }
99
-
100
- size_t TypedArrayBase::size(jsi::Runtime& runtime) const {
101
- return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
102
- }
103
-
104
- size_t TypedArrayBase::length(jsi::Runtime& runtime) const {
105
- return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
106
- }
107
-
108
- size_t TypedArrayBase::byteLength(jsi::Runtime& runtime) const {
109
- return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteLength)).asNumber();
110
- }
111
-
112
- size_t TypedArrayBase::byteOffset(jsi::Runtime& runtime) const {
113
- return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteOffset)).asNumber();
114
- }
115
-
116
- bool TypedArrayBase::hasBuffer(jsi::Runtime& runtime) const {
117
- auto buffer = getProperty(runtime, propNameIDCache.get(runtime, Prop::Buffer));
118
- return buffer.isObject() && buffer.asObject(runtime).isArrayBuffer(runtime);
119
- }
120
-
121
- std::vector<uint8_t> TypedArrayBase::toVector(jsi::Runtime& runtime) {
122
- auto start = reinterpret_cast<uint8_t*>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
123
- auto end = start + byteLength(runtime);
124
- return std::vector<uint8_t>(start, end);
125
- }
126
-
127
- jsi::ArrayBuffer TypedArrayBase::getBuffer(jsi::Runtime& runtime) const {
128
- auto buffer = getProperty(runtime, propNameIDCache.get(runtime, Prop::Buffer));
129
- if (buffer.isObject() && buffer.asObject(runtime).isArrayBuffer(runtime)) {
130
- return buffer.asObject(runtime).getArrayBuffer(runtime);
131
- } else {
132
- throw std::runtime_error("no ArrayBuffer attached");
133
- }
134
- }
135
-
136
- bool isTypedArray(jsi::Runtime& runtime, const jsi::Object& jsObj) {
137
- auto jsVal = runtime.global()
138
- .getProperty(runtime, propNameIDCache.get(runtime, Prop::ArrayBuffer))
139
- .asObject(runtime)
140
- .getProperty(runtime, propNameIDCache.get(runtime, Prop::IsView))
141
- .asObject(runtime)
142
- .asFunction(runtime)
143
- .callWithThis(runtime, runtime.global(), {jsi::Value(runtime, jsObj)});
144
- if (jsVal.isBool()) {
145
- return jsVal.getBool();
146
- } else {
147
- throw std::runtime_error("value is not a boolean");
148
- }
149
- }
150
-
151
- TypedArrayBase getTypedArray(jsi::Runtime& runtime, const jsi::Object& jsObj) {
152
- auto jsVal = runtime.global()
153
- .getProperty(runtime, propNameIDCache.get(runtime, Prop::ArrayBuffer))
154
- .asObject(runtime)
155
- .getProperty(runtime, propNameIDCache.get(runtime, Prop::IsView))
156
- .asObject(runtime)
157
- .asFunction(runtime)
158
- .callWithThis(runtime, runtime.global(), {jsi::Value(runtime, jsObj)});
159
- if (jsVal.isBool()) {
160
- return TypedArrayBase(runtime, jsObj);
161
- } else {
162
- throw std::runtime_error("value is not a boolean");
163
- }
164
- }
165
-
166
- std::vector<uint8_t> arrayBufferToVector(jsi::Runtime& runtime, jsi::Object& jsObj) {
167
- if (!jsObj.isArrayBuffer(runtime)) {
168
- throw std::runtime_error("Object is not an ArrayBuffer");
169
- }
170
- auto jsArrayBuffer = jsObj.getArrayBuffer(runtime);
171
-
172
- uint8_t* dataBlock = jsArrayBuffer.data(runtime);
173
- size_t blockSize =
174
- jsArrayBuffer.getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteLength)).asNumber();
175
- return std::vector<uint8_t>(dataBlock, dataBlock + blockSize);
176
- }
177
-
178
- void arrayBufferUpdate(jsi::Runtime& runtime, jsi::ArrayBuffer& buffer, std::vector<uint8_t> data,
179
- size_t offset) {
180
- uint8_t* dataBlock = buffer.data(runtime);
181
- size_t blockSize = buffer.size(runtime);
182
- if (data.size() > blockSize) {
183
- throw jsi::JSError(runtime, "ArrayBuffer is to small to fit data");
184
- }
185
- std::copy(data.begin(), data.end(), dataBlock + offset);
186
- }
187
-
188
- template <TypedArrayKind T>
189
- TypedArray<T>::TypedArray(jsi::Runtime& runtime, size_t size) : TypedArrayBase(runtime, size, T) {}
190
-
191
- template <TypedArrayKind T>
192
- TypedArray<T>::TypedArray(jsi::Runtime& runtime, std::vector<ContentType<T>> data)
193
- : TypedArrayBase(runtime, data.size(), T) {
194
- update(runtime, data);
195
- }
196
-
197
- template <TypedArrayKind T>
198
- TypedArray<T>::TypedArray(TypedArrayBase&& base) : TypedArrayBase(std::move(base)) {}
199
-
200
- template <TypedArrayKind T>
201
- std::vector<ContentType<T>> TypedArray<T>::toVector(jsi::Runtime& runtime) {
202
- auto start =
203
- reinterpret_cast<ContentType<T>*>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
204
- auto end = start + size(runtime);
205
- return std::vector<ContentType<T>>(start, end);
206
- }
207
-
208
- template <TypedArrayKind T>
209
- void TypedArray<T>::update(jsi::Runtime& runtime, const std::vector<ContentType<T>>& data) {
210
- if (data.size() != size(runtime)) {
211
- throw jsi::JSError(runtime, "TypedArray can only be updated with a vector of the same size");
212
- }
213
- uint8_t* rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
214
- std::copy(data.begin(), data.end(), reinterpret_cast<ContentType<T>*>(rawData));
215
- }
216
-
217
- template <TypedArrayKind T>
218
- void TypedArray<T>::updateUnsafe(jsi::Runtime& runtime, ContentType<T>* data, size_t length) {
219
- if (length != size(runtime)) {
220
- throw jsi::JSError(runtime, "TypedArray can only be updated with an array of the same size");
221
- }
222
- uint8_t* rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
223
- memcpy(rawData, data, length);
224
- }
225
-
226
- template <TypedArrayKind T> uint8_t* TypedArray<T>::data(jsi::Runtime& runtime) {
227
- return getBuffer(runtime).data(runtime) + byteOffset(runtime);
228
- }
229
-
230
- const jsi::PropNameID& PropNameIDCache::getConstructorNameProp(jsi::Runtime& runtime,
231
- TypedArrayKind kind) {
232
- switch (kind) {
233
- case TypedArrayKind::Int8Array:
234
- return get(runtime, Prop::Int8Array);
235
- case TypedArrayKind::Int16Array:
236
- return get(runtime, Prop::Int16Array);
237
- case TypedArrayKind::Int32Array:
238
- return get(runtime, Prop::Int32Array);
239
- case TypedArrayKind::Uint8Array:
240
- return get(runtime, Prop::Uint8Array);
241
- case TypedArrayKind::Uint8ClampedArray:
242
- return get(runtime, Prop::Uint8ClampedArray);
243
- case TypedArrayKind::Uint16Array:
244
- return get(runtime, Prop::Uint16Array);
245
- case TypedArrayKind::Uint32Array:
246
- return get(runtime, Prop::Uint32Array);
247
- case TypedArrayKind::Float32Array:
248
- return get(runtime, Prop::Float32Array);
249
- case TypedArrayKind::Float64Array:
250
- return get(runtime, Prop::Float64Array);
251
- }
252
- }
253
-
254
- jsi::PropNameID PropNameIDCache::createProp(jsi::Runtime& runtime, Prop prop) {
255
- auto create = [&](const std::string& propName) {
256
- return jsi::PropNameID::forUtf8(runtime, propName);
257
- };
258
- switch (prop) {
259
- case Prop::Buffer:
260
- return create("buffer");
261
- case Prop::Constructor:
262
- return create("constructor");
263
- case Prop::Name:
264
- return create("name");
265
- case Prop::Proto:
266
- return create("__proto__");
267
- case Prop::Length:
268
- return create("length");
269
- case Prop::ByteLength:
270
- return create("byteLength");
271
- case Prop::ByteOffset:
272
- return create("byteOffset");
273
- case Prop::IsView:
274
- return create("isView");
275
- case Prop::ArrayBuffer:
276
- return create("ArrayBuffer");
277
- case Prop::Int8Array:
278
- return create("Int8Array");
279
- case Prop::Int16Array:
280
- return create("Int16Array");
281
- case Prop::Int32Array:
282
- return create("Int32Array");
283
- case Prop::Uint8Array:
284
- return create("Uint8Array");
285
- case Prop::Uint8ClampedArray:
286
- return create("Uint8ClampedArray");
287
- case Prop::Uint16Array:
288
- return create("Uint16Array");
289
- case Prop::Uint32Array:
290
- return create("Uint32Array");
291
- case Prop::Float32Array:
292
- return create("Float32Array");
293
- case Prop::Float64Array:
294
- return create("Float64Array");
295
- }
296
- }
297
-
298
- std::unordered_map<std::string, TypedArrayKind> nameToKindMap = {
299
- {"Int8Array", TypedArrayKind::Int8Array},
300
- {"Int16Array", TypedArrayKind::Int16Array},
301
- {"Int32Array", TypedArrayKind::Int32Array},
302
- {"Uint8Array", TypedArrayKind::Uint8Array},
303
- {"Uint8ClampedArray", TypedArrayKind::Uint8ClampedArray},
304
- {"Uint16Array", TypedArrayKind::Uint16Array},
305
- {"Uint32Array", TypedArrayKind::Uint32Array},
306
- {"Float32Array", TypedArrayKind::Float32Array},
307
- {"Float64Array", TypedArrayKind::Float64Array},
308
- };
309
-
310
- TypedArrayKind getTypedArrayKindForName(const std::string& name) {
311
- return nameToKindMap.at(name);
312
- }
313
-
314
- template class TypedArray<TypedArrayKind::Int8Array>;
315
- template class TypedArray<TypedArrayKind::Int16Array>;
316
- template class TypedArray<TypedArrayKind::Int32Array>;
317
- template class TypedArray<TypedArrayKind::Uint8Array>;
318
- template class TypedArray<TypedArrayKind::Uint8ClampedArray>;
319
- template class TypedArray<TypedArrayKind::Uint16Array>;
320
- template class TypedArray<TypedArrayKind::Uint32Array>;
321
- template class TypedArray<TypedArrayKind::Float32Array>;
322
- template class TypedArray<TypedArrayKind::Float64Array>;
package/cpp/TypedArray.h DELETED
@@ -1,153 +0,0 @@
1
- //
2
- // TypedArray.h
3
- // react-native-mmkv
4
- //
5
- // Created by Marc Rousavy on 11.10.2022.
6
- // Originally created by Expo (expo-gl)
7
- //
8
-
9
- #pragma once
10
-
11
- #include <jsi/jsi.h>
12
- #include <utility>
13
- #include <vector>
14
-
15
- namespace jsi = facebook::jsi;
16
-
17
- enum class TypedArrayKind {
18
- Int8Array,
19
- Int16Array,
20
- Int32Array,
21
- Uint8Array,
22
- Uint8ClampedArray,
23
- Uint16Array,
24
- Uint32Array,
25
- Float32Array,
26
- Float64Array,
27
- };
28
-
29
- template <TypedArrayKind T> class TypedArray;
30
-
31
- template <TypedArrayKind T> struct typedArrayTypeMap;
32
- template <> struct typedArrayTypeMap<TypedArrayKind::Int8Array> {
33
- typedef int8_t type;
34
- };
35
- template <> struct typedArrayTypeMap<TypedArrayKind::Int16Array> {
36
- typedef int16_t type;
37
- };
38
- template <> struct typedArrayTypeMap<TypedArrayKind::Int32Array> {
39
- typedef int32_t type;
40
- };
41
- template <> struct typedArrayTypeMap<TypedArrayKind::Uint8Array> {
42
- typedef uint8_t type;
43
- };
44
- template <> struct typedArrayTypeMap<TypedArrayKind::Uint8ClampedArray> {
45
- typedef uint8_t type;
46
- };
47
- template <> struct typedArrayTypeMap<TypedArrayKind::Uint16Array> {
48
- typedef uint16_t type;
49
- };
50
- template <> struct typedArrayTypeMap<TypedArrayKind::Uint32Array> {
51
- typedef uint32_t type;
52
- };
53
- template <> struct typedArrayTypeMap<TypedArrayKind::Float32Array> {
54
- typedef float type;
55
- };
56
- template <> struct typedArrayTypeMap<TypedArrayKind::Float64Array> {
57
- typedef double type;
58
- };
59
-
60
- // Instance of this class will invalidate PropNameIDCache when destructor is called.
61
- // Attach this object to global in specific jsi::Runtime to make sure lifecycle of
62
- // the cache object is connected to the lifecycle of the js runtime
63
- class InvalidateCacheOnDestroy : public jsi::HostObject {
64
- public:
65
- explicit InvalidateCacheOnDestroy(jsi::Runtime& runtime);
66
- virtual ~InvalidateCacheOnDestroy();
67
- virtual jsi::Value get(jsi::Runtime&, const jsi::PropNameID& name) {
68
- return jsi::Value::null();
69
- }
70
- virtual void set(jsi::Runtime&, const jsi::PropNameID& name, const jsi::Value& value) {}
71
- virtual std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& rt) {
72
- return {};
73
- }
74
-
75
- private:
76
- uintptr_t key;
77
- };
78
-
79
- class TypedArrayBase : public jsi::Object {
80
- public:
81
- template <TypedArrayKind T> using ContentType = typename typedArrayTypeMap<T>::type;
82
-
83
- TypedArrayBase(jsi::Runtime&, size_t, TypedArrayKind);
84
- TypedArrayBase(jsi::Runtime&, const jsi::Object&);
85
- TypedArrayBase(TypedArrayBase&&) = default;
86
- TypedArrayBase& operator=(TypedArrayBase&&) = default;
87
-
88
- TypedArrayKind getKind(jsi::Runtime& runtime) const;
89
-
90
- template <TypedArrayKind T> TypedArray<T> get(jsi::Runtime& runtime) const&;
91
- template <TypedArrayKind T> TypedArray<T> get(jsi::Runtime& runtime) &&;
92
- template <TypedArrayKind T> TypedArray<T> as(jsi::Runtime& runtime) const&;
93
- template <TypedArrayKind T> TypedArray<T> as(jsi::Runtime& runtime) &&;
94
-
95
- size_t size(jsi::Runtime& runtime) const;
96
- size_t length(jsi::Runtime& runtime) const;
97
- size_t byteLength(jsi::Runtime& runtime) const;
98
- size_t byteOffset(jsi::Runtime& runtime) const;
99
- bool hasBuffer(jsi::Runtime& runtime) const;
100
-
101
- std::vector<uint8_t> toVector(jsi::Runtime& runtime);
102
- jsi::ArrayBuffer getBuffer(jsi::Runtime& runtime) const;
103
-
104
- private:
105
- template <TypedArrayKind> friend class TypedArray;
106
- };
107
-
108
- bool isTypedArray(jsi::Runtime& runtime, const jsi::Object& jsObj);
109
- TypedArrayBase getTypedArray(jsi::Runtime& runtime, const jsi::Object& jsObj);
110
-
111
- std::vector<uint8_t> arrayBufferToVector(jsi::Runtime& runtime, jsi::Object& jsObj);
112
- void arrayBufferUpdate(jsi::Runtime& runtime, jsi::ArrayBuffer& buffer, std::vector<uint8_t> data,
113
- size_t offset);
114
-
115
- template <TypedArrayKind T> class TypedArray : public TypedArrayBase {
116
- public:
117
- explicit TypedArray(TypedArrayBase&& base);
118
- TypedArray(jsi::Runtime& runtime, size_t size);
119
- TypedArray(jsi::Runtime& runtime, std::vector<ContentType<T>> data);
120
- TypedArray(TypedArray&&) = default;
121
- TypedArray& operator=(TypedArray&&) = default;
122
-
123
- std::vector<ContentType<T>> toVector(jsi::Runtime& runtime);
124
- void update(jsi::Runtime& runtime, const std::vector<ContentType<T>>& data);
125
- void updateUnsafe(jsi::Runtime& runtime, ContentType<T>* data, size_t length);
126
- uint8_t* data(jsi::Runtime& runtime);
127
- };
128
-
129
- template <TypedArrayKind T> TypedArray<T> TypedArrayBase::get(jsi::Runtime& runtime) const& {
130
- assert(getKind(runtime) == T);
131
- (void)runtime; // when assert is disabled we need to mark this as used
132
- return TypedArray<T>(jsi::Value(runtime, jsi::Value(runtime, *this).asObject(runtime)));
133
- }
134
-
135
- template <TypedArrayKind T> TypedArray<T> TypedArrayBase::get(jsi::Runtime& runtime) && {
136
- assert(getKind(runtime) == T);
137
- (void)runtime; // when assert is disabled we need to mark this as used
138
- return TypedArray<T>(std::move(*this));
139
- }
140
-
141
- template <TypedArrayKind T> TypedArray<T> TypedArrayBase::as(jsi::Runtime& runtime) const& {
142
- if (getKind(runtime) != T) {
143
- throw jsi::JSError(runtime, "Object is not a TypedArray");
144
- }
145
- return get<T>(runtime);
146
- }
147
-
148
- template <TypedArrayKind T> TypedArray<T> TypedArrayBase::as(jsi::Runtime& runtime) && {
149
- if (getKind(runtime) != T) {
150
- throw jsi::JSError(runtime, "Object is not a TypedArray");
151
- }
152
- return std::move(*this).get<T>(runtime);
153
- }
package/ios/JSIUtils.h DELETED
@@ -1,38 +0,0 @@
1
- #import <Foundation/Foundation.h>
2
- #import <React/RCTBridgeModule.h>
3
- #import <jsi/jsi.h>
4
-
5
- using namespace facebook;
6
-
7
- jsi::Value convertNSNumberToJSIBoolean(jsi::Runtime& runtime, NSNumber* value);
8
- jsi::Value convertNSNumberToJSINumber(jsi::Runtime& runtime, NSNumber* value);
9
- jsi::String convertNSStringToJSIString(jsi::Runtime& runtime, NSString* value);
10
- jsi::Value convertObjCObjectToJSIValue(jsi::Runtime& runtime, id value);
11
- ;
12
- jsi::Object convertNSDictionaryToJSIObject(jsi::Runtime& runtime, NSDictionary* value);
13
- jsi::Array convertNSArrayToJSIArray(jsi::Runtime& runtime, NSArray* value);
14
- std::vector<jsi::Value> convertNSArrayToStdVector(jsi::Runtime& runtime, NSArray* value);
15
- jsi::Value convertObjCObjectToJSIValue(jsi::Runtime& runtime, id value);
16
- id convertJSIValueToObjCObject(jsi::Runtime& runtime, const jsi::Value& value);
17
- NSString* convertJSIStringToNSString(jsi::Runtime& runtime, const jsi::String& value);
18
- NSArray* convertJSIArrayToNSArray(jsi::Runtime& runtime, const jsi::Array& value);
19
- NSDictionary* convertJSIObjectToNSDictionary(jsi::Runtime& runtime, const jsi::Object& value);
20
- RCTResponseSenderBlock convertJSIFunctionToCallback(jsi::Runtime& runtime,
21
- const jsi::Function& value);
22
- id convertJSIValueToObjCObject(jsi::Runtime& runtime, const jsi::Value& value);
23
- RCTResponseSenderBlock convertJSIFunctionToCallback(jsi::Runtime& runtime,
24
- const jsi::Function& value);
25
-
26
- struct Promise {
27
- Promise(jsi::Runtime& rt, jsi::Function resolve, jsi::Function reject);
28
-
29
- void resolve(const jsi::Value& result);
30
- void reject(const std::string& error);
31
-
32
- jsi::Runtime& runtime_;
33
- jsi::Function resolve_;
34
- jsi::Function reject_;
35
- };
36
-
37
- using PromiseSetupFunctionType = std::function<void(jsi::Runtime& rt, std::shared_ptr<Promise>)>;
38
- jsi::Value createPromiseAsJSIValue(jsi::Runtime& rt, const PromiseSetupFunctionType func);
package/ios/JSIUtils.mm DELETED
@@ -1,167 +0,0 @@
1
- //
2
- // REAJsiUtilities.cpp
3
- // RNReanimated
4
- //
5
- // Created by Christian Falch on 25/04/2019.
6
- // Copyright © 2019 Facebook. All rights reserved.
7
- //
8
-
9
- #include "JSIUtils.h"
10
- #import <React/RCTBridgeModule.h>
11
- #import <React/RCTConvert.h>
12
-
13
- /**
14
- * All helper functions are ObjC++ specific.
15
- */
16
- jsi::Value convertNSNumberToJSIBoolean(jsi::Runtime& runtime, NSNumber* value) {
17
- return jsi::Value((bool)[value boolValue]);
18
- }
19
-
20
- jsi::Value convertNSNumberToJSINumber(jsi::Runtime& runtime, NSNumber* value) {
21
- return jsi::Value([value doubleValue]);
22
- }
23
-
24
- jsi::String convertNSStringToJSIString(jsi::Runtime& runtime, NSString* value) {
25
- return jsi::String::createFromUtf8(runtime, [value UTF8String] ?: "");
26
- }
27
-
28
- jsi::Value convertObjCObjectToJSIValue(jsi::Runtime& runtime, id value);
29
-
30
- jsi::Object convertNSDictionaryToJSIObject(jsi::Runtime& runtime, NSDictionary* value) {
31
- jsi::Object result = jsi::Object(runtime);
32
- for (NSString* k in value) {
33
- result.setProperty(runtime, [k UTF8String], convertObjCObjectToJSIValue(runtime, value[k]));
34
- }
35
- return result;
36
- }
37
-
38
- jsi::Array convertNSArrayToJSIArray(jsi::Runtime& runtime, NSArray* value) {
39
- jsi::Array result = jsi::Array(runtime, value.count);
40
- for (size_t i = 0; i < value.count; i++) {
41
- result.setValueAtIndex(runtime, i, convertObjCObjectToJSIValue(runtime, value[i]));
42
- }
43
- return result;
44
- }
45
-
46
- std::vector<jsi::Value> convertNSArrayToStdVector(jsi::Runtime& runtime, NSArray* value) {
47
- std::vector<jsi::Value> result;
48
- for (size_t i = 0; i < value.count; i++) {
49
- result.emplace_back(convertObjCObjectToJSIValue(runtime, value[i]));
50
- }
51
- return result;
52
- }
53
-
54
- jsi::Value convertObjCObjectToJSIValue(jsi::Runtime& runtime, id value) {
55
- if ([value isKindOfClass:[NSString class]]) {
56
- return convertNSStringToJSIString(runtime, (NSString*)value);
57
- } else if ([value isKindOfClass:[NSNumber class]]) {
58
- if ([value isKindOfClass:[@YES class]]) {
59
- return convertNSNumberToJSIBoolean(runtime, (NSNumber*)value);
60
- }
61
- return convertNSNumberToJSINumber(runtime, (NSNumber*)value);
62
- } else if ([value isKindOfClass:[NSDictionary class]]) {
63
- return convertNSDictionaryToJSIObject(runtime, (NSDictionary*)value);
64
- } else if ([value isKindOfClass:[NSArray class]]) {
65
- return convertNSArrayToJSIArray(runtime, (NSArray*)value);
66
- } else if (value == (id)kCFNull) {
67
- return jsi::Value::null();
68
- }
69
- return jsi::Value::undefined();
70
- }
71
-
72
- id convertJSIValueToObjCObject(jsi::Runtime& runtime, const jsi::Value& value);
73
-
74
- NSString* convertJSIStringToNSString(jsi::Runtime& runtime, const jsi::String& value) {
75
- auto string = value.utf8(runtime);
76
- return [NSString stringWithUTF8String:string.c_str()];
77
- }
78
-
79
- NSArray* convertJSIArrayToNSArray(jsi::Runtime& runtime, const jsi::Array& value) {
80
- size_t size = value.size(runtime);
81
- NSMutableArray* result = [NSMutableArray new];
82
- for (size_t i = 0; i < size; i++) {
83
- // Insert kCFNull when it's `undefined` value to preserve the indices.
84
- [result addObject:convertJSIValueToObjCObject(runtime, value.getValueAtIndex(runtime, i))
85
- ?: (id)kCFNull];
86
- }
87
- return [result copy];
88
- }
89
-
90
- NSDictionary* convertJSIObjectToNSDictionary(jsi::Runtime& runtime, const jsi::Object& value) {
91
- jsi::Array propertyNames = value.getPropertyNames(runtime);
92
- size_t size = propertyNames.size(runtime);
93
- NSMutableDictionary* result = [NSMutableDictionary new];
94
- for (size_t i = 0; i < size; i++) {
95
- jsi::String name = propertyNames.getValueAtIndex(runtime, i).getString(runtime);
96
- NSString* k = convertJSIStringToNSString(runtime, name);
97
- id v = convertJSIValueToObjCObject(runtime, value.getProperty(runtime, name));
98
- if (v) {
99
- result[k] = v;
100
- }
101
- }
102
- return [result copy];
103
- }
104
-
105
- RCTResponseSenderBlock convertJSIFunctionToCallback(jsi::Runtime& runtime,
106
- const jsi::Function& value) {
107
- __block auto cb = value.getFunction(runtime);
108
-
109
- return ^(NSArray* responses) {
110
- cb.call(runtime, convertNSArrayToJSIArray(runtime, responses), 2);
111
- };
112
- }
113
-
114
- id convertJSIValueToObjCObject(jsi::Runtime& runtime, const jsi::Value& value) {
115
- if (value.isUndefined() || value.isNull()) {
116
- return nil;
117
- }
118
- if (value.isBool()) {
119
- return @(value.getBool());
120
- }
121
- if (value.isNumber()) {
122
- return @(value.getNumber());
123
- }
124
- if (value.isString()) {
125
- return convertJSIStringToNSString(runtime, value.getString(runtime));
126
- }
127
- if (value.isObject()) {
128
- jsi::Object o = value.getObject(runtime);
129
- if (o.isArray(runtime)) {
130
- return convertJSIArrayToNSArray(runtime, o.getArray(runtime));
131
- }
132
- if (o.isFunction(runtime)) {
133
- return convertJSIFunctionToCallback(runtime, std::move(o.getFunction(runtime)));
134
- }
135
- return convertJSIObjectToNSDictionary(runtime, o);
136
- }
137
-
138
- throw std::runtime_error("Unsupported jsi::jsi::Value kind");
139
- }
140
-
141
- Promise::Promise(jsi::Runtime& rt, jsi::Function resolve, jsi::Function reject)
142
- : runtime_(rt), resolve_(std::move(resolve)), reject_(std::move(reject)) {}
143
-
144
- void Promise::resolve(const jsi::Value& result) {
145
- resolve_.call(runtime_, result);
146
- }
147
-
148
- void Promise::reject(const std::string& message) {
149
- jsi::Object error(runtime_);
150
- error.setProperty(runtime_, "message", jsi::String::createFromUtf8(runtime_, message));
151
- reject_.call(runtime_, error);
152
- }
153
-
154
- jsi::Value createPromiseAsJSIValue(jsi::Runtime& rt, const PromiseSetupFunctionType func) {
155
- jsi::Function JSPromise = rt.global().getPropertyAsFunction(rt, "Promise");
156
- jsi::Function fn = jsi::Function::createFromHostFunction(
157
- rt, jsi::PropNameID::forAscii(rt, "fn"), 2,
158
- [func](jsi::Runtime& rt2, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
159
- jsi::Function resolve = args[0].getObject(rt2).getFunction(rt2);
160
- jsi::Function reject = args[1].getObject(rt2).getFunction(rt2);
161
- auto wrapper = std::make_shared<Promise>(rt2, std::move(resolve), std::move(reject));
162
- func(rt2, wrapper);
163
- return jsi::Value::undefined();
164
- });
165
-
166
- return JSPromise.callAsConstructor(rt, fn);
167
- }