Skip to content

Commit

Permalink
Makes HermesRuntimeImpl::isArray proxy-compatible
Browse files Browse the repository at this point in the history
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](26840ed#diff-059b8f2fcb0235b35582efc591e065bd8caa898c3ea2ef2a3e453bff97584e4dR1575) and makes `isArray` proxy-compatible.
  • Loading branch information
dorentus committed Aug 21, 2024
1 parent 73cb666 commit 7800479
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion API/hermes/hermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "hermes/VM/JSArrayBuffer.h"
#include "hermes/VM/JSLib.h"
#include "hermes/VM/JSLib/RuntimeJSONUtils.h"
#include "hermes/VM/JSObject.h"
#include "hermes/VM/JSProxy.h"
#include "hermes/VM/NativeState.h"
#include "hermes/VM/Operations.h"
#include "hermes/VM/Profiler/CodeCoverageProfiler.h"
Expand Down Expand Up @@ -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());
while (true) {
if (vm::vmisa<vm::JSArray>(jsobj)) {
return true;
}
if (LLVM_LIKELY(!jsobj->isProxyObject())) {
return false;
}
if (vm::JSProxy::isRevoked(jsobj, runtime_)) {
return false;
}
jsobj = vm::JSProxy::getTarget(jsobj, runtime_).get();
assert(jsobj && "target of non-revoked Proxy is null");
}
}

bool HermesRuntimeImpl::isArrayBuffer(const jsi::Object &obj) const {
Expand Down

0 comments on commit 7800479

Please sign in to comment.