react-native-mmkv 2.10.1 → 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.
@@ -8,40 +8,39 @@
8
8
 
9
9
  #include "TypedArray.h"
10
10
 
11
- #include <unordered_map>
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, // "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"
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, // "Uint16Array"
37
- Uint32Array, // "Uint32Array"
38
- Float32Array, // "Float32Array"
39
- Float64Array, // "Float64Array"
35
+ Uint16Array, // "Uint16Array"
36
+ Uint32Array, // "Uint32Array"
37
+ Float32Array, // "Float32Array"
38
+ Float64Array, // "Float64Array"
40
39
  };
41
40
 
42
41
  class PropNameIDCache {
43
- public:
44
- const jsi::PropNameID &get(jsi::Runtime &runtime, Prop prop) {
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 &getConstructorNameProp(jsi::Runtime &runtime, TypedArrayKind kind);
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
- private:
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 &runtime, Prop prop);
65
+ jsi::PropNameID createProp(jsi::Runtime& runtime, Prop prop);
67
66
  };
68
67
 
69
68
  PropNameIDCache propNameIDCache;
70
69
 
71
- InvalidateCacheOnDestroy::InvalidateCacheOnDestroy(jsi::Runtime &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 &name);
77
+ TypedArrayKind getTypedArrayKindForName(const std::string& name);
79
78
 
