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

Makes HermesRuntimeImpl::isArray proxy-compatible #1489

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

Conversation

dorentus
Copy link

@dorentus dorentus commented Aug 21, 2024

When using hermes engine, dynamicFromValue in React Native calls isArray here to check if the input object is an array.

Consider this piece of code:

const list = ['a', 'b', 'c', 'd'];
const proxyed_list = new Proxy(list, {});

The proxyed_list behaves exactly the same as list in Javascript. But when been passed to native and converted to folly::dynamic by dynamicFromValue, proxyed_list will be converted to {"0": "a", "1": "b", "2": "c", "3": "d"} but not the expected ["a", "b", "c", "d"].

This patch implements similar routines in commit 26840ed441d614a07809c612521074ca9544086c and makes isArray proxy-compatible.

Summary

Test Plan

@facebook-github-bot
Copy link
Contributor

Hi @dorentus!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Aug 21, 2024
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@dorentus dorentus force-pushed the patch-1 branch 2 times, most recently from 7800479 to 8b32930 Compare August 21, 2024 13:21
@neildhar
Copy link
Contributor

Hey @dorentus, thanks for submitting this. This is a little tricky because Hermes today assumes that any jsi::Array must be an instance of a vm::JSArray. Supporting this would require changing this assumption throughout hermes.cpp. Given that the API is documented to be equivalent to Array.isArray(), that is probably the right thing to do, but we'll look into it a bit first.

Copy link
Contributor

@neildhar neildhar left a comment

Choose a reason for hiding this comment

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

To support this properly, we should fix all users of jsi::Array in this file to no longer imply that the underlying object is a vm::JSArray. In most cases, this should just be a matter of using handle() instead of arrayHandle(). But for length we will need to have a fast path that follows the current behaviour, and a slow path that does a regular property lookup.

@@ -1996,7 +1998,20 @@ void HermesRuntimeImpl::setPropertyValue(
}

bool HermesRuntimeImpl::isArray(const jsi::Object &obj) const {
return vm::vmisa<vm::JSArray>(phv(obj));
vm::JSObject *jsobj = static_cast<vm::JSObject *>(phv(obj).getPointer());
Copy link
Contributor

Choose a reason for hiding this comment

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

We have an isArray in Operations.h that you can call into that will provide the right functionality here.

Copy link
Author

Choose a reason for hiding this comment

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

Code updated.

Question: Should I call runtime_.clearThrownValue() on ExecutionStatus::EXCEPTION before returning false?

Copy link
Contributor

Choose a reason for hiding this comment

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

You should use checkStatus to rethrow the exception as a C++ exception

Copy link
Author

Choose a reason for hiding this comment

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

checkStatus isn't const thus cannot be used here without modification...

Copy link
Author

Choose a reason for hiding this comment

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

It should be safe to remove the constness and call checkStatus here. Code updated.

When using hermes engine, [`dynamicFromValue` in React Native](https://github.com/facebook/hermes/blob/main/API/jsi/jsi/JSIDynamic.cpp#L166) calls `isArray` here to check if the input object is an array.

Consider this piece of code:
```js
const list = ['a', 'b', 'c', 'd'];
const proxyed_list = new Proxy(list, {});
```

The `proxyed_list` behaves exactly the same as list in Javascript. But when been passed to native and converted to `folly::dynamic` by `dynamicFromValue`, `proxyed_list` will be converted to `{"0": "a", "1": "b", "2": "c", "3": "d"}` but not the expected `["a", "b", "c", "d"]`.

This patch implements similar routines in commit [26840ed](facebook@26840ed#diff-059b8f2fcb0235b35582efc591e065bd8caa898c3ea2ef2a3e453bff97584e4dR1575) and makes `isArray` proxy-compatible.
`runtime_` is modified on error and restored in `checkStatus`, So it
should be safe to remove the constness and call `checkStatus` here.
@dorentus
Copy link
Author

https://github.com/facebook/react-native/blame/321f7dbcadb78dede9048500ab8abe86af863061/packages/react-native/ReactCommon/jsc/JSCRuntime.cpp#L993

JSC used to implement isArray by calling Array.isArray when _JSC_FAST_IS_ARRAY is defined, but now it only calls JSValueIsArray.

And JSValueIsArray behaves the same as HermesRuntimeImpl::isArray and returns false for new Proxy([], {}).

So yes, changes suggested by this PR might cause more problems than expected.


As for the dynamicFromValue problem, I'm trying the Array.isArray approach in my local copy of code to see if is has any performance glitches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants