@@ -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 }
0 commit comments