-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix "Not implemented" exception on JSC #6028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
adec927
bbcb11f
285670b
bd7fbcd
69b72db
ddc46d1
fff280c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,9 @@ using namespace facebook; | |
|
|
||
| namespace reanimated { | ||
|
|
||
| std::atomic<IsNativeStateImplemented> ShareableObject::isNativeStateImplemented_ = | ||
| IsNativeStateImplemented::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<ShareableString>(value.getSymbol(rt).toString(rt)); | ||
| } else { | ||
|
|
@@ -198,8 +201,11 @@ 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 (isNativeStateImplemented_ == IsNativeStateImplemented::Yes) { | ||
| makeNativeStateFromObject(rt, object); | ||
| } else if (isNativeStateImplemented_ == IsNativeStateImplemented::Unknown) { | ||
| runWithNativeStateProbe( | ||
| [&]() { makeNativeStateFromObject(rt, object); }); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -208,9 +214,11 @@ 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 (isNativeStateImplemented_ == IsNativeStateImplemented::Yes) { | ||
| makeNativeStateFromNativeStateSource(rt, nativeStateSource); | ||
| } else if (isNativeStateImplemented_ == IsNativeStateImplemented::Unknown) { | ||
| runWithNativeStateProbe( | ||
| [&]() { makeNativeStateFromNativeStateSource(rt, nativeStateSource); }); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -221,11 +229,45 @@ 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 (isNativeStateImplemented_ == IsNativeStateImplemented::Yes) { | ||
| obj.setNativeState(rt, nativeState_); | ||
| } else if (isNativeStateImplemented_ == IsNativeStateImplemented::Unknown) { | ||
| runWithNativeStateProbe( | ||
| [&]() { obj.setNativeState(rt, nativeState_); }); | ||
| } | ||
| } | ||
| return obj; | ||
| } | ||
|
|
||
| void ShareableObject::makeNativeStateFromObject( | ||
| jsi::Runtime &rt, | ||
| const jsi::Object &object) { | ||
| if (object.hasNativeState(rt)) { | ||
| nativeState_ = object.getNativeState(rt); | ||
| isNativeStateImplemented_ = IsNativeStateImplemented::Yes; | ||
| } | ||
| } | ||
|
|
||
| void ShareableObject::makeNativeStateFromNativeStateSource( | ||
| jsi::Runtime &rt, | ||
| const jsi::Value &nativeStateSource) { | ||
| if (nativeStateSource.isObject() && | ||
| nativeStateSource.asObject(rt).hasNativeState(rt)) { | ||
| nativeState_ = nativeStateSource.asObject(rt).getNativeState(rt); | ||
| isNativeStateImplemented_ = IsNativeStateImplemented::Yes; | ||
| } | ||
| } | ||
|
|
||
| void ShareableObject::runWithNativeStateProbe( | ||
| std::function<void()> &&block) { | ||
| try { | ||
| block(); | ||
| isNativeStateImplemented_ = IsNativeStateImplemented::Yes; | ||
| } catch (...) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this 'Not implemented' exception have a specific type that we can implicitly catch here?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to search through all of current implementations of it 🫣 |
||
| isNativeStateImplemented_ = IsNativeStateImplemented::No; | ||
| } | ||
| } | ||
|
|
||
| jsi::Value ShareableHostObject::toJSValue(jsi::Runtime &rt) { | ||
| return jsi::Object::createFromHostObject(rt, hostObject_); | ||
| } | ||
|
|
@@ -270,12 +312,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<std::mutex> lock(initializationMutex_); | ||
| if (remoteValue_ == nullptr) { | ||
| remoteValue_ = std::move(value); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we need to pass an lambda here instead of just execute
obj.setNativeState(rt, nativeState_);?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have more than just
setNativeStatefunction handled by it.