react-native-mmkv 2.10.2 → 2.11.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 +9 -1
- package/android/build.gradle +7 -0
- package/android/src/main/cpp/MmkvHostObject.cpp +191 -204
- package/android/src/main/cpp/MmkvHostObject.h +2 -2
- package/android/src/main/cpp/cpp-adapter.cpp +56 -49
- package/cpp/TypedArray.cpp +69 -76
- package/cpp/TypedArray.h +66 -91
- package/img/banner-dark.png +0 -0
- package/img/banner-light.png +0 -0
- package/ios/JSIUtils.h +27 -39
- package/ios/JSIUtils.mm +110 -138
- package/ios/MmkvHostObject.h +2 -2
- package/ios/MmkvHostObject.mm +188 -205
- package/ios/MmkvModule.mm +62 -52
- package/lib/commonjs/hooks.js +1 -3
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/module/hooks.js +1 -3
- package/lib/module/hooks.js.map +1 -1
- package/lib/typescript/hooks.d.ts.map +1 -1
- package/package.json +13 -3
- package/src/hooks.ts +1 -3
|
@@ -1,70 +1,77 @@
|
|
|
1
|
-
#include <jni.h>
|
|
2
|
-
#include <jsi/jsi.h>
|
|
3
|
-
#include <MMKV.h>
|
|
4
1
|
#include "MmkvHostObject.h"
|
|
5
2
|
#include "TypedArray.h"
|
|
3
|
+
#include <MMKV.h>
|
|
4
|
+
#include <jni.h>
|
|
5
|
+
#include <jsi/jsi.h>
|
|
6
6
|
|
|
7
7
|
using namespace facebook;
|
|
8
8
|
|
|
9
|
-
std::string getPropertyAsStringOrEmptyFromObject(jsi::Object& object,
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
std::string getPropertyAsStringOrEmptyFromObject(jsi::Object& object,
|
|
10
|
+
const std::string& propertyName,
|
|
11
|
+
jsi::Runtime& runtime) {
|
|
12
|
+
jsi::Value value = object.getProperty(runtime, propertyName.c_str());
|
|
13
|
+
return value.isString() ? value.asString(runtime).utf8(runtime) : "";
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
void install(jsi::Runtime& jsiRuntime) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
throw jsi::JSError(runtime, "MMKV.createNewInstance(..) expects one argument (object)!");
|
|
25
|
-
}
|
|
26
|
-
jsi::Object config = arguments[0].asObject(runtime);
|
|
17
|
+
// MMKV.createNewInstance()
|
|
18
|
+
auto mmkvCreateNewInstance = jsi::Function::createFromHostFunction(
|
|
19
|
+
jsiRuntime, jsi::PropNameID::forAscii(jsiRuntime, "mmkvCreateNewInstance"), 1,
|
|
20
|
+
[](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
|
|
21
|
+
size_t count) -> jsi::Value {
|
|
22
|
+
if (count != 1) {
|
|
23
|
+
throw jsi::JSError(runtime, "MMKV.createNewInstance(..) expects one argument (object)!");
|
|
24
|
+
}
|
|
25
|
+
jsi::Object config = arguments[0].asObject(runtime);
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
std::string instanceId = getPropertyAsStringOrEmptyFromObject(config, "id", runtime);
|
|
28
|
+
std::string path = getPropertyAsStringOrEmptyFromObject(config, "path", runtime);
|
|
29
|
+
std::string encryptionKey =
|
|
30
|
+
getPropertyAsStringOrEmptyFromObject(config, "encryptionKey", runtime);
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
auto instance = std::make_shared<MmkvHostObject>(instanceId, path, encryptionKey);
|
|
33
|
+
return jsi::Object::createFromHostObject(runtime, instance);
|
|
34
|
+
});
|
|
35
|
+
jsiRuntime.global().setProperty(jsiRuntime, "mmkvCreateNewInstance",
|
|
36
|
+
std::move(mmkvCreateNewInstance));
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
// Adds the PropNameIDCache object to the Runtime. If the Runtime gets destroyed, the Object gets
|
|
39
|
+
// destroyed and the cache gets invalidated.
|
|
40
|
+
auto propNameIdCache = std::make_shared<InvalidateCacheOnDestroy>(jsiRuntime);
|
|
41
|
+
jsiRuntime.global().setProperty(jsiRuntime, "mmkvArrayBufferPropNameIdCache",
|
|
42
|
+
jsi::Object::createFromHostObject(jsiRuntime, propNameIdCache));
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
std::string jstringToStdString(JNIEnv
|
|
43
|
-
|
|
45
|
+
std::string jstringToStdString(JNIEnv* env, jstring jStr) {
|
|
46
|
+
if (!jStr)
|
|
47
|
+
return "";
|
|
44
48
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
const auto stringClass = env->GetObjectClass(jStr);
|
|
50
|
+
const auto getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
|
|
51
|
+
const auto stringJbytes =
|
|
52
|
+
(jbyteArray)env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));
|
|
48
53
|
|
|
49
|
-
|
|
50
|
-
|
|
54
|
+
auto length = (size_t)env->GetArrayLength(stringJbytes);
|
|
55
|
+
auto pBytes = env->GetByteArrayElements(stringJbytes, nullptr);
|
|
51
56
|
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
std::string ret = std::string((char*)pBytes, length);
|
|
58
|
+
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
env->DeleteLocalRef(stringJbytes);
|
|
61
|
+
env->DeleteLocalRef(stringClass);
|
|
62
|
+
return ret;
|
|
58
63
|
}
|
|
59
64
|
|
|
60
|
-
extern "C"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
extern "C" JNIEXPORT void JNICALL Java_com_reactnativemmkv_MmkvModule_nativeInstall(JNIEnv* env,
|
|
66
|
+
jobject clazz,
|
|
67
|
+
jlong jsiPtr,
|
|
68
|
+
jstring path) {
|
|
69
|
+
MMKV::initializeMMKV(jstringToStdString(env, path));
|
|
64
70
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
auto runtime = reinterpret_cast<jsi::Runtime*>(jsiPtr);
|
|
72
|
+
if (runtime) {
|
|
73
|
+
install(*runtime);
|
|
74
|
+
}
|
|
75
|
+
// if runtime was nullptr, MMKV will not be installed. This should only happen while Remote
|
|
76
|
+
// Debugging (Chrome), but will be weird either way.
|
|
70
77
|
}
|
package/cpp/TypedArray.cpp
CHANGED
|
@@ -8,40 +8,39 @@
|
|
|
8
8
|
|
|
9
9
|
#include "TypedArray.h"
|
|
10
10
|
|
|
11
|
-
#include <
|
|
11
|
+
#include <algorithm>
|
|
12
12
|
#include <memory>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <unordered_map>
|
|
13
15
|
#include <utility>
|
|
14
16
|
#include <vector>
|
|
15
|
-
#include <algorithm>
|
|
16
|
-
#include <string>
|
|
17
17
|
|
|
18
|
-
template <TypedArrayKind T>
|
|
19
|
-
using ContentType = typename typedArrayTypeMap<T>::type;
|
|
18
|
+
template <TypedArrayKind T> using ContentType = typename typedArrayTypeMap<T>::type;
|
|
20
19
|
|
|
21
20
|
enum class Prop {
|
|
22
|
-
Buffer,
|
|
23
|
-
Constructor,
|
|
24
|
-
Name,
|
|
25
|
-
Proto,
|
|
26
|
-
Length,
|
|
27
|
-
ByteLength,
|
|
28
|
-
ByteOffset,
|
|
29
|
-
IsView,
|
|
30
|
-
ArrayBuffer,
|
|
31
|
-
Int8Array,
|
|
32
|
-
Int16Array,
|
|
33
|
-
Int32Array,
|
|
34
|
-
Uint8Array,
|
|
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"
|
|
35
34
|
Uint8ClampedArray, // "Uint8ClampedArray"
|
|
36
|
-
Uint16Array,
|
|
37
|
-
Uint32Array,
|
|
38
|
-
Float32Array,
|
|
39
|
-
Float64Array,
|
|
35
|
+
Uint16Array, // "Uint16Array"
|
|
36
|
+
Uint32Array, // "Uint32Array"
|
|
37
|
+
Float32Array, // "Float32Array"
|
|
38
|
+
Float64Array, // "Float64Array"
|
|
40
39
|
};
|
|
41
40
|
|
|
42
41
|
class PropNameIDCache {
|
|
43
|
-
|
|
44
|
-
const jsi::PropNameID
|
|
42
|
+
public:
|
|
43
|
+
const jsi::PropNameID& get(jsi::Runtime& runtime, Prop prop) {
|
|
45
44
|
auto key = reinterpret_cast<uintptr_t>(&runtime);
|
|
46
45
|
if (this->props.find(key) == this->props.end()) {
|
|
47
46
|
this->props[key] = std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>>();
|
|
@@ -52,7 +51,7 @@ class PropNameIDCache {
|
|
|
52
51
|
return *(this->props[key][prop]);
|
|
53
52
|
}
|
|
54
53
|
|
|
55
|
-
const jsi::PropNameID
|
|
54
|
+
const jsi::PropNameID& getConstructorNameProp(jsi::Runtime& runtime, TypedArrayKind kind);
|
|
56
55
|
|
|
57
56
|
void invalidate(uintptr_t key) {
|
|
58
57
|
if (props.find(key) != props.end()) {
|
|
@@ -60,37 +59,36 @@ class PropNameIDCache {
|
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
61
|
|
|
63
|
-
|
|
62
|
+
private:
|
|
64
63
|
std::unordered_map<uintptr_t, std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>>> props;
|
|
65
64
|
|
|
66
|
-
jsi::PropNameID createProp(jsi::Runtime
|
|
65
|
+
jsi::PropNameID createProp(jsi::Runtime& runtime, Prop prop);
|
|
67
66
|
};
|
|
68
67
|
|
|
69
68
|
PropNameIDCache propNameIDCache;
|
|
70
69
|
|
|
71
|
-
InvalidateCacheOnDestroy::InvalidateCacheOnDestroy(jsi::Runtime
|
|
70
|
+
InvalidateCacheOnDestroy::InvalidateCacheOnDestroy(jsi::Runtime& runtime) {
|
|
72
71
|
key = reinterpret_cast<uintptr_t>(&runtime);
|
|
73
72
|
}
|
|
74
73
|
InvalidateCacheOnDestroy::~InvalidateCacheOnDestroy() {
|
|
75
74
|
propNameIDCache.invalidate(key);
|
|
76
75
|
}
|
|
77
76
|
|
|
78
|
-
TypedArrayKind getTypedArrayKindForName(const std::string
|
|
77
|
+
TypedArrayKind getTypedArrayKindForName(const std::string& name);
|
|
79
78
|
|
|
80
|
-
TypedArrayBase::TypedArrayBase(jsi::Runtime
|
|
79
|
+
TypedArrayBase::TypedArrayBase(jsi::Runtime& runtime, size_t size, TypedArrayKind kind)
|
|
81
80
|
: TypedArrayBase(
|
|
82
|
-
runtime,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
TypedArrayBase::TypedArrayBase(jsi::Runtime &runtime, const jsi::Object &obj)
|
|
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)
|
|
91
89
|
: jsi::Object(jsi::Value(runtime, obj).asObject(runtime)) {}
|
|
92
90
|
|
|
93
|
-
TypedArrayKind TypedArrayBase::getKind(jsi::Runtime
|
|
91
|
+
TypedArrayKind TypedArrayBase::getKind(jsi::Runtime& runtime) const {
|
|
94
92
|
auto constructorName = this->getProperty(runtime, propNameIDCache.get(runtime, Prop::Constructor))
|
|
95
93
|
.asObject(runtime)
|
|
96
94
|
.getProperty(runtime, propNameIDCache.get(runtime, Prop::Name))
|
|
@@ -99,34 +97,34 @@ TypedArrayKind TypedArrayBase::getKind(jsi::Runtime &runtime) const {
|
|
|
99
97
|
return getTypedArrayKindForName(constructorName);
|
|
100
98
|
}
|
|
101
99
|
|
|
102
|
-
size_t TypedArrayBase::size(jsi::Runtime
|
|
100
|
+
size_t TypedArrayBase::size(jsi::Runtime& runtime) const {
|
|
103
101
|
return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
|
|
104
102
|
}
|
|
105
103
|
|
|
106
|
-
size_t TypedArrayBase::length(jsi::Runtime
|
|
104
|
+
size_t TypedArrayBase::length(jsi::Runtime& runtime) const {
|
|
107
105
|
return getProperty(runtime, propNameIDCache.get(runtime, Prop::Length)).asNumber();
|
|
108
106
|
}
|
|
109
107
|
|
|
110
|
-
size_t TypedArrayBase::byteLength(jsi::Runtime
|
|
108
|
+
size_t TypedArrayBase::byteLength(jsi::Runtime& runtime) const {
|
|
111
109
|
return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteLength)).asNumber();
|
|
112
110
|
}
|
|
113
111
|
|
|
114
|
-
size_t TypedArrayBase::byteOffset(jsi::Runtime
|
|
112
|
+
size_t TypedArrayBase::byteOffset(jsi::Runtime& runtime) const {
|
|
115
113
|
return getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteOffset)).asNumber();
|
|
116
114
|
}
|
|
117
115
|
|
|
118
|
-
bool TypedArrayBase::hasBuffer(jsi::Runtime
|
|
116
|
+
bool TypedArrayBase::hasBuffer(jsi::Runtime& runtime) const {
|
|
119
117
|
auto buffer = getProperty(runtime, propNameIDCache.get(runtime, Prop::Buffer));
|
|
120
118
|
return buffer.isObject() && buffer.asObject(runtime).isArrayBuffer(runtime);
|
|
121
119
|
}
|
|
122
120
|
|
|
123
|
-
std::vector<uint8_t> TypedArrayBase::toVector(jsi::Runtime
|
|
124
|
-
auto start = reinterpret_cast<uint8_t
|
|
121
|
+
std::vector<uint8_t> TypedArrayBase::toVector(jsi::Runtime& runtime) {
|
|
122
|
+
auto start = reinterpret_cast<uint8_t*>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
|
|
125
123
|
auto end = start + byteLength(runtime);
|
|
126
124
|
return std::vector<uint8_t>(start, end);
|
|
127
125
|
}
|
|
128
126
|
|
|
129
|
-
jsi::ArrayBuffer TypedArrayBase::getBuffer(jsi::Runtime
|
|
127
|
+
jsi::ArrayBuffer TypedArrayBase::getBuffer(jsi::Runtime& runtime) const {
|
|
130
128
|
auto buffer = getProperty(runtime, propNameIDCache.get(runtime, Prop::Buffer));
|
|
131
129
|
if (buffer.isObject() && buffer.asObject(runtime).isArrayBuffer(runtime)) {
|
|
132
130
|
return buffer.asObject(runtime).getArrayBuffer(runtime);
|
|
@@ -135,7 +133,7 @@ jsi::ArrayBuffer TypedArrayBase::getBuffer(jsi::Runtime &runtime) const {
|
|
|
135
133
|
}
|
|
136
134
|
}
|
|
137
135
|
|
|
138
|
-
bool isTypedArray(jsi::Runtime
|
|
136
|
+
bool isTypedArray(jsi::Runtime& runtime, const jsi::Object& jsObj) {
|
|
139
137
|
auto jsVal = runtime.global()
|
|
140
138
|
.getProperty(runtime, propNameIDCache.get(runtime, Prop::ArrayBuffer))
|
|
141
139
|
.asObject(runtime)
|
|
@@ -150,7 +148,7 @@ bool isTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj) {
|
|
|
150
148
|
}
|
|
151
149
|
}
|
|
152
150
|
|
|
153
|
-
TypedArrayBase getTypedArray(jsi::Runtime
|
|
151
|
+
TypedArrayBase getTypedArray(jsi::Runtime& runtime, const jsi::Object& jsObj) {
|
|
154
152
|
auto jsVal = runtime.global()
|
|
155
153
|
.getProperty(runtime, propNameIDCache.get(runtime, Prop::ArrayBuffer))
|
|
156
154
|
.asObject(runtime)
|
|
@@ -165,24 +163,21 @@ TypedArrayBase getTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj) {
|
|
|
165
163
|
}
|
|
166
164
|
}
|
|
167
165
|
|
|
168
|
-
std::vector<uint8_t> arrayBufferToVector(jsi::Runtime
|
|
166
|
+
std::vector<uint8_t> arrayBufferToVector(jsi::Runtime& runtime, jsi::Object& jsObj) {
|
|
169
167
|
if (!jsObj.isArrayBuffer(runtime)) {
|
|
170
168
|
throw std::runtime_error("Object is not an ArrayBuffer");
|
|
171
169
|
}
|
|
172
170
|
auto jsArrayBuffer = jsObj.getArrayBuffer(runtime);
|
|
173
171
|
|
|
174
|
-
uint8_t
|
|
172
|
+
uint8_t* dataBlock = jsArrayBuffer.data(runtime);
|
|
175
173
|
size_t blockSize =
|
|
176
174
|
jsArrayBuffer.getProperty(runtime, propNameIDCache.get(runtime, Prop::ByteLength)).asNumber();
|
|
177
175
|
return std::vector<uint8_t>(dataBlock, dataBlock + blockSize);
|
|
178
176
|
}
|
|
179
177
|
|
|
180
|
-
void arrayBufferUpdate(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
std::vector<uint8_t> data,
|
|
184
|
-
size_t offset) {
|
|
185
|
-
uint8_t *dataBlock = buffer.data(runtime);
|
|
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);
|
|
186
181
|
size_t blockSize = buffer.size(runtime);
|
|
187
182
|
if (data.size() > blockSize) {
|
|
188
183
|
throw jsi::JSError(runtime, "ArrayBuffer is to small to fit data");
|
|
@@ -191,51 +186,49 @@ void arrayBufferUpdate(
|
|
|
191
186
|
}
|
|
192
187
|
|
|
193
188
|
template <TypedArrayKind T>
|
|
194
|
-
TypedArray<T>::TypedArray(jsi::Runtime
|
|
189
|
+
TypedArray<T>::TypedArray(jsi::Runtime& runtime, size_t size) : TypedArrayBase(runtime, size, T) {}
|
|
195
190
|
|
|
196
191
|
template <TypedArrayKind T>
|
|
197
|
-
TypedArray<T>::TypedArray(jsi::Runtime
|
|
192
|
+
TypedArray<T>::TypedArray(jsi::Runtime& runtime, std::vector<ContentType<T>> data)
|
|
198
193
|
: TypedArrayBase(runtime, data.size(), T) {
|
|
199
194
|
update(runtime, data);
|
|
200
195
|
}
|
|
201
196
|
|
|
202
197
|
template <TypedArrayKind T>
|
|
203
|
-
TypedArray<T>::TypedArray(TypedArrayBase
|
|
198
|
+
TypedArray<T>::TypedArray(TypedArrayBase&& base) : TypedArrayBase(std::move(base)) {}
|
|
204
199
|
|
|
205
200
|
template <TypedArrayKind T>
|
|
206
|
-
std::vector<ContentType<T>> TypedArray<T>::toVector(jsi::Runtime
|
|
201
|
+
std::vector<ContentType<T>> TypedArray<T>::toVector(jsi::Runtime& runtime) {
|
|
207
202
|
auto start =
|
|
208
|
-
reinterpret_cast<ContentType<T
|
|
203
|
+
reinterpret_cast<ContentType<T>*>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
|
|
209
204
|
auto end = start + size(runtime);
|
|
210
205
|
return std::vector<ContentType<T>>(start, end);
|
|
211
206
|
}
|
|
212
207
|
|
|
213
208
|
template <TypedArrayKind T>
|
|
214
|
-
void TypedArray<T>::update(jsi::Runtime
|
|
209
|
+
void TypedArray<T>::update(jsi::Runtime& runtime, const std::vector<ContentType<T>>& data) {
|
|
215
210
|
if (data.size() != size(runtime)) {
|
|
216
211
|
throw jsi::JSError(runtime, "TypedArray can only be updated with a vector of the same size");
|
|
217
212
|
}
|
|
218
|
-
uint8_t
|
|
219
|
-
std::copy(data.begin(), data.end(), reinterpret_cast<ContentType<T
|
|
213
|
+
uint8_t* rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
|
|
214
|
+
std::copy(data.begin(), data.end(), reinterpret_cast<ContentType<T>*>(rawData));
|
|
220
215
|
}
|
|
221
216
|
|
|
222
217
|
template <TypedArrayKind T>
|
|
223
|
-
void TypedArray<T>::updateUnsafe(jsi::Runtime
|
|
218
|
+
void TypedArray<T>::updateUnsafe(jsi::Runtime& runtime, ContentType<T>* data, size_t length) {
|
|
224
219
|
if (length != size(runtime)) {
|
|
225
220
|
throw jsi::JSError(runtime, "TypedArray can only be updated with an array of the same size");
|
|
226
221
|
}
|
|
227
|
-
uint8_t
|
|
222
|
+
uint8_t* rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
|
|
228
223
|
memcpy(rawData, data, length);
|
|
229
224
|
}
|
|
230
225
|
|
|
231
|
-
template <TypedArrayKind T>
|
|
232
|
-
uint8_t* TypedArray<T>::data(jsi::Runtime &runtime) {
|
|
226
|
+
template <TypedArrayKind T> uint8_t* TypedArray<T>::data(jsi::Runtime& runtime) {
|
|
233
227
|
return getBuffer(runtime).data(runtime) + byteOffset(runtime);
|
|
234
228
|
}
|
|
235
229
|
|
|
236
|
-
const jsi::PropNameID
|
|
237
|
-
|
|
238
|
-
TypedArrayKind kind) {
|
|
230
|
+
const jsi::PropNameID& PropNameIDCache::getConstructorNameProp(jsi::Runtime& runtime,
|
|
231
|
+
TypedArrayKind kind) {
|
|
239
232
|
switch (kind) {
|
|
240
233
|
case TypedArrayKind::Int8Array:
|
|
241
234
|
return get(runtime, Prop::Int8Array);
|
|
@@ -258,8 +251,8 @@ const jsi::PropNameID &PropNameIDCache::getConstructorNameProp(
|
|
|
258
251
|
}
|
|
259
252
|
}
|
|
260
253
|
|
|
261
|
-
jsi::PropNameID PropNameIDCache::createProp(jsi::Runtime
|
|
262
|
-
auto create = [&](const std::string
|
|
254
|
+
jsi::PropNameID PropNameIDCache::createProp(jsi::Runtime& runtime, Prop prop) {
|
|
255
|
+
auto create = [&](const std::string& propName) {
|
|
263
256
|
return jsi::PropNameID::forUtf8(runtime, propName);
|
|
264
257
|
};
|
|
265
258
|
switch (prop) {
|
|
@@ -314,7 +307,7 @@ std::unordered_map<std::string, TypedArrayKind> nameToKindMap = {
|
|
|
314
307
|
{"Float64Array", TypedArrayKind::Float64Array},
|
|
315
308
|
};
|
|
316
309
|
|
|
317
|
-
TypedArrayKind getTypedArrayKindForName(const std::string
|
|
310
|
+
TypedArrayKind getTypedArrayKindForName(const std::string& name) {
|
|
318
311
|
return nameToKindMap.at(name);
|
|
319
312
|
}
|
|
320
313
|
|
package/cpp/TypedArray.h
CHANGED
|
@@ -26,45 +26,34 @@ enum class TypedArrayKind {
|
|
|
26
26
|
Float64Array,
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
template <TypedArrayKind T>
|
|
30
|
-
class TypedArray;
|
|
29
|
+
template <TypedArrayKind T> class TypedArray;
|
|
31
30
|
|
|
32
|
-
template <TypedArrayKind T>
|
|
33
|
-
struct typedArrayTypeMap
|
|
34
|
-
template <>
|
|
35
|
-
struct typedArrayTypeMap<TypedArrayKind::Int8Array> {
|
|
31
|
+
template <TypedArrayKind T> struct typedArrayTypeMap;
|
|
32
|
+
template <> struct typedArrayTypeMap<TypedArrayKind::Int8Array> {
|
|
36
33
|
typedef int8_t type;
|
|
37
34
|
};
|
|
38
|
-
template <>
|
|
39
|
-
struct typedArrayTypeMap<TypedArrayKind::Int16Array> {
|
|
35
|
+
template <> struct typedArrayTypeMap<TypedArrayKind::Int16Array> {
|
|
40
36
|
typedef int16_t type;
|
|
41
37
|
};
|
|
42
|
-
template <>
|
|
43
|
-
struct typedArrayTypeMap<TypedArrayKind::Int32Array> {
|
|
38
|
+
template <> struct typedArrayTypeMap<TypedArrayKind::Int32Array> {
|
|
44
39
|
typedef int32_t type;
|
|
45
40
|
};
|
|
46
|
-
template <>
|
|
47
|
-
struct typedArrayTypeMap<TypedArrayKind::Uint8Array> {
|
|
41
|
+
template <> struct typedArrayTypeMap<TypedArrayKind::Uint8Array> {
|
|
48
42
|
typedef uint8_t type;
|
|
49
43
|
};
|
|
50
|
-
template <>
|
|
51
|
-
struct typedArrayTypeMap<TypedArrayKind::Uint8ClampedArray> {
|
|
44
|
+
template <> struct typedArrayTypeMap<TypedArrayKind::Uint8ClampedArray> {
|
|
52
45
|
typedef uint8_t type;
|
|
53
46
|
};
|
|
54
|
-
template <>
|
|
55
|
-
struct typedArrayTypeMap<TypedArrayKind::Uint16Array> {
|
|
47
|
+
template <> struct typedArrayTypeMap<TypedArrayKind::Uint16Array> {
|
|
56
48
|
typedef uint16_t type;
|
|
57
49
|
};
|
|
58
|
-
template <>
|
|
59
|
-
struct typedArrayTypeMap<TypedArrayKind::Uint32Array> {
|
|
50
|
+
template <> struct typedArrayTypeMap<TypedArrayKind::Uint32Array> {
|
|
60
51
|
typedef uint32_t type;
|
|
61
52
|
};
|
|
62
|
-
template <>
|
|
63
|
-
struct typedArrayTypeMap<TypedArrayKind::Float32Array> {
|
|
53
|
+
template <> struct typedArrayTypeMap<TypedArrayKind::Float32Array> {
|
|
64
54
|
typedef float type;
|
|
65
55
|
};
|
|
66
|
-
template <>
|
|
67
|
-
struct typedArrayTypeMap<TypedArrayKind::Float64Array> {
|
|
56
|
+
template <> struct typedArrayTypeMap<TypedArrayKind::Float64Array> {
|
|
68
57
|
typedef double type;
|
|
69
58
|
};
|
|
70
59
|
|
|
@@ -72,105 +61,91 @@ struct typedArrayTypeMap<TypedArrayKind::Float64Array> {
|
|
|
72
61
|
// Attach this object to global in specific jsi::Runtime to make sure lifecycle of
|
|
73
62
|
// the cache object is connected to the lifecycle of the js runtime
|
|
74
63
|
class InvalidateCacheOnDestroy : public jsi::HostObject {
|
|
75
|
-
|
|
76
|
-
explicit InvalidateCacheOnDestroy(jsi::Runtime
|
|
64
|
+
public:
|
|
65
|
+
explicit InvalidateCacheOnDestroy(jsi::Runtime& runtime);
|
|
77
66
|
virtual ~InvalidateCacheOnDestroy();
|
|
78
|
-
virtual jsi::Value get(jsi::Runtime
|
|
67
|
+
virtual jsi::Value get(jsi::Runtime&, const jsi::PropNameID& name) {
|
|
79
68
|
return jsi::Value::null();
|
|
80
69
|
}
|
|
81
|
-
virtual void set(jsi::Runtime
|
|
82
|
-
virtual std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime
|
|
70
|
+
virtual void set(jsi::Runtime&, const jsi::PropNameID& name, const jsi::Value& value) {}
|
|
71
|
+
virtual std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& rt) {
|
|
83
72
|
return {};
|
|
84
73
|
}
|
|
85
74
|
|
|
86
|
-
|
|
75
|
+
private:
|
|
87
76
|
uintptr_t key;
|
|
88
77
|
};
|
|
89
78
|
|
|
90
79
|
class TypedArrayBase : public jsi::Object {
|
|
91
|
-
|
|
92
|
-
template <TypedArrayKind T>
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
TypedArrayBase(jsi::Runtime
|
|
96
|
-
TypedArrayBase(
|
|
97
|
-
TypedArrayBase(TypedArrayBase
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
template <TypedArrayKind T>
|
|
103
|
-
TypedArray<T>
|
|
104
|
-
template <TypedArrayKind T>
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
std::vector<uint8_t> toVector(jsi::Runtime &runtime);
|
|
118
|
-
jsi::ArrayBuffer getBuffer(jsi::Runtime &runtime) const;
|
|
119
|
-
|
|
120
|
-
private:
|
|
121
|
-
template <TypedArrayKind>
|
|
122
|
-
friend class TypedArray;
|
|
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;
|
|
123
106
|
};
|
|
124
107
|
|
|
125
|
-
bool isTypedArray(jsi::Runtime
|
|
126
|
-
TypedArrayBase getTypedArray(jsi::Runtime
|
|
127
|
-
|
|
128
|
-
std::vector<uint8_t> arrayBufferToVector(jsi::Runtime
|
|
129
|
-
void arrayBufferUpdate(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
std::vector<ContentType<T>> toVector(jsi::Runtime &runtime);
|
|
145
|
-
void update(jsi::Runtime &runtime, const std::vector<ContentType<T>> &data);
|
|
146
|
-
void updateUnsafe(jsi::Runtime &runtime, ContentType<T> *data, size_t length);
|
|
147
|
-
uint8_t* data(jsi::Runtime &runtime);
|
|
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);
|
|
148
127
|
};
|
|
149
128
|
|
|
150
|
-
template <TypedArrayKind T>
|
|
151
|
-
TypedArray<T> TypedArrayBase::get(jsi::Runtime &runtime) const & {
|
|
129
|
+
template <TypedArrayKind T> TypedArray<T> TypedArrayBase::get(jsi::Runtime& runtime) const& {
|
|
152
130
|
assert(getKind(runtime) == T);
|
|
153
131
|
(void)runtime; // when assert is disabled we need to mark this as used
|
|
154
132
|
return TypedArray<T>(jsi::Value(runtime, jsi::Value(runtime, *this).asObject(runtime)));
|
|
155
133
|
}
|
|
156
134
|
|
|
157
|
-
template <TypedArrayKind T>
|
|
158
|
-
TypedArray<T> TypedArrayBase::get(jsi::Runtime &runtime) && {
|
|
135
|
+
template <TypedArrayKind T> TypedArray<T> TypedArrayBase::get(jsi::Runtime& runtime) && {
|
|
159
136
|
assert(getKind(runtime) == T);
|
|
160
137
|
(void)runtime; // when assert is disabled we need to mark this as used
|
|
161
138
|
return TypedArray<T>(std::move(*this));
|
|
162
139
|
}
|
|
163
140
|
|
|
164
|
-
template <TypedArrayKind T>
|
|
165
|
-
TypedArray<T> TypedArrayBase::as(jsi::Runtime &runtime) const & {
|
|
141
|
+
template <TypedArrayKind T> TypedArray<T> TypedArrayBase::as(jsi::Runtime& runtime) const& {
|
|
166
142
|
if (getKind(runtime) != T) {
|
|
167
143
|
throw jsi::JSError(runtime, "Object is not a TypedArray");
|
|
168
144
|
}
|
|
169
145
|
return get<T>(runtime);
|
|
170
146
|
}
|
|
171
147
|
|
|
172
|
-
template <TypedArrayKind T>
|
|
173
|
-
TypedArray<T> TypedArrayBase::as(jsi::Runtime &runtime) && {
|
|
148
|
+
template <TypedArrayKind T> TypedArray<T> TypedArrayBase::as(jsi::Runtime& runtime) && {
|
|
174
149
|
if (getKind(runtime) != T) {
|
|
175
150
|
throw jsi::JSError(runtime, "Object is not a TypedArray");
|
|
176
151
|
}
|
|
Binary file
|
|
Binary file
|