1010
1111#include < algorithm>
1212#include < memory>
13+ #include < utility>
14+ #include < vector>
1315#include < string>
1416#include < unordered_map>
1517
@@ -40,33 +42,37 @@ enum class Prop {
4042class PropNameIDCache {
4143 public:
4244 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>>( );
4648 }
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]);
4853 }
4954
50- const jsi::PropNameID &getConstructorNameProp (jsi::Runtime &runtime,
51- MGLTypedArrayKind kind);
55+ const jsi::PropNameID &getConstructorNameProp (jsi::Runtime &runtime, MGLTypedArrayKind kind);
5256
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+ }
5961 }
60-
6162 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;
6364
6465 jsi::PropNameID createProp (jsi::Runtime &runtime, Prop prop);
6566};
6667
6768PropNameIDCache propNameIDCache;
6869
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+ }
7076
7177MGLTypedArrayKind getTypedArrayKindForName (const std::string &name);
7278
@@ -75,8 +81,9 @@ MGLTypedArrayBase::MGLTypedArrayBase(jsi::Runtime &runtime, size_t size,
7581 : MGLTypedArrayBase(
7682 runtime,
7783 runtime.global()
78- .getProperty(runtime, propNameIDCache.getConstructorNameProp(
79- runtime, kind))
84+ .getProperty(
85+ runtime,
86+ propNameIDCache.getConstructorNameProp(runtime, kind))
8087 .asObject(runtime)
8188 .asFunction(runtime)
8289 .callAsConstructor(runtime, {static_cast <double >(size)})
@@ -236,6 +243,20 @@ void MGLTypedArray<T>::update(jsi::Runtime &runtime,
236243 reinterpret_cast <ContentType<T> *>(rawData));
237244}
238245
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+
239260const jsi::PropNameID &PropNameIDCache::getConstructorNameProp (
240261 jsi::Runtime &runtime, MGLTypedArrayKind kind) {
241262 switch (kind) {
0 commit comments