hermes icon indicating copy to clipboard operation
hermes copied to clipboard

Makes HermesRuntimeImpl::isArray proxy-compatible

Open dorentus opened this issue 1 year ago • 4 comments

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

dorentus avatar Aug 21 '24 07:08 dorentus

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 avatar Aug 21 '24 07:08 facebook-github-bot

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

facebook-github-bot avatar Aug 21 '24 08:08 facebook-github-bot

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.

neildhar avatar Aug 21 '24 16:08 neildhar

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.

dorentus avatar Aug 29 '24 07:08 dorentus

Closing for now, since no progress. Please feel free to re-open when there are updates.

tmikov avatar Dec 16 '24 23:12 tmikov