react-native-mmkv-dz 2.5.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 (138) hide show
  1. package/LICENSE +219 -0
  2. package/MMKV/CHANGELOG.md +553 -0
  3. package/MMKV/Core/CMakeLists.txt +153 -0
  4. package/MMKV/Core/CodedInputData.cpp +228 -0
  5. package/MMKV/Core/CodedInputData.h +83 -0
  6. package/MMKV/Core/CodedInputDataCrypt.cpp +280 -0
  7. package/MMKV/Core/CodedInputDataCrypt.h +87 -0
  8. package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +62 -0
  9. package/MMKV/Core/CodedInputData_OSX.cpp +92 -0
  10. package/MMKV/Core/CodedOutputData.cpp +174 -0
  11. package/MMKV/Core/CodedOutputData.h +82 -0
  12. package/MMKV/Core/Core.xcodeproj/project.pbxproj +702 -0
  13. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  14. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  15. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +67 -0
  16. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +67 -0
  17. package/MMKV/Core/InterProcessLock.cpp +186 -0
  18. package/MMKV/Core/InterProcessLock.h +119 -0
  19. package/MMKV/Core/InterProcessLock_Android.cpp +103 -0
  20. package/MMKV/Core/InterProcessLock_Win32.cpp +108 -0
  21. package/MMKV/Core/KeyValueHolder.cpp +236 -0
  22. package/MMKV/Core/KeyValueHolder.h +118 -0
  23. package/MMKV/Core/MMBuffer.cpp +185 -0
  24. package/MMKV/Core/MMBuffer.h +107 -0
  25. package/MMKV/Core/MMKV.cpp +1418 -0
  26. package/MMKV/Core/MMKV.h +386 -0
  27. package/MMKV/Core/MMKVLog.cpp +127 -0
  28. package/MMKV/Core/MMKVLog.h +86 -0
  29. package/MMKV/Core/MMKVLog_Android.cpp +79 -0
  30. package/MMKV/Core/MMKVMetaInfo.hpp +81 -0
  31. package/MMKV/Core/MMKVPredef.h +245 -0
  32. package/MMKV/Core/MMKV_Android.cpp +259 -0
  33. package/MMKV/Core/MMKV_IO.cpp +1119 -0
  34. package/MMKV/Core/MMKV_IO.h +57 -0
  35. package/MMKV/Core/MMKV_OSX.cpp +347 -0
  36. package/MMKV/Core/MMKV_OSX.h +51 -0
  37. package/MMKV/Core/MemoryFile.cpp +537 -0
  38. package/MMKV/Core/MemoryFile.h +182 -0
  39. package/MMKV/Core/MemoryFile_Android.cpp +211 -0
  40. package/MMKV/Core/MemoryFile_Linux.cpp +120 -0
  41. package/MMKV/Core/MemoryFile_OSX.cpp +142 -0
  42. package/MMKV/Core/MemoryFile_Win32.cpp +536 -0
  43. package/MMKV/Core/MiniPBCoder.cpp +366 -0
  44. package/MMKV/Core/MiniPBCoder.h +129 -0
  45. package/MMKV/Core/MiniPBCoder_OSX.cpp +228 -0
  46. package/MMKV/Core/PBEncodeItem.hpp +86 -0
  47. package/MMKV/Core/PBUtility.cpp +61 -0
  48. package/MMKV/Core/PBUtility.h +153 -0
  49. package/MMKV/Core/ScopedLock.hpp +69 -0
  50. package/MMKV/Core/ThreadLock.cpp +68 -0
  51. package/MMKV/Core/ThreadLock.h +78 -0
  52. package/MMKV/Core/ThreadLock_Win32.cpp +89 -0
  53. package/MMKV/Core/aes/AESCrypt.cpp +256 -0
  54. package/MMKV/Core/aes/AESCrypt.h +107 -0
  55. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +1231 -0
  56. package/MMKV/Core/aes/openssl/openssl_aes.h +118 -0
  57. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +1044 -0
  58. package/MMKV/Core/aes/openssl/openssl_aes_locl.h +38 -0
  59. package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +308 -0
  60. package/MMKV/Core/aes/openssl/openssl_arm_arch.h +84 -0
  61. package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +97 -0
  62. package/MMKV/Core/aes/openssl/openssl_md32_common.h +254 -0
  63. package/MMKV/Core/aes/openssl/openssl_md5.h +49 -0
  64. package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +166 -0
  65. package/MMKV/Core/aes/openssl/openssl_md5_locl.h +75 -0
  66. package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +30 -0
  67. package/MMKV/Core/aes/openssl/openssl_opensslconf.h +271 -0
  68. package/MMKV/Core/core.vcxproj +186 -0
  69. package/MMKV/Core/core.vcxproj.filters +150 -0
  70. package/MMKV/Core/crc32/Checksum.h +67 -0
  71. package/MMKV/Core/crc32/crc32_armv8.cpp +132 -0
  72. package/MMKV/Core/crc32/zlib/crc32.cpp +55 -0
  73. package/MMKV/Core/crc32/zlib/crc32.h +48 -0
  74. package/MMKV/Core/crc32/zlib/zconf.h +380 -0
  75. package/MMKV/Core/crc32/zlib/zutil.h +25 -0
  76. package/MMKV/LICENSE.TXT +193 -0
  77. package/MMKV/README.md +288 -0
  78. package/README.md +221 -0
  79. package/android/CMakeLists.txt +71 -0
  80. package/android/build.gradle +371 -0
  81. package/android/gradle.properties +4 -0
  82. package/android/src/main/AndroidManifest.xml +4 -0
  83. package/android/src/main/cpp/MmkvHostObject.cpp +302 -0
  84. package/android/src/main/cpp/MmkvHostObject.h +26 -0
  85. package/android/src/main/cpp/cpp-adapter.cpp +65 -0
  86. package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +49 -0
  87. package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +26 -0
  88. package/cpp/TypedArray.cpp +341 -0
  89. package/cpp/TypedArray.h +175 -0
  90. package/ios/JSIUtils.h +50 -0
  91. package/ios/JSIUtils.mm +194 -0
  92. package/ios/Mmkv.xcodeproj/project.pbxproj +291 -0
  93. package/ios/MmkvHostObject.h +27 -0
  94. package/ios/MmkvHostObject.mm +299 -0
  95. package/ios/MmkvModule.h +5 -0
  96. package/ios/MmkvModule.mm +73 -0
  97. package/lib/commonjs/MMKV.js +146 -0
  98. package/lib/commonjs/MMKV.js.map +1 -0
  99. package/lib/commonjs/PlatformChecker.js +16 -0
  100. package/lib/commonjs/PlatformChecker.js.map +1 -0
  101. package/lib/commonjs/createMMKV.js +66 -0
  102. package/lib/commonjs/createMMKV.js.map +1 -0
  103. package/lib/commonjs/createMMKV.mock.js +40 -0
  104. package/lib/commonjs/createMMKV.mock.js.map +1 -0
  105. package/lib/commonjs/createMMKV.web.js +77 -0
  106. package/lib/commonjs/createMMKV.web.js.map +1 -0
  107. package/lib/commonjs/createTextEncoder.js +24 -0
  108. package/lib/commonjs/createTextEncoder.js.map +1 -0
  109. package/lib/commonjs/hooks.js +200 -0
  110. package/lib/commonjs/hooks.js.map +1 -0
  111. package/lib/commonjs/index.js +32 -0
  112. package/lib/commonjs/index.js.map +1 -0
  113. package/lib/module/MMKV.js +134 -0
  114. package/lib/module/MMKV.js.map +1 -0
  115. package/lib/module/PlatformChecker.js +9 -0
  116. package/lib/module/PlatformChecker.js.map +1 -0
  117. package/lib/module/createMMKV.js +55 -0
  118. package/lib/module/createMMKV.js.map +1 -0
  119. package/lib/module/createMMKV.mock.js +31 -0
  120. package/lib/module/createMMKV.mock.js.map +1 -0
  121. package/lib/module/createMMKV.web.js +67 -0
  122. package/lib/module/createMMKV.web.js.map +1 -0
  123. package/lib/module/createTextEncoder.js +17 -0
  124. package/lib/module/createTextEncoder.js.map +1 -0
  125. package/lib/module/hooks.js +181 -0
  126. package/lib/module/hooks.js.map +1 -0
  127. package/lib/module/index.js +3 -0
  128. package/lib/module/index.js.map +1 -0
  129. package/lib/typescript/MMKV.d.ts +137 -0
  130. package/lib/typescript/PlatformChecker.d.ts +1 -0
  131. package/lib/typescript/createMMKV.d.ts +6 -0
  132. package/lib/typescript/createMMKV.mock.d.ts +2 -0
  133. package/lib/typescript/createMMKV.web.d.ts +2 -0
  134. package/lib/typescript/createTextEncoder.d.ts +1 -0
  135. package/lib/typescript/hooks.d.ts +81 -0
  136. package/lib/typescript/index.d.ts +2 -0
  137. package/package.json +168 -0
  138. package/react-native-mmkv.podspec +32 -0
