Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

tjzel
Copy link
Contributor

@tjzel tjzel commented May 17, 2024

Summary

In some implementations of JSC, at least react-native-macos here native state methods are not implemented and they are throwing errors. This PR adds logic that will go around this issue, by probing the hasNativeState method to determine whether it should use that.

Fixes #5974

Test plan

  • MacOS example builds now.
  • Check if all "not implemented" methods in the link are safely accessed.

@tjzel tjzel requested review from tomekzaw and piaskowyk May 17, 2024 10:49
Copy link
Member

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just call hasNativeState in try/catch block?

@tjzel
Copy link
Contributor Author

tjzel commented May 20, 2024

@tomekzaw It's for performance. Why would we surround it with the try-catch on every call, since we will know everything after the first call.

@tomekzaw
Copy link
Member

tomekzaw commented May 20, 2024

@tjzel I wouldn't worry much about performance in this case, C++ try/catch blocks are quite fast.

SInce we know that some versions react-native-macos don't support jsi::NativeState when using JSC, why can't we just use #ifdefs instead?

Also, the names nativeStateAccess, AccessProbe, Safe and Unsafe are a bit overwhelming, how about simply supportsNativeState with Yes and No instead?

Finally, I don't like the fact that some unrelated files have been formatted, can we move them to a separate PR?

@mrousavy
Copy link
Contributor

Sorry about that, I added jsi::NativeState here.. I didn't test on MacOS / JSC.

@tomekzaw
Copy link
Member

@mrousavy No worries

@tjzel
Copy link
Contributor Author

tjzel commented May 27, 2024

@tomekzaw I read an interesting article about try/catch blocks performance from which it seems that try blocks are fast but the catch ones are expensive. Since these calls would constantly throw when nativeState is not implemented, I'd rather keep the current approach.

Regarding #ifdef I don't know how would we do that. I don't think there's any flag that would tell us if nativeState is implemented on a given engine.

I'll revert formatting and make the names more approachable.

void ShareableObject::runWithNativeStateProbe(
std::function<void()> &&block) {
try {
block();
Copy link
Member

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_);?

try {
block();
isNativeStateImplemented_ = IsNativeStateImplemented::Yes;
} catch (...) {
Copy link
Member

Choose a reason for hiding this comment

The 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception in Hostfunction: not implemented
4 participants