react-native-mmkv 2.7.0 → 2.8.0
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/README.md +2 -3
- package/android/build.gradle +1 -0
- package/android/src/main/cpp/cpp-adapter.cpp +5 -0
- package/cpp/TypedArray.cpp +187 -199
- package/cpp/TypedArray.h +112 -109
- package/ios/JSIUtils.mm +2 -1
- package/ios/MmkvModule.mm +12 -7
- package/lib/commonjs/MMKV.js +0 -10
- package/lib/commonjs/MMKV.js.map +1 -1
- package/lib/commonjs/createMMKV.web.js +32 -12
- package/lib/commonjs/createMMKV.web.js.map +1 -1
- package/lib/commonjs/hooks.js +7 -0
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/module/MMKV.js +0 -10
- package/lib/module/MMKV.js.map +1 -1
- package/lib/module/createMMKV.web.js +32 -12
- package/lib/module/createMMKV.web.js.map +1 -1
- package/lib/module/hooks.js +7 -0
- package/lib/module/hooks.js.map +1 -1
- package/lib/typescript/MMKV.d.ts +0 -12
- package/lib/typescript/MMKV.d.ts.map +1 -1
- package/lib/typescript/createMMKV.web.d.ts.map +1 -1
- package/lib/typescript/hooks.d.ts.map +1 -1
- package/package.json +13 -9
- package/MMKV/LICENSE.TXT +0 -193
- package/MMKV/README.md +0 -291
package/README.md
CHANGED
|
@@ -104,8 +104,7 @@ import { MMKV } from 'react-native-mmkv'
|
|
|
104
104
|
export const storage = new MMKV({
|
|
105
105
|
id: `user-${userId}-storage`,
|
|
106
106
|
path: `${USER_DIRECTORY}/storage`,
|
|
107
|
-
encryptionKey: 'hunter2'
|
|
108
|
-
fastWrites: true
|
|
107
|
+
encryptionKey: 'hunter2'
|
|
109
108
|
})
|
|
110
109
|
```
|
|
111
110
|
|
|
@@ -116,7 +115,6 @@ The following values can be configured:
|
|
|
116
115
|
* `id`: The MMKV instance's ID. If you want to use multiple instances, use different IDs. For example, you can separate the global app's storage and a logged-in user's storage. (required if `path` or `encryptionKey` fields are specified, otherwise defaults to: `'mmkv.default'`)
|
|
117
116
|
* `path`: The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization (documentation: [iOS](https://github.com/Tencent/MMKV/wiki/iOS_advance#customize-location) / [Android](https://github.com/Tencent/MMKV/wiki/android_advance#customize-location))
|
|
118
117
|
* `encryptionKey`: The MMKV instance's encryption/decryption key. By default, MMKV stores all key-values in plain text on file, relying on iOS's/Android's sandbox to make sure the file is encrypted. Should you worry about information leaking, you can choose to encrypt MMKV. (documentation: [iOS](https://github.com/Tencent/MMKV/wiki/iOS_advance#encryption) / [Android](https://github.com/Tencent/MMKV/wiki/android_advance#encryption))
|
|
119
|
-
* `fastWrites`: Configure fast writes for MMKV. When set to `true`, all calls to `set(..)` do not overwrite the previous value to avoid storage resizing, this speeds up write speed. Only enable this if you use a small MMKV storage, as this uses more RAM.
|
|
120
118
|
|
|
121
119
|
### Set
|
|
122
120
|
|
|
@@ -198,6 +196,7 @@ A mocked MMKV instance is automatically used when testing with Jest, so you will
|
|
|
198
196
|
* [Using MMKV with mobx-persist](./docs/WRAPPER_MOBXPERSIST.md)
|
|
199
197
|
* [Using MMKV with zustand persist-middleware](./docs/WRAPPER_ZUSTAND_PERSIST_MIDDLEWARE.md)
|
|
200
198
|
* [Using MMKV with jotai](./docs/WRAPPER_JOTAI.md)
|
|
199
|
+
* [Using MMKV with react-query](./docs/WRAPPER_REACT_QUERY.md)
|
|
201
200
|
* [How is this library different from **react-native-mmkv-storage**?](https://github.com/mrousavy/react-native-mmkv/issues/100#issuecomment-886477361)
|
|
202
201
|
|
|
203
202
|
## Limitations
|
package/android/build.gradle
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
#include <jsi/jsi.h>
|
|
3
3
|
#include <MMKV.h>
|
|
4
4
|
#include "MmkvHostObject.h"
|
|
5
|
+
#include "TypedArray.h"
|
|
5
6
|
|
|
6
7
|
using namespace facebook;
|
|
7
8
|
|
|
@@ -32,6 +33,10 @@ void install(jsi::Runtime& jsiRuntime) {
|
|
|
32
33
|
return jsi::Object::createFromHostObject(runtime, instance);
|
|
33
34
|
});
|
|
34
35
|
jsiRuntime.global().setProperty(jsiRuntime, "mmkvCreateNewInstance", std::move(mmkvCreateNewInstance));
|
|
36
|
+
|
|
37
|
+
// Adds the PropNameIDCache object to the Runtime. If the Runtime gets destroyed, the Object gets destroyed and the cache gets invalidated.
|
|
38
|
+
auto propNameIdCache = std::make_shared<InvalidateCacheOnDestroy>(jsiRuntime);
|
|
39
|
+
jsiRuntime.global().setProperty(jsiRuntime, "mmkvArrayBufferPropNameIdCache", jsi::Object::createFromHostObject(jsiRuntime, propNameIdCache));
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
std::string jstringToStdString(JNIEnv *env, jstring jStr) {
|
package/cpp/TypedArray.cpp
CHANGED
|
@@ -9,62 +9,70 @@
|
|
|
9
9
|
#include "TypedArray.h"
|
|
10
10
|
|
|
11
11
|
#include <unordered_map>
|
|
12
|
+
#include <memory>
|
|
13
|
+
#include <utility>
|
|
14
|
+
#include <vector>
|
|
15
|
+
#include <algorithm>
|
|
16
|
+
#include <string>
|
|
12
17
|
|
|
13
18
|
template <TypedArrayKind T>
|
|
14
19
|
using ContentType = typename typedArrayTypeMap<T>::type;
|
|
15
20
|
|
|
16
|
-
enum class Prop
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
Float64Array, // "Float64Array"
|
|
21
|
+
enum class Prop {
|
|
22
|
+
Buffer, // "buffer"
|
|
23
|
+
Constructor, // "constructor"
|
|
24
|
+
Name, // "name"
|
|
25
|
+
Proto, // "__proto__"
|
|
26
|
+
Length, // "length"
|
|
27
|
+
ByteLength, // "byteLength"
|
|
28
|
+
ByteOffset, // "offset"
|
|
29
|
+
IsView, // "isView"
|
|
30
|
+
ArrayBuffer, // "ArrayBuffer"
|
|
31
|
+
Int8Array, // "Int8Array"
|
|
32
|
+
Int16Array, // "Int16Array"
|
|
33
|
+
Int32Array, // "Int32Array"
|
|
34
|
+
Uint8Array, // "Uint8Array"
|
|
35
|
+
Uint8ClampedArray, // "Uint8ClampedArray"
|
|
36
|
+
Uint16Array, // "Uint16Array"
|
|
37
|
+
Uint32Array, // "Uint32Array"
|
|
38
|
+
Float32Array, // "Float32Array"
|
|
39
|
+
Float64Array, // "Float64Array"
|
|
36
40
|
};
|
|
37
41
|
|
|
38
|
-
class PropNameIDCache
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
{
|
|
43
|
-
|
|
44
|
-
{
|
|
45
|
-
this->props[prop] = std::make_unique<jsi::PropNameID>(createProp(runtime, prop));
|
|
46
|
-
}
|
|
47
|
-
return *(this->props[prop]);
|
|
42
|
+
class PropNameIDCache {
|
|
43
|
+
public:
|
|
44
|
+
const jsi::PropNameID &get(jsi::Runtime &runtime, Prop prop) {
|
|
45
|
+
auto key = reinterpret_cast<uintptr_t>(&runtime);
|
|
46
|
+
if (this->props.find(key) == this->props.end()) {
|
|
47
|
+
this->props[key] = std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>>();
|
|
48
48
|
}
|
|
49
|
+
if (!this->props[key][prop]) {
|
|
50
|
+
this->props[key][prop] = std::make_unique<jsi::PropNameID>(createProp(runtime, prop));
|
|
51
|
+
}
|
|
52
|
+
return *(this->props[key][prop]);
|
|
53
|
+
}
|
|
49
54
|
|
|
50
|
-
|
|
55
|
+
const jsi::PropNameID &getConstructorNameProp(jsi::Runtime &runtime, TypedArrayKind kind);
|
|
51
56
|
|
|
52
|
-
|
|
53
|
-
{
|
|
54
|
-
|
|
57
|
+
void invalidate(uintptr_t key) {
|
|
58
|
+
if (props.find(key) != props.end()) {
|
|
59
|
+
props[key].clear();
|
|
55
60
|
}
|
|
61
|
+
}
|
|
56
62
|
|
|
57
|
-
private:
|
|
58
|
-
|
|
63
|
+
private:
|
|
64
|
+
std::unordered_map<uintptr_t, std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>>> props;
|
|
59
65
|
|
|
60
|
-
|
|
66
|
+
jsi::PropNameID createProp(jsi::Runtime &runtime, Prop prop);
|
|
61
67
|
};
|
|
62
68
|
|
|
63
69
|
PropNameIDCache propNameIDCache;
|
|
64
70
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
71
|
+
InvalidateCacheOnDestroy::InvalidateCacheOnDestroy(jsi::Runtime &runtime) {
|
|
72
|
+
key = reinterpret_cast<uintptr_t>(&runtime);
|
|
73
|
+
}
|
|
74
|
+
InvalidateCacheOnDestroy::~InvalidateCacheOnDestroy() {
|
|
75
|
+
propNameIDCache.invalidate(key);
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
TypedArrayKind getTypedArrayKindForName(const std::string &name);
|
|
@@ -82,235 +90,216 @@ TypedArrayBase::TypedArrayBase(jsi::Runtime &runtime, size_t size, TypedArrayKin
|
|
|
82
90
|
TypedArrayBase::TypedArrayBase(jsi::Runtime &runtime, const jsi::Object &obj)
|
|
83
91
|
: jsi::Object(jsi::Value(runtime, obj).asObject(runtime)) {}
|
|
84
92
|
|
|
85
|
-
TypedArrayKind TypedArrayBase::getKind(jsi::Runtime &runtime) const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
};
|
|
93
|
+
TypedArrayKind TypedArrayBase::getKind(jsi::Runtime &runtime) const {
|
|
94
|
+
auto constructorName = this->getProperty(runtime, propNameIDCache.get(runtime, Prop::Constructor))
|
|
95
|
+
.asObject(runtime)
|
|
96
|
+
.getProperty(runtime, propNameIDCache.get(runtime, Prop::Name))
|
|
97
|
+
.asString(runtime)
|
|
98
|
+
.utf8(runtime);
|
|
99
|
+
return getTypedArrayKindForName(constructorName);
|
|
100
|
+
}
|
|
94
101
|
|
|
95
|
-
size_t TypedArrayBase::size(jsi::Runtime &runtime) const
|
|
96
|
-
|
|
97
|
-
return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
|
|
102
|
+
size_t TypedArrayBase::size(jsi::Runtime &runtime) const {
|
|
103
|
+
return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
|
|
98
104
|
}
|
|
99
105
|
|
|
100
|
-
size_t TypedArrayBase::length(jsi::Runtime &runtime) const
|
|
101
|
-
|
|
102
|
-
return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
|
|
106
|
+
size_t TypedArrayBase::length(jsi::Runtime &runtime) const {
|
|
107
|
+
return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
|
|
103
108
|
}
|
|
104
109
|
|
|
105
|
-
size_t TypedArrayBase::byteLength(jsi::Runtime &runtime) const
|
|
106
|
-
|
|
107
|
-
return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteLength)).asNumber();
|
|
110
|
+
size_t TypedArrayBase::byteLength(jsi::Runtime &runtime) const {
|
|
111
|
+
return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteLength)).asNumber();
|
|
108
112
|
}
|
|
109
113
|
|
|
110
|
-
size_t TypedArrayBase::byteOffset(jsi::Runtime &runtime) const
|
|
111
|
-
|
|
112
|
-
return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteOffset)).asNumber();
|
|
114
|
+
size_t TypedArrayBase::byteOffset(jsi::Runtime &runtime) const {
|
|
115
|
+
return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteOffset)).asNumber();
|
|
113
116
|
}
|
|
114
117
|
|
|
115
|
-
bool TypedArrayBase::hasBuffer(jsi::Runtime &runtime) const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return buffer.isObject() && buffer.asObject(runtime).isArrayBuffer(runtime);
|
|
118
|
+
bool TypedArrayBase::hasBuffer(jsi::Runtime &runtime) const {
|
|
119
|
+
auto buffer = getProperty(runtime, propNameIDCache.get(runtime, Prop::Buffer));
|
|
120
|
+
return buffer.isObject() && buffer.asObject(runtime).isArrayBuffer(runtime);
|
|
119
121
|
}
|
|
120
122
|
|
|
121
|
-
std::vector<uint8_t> TypedArrayBase::toVector(jsi::Runtime &runtime)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
auto end = start + byteLength(runtime);
|
|
126
|
-
return std::vector<uint8_t>(start, end);
|
|
123
|
+
std::vector<uint8_t> TypedArrayBase::toVector(jsi::Runtime &runtime) {
|
|
124
|
+
auto start = 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
127
|
}
|
|
128
128
|
|
|
129
|
-
jsi::ArrayBuffer TypedArrayBase::getBuffer(jsi::Runtime &runtime) const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
else
|
|
137
|
-
{
|
|
138
|
-
throw std::runtime_error("no ArrayBuffer attached");
|
|
139
|
-
}
|
|
129
|
+
jsi::ArrayBuffer TypedArrayBase::getBuffer(jsi::Runtime &runtime) const {
|
|
130
|
+
auto buffer = getProperty(runtime, propNameIDCache.get(runtime, Prop::Buffer));
|
|
131
|
+
if (buffer.isObject() && buffer.asObject(runtime).isArrayBuffer(runtime)) {
|
|
132
|
+
return buffer.asObject(runtime).getArrayBuffer(runtime);
|
|
133
|
+
} else {
|
|
134
|
+
throw std::runtime_error("no ArrayBuffer attached");
|
|
135
|
+
}
|
|
140
136
|
}
|
|
141
137
|
|
|
142
|
-
bool isTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj)
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
else
|
|
156
|
-
{
|
|
157
|
-
throw std::runtime_error("value is not a boolean");
|
|
158
|
-
}
|
|
138
|
+
bool isTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj) {
|
|
139
|
+
auto jsVal = runtime.global()
|
|
140
|
+
.getProperty(runtime, propNameIDCache.get(runtime, Prop::ArrayBuffer))
|
|
141
|
+
.asObject(runtime)
|
|
142
|
+
.getProperty(runtime, propNameIDCache.get(runtime, Prop::IsView))
|
|
143
|
+
.asObject(runtime)
|
|
144
|
+
.asFunction(runtime)
|
|
145
|
+
.callWithThis(runtime, runtime.global(), {jsi::Value(runtime, jsObj)});
|
|
146
|
+
if (jsVal.isBool()) {
|
|
147
|
+
return jsVal.getBool();
|
|
148
|
+
} else {
|
|
149
|
+
throw std::runtime_error("value is not a boolean");
|
|
150
|
+
}
|
|
159
151
|
}
|
|
160
152
|
|
|
161
|
-
TypedArrayBase getTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
else
|
|
175
|
-
{
|
|
176
|
-
throw std::runtime_error("value is not a boolean");
|
|
177
|
-
}
|
|
153
|
+
TypedArrayBase getTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj) {
|
|
154
|
+
auto jsVal = runtime.global()
|
|
155
|
+
.getProperty(runtime, propNameIDCache.get(runtime, Prop::ArrayBuffer))
|
|
156
|
+
.asObject(runtime)
|
|
157
|
+
.getProperty(runtime, propNameIDCache.get(runtime, Prop::IsView))
|
|
158
|
+
.asObject(runtime)
|
|
159
|
+
.asFunction(runtime)
|
|
160
|
+
.callWithThis(runtime, runtime.global(), {jsi::Value(runtime, jsObj)});
|
|
161
|
+
if (jsVal.isBool()) {
|
|
162
|
+
return TypedArrayBase(runtime, jsObj);
|
|
163
|
+
} else {
|
|
164
|
+
throw std::runtime_error("value is not a boolean");
|
|
165
|
+
}
|
|
178
166
|
}
|
|
179
167
|
|
|
180
|
-
std::vector<uint8_t> arrayBufferToVector(jsi::Runtime &runtime, jsi::Object &jsObj)
|
|
181
|
-
{
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
auto jsArrayBuffer = jsObj.getArrayBuffer(runtime);
|
|
168
|
+
std::vector<uint8_t> arrayBufferToVector(jsi::Runtime &runtime, jsi::Object &jsObj) {
|
|
169
|
+
if (!jsObj.isArrayBuffer(runtime)) {
|
|
170
|
+
throw std::runtime_error("Object is not an ArrayBuffer");
|
|
171
|
+
}
|
|
172
|
+
auto jsArrayBuffer = jsObj.getArrayBuffer(runtime);
|
|
187
173
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
174
|
+
uint8_t *dataBlock = jsArrayBuffer.data(runtime);
|
|
175
|
+
size_t blockSize =
|
|
176
|
+
jsArrayBuffer.getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteLength)).asNumber();
|
|
177
|
+
return std::vector<uint8_t>(dataBlock, dataBlock + blockSize);
|
|
192
178
|
}
|
|
193
179
|
|
|
194
180
|
void arrayBufferUpdate(
|
|
195
181
|
jsi::Runtime &runtime,
|
|
196
182
|
jsi::ArrayBuffer &buffer,
|
|
197
183
|
std::vector<uint8_t> data,
|
|
198
|
-
size_t offset)
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
std::copy(data.begin(), data.end(), dataBlock + offset);
|
|
184
|
+
size_t offset) {
|
|
185
|
+
uint8_t *dataBlock = buffer.data(runtime);
|
|
186
|
+
size_t blockSize = buffer.size(runtime);
|
|
187
|
+
if (data.size() > blockSize) {
|
|
188
|
+
throw jsi::JSError(runtime, "ArrayBuffer is to small to fit data");
|
|
189
|
+
}
|
|
190
|
+
std::copy(data.begin(), data.end(), dataBlock + offset);
|
|
207
191
|
}
|
|
208
192
|
|
|
209
193
|
template <TypedArrayKind T>
|
|
210
|
-
TypedArray<T>::TypedArray(jsi::Runtime &runtime, size_t size) : TypedArrayBase(runtime, size, T){}
|
|
194
|
+
TypedArray<T>::TypedArray(jsi::Runtime &runtime, size_t size) : TypedArrayBase(runtime, size, T) {}
|
|
211
195
|
|
|
212
196
|
template <TypedArrayKind T>
|
|
213
197
|
TypedArray<T>::TypedArray(jsi::Runtime &runtime, std::vector<ContentType<T>> data)
|
|
214
|
-
: TypedArrayBase(runtime, data.size(), T)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
};
|
|
198
|
+
: TypedArrayBase(runtime, data.size(), T) {
|
|
199
|
+
update(runtime, data);
|
|
200
|
+
}
|
|
218
201
|
|
|
219
202
|
template <TypedArrayKind T>
|
|
220
203
|
TypedArray<T>::TypedArray(TypedArrayBase &&base) : TypedArrayBase(std::move(base)) {}
|
|
221
204
|
|
|
222
205
|
template <TypedArrayKind T>
|
|
223
|
-
std::vector<ContentType<T>> TypedArray<T>::toVector(jsi::Runtime &runtime)
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
return std::vector<ContentType<T>>(start, end);
|
|
206
|
+
std::vector<ContentType<T>> TypedArray<T>::toVector(jsi::Runtime &runtime) {
|
|
207
|
+
auto start =
|
|
208
|
+
reinterpret_cast<ContentType<T> *>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
|
|
209
|
+
auto end = start + size(runtime);
|
|
210
|
+
return std::vector<ContentType<T>>(start, end);
|
|
229
211
|
}
|
|
230
212
|
|
|
231
213
|
template <TypedArrayKind T>
|
|
232
|
-
void TypedArray<T>::update(jsi::Runtime &runtime, const std::vector<ContentType<T>> &data)
|
|
233
|
-
{
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
214
|
+
void TypedArray<T>::update(jsi::Runtime &runtime, const std::vector<ContentType<T>> &data) {
|
|
215
|
+
if (data.size() != size(runtime)) {
|
|
216
|
+
throw jsi::JSError(runtime, "TypedArray can only be updated with a vector of the same size");
|
|
217
|
+
}
|
|
218
|
+
uint8_t *rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
|
|
219
|
+
std::copy(data.begin(), data.end(), reinterpret_cast<ContentType<T> *>(rawData));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
template <TypedArrayKind T>
|
|
223
|
+
void TypedArray<T>::updateUnsafe(jsi::Runtime &runtime, ContentType<T> *data, size_t length) {
|
|
224
|
+
if (length != size(runtime)) {
|
|
225
|
+
throw jsi::JSError(runtime, "TypedArray can only be updated with an array of the same size");
|
|
226
|
+
}
|
|
227
|
+
uint8_t *rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
|
|
228
|
+
memcpy(rawData, data, length);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
template <TypedArrayKind T>
|
|
232
|
+
uint8_t* TypedArray<T>::data(jsi::Runtime &runtime) {
|
|
233
|
+
return getBuffer(runtime).data(runtime) + byteOffset(runtime);
|
|
240
234
|
}
|
|
241
235
|
|
|
242
236
|
const jsi::PropNameID &PropNameIDCache::getConstructorNameProp(
|
|
243
237
|
jsi::Runtime &runtime,
|
|
244
|
-
TypedArrayKind kind)
|
|
245
|
-
{
|
|
246
|
-
switch (kind)
|
|
247
|
-
{
|
|
238
|
+
TypedArrayKind kind) {
|
|
239
|
+
switch (kind) {
|
|
248
240
|
case TypedArrayKind::Int8Array:
|
|
249
|
-
|
|
241
|
+
return get(runtime, Prop::Int8Array);
|
|
250
242
|
case TypedArrayKind::Int16Array:
|
|
251
|
-
|
|
243
|
+
return get(runtime, Prop::Int16Array);
|
|
252
244
|
case TypedArrayKind::Int32Array:
|
|
253
|
-
|
|
245
|
+
return get(runtime, Prop::Int32Array);
|
|
254
246
|
case TypedArrayKind::Uint8Array:
|
|
255
|
-
|
|
247
|
+
return get(runtime, Prop::Uint8Array);
|
|
256
248
|
case TypedArrayKind::Uint8ClampedArray:
|
|
257
|
-
|
|
249
|
+
return get(runtime, Prop::Uint8ClampedArray);
|
|
258
250
|
case TypedArrayKind::Uint16Array:
|
|
259
|
-
|
|
251
|
+
return get(runtime, Prop::Uint16Array);
|
|
260
252
|
case TypedArrayKind::Uint32Array:
|
|
261
|
-
|
|
253
|
+
return get(runtime, Prop::Uint32Array);
|
|
262
254
|
case TypedArrayKind::Float32Array:
|
|
263
|
-
|
|
255
|
+
return get(runtime, Prop::Float32Array);
|
|
264
256
|
case TypedArrayKind::Float64Array:
|
|
265
|
-
|
|
266
|
-
|
|
257
|
+
return get(runtime, Prop::Float64Array);
|
|
258
|
+
}
|
|
267
259
|
}
|
|
268
260
|
|
|
269
|
-
jsi::PropNameID PropNameIDCache::createProp(jsi::Runtime &runtime, Prop prop)
|
|
270
|
-
{
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
};
|
|
275
|
-
switch (prop)
|
|
276
|
-
{
|
|
261
|
+
jsi::PropNameID PropNameIDCache::createProp(jsi::Runtime &runtime, Prop prop) {
|
|
262
|
+
auto create = [&](const std::string &propName) {
|
|
263
|
+
return jsi::PropNameID::forUtf8(runtime, propName);
|
|
264
|
+
};
|
|
265
|
+
switch (prop) {
|
|
277
266
|
case Prop::Buffer:
|
|
278
|
-
|
|
267
|
+
return create("buffer");
|
|
279
268
|
case Prop::Constructor:
|
|
280
|
-
|
|
269
|
+
return create("constructor");
|
|
281
270
|
case Prop::Name:
|
|
282
|
-
|
|
271
|
+
return create("name");
|
|
283
272
|
case Prop::Proto:
|
|
284
|
-
|
|
273
|
+
return create("__proto__");
|
|
285
274
|
case Prop::Length:
|
|
286
|
-
|
|
275
|
+
return create("length");
|
|
287
276
|
case Prop::ByteLength:
|
|
288
|
-
|
|
277
|
+
return create("byteLength");
|
|
289
278
|
case Prop::ByteOffset:
|
|
290
|
-
|
|
279
|
+
return create("byteOffset");
|
|
291
280
|
case Prop::IsView:
|
|
292
|
-
|
|
281
|
+
return create("isView");
|
|
293
282
|
case Prop::ArrayBuffer:
|
|
294
|
-
|
|
283
|
+
return create("ArrayBuffer");
|
|
295
284
|
case Prop::Int8Array:
|
|
296
|
-
|
|
285
|
+
return create("Int8Array");
|
|
297
286
|
case Prop::Int16Array:
|
|
298
|
-
|
|
287
|
+
return create("Int16Array");
|
|
299
288
|
case Prop::Int32Array:
|
|
300
|
-
|
|
289
|
+
return create("Int32Array");
|
|
301
290
|
case Prop::Uint8Array:
|
|
302
|
-
|
|
291
|
+
return create("Uint8Array");
|
|
303
292
|
case Prop::Uint8ClampedArray:
|
|
304
|
-
|
|
293
|
+
return create("Uint8ClampedArray");
|
|
305
294
|
case Prop::Uint16Array:
|
|
306
|
-
|
|
295
|
+
return create("Uint16Array");
|
|
307
296
|
case Prop::Uint32Array:
|
|
308
|
-
|
|
297
|
+
return create("Uint32Array");
|
|
309
298
|
case Prop::Float32Array:
|
|
310
|
-
|
|
299
|
+
return create("Float32Array");
|
|
311
300
|
case Prop::Float64Array:
|
|
312
|
-
|
|
313
|
-
|
|
301
|
+
return create("Float64Array");
|
|
302
|
+
}
|
|
314
303
|
}
|
|
315
304
|
|
|
316
305
|
std::unordered_map<std::string, TypedArrayKind> nameToKindMap = {
|
|
@@ -325,9 +314,8 @@ std::unordered_map<std::string, TypedArrayKind> nameToKindMap = {
|
|
|
325
314
|
{"Float64Array", TypedArrayKind::Float64Array},
|
|
326
315
|
};
|
|
327
316
|
|
|
328
|
-
TypedArrayKind getTypedArrayKindForName(const std::string &name)
|
|
329
|
-
|
|
330
|
-
return nameToKindMap.at(name);
|
|
317
|
+
TypedArrayKind getTypedArrayKindForName(const std::string &name) {
|
|
318
|
+
return nameToKindMap.at(name);
|
|
331
319
|
}
|
|
332
320
|
|
|
333
321
|
template class TypedArray<TypedArrayKind::Int8Array>;
|