Skip to content

Commit 2ca5256

Browse files
committed
eval pow function once instead of using eval on every call
1 parent bf283df commit 2ca5256

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

BigInteger.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,13 @@ var bigInt = (function (undefined) {
647647
return y;
648648
};
649649
SmallInteger.prototype.pow = BigInteger.prototype.pow;
650+
651+
var pow;
652+
if (supportsNativeBigInt) {
653+
// forced to use eval because ** is a syntax error on pre-ECMAScript2017 environments.
654+
pow = eval("(a,b)=>a**b");
655+
}
656+
650657
NativeBigInt.prototype.pow = function (v) {
651658
var n = parseValue(v);
652659
var a = this.value, b = n.value;
@@ -655,9 +662,7 @@ var bigInt = (function (undefined) {
655662
if (a === BigInt(1)) return Integer[1];
656663
if (a === BigInt(-1)) return n.isEven() ? Integer[1] : Integer[-1];
657664
if (n.isNegative()) return new NativeBigInt(BigInt(0));
658-
// forced to use eval because ** is a syntax error on pre-ECMAScript2017 environments.
659-
// should be fine w.r.t. security because parseValue(v) will throw an error if v is a malicious string.
660-
return new NativeBigInt(eval("BigInt('" + a + "') ** BigInt('" + b + "')"));
665+
return new NativeBigInt(pow(a, b));
661666
}
662667

663668
BigInteger.prototype.modPow = function (exp, mod) {

0 commit comments

Comments
 (0)