Skip to content

Commit

Permalink
Add GetByIndex in native backend
Browse files Browse the repository at this point in the history
Reviewed By: tmikov

Differential Revision: D61743977

fbshipit-source-id: 71c8e14ed13a70238774f2ef1fb9dc4bb871d1f6
  • Loading branch information
avp authored and facebook-github-bot committed Aug 28, 2024
1 parent 6bfa3a0 commit 63eb199
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/hermes/VM/static_h.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ SHERMES_EXPORT SHLegacyValue _sh_ljs_get_by_val_rjs(
SHRuntime *shr,
SHLegacyValue *source,
SHLegacyValue *key);
SHERMES_EXPORT SHLegacyValue
_sh_ljs_get_by_index_rjs(SHRuntime *shr, SHLegacyValue *source, uint8_t key);

/// Put an enumerable property.
SHERMES_EXPORT void _sh_ljs_put_own_by_val(
Expand Down
9 changes: 9 additions & 0 deletions lib/BCGen/SH/SH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,15 @@ class InstrGen {
genStringConstIC(LS) << ");\n";
return;
}
// If the prop is a uint8_t constant, generate the special bytecode.
if (auto *litNum = llvh::dyn_cast<LiteralNumber>(inst.getProperty());
litNum && litNum->isUInt8Representible()) {
os_ << "_sh_ljs_get_by_index_rjs(shr,&";
generateRegister(*inst.getObject());
os_ << ", ";
os_ << litNum->asUInt8() << ");\n";
return;
}
os_ << "_sh_ljs_get_by_val_rjs(shr,&";
generateRegister(*inst.getObject());
os_ << ", &";
Expand Down
33 changes: 33 additions & 0 deletions lib/VM/StaticH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,39 @@ extern "C" SHLegacyValue _sh_ljs_get_by_val_rjs(
return res->getHermesValue();
}

extern "C" SHLegacyValue
_sh_ljs_get_by_index_rjs(SHRuntime *shr, SHLegacyValue *source, uint8_t key) {
Handle<> sourceHandle{toPHV(source)};
Runtime &runtime = getRuntime(shr);
if (LLVM_LIKELY(sourceHandle->isObject())) {
Handle<JSObject> objHandle = Handle<JSObject>::vmcast(sourceHandle);
if (LLVM_LIKELY(objHandle->hasFastIndexProperties())) {
auto ourValue = createPseudoHandle(JSObject::getOwnIndexed(
createPseudoHandle(*objHandle), runtime, key));
if (LLVM_LIKELY(!ourValue->isEmpty())) {
return ourValue.getHermesValue();
}
}
}

// Otherwise...
// This is the "slow path".
auto res = [&]() {
struct : public Locals {
PinnedValue<> key;
} lv;
LocalsRAII lraii{runtime, &lv};

lv.key = HermesValue::encodeTrustedNumberValue(key);
return Interpreter::getByValTransient_RJS(runtime, sourceHandle, lv.key);
}();

if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
_sh_throw_current(shr);
}
return res->getHermesValue();
}

extern "C" SHLegacyValue _sh_catch(
SHRuntime *shr,
SHLocals *locals,
Expand Down

0 comments on commit 63eb199

Please sign in to comment.