Skip to content

Commit e771eb2

Browse files
committed
Simplify and comment a bit
1 parent 95a2519 commit e771eb2

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

rhino/src/main/java/org/mozilla/javascript/NativeNumber.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,7 @@ private static Object js_toFixed(
148148
int precisionMin = cx.version < Context.VERSION_ES6 ? -20 : 0;
149149
/* We allow a larger range of precision than
150150
ECMA requires; this is permitted by ECMA. */
151-
if (p < precisionMin || p > MAX_PRECISION) {
152-
String msg =
153-
ScriptRuntime.getMessageById(
154-
"msg.bad.precision", ScriptRuntime.toString(args[0]));
155-
throw ScriptRuntime.rangeError(msg);
156-
}
151+
checkPrecision(p, precisionMin, args[0]);
157152
fractionDigits = ScriptRuntime.toInt32(p);
158153
} else {
159154
fractionDigits = 0;
@@ -182,13 +177,7 @@ private static Object js_toExponential(
182177
if (!Double.isFinite(value)) {
183178
return ScriptRuntime.toString(value);
184179
}
185-
186-
if (p < 0.0 || p > MAX_PRECISION) {
187-
String msg =
188-
ScriptRuntime.getMessageById(
189-
"msg.bad.precision", ScriptRuntime.toString(args[0]));
190-
throw ScriptRuntime.rangeError(msg);
191-
}
180+
checkPrecision(p, 0.0, args.length > 0 ? args[0] : Undefined.instance);
192181

193182
// Trigger the special handling for undefined, which requires that
194183
// we hold off on this bit until the checks above,.
@@ -209,17 +198,20 @@ private static Object js_toPrecision(
209198
if (!Double.isFinite(value)) {
210199
return ScriptRuntime.toString(value);
211200
}
212-
if (p < 1 || p > MAX_PRECISION) {
213-
String msg =
214-
ScriptRuntime.getMessageById(
215-
"msg.bad.precision", ScriptRuntime.toString(args[0]));
216-
throw ScriptRuntime.rangeError(msg);
217-
}
201+
checkPrecision(p, 1.0, args[0]);
218202
int precision = ScriptRuntime.toInt32(p);
219203

220204
return DecimalFormatter.toPrecision(value, precision);
221205
}
222206

207+
private static void checkPrecision(double p, double min, Object arg) {
208+
if (p < min || p > MAX_PRECISION) {
209+
String msg =
210+
ScriptRuntime.getMessageById("msg.bad.precision", ScriptRuntime.toString(arg));
211+
throw ScriptRuntime.rangeError(msg);
212+
}
213+
}
214+
223215
private static NativeNumber toSelf(Scriptable thisObj) {
224216
return LambdaConstructor.convertThisObject(thisObj, NativeNumber.class);
225217
}

rhino/src/main/java/org/mozilla/javascript/dtoa/DecimalFormatter.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ public static String toExponential(double v, int fractionDigits) {
2626
if (negative) {
2727
val = Math.abs(v);
2828
}
29-
BigDecimal bd = new BigDecimal(val, MathContext.UNLIMITED);
30-
if (bd.precision() > fractionDigits + 1) {
31-
bd = bd.round(new MathContext(fractionDigits + 1, RoundingMode.HALF_UP));
32-
}
29+
var bd = new BigDecimal(val, new MathContext(fractionDigits + 1, RoundingMode.HALF_UP));
3330

3431
int exponent;
3532
if (bd.scale() >= 0) {
@@ -72,10 +69,7 @@ public static String toPrecision(double v, int precision) {
7269
} else {
7370
val = v;
7471
}
75-
var bd = new BigDecimal(val, MathContext.UNLIMITED);
76-
if (bd.precision() > precision) {
77-
bd = bd.round(new MathContext(precision, RoundingMode.HALF_UP));
78-
}
72+
var bd = new BigDecimal(val, new MathContext(precision, RoundingMode.HALF_UP));
7973

8074
int scale = bd.scale();
8175
int numDigits = bd.precision();

rhino/src/main/java/org/mozilla/javascript/dtoa/DoubleFormatter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ public static String toString(double v) {
9797
return bits > 0 ? "Infinity" : "-Infinity";
9898
}
9999

100+
/**
101+
* Convert a double to a Decimal object that may be output in various ways. Unlike toString, and
102+
* since it always returns a Decimal, it must be used with finite numbers only or the result is
103+
* undetermined.
104+
*/
100105
public static Decimal toDecimal(double v) {
106+
assert Double.isFinite(v);
101107
long bits = Double.doubleToRawLongBits(v);
102108
long t = bits & T_MASK;
103109
int bq = (int) (bits >>> (P - 1)) & BQ_MASK;

0 commit comments

Comments
 (0)