80
- TypedArrayBase::TypedArrayBase(jsi::Runtime &runtime, size_t size, TypedArrayKind kind)
79
+ TypedArrayBase::TypedArrayBase(jsi::Runtime& runtime, size_t size, TypedArrayKind kind)
81
80
  : TypedArrayBase(
82
- runtime,
83
- runtime.global()
84
- .getProperty(runtime, propNameIDCache.getConstructorNameProp(runtime, kind))
85
- .asObject(runtime)
86
- .asFunction(runtime)
87
- .callAsConstructor(runtime, {static_cast<double>(size)})
88
- .asObject(runtime)) {}
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 &runtime) const {
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 &runtime) const {
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 &runtime) const {
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 &runtime) const {
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 &runtime) const {
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 &runtime) const {
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 &runtime) {
124
- auto start = reinterpret_cast<uint8_t *>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
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 &runtime) const {
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 &runtime, const jsi::Object &jsObj) {
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 &runtime, const jsi::Object &jsObj) {
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 &runtime, jsi::Object &jsObj) {
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 *dataBlock = jsArrayBuffer.data(runtime);
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
- jsi::Runtime &runtime,
182
- jsi::ArrayBuffer &buffer,
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 &runtime, size_t size) : TypedArrayBase(runtime, size, T) {}
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 &runtime, std::vector<ContentType<T>> data)
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 &&base) : TypedArrayBase(std::move(base)) {}
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 &runtime) {
201
+ std::vector<ContentType<T>> TypedArray<T>::toVector(jsi::Runtime& runtime) {
207
202
  auto start =
208
- reinterpret_cast<ContentType<T> *>(getBuffer(runtime).data(runtime) + byteOffset(runtime));
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 &runtime, const std::vector<ContentType<T>> &data) {
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 *rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
219
- std::copy(data.begin(), data.end(), reinterpret_cast<ContentType<T> *>(rawData));
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 &runtime, ContentType<T> *data, size_t length) {
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 *rawData = getBuffer(runtime).data(runtime) + byteOffset(runtime);
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 &PropNameIDCache::getConstructorNameProp(
237
- jsi::Runtime &runtime,
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 &runtime, Prop prop) {
262
- auto create = [&](const std::string &propName) {
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 &name) {
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
- public:
76
- explicit InvalidateCacheOnDestroy(jsi::Runtime &runtime);
64
+ public:
65
+ explicit InvalidateCacheOnDestroy(jsi::Runtime& runtime);
77
66
  virtual ~InvalidateCacheOnDestroy();
78
- virtual jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) {
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 &, const jsi::PropNameID &name, const jsi::Value &value) {}
82
- virtual std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) {
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
- private:
75
+ private:
87
76
  uintptr_t key;
88
77
  };
89
78
 
90
79
  class TypedArrayBase : public jsi::Object {
91
- public:
92
- template <TypedArrayKind T>
93
- using ContentType = typename typedArrayTypeMap<T>::type;
94
-
95
- TypedArrayBase(jsi::Runtime &, size_t, TypedArrayKind);
96
- TypedArrayBase(jsi::Runtime &, const jsi::Object &);
97
- TypedArrayBase(TypedArrayBase &&) = default;
98
- TypedArrayBase &operator=(TypedArrayBase &&) = default;
99
-
100
- TypedArrayKind getKind(jsi::Runtime &runtime) const;
101
-
102
- template <TypedArrayKind T>
103
- TypedArray<T> get(jsi::Runtime &runtime) const &;
104
- template <TypedArrayKind T>
105
- TypedArray<T> get(jsi::Runtime &runtime) &&;
106
- template <TypedArrayKind T>
107
- TypedArray<T> as(jsi::Runtime &runtime) const &;
108
- template <TypedArrayKind T>
109
- TypedArray<T> as(jsi::Runtime &runtime) &&;
110
-
111
- size_t size(jsi::Runtime &runtime) const;
112
- size_t length(jsi::Runtime &runtime) const;
113
- size_t byteLength(jsi::Runtime &runtime) const;
114
- size_t byteOffset(jsi::Runtime &runtime) const;
115
- bool hasBuffer(jsi::Runtime &runtime) const;
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 &runtime, const jsi::Object &jsObj);
126
- TypedArrayBase getTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj);
127
-
128
- std::vector<uint8_t> arrayBufferToVector(jsi::Runtime &runtime, jsi::Object &jsObj);
129
- void arrayBufferUpdate(
130
- jsi::Runtime &runtime,
131
- jsi::ArrayBuffer &buffer,
132
- std::vector<uint8_t> data,
133
- size_t offset);
134
-
135
- template <TypedArrayKind T>
136
- class TypedArray : public TypedArrayBase {
137
- public:
138
- explicit TypedArray(TypedArrayBase &&base);
139
- TypedArray(jsi::Runtime &runtime, size_t size);
140
- TypedArray(jsi::Runtime &runtime, std::vector<ContentType<T>> data);
141
- TypedArray(TypedArray &&) = default;
142
- TypedArray &operator=(TypedArray &&) = default;
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
package/ios/JSIUtils.h CHANGED
@@ -1,50 +1,38 @@
1
1
  #import <Foundation/Foundation.h>
2
- #import <jsi/jsi.h>
3
2
  #import <React/RCTBridgeModule.h>
3
+ #import <jsi/jsi.h>
4
4
 
5
5
  using namespace facebook;
6
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);
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);
34
25
 
35
26
  struct Promise {
36
- Promise(jsi::Runtime &rt, jsi::Function resolve, jsi::Function reject);
27
+ Promise(jsi::Runtime& rt, jsi::Function resolve, jsi::Function reject);
37
28
 
38
- void resolve(const jsi::Value &result);
39
- void reject(const std::string &error);
29
+ void resolve(const jsi::Value& result);
30
+ void reject(const std::string& error);
40
31
 
41
- jsi::Runtime &runtime_;
42
- jsi::Function resolve_;
43
- jsi::Function reject_;
32
+ jsi::Runtime& runtime_;
33
+ jsi::Function resolve_;
34
+ jsi::Function reject_;
44
35
  };
45
36
 
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);
37
+ using PromiseSetupFunctionType = std::function<void(jsi::Runtime& rt, std::shared_ptr<Promise>)>;
38
+ jsi::Value createPromiseAsJSIValue(jsi::Runtime& rt, const PromiseSetupFunctionType func);