| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
final class NativeNumber extends IdScriptableObject |
| 17 |
{ |
| 18 |
static final long serialVersionUID = 3504516769741512101L; |
| 19 |
|
| 20 |
private static final Object NUMBER_TAG = "Number"; |
| 21 |
|
| 22 |
private static final int MAX_PRECISION = 100; |
| 23 |
private static final double MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; |
| 24 |
private static final double MIN_SAFE_INTEGER = -MAX_SAFE_INTEGER; |
| 25 |
|
| 26 |
static void init(Scriptable scope, boolean sealed) |
| 27 |
{ |
| 28 |
NativeNumber obj = new NativeNumber(0.0); |
| 29 |
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); |
| 30 |
} |
| 31 |
|
| 32 |
NativeNumber(double number) |
| 33 |
{ |
| 34 |
doubleValue = number; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public String getClassName() |
| 39 |
{ |
| 40 |
return "Number"; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
protected void fillConstructorProperties(IdFunctionObject ctor) |
| 45 |
{ |
| 46 |
final int attr = ScriptableObject.DONTENUM | |
| 47 |
ScriptableObject.PERMANENT | |
| 48 |
ScriptableObject.READONLY; |
| 49 |
|
| 50 |
ctor.defineProperty("NaN", ScriptRuntime.NaNobj, attr); |
| 51 |
ctor.defineProperty("POSITIVE_INFINITY", |
| 52 |
ScriptRuntime.wrapNumber(Double.POSITIVE_INFINITY), |
| 53 |
attr); |
| 54 |
ctor.defineProperty("NEGATIVE_INFINITY", |
| 55 |
ScriptRuntime.wrapNumber(Double.NEGATIVE_INFINITY), |
| 56 |
attr); |
| 57 |
ctor.defineProperty("MAX_VALUE", |
| 58 |
ScriptRuntime.wrapNumber(Double.MAX_VALUE), |
| 59 |
attr); |
| 60 |
ctor.defineProperty("MIN_VALUE", |
| 61 |
ScriptRuntime.wrapNumber(Double.MIN_VALUE), |
| 62 |
attr); |
| 63 |
ctor.defineProperty("MAX_SAFE_INTEGER", |
| 64 |
ScriptRuntime.wrapNumber(MAX_SAFE_INTEGER), |
| 65 |
attr); |
| 66 |
ctor.defineProperty("MIN_SAFE_INTEGER", |
| 67 |
ScriptRuntime.wrapNumber(MIN_SAFE_INTEGER), |
| 68 |
attr); |
| 69 |
|
| 70 |
addIdFunctionProperty(ctor, NUMBER_TAG, ConstructorId_isFinite, "isFinite", 1); |
| 71 |
addIdFunctionProperty(ctor, NUMBER_TAG, ConstructorId_isNaN, "isNaN", 1); |
| 72 |
addIdFunctionProperty(ctor, NUMBER_TAG, ConstructorId_isInteger, "isInteger", 1); |
| 73 |
addIdFunctionProperty(ctor, NUMBER_TAG, ConstructorId_isSafeInteger, "isSafeInteger", 1); |
| 74 |
addIdFunctionProperty(ctor, NUMBER_TAG, ConstructorId_parseFloat, "parseFloat", 1); |
| 75 |
addIdFunctionProperty(ctor, NUMBER_TAG, ConstructorId_parseInt, "parseInt", 1); |
| 76 |
|
| 77 |
super.fillConstructorProperties(ctor); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
protected void initPrototypeId(int id) |
| 82 |
{ |
| 83 |
String s; |
| 84 |
int arity; |
| 85 |
switch (id) { |
| 86 |
case Id_constructor: arity=1; s="constructor"; break; |
| 87 |
case Id_toString: arity=1; s="toString"; break; |
| 88 |
case Id_toLocaleString: arity=1; s="toLocaleString"; break; |
| 89 |
case Id_toSource: arity=0; s="toSource"; break; |
| 90 |
case Id_valueOf: arity=0; s="valueOf"; break; |
| 91 |
case Id_toFixed: arity=1; s="toFixed"; break; |
| 92 |
case Id_toExponential: arity=1; s="toExponential"; break; |
| 93 |
case Id_toPrecision: arity=1; s="toPrecision"; break; |
| 94 |
default: throw new IllegalArgumentException(String.valueOf(id)); |
| 95 |
} |
| 96 |
initPrototypeMethod(NUMBER_TAG, id, s, arity); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 101 |
Scriptable thisObj, Object[] args) |
| 102 |
{ |
| 103 |
if (!f.hasTag(NUMBER_TAG)) { |
| 104 |
return super.execIdCall(f, cx, scope, thisObj, args); |
| 105 |
} |
| 106 |
int id = f.methodId(); |
| 107 |
if (id == Id_constructor) { |
| 108 |
double val = (args.length >= 1) |
| 109 |
? ScriptRuntime.toNumber(args[0]) : 0.0; |
| 110 |
if (thisObj == null) { |
| 111 |
|
| 112 |
return new NativeNumber(val); |
| 113 |
} |
| 114 |
|
| 115 |
return ScriptRuntime.wrapNumber(val); |
| 116 |
|
| 117 |
} else if (id < Id_constructor) { |
| 118 |
return execConstructorCall(id, args); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if (!(thisObj instanceof NativeNumber)) |
| 124 |
throw incompatibleCallError(f); |
| 125 |
double value = ((NativeNumber)thisObj).doubleValue; |
| 126 |
|
| 127 |
switch (id) { |
| 128 |
|
| 129 |
case Id_toString: |
| 130 |
case Id_toLocaleString: |
| 131 |
{ |
| 132 |
|
| 133 |
int base = (args.length == 0 || args[0] == Undefined.instance) |
| 134 |
? 10 : ScriptRuntime.toInt32(args[0]); |
| 135 |
return ScriptRuntime.numberToString(value, base); |
| 136 |
} |
| 137 |
|
| 138 |
case Id_toSource: |
| 139 |
return "(new Number("+ScriptRuntime.toString(value)+"))"; |
| 140 |
|
| 141 |
case Id_valueOf: |
| 142 |
return ScriptRuntime.wrapNumber(value); |
| 143 |
|
| 144 |
case Id_toFixed: |
| 145 |
return num_to(value, args, DToA.DTOSTR_FIXED, |
| 146 |
DToA.DTOSTR_FIXED, -20, 0); |
| 147 |
|
| 148 |
case Id_toExponential: { |
| 149 |
|
| 150 |
if(Double.isNaN(value)) { |
| 151 |
return "NaN"; |
| 152 |
} |
| 153 |
if(Double.isInfinite(value)) { |
| 154 |
if(value >= 0) { |
| 155 |
return "Infinity"; |
| 156 |
} |
| 157 |
else { |
| 158 |
return "-Infinity"; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return num_to(value, args, DToA.DTOSTR_STANDARD_EXPONENTIAL, |
| 163 |
DToA.DTOSTR_EXPONENTIAL, 0, 1); |
| 164 |
} |
| 165 |
|
| 166 |
case Id_toPrecision: { |
| 167 |
|
| 168 |
if(args.length == 0 || args[0] == Undefined.instance) { |
| 169 |
return ScriptRuntime.numberToString(value, 10); |
| 170 |
} |
| 171 |
|
| 172 |
if(Double.isNaN(value)) { |
| 173 |
return "NaN"; |
| 174 |
} |
| 175 |
if(Double.isInfinite(value)) { |
| 176 |
if(value >= 0) { |
| 177 |
return "Infinity"; |
| 178 |
} |
| 179 |
else { |
| 180 |
return "-Infinity"; |
| 181 |
} |
| 182 |
} |
| 183 |
return num_to(value, args, DToA.DTOSTR_STANDARD, |
| 184 |
DToA.DTOSTR_PRECISION, 1, 0); |
| 185 |
} |
| 186 |
|
| 187 |
default: throw new IllegalArgumentException(String.valueOf(id)); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
private Object execConstructorCall(int id, Object[] args) |
| 192 |
{ |
| 193 |
switch (id) { |
| 194 |
case ConstructorId_isFinite: |
| 195 |
if ((args.length == 0) || (Undefined.instance == args[0])) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
if (args[0] instanceof Number) { |
| 199 |
|
| 200 |
return isFinite(args[0]); |
| 201 |
} |
| 202 |
return false; |
| 203 |
|
| 204 |
case ConstructorId_isNaN: |
| 205 |
if ((args.length == 0) || (Undefined.instance == args[0])) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
if (args[0] instanceof Number) { |
| 209 |
return isNaN((Number)args[0]); |
| 210 |
} |
| 211 |
return false; |
| 212 |
|
| 213 |
case ConstructorId_isInteger: |
| 214 |
if ((args.length == 0) || (Undefined.instance == args[0])) { |
| 215 |
return false; |
| 216 |
} |
| 217 |
if (args[0] instanceof Number) { |
| 218 |
return isInteger((Number)args[0]); |
| 219 |
} |
| 220 |
return false; |
| 221 |
|
| 222 |
case ConstructorId_isSafeInteger: |
| 223 |
if ((args.length == 0) || (Undefined.instance == args[0])) { |
| 224 |
return false; |
| 225 |
} |
| 226 |
if (args[0] instanceof Number) { |
| 227 |
return isSafeInteger((Number)args[0]); |
| 228 |
} |
| 229 |
return false; |
| 230 |
|
| 231 |
case ConstructorId_parseFloat: |
| 232 |
return NativeGlobal.js_parseFloat(args); |
| 233 |
|
| 234 |
case ConstructorId_parseInt: |
| 235 |
return NativeGlobal.js_parseInt(args); |
| 236 |
|
| 237 |
default: |
| 238 |
throw new IllegalArgumentException(String.valueOf(id)); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public String toString() { |
| 244 |
return ScriptRuntime.numberToString(doubleValue, 10); |
| 245 |
} |
| 246 |
|
| 247 |
private static String num_to(double val, |
| 248 |
Object[] args, |
| 249 |
int zeroArgMode, int oneArgMode, |
| 250 |
int precisionMin, int precisionOffset) |
| 251 |
{ |
| 252 |
int precision; |
| 253 |
if (args.length == 0) { |
| 254 |
precision = 0; |
| 255 |
oneArgMode = zeroArgMode; |
| 256 |
} else { |
| 257 |
|
| 258 |
|
| 259 |
double p = ScriptRuntime.toInteger(args[0]); |
| 260 |
if (p < precisionMin || p > MAX_PRECISION) { |
| 261 |
String msg = ScriptRuntime.getMessage1( |
| 262 |
"msg.bad.precision", ScriptRuntime.toString(args[0])); |
| 263 |
throw ScriptRuntime.constructError("RangeError", msg); |
| 264 |
} |
| 265 |
precision = ScriptRuntime.toInt32(p); |
| 266 |
} |
| 267 |
StringBuilder sb = new StringBuilder(); |
| 268 |
DToA.JS_dtostr(sb, oneArgMode, precision + precisionOffset, val); |
| 269 |
return sb.toString(); |
| 270 |
} |
| 271 |
|
| 272 |
static Object isFinite(Object val) |
| 273 |
{ |
| 274 |
double d = ScriptRuntime.toNumber(val); |
| 275 |
Double nd = Double.valueOf(d); |
| 276 |
return ScriptRuntime.wrapBoolean(!nd.isInfinite() && !nd.isNaN()); |
| 277 |
} |
| 278 |
|
| 279 |
private Object isNaN(Number val) |
| 280 |
{ |
| 281 |
Double nd = doubleVal(val); |
| 282 |
return ScriptRuntime.toBoolean(isDoubleNan(nd)); |
| 283 |
} |
| 284 |
|
| 285 |
private boolean isDoubleNan(Double d) |
| 286 |
{ |
| 287 |
return d.isNaN(); |
| 288 |
} |
| 289 |
|
| 290 |
private boolean isInteger(Number val) |
| 291 |
{ |
| 292 |
Double nd = doubleVal(val); |
| 293 |
return ScriptRuntime.toBoolean(isDoubleInteger(nd)); |
| 294 |
} |
| 295 |
|
| 296 |
private boolean isDoubleInteger(Double d) |
| 297 |
{ |
| 298 |
return (!d.isInfinite() && !d.isNaN() && |
| 299 |
(Math.floor(d.doubleValue()) == d.doubleValue())); |
| 300 |
} |
| 301 |
|
| 302 |
private boolean isSafeInteger(Number val) |
| 303 |
{ |
| 304 |
Double nd = doubleVal(val); |
| 305 |
return ScriptRuntime.toBoolean(isDoubleSafeInteger(nd)); |
| 306 |
} |
| 307 |
|
| 308 |
private boolean isDoubleSafeInteger(Double d) |
| 309 |
{ |
| 310 |
return (isDoubleInteger(d) && |
| 311 |
(d.doubleValue() <= MAX_SAFE_INTEGER) && |
| 312 |
(d.doubleValue() >= MIN_SAFE_INTEGER)); |
| 313 |
} |
| 314 |
|
| 315 |
private Double doubleVal(Number val) |
| 316 |
{ |
| 317 |
if (val instanceof Double) { |
| 318 |
return (Double)val; |
| 319 |
} else { |
| 320 |
double d = val.doubleValue(); |
| 321 |
return Double.valueOf(d); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
@Override |
| 328 |
protected int findPrototypeId(String s) |
| 329 |
{ |
| 330 |
int id; |
| 331 |
|
| 332 |
L0: { id = 0; String X = null; int c; |
| 333 |
L: switch (s.length()) { |
| 334 |
case 7: c=s.charAt(0); |
| 335 |
if (c=='t') { X="toFixed";id=Id_toFixed; } |
| 336 |
else if (c=='v') { X="valueOf";id=Id_valueOf; } |
| 337 |
break L; |
| 338 |
case 8: c=s.charAt(3); |
| 339 |
if (c=='o') { X="toSource";id=Id_toSource; } |
| 340 |
else if (c=='t') { X="toString";id=Id_toString; } |
| 341 |
break L; |
| 342 |
case 11: c=s.charAt(0); |
| 343 |
if (c=='c') { X="constructor";id=Id_constructor; } |
| 344 |
else if (c=='t') { X="toPrecision";id=Id_toPrecision; } |
| 345 |
break L; |
| 346 |
case 13: X="toExponential";id=Id_toExponential; break L; |
| 347 |
case 14: X="toLocaleString";id=Id_toLocaleString; break L; |
| 348 |
} |
| 349 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 350 |
break L0; |
| 351 |
} |
| 352 |
|
| 353 |
return id; |
| 354 |
} |
| 355 |
|
| 356 |
private static final int |
| 357 |
ConstructorId_isFinite = -1, |
| 358 |
ConstructorId_isNaN = -2, |
| 359 |
ConstructorId_isInteger = -3, |
| 360 |
ConstructorId_isSafeInteger = -4, |
| 361 |
ConstructorId_parseFloat = -5, |
| 362 |
ConstructorId_parseInt = -6, |
| 363 |
|
| 364 |
Id_constructor = 1, |
| 365 |
Id_toString = 2, |
| 366 |
Id_toLocaleString = 3, |
| 367 |
Id_toSource = 4, |
| 368 |
Id_valueOf = 5, |
| 369 |
Id_toFixed = 6, |
| 370 |
Id_toExponential = 7, |
| 371 |
Id_toPrecision = 8, |
| 372 |
MAX_PROTOTYPE_ID = 8; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
private double doubleValue; |
| 377 |
} |