forked from mrousavy/react-native-mmkv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMmkvHostObject.cpp
291 lines (264 loc) · 11.3 KB
/
MmkvHostObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// MmkvHostObject.cpp
// Mmkv
//
// Created by Marc Rousavy on 03.09.21.
// Copyright © 2021 Facebook. All rights reserved.
//
#include "MmkvHostObject.h"
#include "TypedArray.h"
#include <MMKV.h>
#include <android/log.h>
#include <string>
#include <vector>
MmkvHostObject::MmkvHostObject(const std::string& instanceId, std::string path,
std::string cryptKey) {
__android_log_print(ANDROID_LOG_INFO, "RNMMKV",
"Creating MMKV instance \"%s\"... (Path: %s, Encryption-Key: %s)",
instanceId.c_str(), path.c_str(), cryptKey.c_str());
std::string* pathPtr = path.size() > 0 ? &path : nullptr;
std::string* cryptKeyPtr = cryptKey.size() > 0 ? &cryptKey : nullptr;
instance = MMKV::mmkvWithID(instanceId, mmkv::DEFAULT_MMAP_SIZE, MMKV_SINGLE_PROCESS, cryptKeyPtr,
pathPtr);
if (instance == nullptr) {
// Check if instanceId is invalid
if (instanceId.empty()) {
throw std::runtime_error("Failed to create MMKV instance! `id` cannot be empty!");
}
// Check if encryptionKey is invalid
if (cryptKey.size() > 16) {
throw std::runtime_error(
"Failed to create MMKV instance! `encryptionKey` cannot be longer than 16 bytes!");
}
throw std::runtime_error("Failed to create MMKV instance!");
}
}
std::vector<jsi::PropNameID> MmkvHostObject::getPropertyNames(jsi::Runtime& rt) {
std::vector<jsi::PropNameID> result;
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("set")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getBoolean")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getBuffer")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getString")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getNumber")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("contains")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("delete")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getAllKeys")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("deleteAll")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("recrypt")));
return result;
}
jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId) {
auto propName = propNameId.utf8(runtime);
auto funcName = "MMKV." + propName;
if (propName == "set") {
// MMKV.set(key: string, value: string | number | bool | Uint8Array)
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName),
2, // key, value
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
if (!arguments[0].isString()) {
throw jsi::JSError(runtime,
"MMKV::set: First argument ('key') has to be of type string!");
}
bool result = false;
auto keyName = arguments[0].getString(runtime).utf8(runtime);
if (arguments[1].isBool()) {
// bool
result = instance->set(arguments[1].getBool(), keyName);
} else if (arguments[1].isNumber()) {
// number
result = instance->set(arguments[1].getNumber(), keyName);
} else if (arguments[1].isString()) {
// string
auto stringValue = arguments[1].getString(runtime).utf8(runtime);
result = instance->set(stringValue, keyName);
} else if (arguments[1].isObject()) {
// object
auto object = arguments[1].asObject(runtime);
if (isTypedArray(runtime, object)) {
// Uint8Array
auto typedArray = getTypedArray(runtime, object);
auto bufferValue = typedArray.getBuffer(runtime);
mmkv::MMBuffer buffer(bufferValue.data(runtime), bufferValue.size(runtime),
mmkv::MMBufferCopyFlag::MMBufferNoCopy);
result = instance->set(buffer, keyName);
} else {
// unknown object
throw jsi::JSError(
runtime, "MMKV::set: 'value' argument is an object, but not of type Uint8Array!");
}
} else {
// unknown type
throw jsi::JSError(
runtime,
"MMKV::set: 'value' argument is not of type bool, number, string or buffer!");
}
return jsi::Value(result);
});
}
if (propName == "getBoolean") {
// MMKV.getBoolean(key: string)
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName),
1, // key
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
if (!arguments[0].isString()) {
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
}
auto keyName = arguments[0].getString(runtime).utf8(runtime);
bool hasValue;
auto value = instance->getBool(keyName, false, &hasValue);
if (hasValue) {
return jsi::Value(value);
} else {
return jsi::Value::undefined();
}
});
}
if (propName == "getString") {
// MMKV.getString(key: string)
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName),
1, // key
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
if (!arguments[0].isString()) {
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
}
auto keyName = arguments[0].getString(runtime).utf8(runtime);
std::string result;
bool hasValue = instance->getString(keyName, result);
if (hasValue) {
return jsi::Value(runtime, jsi::String::createFromUtf8(runtime, result));
} else {
return jsi::Value::undefined();
}
});
}
if (propName == "getNumber") {
// MMKV.getNumber(key: string)
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName),
1, // key
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
if (!arguments[0].isString()) {
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
}
auto keyName = arguments[0].getString(runtime).utf8(runtime);
bool hasValue;
auto value = instance->getDouble(keyName, 0.0, &hasValue);
if (hasValue) {
return jsi::Value(value);
} else {
return jsi::Value::undefined();
}
});
}
if (propName == "getBuffer") {
// MMKV.getBuffer(key: string)
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName),
1, // key
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
if (!arguments[0].isString()) {
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
}
auto keyName = arguments[0].getString(runtime).utf8(runtime);
mmkv::MMBuffer buffer;
bool hasValue = instance->getBytes(keyName, buffer);
if (hasValue) {
auto length = buffer.length();
TypedArray<TypedArrayKind::Uint8Array> array(runtime, length);
auto data = static_cast<const unsigned char*>(buffer.getPtr());
std::vector<unsigned char> vector(length);
vector.assign(data, data + length);
array.update(runtime, vector);
return array;
} else {
return jsi::Value::undefined();
}
});
}
if (propName == "contains") {
// MMKV.contains(key: string)
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName),
1, // key
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
if (!arguments[0].isString()) {
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
}
auto keyName = arguments[0].getString(runtime).utf8(runtime);
bool containsKey = instance->containsKey(keyName);
return jsi::Value(containsKey);
});
}
if (propName == "delete") {
// MMKV.delete(key: string)
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName),
1, // key
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
if (!arguments[0].isString()) {
throw jsi::JSError(runtime, "First argument ('key') has to be of type string!");
}
auto keyName = arguments[0].getString(runtime).utf8(runtime);
instance->removeValueForKey(keyName);
return jsi::Value::undefined();
});
}
if (propName == "getAllKeys") {
// MMKV.getAllKeys()
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName), 0,
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
auto keys = instance->allKeys();
auto array = jsi::Array(runtime, keys.size());
for (int i = 0; i < keys.size(); i++) {
array.setValueAtIndex(runtime, i, keys[i]);
}
return array;
});
}
if (propName == "clearAll") {
// MMKV.clearAll()
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName), 0,
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
instance->clearAll();
return jsi::Value::undefined();
});
}
if (propName == "recrypt") {
// MMKV.recrypt(encryptionKey)
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, funcName),
1, // encryptionKey
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
bool result;
if (arguments[0].isUndefined()) {
// reset encryption key to "no encryption"
result = instance->reKey(std::string());
} else if (arguments[0].isString()) {
// reKey(..) with new encryption-key
auto encryptionKey = arguments[0].getString(runtime).utf8(runtime);
result = instance->reKey(encryptionKey);
} else {
throw jsi::JSError(
runtime,
"First argument ('encryptionKey') has to be of type string (or undefined)!");
}
return jsi::Value(result);
});
}
return jsi::Value::undefined();
}