@@ -0,0 +1,341 @@
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 <unordered_map>
12
+
13
+ template <TypedArrayKind T>
14
+ using ContentType = typename typedArrayTypeMap<T>::type;
15
+
16
+ enum class Prop
17
+ {
18
+ Buffer, // "buffer"
19
+ Constructor, // "constructor"
20
+ Name, // "name"
21
+ Proto, // "__proto__"
22
+ Length, // "length"
23
+ ByteLength, // "byteLength"
24
+ ByteOffset, // "offset"
25
+ IsView, // "isView"
26
+ ArrayBuffer, // "ArrayBuffer"
27
+ Int8Array, // "Int8Array"
28
+ Int16Array, // "Int16Array"
29
+ Int32Array, // "Int32Array"
30
+ Uint8Array, // "Uint8Array"
31
+ Uint8ClampedArray, // "Uint8ClampedArray"
32
+ Uint16Array, // "Uint16Array"
33
+ Uint32Array, // "Uint32Array"
34
+ Float32Array, // "Float32Array"
35
+ Float64Array, // "Float64Array"
36
+ };
37
+
38
+ class PropNameIDCache
39
+ {
40
+ public:
41
+ const jsi::PropNameID &get(jsi::Runtime &runtime, Prop prop)
42
+ {
43
+ if (!this->props[prop])
44
+ {
45
+ this->props[prop] = std::make_unique<jsi::PropNameID>(createProp(runtime, prop));
46
+ }
47
+ return *(this->props[prop]);
48
+ }
49
+
50
+ const jsi::PropNameID &getConstructorNameProp(jsi::Runtime &runtime, TypedArrayKind kind);
51
+
52
+ void invalidate()
53
+ {
54
+ props.erase(props.begin(), props.end());
55
+ }
56
+
57
+ private:
58
+ std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>> props;
59
+
60
+ jsi::PropNameID createProp(jsi::Runtime &runtime, Prop prop);
61
+ };
62
+
63
+ PropNameIDCache propNameIDCache;
64
+
65
+ void invalidateJsiPropNameIDCache()
66
+ {
67
+ propNameIDCache.invalidate();
68
+ }
69
+
70
+ TypedArrayKind getTypedArrayKindForName(const std::string &name);
71
+
72
+ TypedArrayBase::TypedArrayBase(jsi::Runtime &runtime, size_t size, TypedArrayKind kind)
73
+ : TypedArrayBase(
74
+ runtime,
75
+ runtime.global()
76
+ .getProperty(runtime, propNameIDCache.getConstructorNameProp(runtime, kind))
77
+ .asObject(runtime)
78
+ .asFunction(runtime)
79
+ .callAsConstructor(runtime, {static_cast<double>(size)})
80
+ .asObject(runtime)) {}
81
+
82
+ TypedArrayBase::TypedArrayBase(jsi::Runtime &runtime, const jsi::Object &obj)
83
+ : jsi::Object(jsi::Value(runtime, obj).asObject(runtime)) {}
84
+
85
+ TypedArrayKind TypedArrayBase::getKind(jsi::Runtime &runtime) const
86
+ {
87
+ auto constructorName = this->getProperty(runtime, propNameIDCache.get(runtime, Prop::Constructor))
88
+ .asObject(runtime)
89
+ .getProperty(runtime, propNameIDCache.get(runtime, Prop::Name))
90
+ .asString(runtime)
91
+ .utf8(runtime);
92
+ return getTypedArrayKindForName(constructorName);
93
+ };
94
+
95
+ size_t TypedArrayBase::size(jsi::Runtime &runtime) const
96
+ {
97
+ return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
98
+ }
99
+
100
+ size_t TypedArrayBase::length(jsi::Runtime &runtime) const
101
+ {
102
+ return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
103
+ }
104
+
105
+ size_t TypedArrayBase::byteLength(jsi::Runtime &runtime) const
106
+ {
107
+ return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteLength)).asNumber();
108
+ }
109
+
110
+ size_t TypedArrayBase::byteOffset(jsi::Runtime &runtime) const
111
+ {
112
+ return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteOffset)).asNumber();
113
+ }
114
+
115
+ bool TypedArrayBase::hasBuffer(jsi::Runtime &runtime) const
116
+ {
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
+ {
123
+ auto start =
124
+ reinterpret_cast<uint8_t *>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
125
+ auto end = start + byteLength(runtime);
126
+ return std::vector<uint8_t>(start, end);
127
+ }
128
+
129
+ jsi::ArrayBuffer TypedArrayBase::getBuffer(jsi::Runtime &runtime) const
130
+ {
131
+ auto buffer = getProperty(runtime, propNameIDCache.get(runtime, Prop::Buffer));
132
+ if (buffer.isObject() && buffer.asObject(runtime).isArrayBuffer(runtime))
133
+ {
134
+ return buffer.asObject(runtime).getArrayBuffer(runtime);
135
+ }
136
+ else
137
+ {
138
+ throw std::runtime_error("no ArrayBuffer attached");
139
+ }
140
+ }
141
+
142
+ bool isTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj)
143
+ {
144
+ auto jsVal = runtime.global()
145
+ .getProperty(runtime, propNameIDCache.get(runtime, Prop::ArrayBuffer))
146
+ .asObject(runtime)
147
+ .getProperty(runtime, propNameIDCache.get(runtime, Prop::IsView))
148
+ .asObject(runtime)
149
+ .asFunction(runtime)
150
+ .callWithThis(runtime, runtime.global(), {jsi::Value(runtime, jsObj)});
151
+ if (jsVal.isBool())
152
+ {
153
+ return jsVal.getBool();
154
+ }
155
+ else
156
+ {
157
+ throw std::runtime_error("value is not a boolean");
158
+ }
159
+ }
160
+
161
+ TypedArrayBase getTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj)
162
+ {
163
+ auto jsVal = runtime.global()
164
+ .getProperty(runtime, propNameIDCache.get(runtime, Prop::ArrayBuffer))
165
+ .asObject(runtime)
166
+ .getProperty(runtime, propNameIDCache.get(runtime, Prop::IsView))
167
+ .asObject(runtime)
168
+ .asFunction(runtime)
169
+ .callWithThis(runtime, runtime.global(), {jsi::Value(runtime, jsObj)});
170
+ if (jsVal.isBool())
171
+ {
172
+ return TypedArrayBase(runtime, jsObj);
173
+ }
174
+ else
175
+ {
176
+ throw std::runtime_error("value is not a boolean");
177
+ }
178
+ }
179
+
180
+ std::vector<uint8_t> arrayBufferToVector(jsi::Runtime &runtime, jsi::Object &jsObj)
181
+ {
182
+ if (!jsObj.isArrayBuffer(runtime))
183
+ {
184
+ throw std::runtime_error("Object is not an ArrayBuffer");
185
+ }
186
+ auto jsArrayBuffer = jsObj.getArrayBuffer(runtime);
187
+
188
+ uint8_t *dataBlock = jsArrayBuffer.data(runtime);
189
+ size_t blockSize =
190
+ jsArrayBuffer.getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteLength)).asNumber();
191
+ return std::vector<uint8_t>(dataBlock, dataBlock + blockSize);
192
+ }
193
+
194
+ void arrayBufferUpdate(
195
+ jsi::Runtime &runtime,
196
+ jsi::ArrayBuffer &buffer,
197
+ std::vector<uint8_t> data,
198
+ size_t offset)
199
+ {
200
+ uint8_t *dataBlock = buffer.data(runtime);
201
+ size_t blockSize = buffer.size(runtime);
202
+ if (data.size() > blockSize)
203
+ {
204
+ throw jsi::JSError(runtime, "ArrayBuffer is to small to fit data");
205
+ }
206
+ std::copy(data.begin(), data.end(), dataBlock + offset);
207
+ }
208
+
209
+ template <TypedArrayKind T>
210
+ TypedArray<T>::TypedArray(jsi::Runtime &runtime, size_t size) : TypedArrayBase(runtime, size, T){};
211
+
212
+ template <TypedArrayKind T>
213
+ TypedArray<T>::TypedArray(jsi::Runtime &runtime, std::vector<ContentType<T>> data)
214
+ : TypedArrayBase(runtime, data.size(), T)
215
+ {
216
+ update(runtime, data);
217
+ };
218
+
219
+ template <TypedArrayKind T>
220
+ TypedArray<T>::TypedArray(TypedArrayBase &&base) : TypedArrayBase(std::move(base)) {}
221
+
222
+ template <TypedArrayKind T>
223
+ std::vector<ContentType<T>> TypedArray<T>::toVector(jsi::Runtime &runtime)
224
+ {
225
+ auto start =
226
+ reinterpret_cast<ContentType<T> *>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
227
+ auto end = start + size(runtime);
228
+ return std::vector<ContentType<T>>(start, end);
229
+ }
230
+
231
+ template <TypedArrayKind T>
232
+ void TypedArray<T>::update(jsi::Runtime &runtime, const std::vector<ContentType<T>> &data)
233
+ {
234
+ if (data.size() != size(runtime))
235
+ {
236
+ throw jsi::JSError(runtime, "TypedArray can only be updated with a vector of the same size");
237
+ }
238
+ uint8_t *rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
239
+ std::copy(data.begin(), data.end(), reinterpret_cast<ContentType<T> *>(rawData));
240
+ }
241
+
242
+ const jsi::PropNameID &PropNameIDCache::getConstructorNameProp(
243
+ jsi::Runtime &runtime,
244
+ TypedArrayKind kind)
245
+ {
246
+ switch (kind)
247
+ {
248
+ case TypedArrayKind::Int8Array:
249
+ return get(runtime, Prop::Int8Array);
250
+ case TypedArrayKind::Int16Array:
251
+ return get(runtime, Prop::Int16Array);
252
+ case TypedArrayKind::Int32Array:
253
+ return get(runtime, Prop::Int32Array);
254
+ case TypedArrayKind::Uint8Array:
255
+ return get(runtime, Prop::Uint8Array);
256
+ case TypedArrayKind::Uint8ClampedArray:
257
+ return get(runtime, Prop::Uint8ClampedArray);
258
+ case TypedArrayKind::Uint16Array:
259
+ return get(runtime, Prop::Uint16Array);
260
+ case TypedArrayKind::Uint32Array:
261
+ return get(runtime, Prop::Uint32Array);
262
+ case TypedArrayKind::Float32Array:
263
+ return get(runtime, Prop::Float32Array);
264
+ case TypedArrayKind::Float64Array:
265
+ return get(runtime, Prop::Float64Array);
266
+ }
267
+ }
268
+
269
+ jsi::PropNameID PropNameIDCache::createProp(jsi::Runtime &runtime, Prop prop)
270
+ {
271
+ auto create = [&](const std::string &propName)
272
+ {
273
+ return jsi::PropNameID::forUtf8(runtime, propName);
274
+ };
275
+ switch (prop)
276
+ {
277
+ case Prop::Buffer:
278
+ return create("buffer");
279
+ case Prop::Constructor:
280
+ return create("constructor");
281
+ case Prop::Name:
282
+ return create("name");
283
+ case Prop::Proto:
284
+ return create("__proto__");
285
+ case Prop::Length:
286
+ return create("length");
287
+ case Prop::ByteLength:
288
+ return create("byteLength");
289
+ case Prop::ByteOffset:
290
+ return create("byteOffset");
291
+ case Prop::IsView:
292
+ return create("isView");
293
+ case Prop::ArrayBuffer:
294
+ return create("ArrayBuffer");
295
+ case Prop::Int8Array:
296
+ return create("Int8Array");
297
+ case Prop::Int16Array:
298
+ return create("Int16Array");
299
+ case Prop::Int32Array:
300
+ return create("Int32Array");
301
+ case Prop::Uint8Array:
302
+ return create("Uint8Array");
303
+ case Prop::Uint8ClampedArray:
304
+ return create("Uint8ClampedArray");
305
+ case Prop::Uint16Array:
306
+ return create("Uint16Array");
307
+ case Prop::Uint32Array:
308
+ return create("Uint32Array");
309
+ case Prop::Float32Array:
310
+ return create("Float32Array");
311
+ case Prop::Float64Array:
312
+ return create("Float64Array");
313
+ }
314
+ }
315
+
316
+ std::unordered_map<std::string, TypedArrayKind> nameToKindMap = {
317
+ {"Int8Array", TypedArrayKind::Int8Array},
318
+ {"Int16Array", TypedArrayKind::Int16Array},
319
+ {"Int32Array", TypedArrayKind::Int32Array},
320
+ {"Uint8Array", TypedArrayKind::Uint8Array},
321
+ {"Uint8ClampedArray", TypedArrayKind::Uint8ClampedArray},
322
+ {"Uint16Array", TypedArrayKind::Uint16Array},
323
+ {"Uint32Array", TypedArrayKind::Uint32Array},
324
+ {"Float32Array", TypedArrayKind::Float32Array},
325
+ {"Float64Array", TypedArrayKind::Float64Array},
326
+ };
327
+
328
+ TypedArrayKind getTypedArrayKindForName(const std::string &name)
329
+ {
330
+ return nameToKindMap.at(name);
331
+ }
332
+
333
+ template class TypedArray<TypedArrayKind::Int8Array>;
334
+ template class TypedArray<TypedArrayKind::Int16Array>;
335
+ template class TypedArray<TypedArrayKind::Int32Array>;
336
+ template class TypedArray<TypedArrayKind::Uint8Array>;
337
+ template class TypedArray<TypedArrayKind::Uint8ClampedArray>;
338
+ template class TypedArray<TypedArrayKind::Uint16Array>;
339
+ template class TypedArray<TypedArrayKind::Uint32Array>;
340
+ template class TypedArray<TypedArrayKind::Float32Array>;
341
+ template class TypedArray<TypedArrayKind::Float64Array>;
@@ -0,0 +1,175 @@
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
+
13
+ namespace jsi = facebook::jsi;
14
+
15
+ enum class TypedArrayKind
16
+ {
17
+ Int8Array,
18
+ Int16Array,
19
+ Int32Array,
20
+ Uint8Array,
21
+ Uint8ClampedArray,
22
+ Uint16Array,
23
+ Uint32Array,
24
+ Float32Array,
25
+ Float64Array,
26
+ };
27
+
28
+ template <TypedArrayKind T>
29
+ class TypedArray;
30
+
31
+ template <TypedArrayKind T>
32
+ struct typedArrayTypeMap;
33
+ template <>
34
+ struct typedArrayTypeMap<TypedArrayKind::Int8Array>
35
+ {
36
+ typedef int8_t type;
37
+ };
38
+ template <>
39
+ struct typedArrayTypeMap<TypedArrayKind::Int16Array>
40
+ {
41
+ typedef int16_t type;
42
+ };
43
+ template <>
44
+ struct typedArrayTypeMap<TypedArrayKind::Int32Array>
45
+ {
46
+ typedef int32_t type;
47
+ };
48
+ template <>
49
+ struct typedArrayTypeMap<TypedArrayKind::Uint8Array>
50
+ {
51
+ typedef uint8_t type;
52
+ };
53
+ template <>
54
+ struct typedArrayTypeMap<TypedArrayKind::Uint8ClampedArray>
55
+ {
56
+ typedef uint8_t type;
57
+ };
58
+ template <>
59
+ struct typedArrayTypeMap<TypedArrayKind::Uint16Array>
60
+ {
61
+ typedef uint16_t type;
62
+ };
63
+ template <>
64
+ struct typedArrayTypeMap<TypedArrayKind::Uint32Array>
65
+ {
66
+ typedef uint32_t type;
67
+ };
68
+ template <>
69
+ struct typedArrayTypeMap<TypedArrayKind::Float32Array>
70
+ {
71
+ typedef float type;
72
+ };
73
+ template <>
74
+ struct typedArrayTypeMap<TypedArrayKind::Float64Array>
75
+ {
76
+ typedef double type;
77
+ };
78
+
79
+ void invalidateJsiPropNameIDCache();
80
+
81
+ class TypedArrayBase : public jsi::Object
82
+ {
83
+ public:
84
+ template <TypedArrayKind T>
85
+ using ContentType = typename typedArrayTypeMap<T>::type;
86
+
87
+ TypedArrayBase(jsi::Runtime &, size_t, TypedArrayKind);
88
+ TypedArrayBase(jsi::Runtime &, const jsi::Object &);
89
+ TypedArrayBase(TypedArrayBase &&) = default;
90
+ TypedArrayBase &operator=(TypedArrayBase &&) = default;
91
+
92
+ TypedArrayKind getKind(jsi::Runtime &runtime) const;
93
+
94
+ template <TypedArrayKind T>
95
+ TypedArray<T> get(jsi::Runtime &runtime) const &;
96
+ template <TypedArrayKind T>
97
+ TypedArray<T> get(jsi::Runtime &runtime) &&;
98
+ template <TypedArrayKind T>
99
+ TypedArray<T> as(jsi::Runtime &runtime) const &;
100
+ template <TypedArrayKind T>
101
+ TypedArray<T> as(jsi::Runtime &runtime) &&;
102
+
103
+ size_t size(jsi::Runtime &runtime) const;
104
+ size_t length(jsi::Runtime &runtime) const;
105
+ size_t byteLength(jsi::Runtime &runtime) const;
106
+ size_t byteOffset(jsi::Runtime &runtime) const;
107
+ bool hasBuffer(jsi::Runtime &runtime) const;
108
+
109
+ std::vector<uint8_t> toVector(jsi::Runtime &runtime);
110
+ jsi::ArrayBuffer getBuffer(jsi::Runtime &runtime) const;
111
+
112
+ private:
113
+ template <TypedArrayKind>
114
+ friend class TypedArray;
115
+ };
116
+
117
+ bool isTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj);
118
+ TypedArrayBase getTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj);
119
+
120
+ std::vector<uint8_t> arrayBufferToVector(jsi::Runtime &runtime, jsi::Object &jsObj);
121
+ void arrayBufferUpdate(
122
+ jsi::Runtime &runtime,
123
+ jsi::ArrayBuffer &buffer,
124
+ std::vector<uint8_t> data,
125
+ size_t offset);
126
+
127
+ template <TypedArrayKind T>
128
+ class TypedArray : public TypedArrayBase
129
+ {
130
+ public:
131
+ TypedArray(jsi::Runtime &runtime, size_t size);
132
+ TypedArray(jsi::Runtime &runtime, std::vector<ContentType<T>> data);
133
+ TypedArray(TypedArrayBase &&base);
134
+ TypedArray(TypedArray &&) = default;
135
+ TypedArray &operator=(TypedArray &&) = default;
136
+
137
+ std::vector<ContentType<T>> toVector(jsi::Runtime &runtime);
138
+ void update(jsi::Runtime &runtime, const std::vector<ContentType<T>> &data);
139
+ };
140
+
141
+ template <TypedArrayKind T>
142
+ TypedArray<T> TypedArrayBase::get(jsi::Runtime &runtime) const &
143
+ {
144
+ assert(getKind(runtime) == T);
145
+ (void)runtime; // when assert is disabled we need to mark this as used
146
+ return TypedArray<T>(jsi::Value(runtime, jsi::Value(runtime, *this).asObject(runtime)));
147
+ }
148
+
149
+ template <TypedArrayKind T>
150
+ TypedArray<T> TypedArrayBase::get(jsi::Runtime &runtime) &&
151
+ {
152
+ assert(getKind(runtime) == T);
153
+ (void)runtime; // when assert is disabled we need to mark this as used
154
+ return TypedArray<T>(std::move(*this));
155
+ }
156
+
157
+ template <TypedArrayKind T>
158
+ TypedArray<T> TypedArrayBase::as(jsi::Runtime &runtime) const &
159
+ {
160
+ if (getKind(runtime) != T)
161
+ {
162
+ throw jsi::JSError(runtime, "Object is not a TypedArray");
163
+ }
164
+ return get<T>(runtime);
165
+ }
166
+
167
+ template <TypedArrayKind T>
168
+ TypedArray<T> TypedArrayBase::as(jsi::Runtime &runtime) &&
169
+ {
170
+ if (getKind(runtime) != T)
171
+ {
172
+ throw jsi::JSError(runtime, "Object is not a TypedArray");
173
+ }
174
+ return std::move(*this).get<T>(runtime);
175
+ }
package/ios/JSIUtils.h ADDED
@@ -0,0 +1,50 @@
1
+ #import <Foundation/Foundation.h>
2
+ #import <jsi/jsi.h>
3
+ #import <React/RCTBridgeModule.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
+ jsi::Object convertNSDictionaryToJSIObject(jsi::Runtime &runtime, NSDictionary *value);
12
+ jsi::Array convertNSArrayToJSIArray(jsi::Runtime &runtime, NSArray *value);
13
+ std::vector<jsi::Value> convertNSArrayToStdVector(jsi::Runtime &runtime, NSArray *value);
14
+ jsi::Value convertObjCObjectToJSIValue(jsi::Runtime &runtime, id value);
15
+ id convertJSIValueToObjCObject(
16
+ jsi::Runtime &runtime,
17
+ const jsi::Value &value);
18
+ NSString* convertJSIStringToNSString(jsi::Runtime &runtime, const jsi::String &value);
19
+ NSArray* convertJSIArrayToNSArray(
20
+ jsi::Runtime &runtime,
21
+ const jsi::Array &value);
22
+ NSDictionary *convertJSIObjectToNSDictionary(
23
+ jsi::Runtime &runtime,
24
+ const jsi::Object &value);
25
+ RCTResponseSenderBlock convertJSIFunctionToCallback(
26
+ jsi::Runtime &runtime,
27
+ const jsi::Function &value);
28
+ id convertJSIValueToObjCObject(
29
+ jsi::Runtime &runtime,
30
+ const jsi::Value &value);
31
+ RCTResponseSenderBlock convertJSIFunctionToCallback(
32
+ jsi::Runtime &runtime,
33
+ const jsi::Function &value);
34
+
35
+ struct Promise {
36
+ Promise(jsi::Runtime &rt, jsi::Function resolve, jsi::Function reject);
37
+
38
+ void resolve(const jsi::Value &result);
39
+ void reject(const std::string &error);
40
+
41
+ jsi::Runtime &runtime_;
42
+ jsi::Function resolve_;
43
+ jsi::Function reject_;
44
+ };
45
+
46
+ using PromiseSetupFunctionType =
47
+ std::function<void(jsi::Runtime &rt, std::shared_ptr<Promise>)>;
48
+ jsi::Value createPromiseAsJSIValue(
49
+ jsi::Runtime &rt,
50
+ const PromiseSetupFunctionType func);