Skip to content

Commit

Permalink
Extract exponentiation operation from mathPow function.
Browse files Browse the repository at this point in the history
Summary:
In anticipation of adding support for `**`, we move the exponentiation
logic out of `mathPow` and place it in `expOp`, where it will be used
to avoid duplicating logic in the interpreter loop.

Reviewed By: dulinriley

Differential Revision: D16628558

fbshipit-source-id: 83e3cfd3c41525498b2e13e31f2164f5d1947cfc
  • Loading branch information
avp authored and facebook-github-bot committed Aug 28, 2019
1 parent f313c1b commit c0d849e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
17 changes: 17 additions & 0 deletions include/hermes/VM/Operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,23 @@ bool isPrimitive(HermesValue val);
CallResult<HermesValue>
addOp_RJS(Runtime *runtime, Handle<> xHandle, Handle<> yHandle);

/// ES9.0 12.6.4
inline double expOp(double x, double y) {
constexpr double nan = std::numeric_limits<double>::quiet_NaN();

// Handle special cases that std::pow handles differently.
if (std::isnan(y)) {
return nan;
} else if (y == 0) {
return 1;
} else if (std::abs(x) == 1 && std::isinf(y)) {
return nan;
}

// std::pow handles the other edge cases as the ES9.0 spec requires.
return std::pow(x, y);
}

/// ES5.1 7.2
inline bool isWhiteSpaceChar(char16_t c) {
return c == u'\u0009' || c == u'\u000B' || c == u'\u000C' || c == u'\u0020' ||
Expand Down
20 changes: 2 additions & 18 deletions lib/VM/JSLib/Math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ CallResult<HermesValue> mathMin(void *, Runtime *runtime, NativeArgs args) {
return HermesValue::encodeDoubleValue(result);
}

// ES5.1 15.8.2.13
// ES9.0 20.2.2.26
CallResult<HermesValue> mathPow(void *, Runtime *runtime, NativeArgs args) {
auto res = toNumber_RJS(runtime, args.getArgHandle(runtime, 0));
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
Expand All @@ -193,23 +193,7 @@ CallResult<HermesValue> mathPow(void *, Runtime *runtime, NativeArgs args) {
}
const double y = res->getNumber();

const double nan = std::numeric_limits<double>::quiet_NaN();

double result;

// Handle special cases that std::pow handles differently.
if (std::isnan(y)) {
result = nan;
} else if (y == 0) {
result = 1;
} else if (std::abs(x) == 1 && std::isinf(y)) {
result = nan;
} else {
// std::pow handles the other edge cases as ES5.1 specifies.
result = std::pow(x, y);
}

return HermesValue::encodeNumberValue(result);
return HermesValue::encodeNumberValue(expOp(x, y));
}

// ES5.1 15.8.2.14
Expand Down

0 comments on commit c0d849e

Please sign in to comment.