10
10
11
11
#include < algorithm>
12
12
#include < memory>
13
+ #include < utility>
14
+ #include < vector>
13
15
#include < string>
14
16
#include < unordered_map>
15
17
@@ -40,33 +42,37 @@ enum class Prop {
40
42
class PropNameIDCache {
41
43
public:
42
44
const jsi::PropNameID &get (jsi::Runtime &runtime, Prop prop) {
43
- if (! this -> props [prop]) {
44
- this ->props [prop] =
45
- std::make_unique <jsi::PropNameID>( createProp (runtime, 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>>( );
46
48
}
47
- return *(this ->props [prop]);
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]);
48
53
}
49
54
50
- const jsi::PropNameID &getConstructorNameProp (jsi::Runtime &runtime,
51
- MGLTypedArrayKind kind);
55
+ const jsi::PropNameID &getConstructorNameProp (jsi::Runtime &runtime, MGLTypedArrayKind kind);
52
56
53
- void invalidate () {
54
- /* * This call (and attempts to use props.clear()) crash 💥 when the
55
- * JSI runtime has already been destroyed. So we are commenting it out
56
- * and waiting for Nitro and 1.0 to fix this the proper way.
57
- */
58
- // props.erase(props.begin(), props.end());
57
+ void invalidate (uintptr_t key) {
58
+ if (props.find (key) != props.end ()) {
59
+ props[key].clear ();
60
+ }
59
61
}
60
-
61
62
private:
62
- std::unordered_map<Prop, std::unique_ptr<jsi::PropNameID>> props;
63
+ std::unordered_map<uintptr_t , std::unordered_map< Prop, std::unique_ptr<jsi::PropNameID> >> props;
63
64
64
65
jsi::PropNameID createProp (jsi::Runtime &runtime, Prop prop);
65
66
};
66
67
67
68
PropNameIDCache propNameIDCache;
68
69
69
- void invalidateJsiPropNameIDCache () { propNameIDCache.invalidate (); }
70
+ InvalidateCacheOnDestroy::InvalidateCacheOnDestroy (jsi::Runtime &runtime) {
71
+ key = reinterpret_cast <uintptr_t >(&runtime);
72
+ }
73
+ InvalidateCacheOnDestroy::~InvalidateCacheOnDestroy () {
74
+ propNameIDCache.invalidate (key);
75
+ }
70
76
71
77
MGLTypedArrayKind getTypedArrayKindForName (const std::string &name);
72
78
@@ -75,8 +81,9 @@ MGLTypedArrayBase::MGLTypedArrayBase(jsi::Runtime &runtime, size_t size,
75
81
: MGLTypedArrayBase(
76
82
runtime,
77
83
runtime.global()
78
- .getProperty(runtime, propNameIDCache.getConstructorNameProp(
79
- runtime, kind))
84
+ .getProperty(
85
+ runtime,
86
+ propNameIDCache.getConstructorNameProp(runtime, kind))
80
87
.asObject(runtime)
81
88
.asFunction(runtime)
82
89
.callAsConstructor(runtime, {static_cast <double >(size)})
@@ -236,6 +243,20 @@ void MGLTypedArray<T>::update(jsi::Runtime &runtime,
236
243
reinterpret_cast <ContentType<T> *>(rawData));
237
244
}
238
245
246
+ template <MGLTypedArrayKind T>
247
+ void MGLTypedArray<T>::updateUnsafe(jsi::Runtime &runtime, ContentType<T> *data, size_t length) {
248
+ if (length != size (runtime)) {
249
+ throw jsi::JSError (runtime, " TypedArray can only be updated with an array of the same size" );
250
+ }
251
+ uint8_t *rawData = getBuffer (runtime).data (runtime) + byteOffset (runtime);
252
+ memcpy (rawData, data, length);
253
+ }
254
+
255
+ template <MGLTypedArrayKind T>
256
+ uint8_t * MGLTypedArray<T>::data(jsi::Runtime &runtime) {
257
+ return getBuffer (runtime).data (runtime) + byteOffset (runtime);
258
+ }
259
+
239
260
const jsi::PropNameID &PropNameIDCache::getConstructorNameProp (
240
261
jsi::Runtime &runtime, MGLTypedArrayKind kind) {
241
262
switch (kind) {
0 commit comments