From adec9273160bc7f265551ee51718a7906de3b084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Fri, 17 May 2024 12:33:42 +0200 Subject: [PATCH 1/6] init --- Common/cpp/SharedItems/Shareables.cpp | 61 +++++++++++++++---- Common/cpp/SharedItems/Shareables.h | 17 ++++++ MacOSExample/macos/Podfile.lock | 6 +- package.json | 4 +- src/createAnimatedComponent/JSPropsUpdater.ts | 4 +- 5 files changed, 72 insertions(+), 20 deletions(-) diff --git a/Common/cpp/SharedItems/Shareables.cpp b/Common/cpp/SharedItems/Shareables.cpp index 1d542933e80..d9b2b31bbae 100644 --- a/Common/cpp/SharedItems/Shareables.cpp +++ b/Common/cpp/SharedItems/Shareables.cpp @@ -4,6 +4,9 @@ using namespace facebook; namespace reanimated { +std::atomic ShareableObject::nativeStateAccess_ = + NativeStateAccess::Unknown; + jsi::Function getValueUnpacker(jsi::Runtime &rt) { auto valueUnpacker = rt.global().getProperty(rt, "__valueUnpacker"); assert(valueUnpacker.isObject() && "valueUnpacker not found"); @@ -99,8 +102,8 @@ jsi::Value makeShareableClone( } else if (value.isSymbol()) { // TODO: this is only a placeholder implementation, here we replace symbols // with strings in order to make certain objects to be captured. There isn't - // yet any usecase for using symbols on the UI runtime so it is fine to keep - // it like this for now. + // yet any use case for using symbols on the UI runtime so it is fine to + // keep it like this for now. shareable = std::make_shared(value.getSymbol(rt).toString(rt)); } else { @@ -198,8 +201,15 @@ ShareableObject::ShareableObject(jsi::Runtime &rt, const jsi::Object &object) auto value = extractShareableOrThrow(rt, object.getProperty(rt, key)); data_.emplace_back(key.utf8(rt), value); } - if (object.hasNativeState(rt)) { - nativeState_ = object.getNativeState(rt); + if (nativeStateAccess_ == NativeStateAccess::Safe) { + makeNativeStateFromObject(rt, object); + } else if (nativeStateAccess_ == NativeStateAccess::Unknown) { + try { + makeNativeStateFromObject(rt, object); + nativeStateAccess_ = NativeStateAccess::Safe; + } catch (...) { + nativeStateAccess_ = NativeStateAccess::Unsafe; + } } } @@ -208,9 +218,15 @@ ShareableObject::ShareableObject( const jsi::Object &object, const jsi::Value &nativeStateSource) : ShareableObject(rt, object) { - if (nativeStateSource.isObject() && - nativeStateSource.asObject(rt).hasNativeState(rt)) { - nativeState_ = nativeStateSource.asObject(rt).getNativeState(rt); + if (nativeStateAccess_ == NativeStateAccess::Safe) { + makeNativeStateFromNativeStateSource(rt, nativeStateSource); + } else if (nativeStateAccess_ == NativeStateAccess::Unknown) { + try { + makeNativeStateFromNativeStateSource(rt, nativeStateSource); + nativeStateAccess_ = NativeStateAccess::Safe; + } catch (...) { + nativeStateAccess_ = NativeStateAccess::Unsafe; + } } } @@ -226,6 +242,25 @@ jsi::Value ShareableObject::toJSValue(jsi::Runtime &rt) { return obj; } +void ShareableObject::makeNativeStateFromObject( + jsi::Runtime &rt, + const jsi::Object &object) { + if (object.hasNativeState(rt)) { + nativeState_ = object.getNativeState(rt); + nativeStateAccess_ = NativeStateAccess::Safe; + } +} + +void ShareableObject::makeNativeStateFromNativeStateSource( + jsi::Runtime &rt, + const jsi::Value &nativeStateSource) { + if (nativeStateSource.isObject() && + nativeStateSource.asObject(rt).hasNativeState(rt)) { + nativeState_ = nativeStateSource.asObject(rt).getNativeState(rt); + nativeStateAccess_ = NativeStateAccess::Safe; + } +} + jsi::Value ShareableHostObject::toJSValue(jsi::Runtime &rt) { return jsi::Object::createFromHostObject(rt, hostObject_); } @@ -270,12 +305,12 @@ jsi::Value ShareableHandle::toJSValue(jsi::Runtime &rt) { rt, initObj, jsi::String::createFromAscii(rt, "Handle"))); // We are locking the initialization here since the thread that is - // initalizing can be pre-empted on runtime lock. E.g. - // UI thread can be pre-empted on initialization of a shared value and then - // JS thread can try to access the shared value, locking the whole runtime. - // If we put the lock on `getValueUnpacker` part (basically any part that - // requires runtime) we would get a deadlock since UI thread would never - // release it. + // initializing can be preempted on runtime lock. E.g. + // UI thread can be preempted on initialization of a shared value and + // then JS thread can try to access the shared value, locking the whole + // runtime. If we put the lock on `getValueUnpacker` part (basically any + // part that requires runtime) we would get a deadlock since UI thread + // would never release it. std::unique_lock lock(initializationMutex_); if (remoteValue_ == nullptr) { remoteValue_ = std::move(value); diff --git a/Common/cpp/SharedItems/Shareables.h b/Common/cpp/SharedItems/Shareables.h index 46b06720f0e..a9a5ab7164e 100644 --- a/Common/cpp/SharedItems/Shareables.h +++ b/Common/cpp/SharedItems/Shareables.h @@ -179,6 +179,12 @@ class ShareableArray : public Shareable { std::vector> data_; }; +enum class NativeStateAccess { + Unknown, + Safe, + Unsafe, +}; + class ShareableObject : public Shareable { public: ShareableObject(jsi::Runtime &rt, const jsi::Object &object); @@ -191,8 +197,19 @@ class ShareableObject : public Shareable { jsi::Value toJSValue(jsi::Runtime &rt) override; protected: + void makeNativeStateFromObject(jsi::Runtime &rt, const jsi::Object &object); + void makeNativeStateFromNativeStateSource( + jsi::Runtime &rt, + const jsi::Value &nativeStateSource); + std::vector>> data_; std::shared_ptr nativeState_; + + // In some implementations of JSC - namely, macOS + // the methods that refer to the native state of an object + // are not implemented and they throw the error. Therefore + // we need to keep track of the access mode to the native state. + static std::atomic nativeStateAccess_; }; class ShareableHostObject : public Shareable { diff --git a/MacOSExample/macos/Podfile.lock b/MacOSExample/macos/Podfile.lock index 00cd2772b76..732dc0261be 100644 --- a/MacOSExample/macos/Podfile.lock +++ b/MacOSExample/macos/Podfile.lock @@ -1203,11 +1203,11 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: boost: 0686b6af8cbd638c784fea5afb789be66699823c - DoubleConversion: acaf5db79676d2e9119015819153f0f99191de12 + DoubleConversion: ca54355f8932558971f6643521d62b9bc8231cee FBLazyVector: 6d4868013ba47ee362b596a3ccc6f256a0412519 FBReactNativeSpec: 030e69b320c718834248085bfd0bd6652141c28c fmt: 03574da4b7ba40de39da59677ca66610ce8c4a02 - glog: 6df0a3d6e2750a50609471fd1a01fd2948d405b5 + glog: 3a72874c0322c7caf24931d3a2777cb7a3090529 RCT-Folly: 68e9c0fd4c0f05964afd447041d3ac2d67298f27 RCTRequired: 80f6978c000be199cc243f876974fd848651a247 RCTTypeSafety: cf3c24b03624d8ed3b7a1723f25ac159ff19ad70 @@ -1255,7 +1255,7 @@ SPEC CHECKSUMS: RNReanimated: 102299541274020227cc99875a2bc080e23e82ce RNSVG: ba3e7232f45e34b7b47e74472386cf4e1a676d0a SocketRocket: f6c6249082c011e6de2de60ed641ef8bbe0cfac9 - Yoga: d7c14f5598b68c9415cc426df87de9d5697e40c4 + Yoga: 24ddb4963e2a3330df762423f0a9de2096325401 PODFILE CHECKSUM: bc6f61f87c0dc3e6c1b90a39188b38c3dcd174b0 diff --git a/package.json b/package.json index f14ab56e8f3..cb2dcc73c5e 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,8 @@ "format:plugin": "cd plugin && yarn format && cd ..", "format:java": "node ./scripts/format-java.js", "format:ios": "find apple/ -iname \"*.h\" -o -iname \"*.m\" -o -iname \"*.mm\" -o -iname \"*.cpp\" | xargs clang-format -i --Werror", - "format:android": "find android/src/ -iname *.h -o -iname *.cpp | xargs clang-format -i", - "format:common": "find Common/ -iname *.h -o -iname *.cpp | xargs clang-format -i", + "format:android": "find android/src/ -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i", + "format:common": "find Common/ -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i", "format:docs": "cd docs && yarn format && cd ..", "find-unused-code:js": "yarn ts-prune --ignore \"index|.web.\" --error ", "type:check:src": "yarn tsc --noEmit", diff --git a/src/createAnimatedComponent/JSPropsUpdater.ts b/src/createAnimatedComponent/JSPropsUpdater.ts index 7222e33bed0..206db88a8e2 100644 --- a/src/createAnimatedComponent/JSPropsUpdater.ts +++ b/src/createAnimatedComponent/JSPropsUpdater.ts @@ -25,8 +25,8 @@ class JSPropsUpdaterPaper implements IJSPropsUpdater { constructor() { this._reanimatedEventEmitter = new NativeEventEmitter( - // NativeEventEmitter only uses this parameter on iOS. - Platform.OS === 'ios' + // NativeEventEmitter only uses this parameter on iOS and macOS. + Platform.OS === 'ios' || Platform.OS === 'macos' ? (NativeReanimatedModule as unknown as NativeModule) : undefined ); From bbcb11fc3bea96476e244f5099e01ac47188b84b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Fri, 17 May 2024 12:48:32 +0200 Subject: [PATCH 2/6] set native state --- Common/cpp/SharedItems/Shareables.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Common/cpp/SharedItems/Shareables.cpp b/Common/cpp/SharedItems/Shareables.cpp index d9b2b31bbae..132234ce639 100644 --- a/Common/cpp/SharedItems/Shareables.cpp +++ b/Common/cpp/SharedItems/Shareables.cpp @@ -237,7 +237,16 @@ jsi::Value ShareableObject::toJSValue(jsi::Runtime &rt) { rt, data_[i].first.c_str(), data_[i].second->getJSValue(rt)); } if (nativeState_ != nullptr) { - obj.setNativeState(rt, nativeState_); + if (nativeStateAccess_ == NativeStateAccess::Safe) { + obj.setNativeState(rt, nativeState_); + } else if (nativeStateAccess_ == NativeStateAccess::Unknown) { + try { + obj.setNativeState(rt, nativeState_); + nativeStateAccess_ = NativeStateAccess::Safe; + } catch (...) { + nativeStateAccess_ = NativeStateAccess::Unsafe; + } + } } return obj; } From 285670ba00837e8e5669a248f7b044a81d11fdba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Fri, 17 May 2024 12:56:52 +0200 Subject: [PATCH 3/6] use lambda --- Common/cpp/Fabric/ShadowTreeCloner.cpp | 14 ++++++----- Common/cpp/SharedItems/Shareables.cpp | 34 ++++++++++++-------------- Common/cpp/SharedItems/Shareables.h | 1 + 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/Common/cpp/Fabric/ShadowTreeCloner.cpp b/Common/cpp/Fabric/ShadowTreeCloner.cpp index 80b191ef5c7..96ad97e5e94 100644 --- a/Common/cpp/Fabric/ShadowTreeCloner.cpp +++ b/Common/cpp/Fabric/ShadowTreeCloner.cpp @@ -26,7 +26,10 @@ ShadowNode::Unshared cloneShadowTreeWithNewProps( const auto props = source->getComponentDescriptor().cloneProps( propsParserContext, source->getProps(), std::move(rawProps)); - auto newChildNode = source->clone({/* .props = */ props, ShadowNodeFragment::childrenPlaceholder(), source->getState()}); + auto newChildNode = source->clone( + {/* .props = */ props, + ShadowNodeFragment::childrenPlaceholder(), + source->getState()}); for (auto it = ancestors.rbegin(); it != ancestors.rend(); ++it) { auto &parentNode = it->first.get(); @@ -50,11 +53,10 @@ ShadowNode::Unshared cloneShadowTreeWithNewProps( children[childIndex] = newChildNode; - newChildNode = parentNode.clone({ - ShadowNodeFragment::propsPlaceholder(), - std::make_shared(children), - parentNode.getState() - }); + newChildNode = parentNode.clone( + {ShadowNodeFragment::propsPlaceholder(), + std::make_shared(children), + parentNode.getState()}); } return std::const_pointer_cast(newChildNode); diff --git a/Common/cpp/SharedItems/Shareables.cpp b/Common/cpp/SharedItems/Shareables.cpp index 132234ce639..45de9343aa3 100644 --- a/Common/cpp/SharedItems/Shareables.cpp +++ b/Common/cpp/SharedItems/Shareables.cpp @@ -204,12 +204,8 @@ ShareableObject::ShareableObject(jsi::Runtime &rt, const jsi::Object &object) if (nativeStateAccess_ == NativeStateAccess::Safe) { makeNativeStateFromObject(rt, object); } else if (nativeStateAccess_ == NativeStateAccess::Unknown) { - try { - makeNativeStateFromObject(rt, object); - nativeStateAccess_ = NativeStateAccess::Safe; - } catch (...) { - nativeStateAccess_ = NativeStateAccess::Unsafe; - } + runWithNativeStateAccessProbe( + [&]() { makeNativeStateFromObject(rt, object); }); } } @@ -221,12 +217,8 @@ ShareableObject::ShareableObject( if (nativeStateAccess_ == NativeStateAccess::Safe) { makeNativeStateFromNativeStateSource(rt, nativeStateSource); } else if (nativeStateAccess_ == NativeStateAccess::Unknown) { - try { - makeNativeStateFromNativeStateSource(rt, nativeStateSource); - nativeStateAccess_ = NativeStateAccess::Safe; - } catch (...) { - nativeStateAccess_ = NativeStateAccess::Unsafe; - } + runWithNativeStateAccessProbe( + [&]() { makeNativeStateFromNativeStateSource(rt, nativeStateSource); }); } } @@ -240,12 +232,8 @@ jsi::Value ShareableObject::toJSValue(jsi::Runtime &rt) { if (nativeStateAccess_ == NativeStateAccess::Safe) { obj.setNativeState(rt, nativeState_); } else if (nativeStateAccess_ == NativeStateAccess::Unknown) { - try { - obj.setNativeState(rt, nativeState_); - nativeStateAccess_ = NativeStateAccess::Safe; - } catch (...) { - nativeStateAccess_ = NativeStateAccess::Unsafe; - } + runWithNativeStateAccessProbe( + [&]() { obj.setNativeState(rt, nativeState_); }); } } return obj; @@ -270,6 +258,16 @@ void ShareableObject::makeNativeStateFromNativeStateSource( } } +void ShareableObject::runWithNativeStateAccessProbe( + std::function block) { + try { + block(); + nativeStateAccess_ = NativeStateAccess::Safe; + } catch (...) { + nativeStateAccess_ = NativeStateAccess::Unsafe; + } +} + jsi::Value ShareableHostObject::toJSValue(jsi::Runtime &rt) { return jsi::Object::createFromHostObject(rt, hostObject_); } diff --git a/Common/cpp/SharedItems/Shareables.h b/Common/cpp/SharedItems/Shareables.h index a9a5ab7164e..45775b2d3eb 100644 --- a/Common/cpp/SharedItems/Shareables.h +++ b/Common/cpp/SharedItems/Shareables.h @@ -210,6 +210,7 @@ class ShareableObject : public Shareable { // are not implemented and they throw the error. Therefore // we need to keep track of the access mode to the native state. static std::atomic nativeStateAccess_; + static void runWithNativeStateAccessProbe(std::function &&block); }; class ShareableHostObject : public Shareable { From bd7fbcd61606c327617f26360b1362a9191bfc2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Fri, 17 May 2024 14:11:30 +0200 Subject: [PATCH 4/6] fix type --- Common/cpp/SharedItems/Shareables.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Common/cpp/SharedItems/Shareables.cpp b/Common/cpp/SharedItems/Shareables.cpp index 45de9343aa3..a98f181ef27 100644 --- a/Common/cpp/SharedItems/Shareables.cpp +++ b/Common/cpp/SharedItems/Shareables.cpp @@ -259,7 +259,7 @@ void ShareableObject::makeNativeStateFromNativeStateSource( } void ShareableObject::runWithNativeStateAccessProbe( - std::function block) { + std::function &&block) { try { block(); nativeStateAccess_ = NativeStateAccess::Safe; From ddc46d1604af77b2801767f47bcda573d6225614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Mon, 27 May 2024 14:07:27 +0200 Subject: [PATCH 5/6] review changes --- Common/cpp/Fabric/ShadowTreeCloner.cpp | 14 ++++++-------- Common/cpp/SharedItems/Shareables.cpp | 22 +++++++++++----------- Common/cpp/SharedItems/Shareables.h | 10 +++++----- package.json | 4 ++-- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/Common/cpp/Fabric/ShadowTreeCloner.cpp b/Common/cpp/Fabric/ShadowTreeCloner.cpp index 96ad97e5e94..80b191ef5c7 100644 --- a/Common/cpp/Fabric/ShadowTreeCloner.cpp +++ b/Common/cpp/Fabric/ShadowTreeCloner.cpp @@ -26,10 +26,7 @@ ShadowNode::Unshared cloneShadowTreeWithNewProps( const auto props = source->getComponentDescriptor().cloneProps( propsParserContext, source->getProps(), std::move(rawProps)); - auto newChildNode = source->clone( - {/* .props = */ props, - ShadowNodeFragment::childrenPlaceholder(), - source->getState()}); + auto newChildNode = source->clone({/* .props = */ props, ShadowNodeFragment::childrenPlaceholder(), source->getState()}); for (auto it = ancestors.rbegin(); it != ancestors.rend(); ++it) { auto &parentNode = it->first.get(); @@ -53,10 +50,11 @@ ShadowNode::Unshared cloneShadowTreeWithNewProps( children[childIndex] = newChildNode; - newChildNode = parentNode.clone( - {ShadowNodeFragment::propsPlaceholder(), - std::make_shared(children), - parentNode.getState()}); + newChildNode = parentNode.clone({ + ShadowNodeFragment::propsPlaceholder(), + std::make_shared(children), + parentNode.getState() + }); } return std::const_pointer_cast(newChildNode); diff --git a/Common/cpp/SharedItems/Shareables.cpp b/Common/cpp/SharedItems/Shareables.cpp index a98f181ef27..10fa9ce697b 100644 --- a/Common/cpp/SharedItems/Shareables.cpp +++ b/Common/cpp/SharedItems/Shareables.cpp @@ -4,7 +4,7 @@ using namespace facebook; namespace reanimated { -std::atomic ShareableObject::nativeStateAccess_ = +std::atomic ShareableObject::hasNativeState_ = NativeStateAccess::Unknown; jsi::Function getValueUnpacker(jsi::Runtime &rt) { @@ -201,9 +201,9 @@ ShareableObject::ShareableObject(jsi::Runtime &rt, const jsi::Object &object) auto value = extractShareableOrThrow(rt, object.getProperty(rt, key)); data_.emplace_back(key.utf8(rt), value); } - if (nativeStateAccess_ == NativeStateAccess::Safe) { + if (hasNativeState_ == HasNativeState::Yes) { makeNativeStateFromObject(rt, object); - } else if (nativeStateAccess_ == NativeStateAccess::Unknown) { + } else if (hasNativeState_ == HasNativeState::Unknown) { runWithNativeStateAccessProbe( [&]() { makeNativeStateFromObject(rt, object); }); } @@ -214,9 +214,9 @@ ShareableObject::ShareableObject( const jsi::Object &object, const jsi::Value &nativeStateSource) : ShareableObject(rt, object) { - if (nativeStateAccess_ == NativeStateAccess::Safe) { + if (hasNativeState_ == HasNativeState::Yes) { makeNativeStateFromNativeStateSource(rt, nativeStateSource); - } else if (nativeStateAccess_ == NativeStateAccess::Unknown) { + } else if (hasNativeState_ == HasNativeState::Unknown) { runWithNativeStateAccessProbe( [&]() { makeNativeStateFromNativeStateSource(rt, nativeStateSource); }); } @@ -229,9 +229,9 @@ jsi::Value ShareableObject::toJSValue(jsi::Runtime &rt) { rt, data_[i].first.c_str(), data_[i].second->getJSValue(rt)); } if (nativeState_ != nullptr) { - if (nativeStateAccess_ == NativeStateAccess::Safe) { + if (hasNativeState_ == HasNativeState::Yes) { obj.setNativeState(rt, nativeState_); - } else if (nativeStateAccess_ == NativeStateAccess::Unknown) { + } else if (hasNativeState_ == HasNativeState::Unknown) { runWithNativeStateAccessProbe( [&]() { obj.setNativeState(rt, nativeState_); }); } @@ -244,7 +244,7 @@ void ShareableObject::makeNativeStateFromObject( const jsi::Object &object) { if (object.hasNativeState(rt)) { nativeState_ = object.getNativeState(rt); - nativeStateAccess_ = NativeStateAccess::Safe; + hasNativeState_ = HasNativeState::Yes; } } @@ -254,7 +254,7 @@ void ShareableObject::makeNativeStateFromNativeStateSource( if (nativeStateSource.isObject() && nativeStateSource.asObject(rt).hasNativeState(rt)) { nativeState_ = nativeStateSource.asObject(rt).getNativeState(rt); - nativeStateAccess_ = NativeStateAccess::Safe; + hasNativeState_ = HasNativeState::Yes; } } @@ -262,9 +262,9 @@ void ShareableObject::runWithNativeStateAccessProbe( std::function &&block) { try { block(); - nativeStateAccess_ = NativeStateAccess::Safe; + hasNativeState_ = HasNativeState::Yes; } catch (...) { - nativeStateAccess_ = NativeStateAccess::Unsafe; + hasNativeState_ = HasNativeState::No; } } diff --git a/Common/cpp/SharedItems/Shareables.h b/Common/cpp/SharedItems/Shareables.h index 45775b2d3eb..9f6ba4e8bca 100644 --- a/Common/cpp/SharedItems/Shareables.h +++ b/Common/cpp/SharedItems/Shareables.h @@ -179,10 +179,10 @@ class ShareableArray : public Shareable { std::vector> data_; }; -enum class NativeStateAccess { +enum class HasNativeState { Unknown, - Safe, - Unsafe, + Yes, + No, }; class ShareableObject : public Shareable { @@ -209,8 +209,8 @@ class ShareableObject : public Shareable { // the methods that refer to the native state of an object // are not implemented and they throw the error. Therefore // we need to keep track of the access mode to the native state. - static std::atomic nativeStateAccess_; - static void runWithNativeStateAccessProbe(std::function &&block); + static std::atomic hasNativeState_; + static void runWithNativeStateProbe(std::function &&block); }; class ShareableHostObject : public Shareable { diff --git a/package.json b/package.json index cb2dcc73c5e..f14ab56e8f3 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,8 @@ "format:plugin": "cd plugin && yarn format && cd ..", "format:java": "node ./scripts/format-java.js", "format:ios": "find apple/ -iname \"*.h\" -o -iname \"*.m\" -o -iname \"*.mm\" -o -iname \"*.cpp\" | xargs clang-format -i --Werror", - "format:android": "find android/src/ -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i", - "format:common": "find Common/ -iname \"*.h\" -o -iname \"*.cpp\" | xargs clang-format -i", + "format:android": "find android/src/ -iname *.h -o -iname *.cpp | xargs clang-format -i", + "format:common": "find Common/ -iname *.h -o -iname *.cpp | xargs clang-format -i", "format:docs": "cd docs && yarn format && cd ..", "find-unused-code:js": "yarn ts-prune --ignore \"index|.web.\" --error ", "type:check:src": "yarn tsc --noEmit", From fff280c91aecde1ef54e0975f959aa05d407aba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Mon, 27 May 2024 14:33:23 +0200 Subject: [PATCH 6/6] renamve v2 --- Common/cpp/SharedItems/Shareables.cpp | 32 +++++++++++++-------------- Common/cpp/SharedItems/Shareables.h | 4 ++-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Common/cpp/SharedItems/Shareables.cpp b/Common/cpp/SharedItems/Shareables.cpp index 10fa9ce697b..da7d17dbe9f 100644 --- a/Common/cpp/SharedItems/Shareables.cpp +++ b/Common/cpp/SharedItems/Shareables.cpp @@ -4,8 +4,8 @@ using namespace facebook; namespace reanimated { -std::atomic ShareableObject::hasNativeState_ = - NativeStateAccess::Unknown; +std::atomic ShareableObject::isNativeStateImplemented_ = + IsNativeStateImplemented::Unknown; jsi::Function getValueUnpacker(jsi::Runtime &rt) { auto valueUnpacker = rt.global().getProperty(rt, "__valueUnpacker"); @@ -201,10 +201,10 @@ ShareableObject::ShareableObject(jsi::Runtime &rt, const jsi::Object &object) auto value = extractShareableOrThrow(rt, object.getProperty(rt, key)); data_.emplace_back(key.utf8(rt), value); } - if (hasNativeState_ == HasNativeState::Yes) { + if (isNativeStateImplemented_ == IsNativeStateImplemented::Yes) { makeNativeStateFromObject(rt, object); - } else if (hasNativeState_ == HasNativeState::Unknown) { - runWithNativeStateAccessProbe( + } else if (isNativeStateImplemented_ == IsNativeStateImplemented::Unknown) { + runWithNativeStateProbe( [&]() { makeNativeStateFromObject(rt, object); }); } } @@ -214,10 +214,10 @@ ShareableObject::ShareableObject( const jsi::Object &object, const jsi::Value &nativeStateSource) : ShareableObject(rt, object) { - if (hasNativeState_ == HasNativeState::Yes) { + if (isNativeStateImplemented_ == IsNativeStateImplemented::Yes) { makeNativeStateFromNativeStateSource(rt, nativeStateSource); - } else if (hasNativeState_ == HasNativeState::Unknown) { - runWithNativeStateAccessProbe( + } else if (isNativeStateImplemented_ == IsNativeStateImplemented::Unknown) { + runWithNativeStateProbe( [&]() { makeNativeStateFromNativeStateSource(rt, nativeStateSource); }); } } @@ -229,10 +229,10 @@ jsi::Value ShareableObject::toJSValue(jsi::Runtime &rt) { rt, data_[i].first.c_str(), data_[i].second->getJSValue(rt)); } if (nativeState_ != nullptr) { - if (hasNativeState_ == HasNativeState::Yes) { + if (isNativeStateImplemented_ == IsNativeStateImplemented::Yes) { obj.setNativeState(rt, nativeState_); - } else if (hasNativeState_ == HasNativeState::Unknown) { - runWithNativeStateAccessProbe( + } else if (isNativeStateImplemented_ == IsNativeStateImplemented::Unknown) { + runWithNativeStateProbe( [&]() { obj.setNativeState(rt, nativeState_); }); } } @@ -244,7 +244,7 @@ void ShareableObject::makeNativeStateFromObject( const jsi::Object &object) { if (object.hasNativeState(rt)) { nativeState_ = object.getNativeState(rt); - hasNativeState_ = HasNativeState::Yes; + isNativeStateImplemented_ = IsNativeStateImplemented::Yes; } } @@ -254,17 +254,17 @@ void ShareableObject::makeNativeStateFromNativeStateSource( if (nativeStateSource.isObject() && nativeStateSource.asObject(rt).hasNativeState(rt)) { nativeState_ = nativeStateSource.asObject(rt).getNativeState(rt); - hasNativeState_ = HasNativeState::Yes; + isNativeStateImplemented_ = IsNativeStateImplemented::Yes; } } -void ShareableObject::runWithNativeStateAccessProbe( +void ShareableObject::runWithNativeStateProbe( std::function &&block) { try { block(); - hasNativeState_ = HasNativeState::Yes; + isNativeStateImplemented_ = IsNativeStateImplemented::Yes; } catch (...) { - hasNativeState_ = HasNativeState::No; + isNativeStateImplemented_ = IsNativeStateImplemented::No; } } diff --git a/Common/cpp/SharedItems/Shareables.h b/Common/cpp/SharedItems/Shareables.h index 9f6ba4e8bca..815b0decf94 100644 --- a/Common/cpp/SharedItems/Shareables.h +++ b/Common/cpp/SharedItems/Shareables.h @@ -179,7 +179,7 @@ class ShareableArray : public Shareable { std::vector> data_; }; -enum class HasNativeState { +enum class IsNativeStateImplemented { Unknown, Yes, No, @@ -209,7 +209,7 @@ class ShareableObject : public Shareable { // the methods that refer to the native state of an object // are not implemented and they throw the error. Therefore // we need to keep track of the access mode to the native state. - static std::atomic hasNativeState_; + static std::atomic isNativeStateImplemented_; static void runWithNativeStateProbe(std::function &&block); };