Skip to content

Commit

Permalink
Add HermesBuiltin applyArguments
Browse files Browse the repository at this point in the history
Summary:
We'll use this new builtin for a fast path when applying with
`arguments`.

Reviewed By: tmikov

Differential Revision: D65348304

fbshipit-source-id: ac45d2b4de048ea0ed22224f6b8ca8d1cfe7a71b
  • Loading branch information
avp authored and facebook-github-bot committed Nov 14, 2024
1 parent aabb385 commit f0bfba1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/hermes/FrontEndDefs/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ PRIVATE_BUILTIN(copyDataProperties)
PRIVATE_BUILTIN(copyRestArgs)
PRIVATE_BUILTIN(arraySpread)
PRIVATE_BUILTIN(apply)
PRIVATE_BUILTIN(applyArguments)
PRIVATE_BUILTIN(exportAll)
PRIVATE_BUILTIN(exponentiationOperator)
PRIVATE_BUILTIN(initRegexNamedGroups)
Expand Down
1 change: 1 addition & 0 deletions include/hermes/VM/NativeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ NATIVE_FUNCTION(hermesBuiltinCopyDataProperties)
NATIVE_FUNCTION(hermesBuiltinCopyRestArgs)
NATIVE_FUNCTION(hermesBuiltinArraySpread)
NATIVE_FUNCTION(hermesBuiltinApply)
NATIVE_FUNCTION(hermesBuiltinApplyArguments)
NATIVE_FUNCTION(hermesBuiltinEnsureObject)
NATIVE_FUNCTION(hermesBuiltinGetMethod)
NATIVE_FUNCTION(hermesBuiltinExponentiate)
Expand Down
1 change: 1 addition & 0 deletions include/hermes/VM/PredefinedStrings.def
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ STR(throwReferenceError, "throwReferenceError")
STR(copyDataProperties, "copyDataProperties")
STR(copyRestArgs, "copyRestArgs")
STR(arraySpread, "arraySpread")
STR(applyArguments, "applyArguments")
STR(exportAll, "exportAll")
STR(exponentiationOperator, "exponentiationOperator")
STR(initRegexNamedGroups, "initRegexNamedGroups")
Expand Down
42 changes: 42 additions & 0 deletions lib/VM/JSLib/HermesBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,43 @@ hermesBuiltinApply(void *, Runtime &runtime, NativeArgs args) {
return res->getHermesValue();
}

/// \code
/// HermesBuiltin.applyArguments = function(fn, thisVal) {}
/// /endcode
/// Faster version of Function.prototype.apply which copies the arguments
/// from the caller to the callee.
CallResult<HermesValue>
hermesBuiltinApplyArguments(void *, Runtime &runtime, NativeArgs args) {
// Copy 'arguments' from the caller's stack, then call the callee.

Handle<Callable> fn = args.dyncastArg<Callable>(0);
if (LLVM_UNLIKELY(!fn)) {
return runtime.raiseTypeErrorForValue(
args.getArgHandle(0), " is not a function");
}

// Obtain the caller's stack frame.
auto frames = runtime.getStackFrames();
auto it = frames.begin();
++it;
// Check for the extremely unlikely case where there is no caller frame.
if (LLVM_UNLIKELY(it == frames.end()))
return HermesValue::encodeUndefinedValue();

uint32_t argCount = it->getArgCount();

ScopedNativeCallFrame newFrame{runtime, argCount, *fn, false, args.getArg(1)};
if (LLVM_UNLIKELY(newFrame.overflowed())) {
return runtime.raiseStackOverflow(Runtime::StackOverflowKind::NativeStack);
}

for (uint32_t i = 0; i < argCount; ++i) {
newFrame->getArgRef(i) = it->getArgRef(i);
}

return Callable::call(fn, runtime).toCallResultHermesValue();
}

/// HermesBuiltin.exportAll(exports, source) will copy exported named
/// properties from `source` to `exports`, defining them on `exports` as
/// non-configurable.
Expand Down Expand Up @@ -836,6 +873,11 @@ void createHermesBuiltins(
hermesBuiltinArraySpread,
2);
defineInternMethod(B::HermesBuiltin_apply, P::apply, hermesBuiltinApply, 2);
defineInternMethod(
B::HermesBuiltin_applyArguments,
P::apply,
hermesBuiltinApplyArguments,
2);
defineInternMethod(
B::HermesBuiltin_exportAll, P::exportAll, hermesBuiltinExportAll);
defineInternMethod(
Expand Down

0 comments on commit f0bfba1

Please sign in to comment.