-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fix "Not implemented" exception on JSC #6028
Changes from 4 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<NativeStateAccess> 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<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 (nativeStateAccess_ == NativeStateAccess::Safe) { | ||
makeNativeStateFromObject(rt, object); | ||
} else if (nativeStateAccess_ == NativeStateAccess::Unknown) { | ||
runWithNativeStateAccessProbe( | ||
[&]() { 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 (nativeStateAccess_ == NativeStateAccess::Safe) { | ||
makeNativeStateFromNativeStateSource(rt, nativeStateSource); | ||
} else if (nativeStateAccess_ == NativeStateAccess::Unknown) { | ||
runWithNativeStateAccessProbe( | ||
[&]() { 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 (nativeStateAccess_ == NativeStateAccess::Safe) { | ||
obj.setNativeState(rt, nativeState_); | ||
} else if (nativeStateAccess_ == NativeStateAccess::Unknown) { | ||
runWithNativeStateAccessProbe( | ||
[&]() { obj.setNativeState(rt, nativeState_); }); | ||
} | ||
} | ||
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; | ||
} | ||
} | ||
|
||
void ShareableObject::runWithNativeStateAccessProbe( | ||
std::function<void()> &&block) { | ||
try { | ||
block(); | ||
nativeStateAccess_ = NativeStateAccess::Safe; | ||
} catch (...) { | ||
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? 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 🫣 |
||
nativeStateAccess_ = NativeStateAccess::Unsafe; | ||
} | ||
} | ||
|
||
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
setNativeState
function handled by it.