| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import org.mozilla.javascript.typedarrays.Conversions; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
final class NativeMath extends IdScriptableObject |
| 18 |
{ |
| 19 |
static final long serialVersionUID = -8838847185801131569L; |
| 20 |
|
| 21 |
private static final Object MATH_TAG = "Math"; |
| 22 |
|
| 23 |
static void init(Scriptable scope, boolean sealed) |
| 24 |
{ |
| 25 |
NativeMath obj = new NativeMath(); |
| 26 |
obj.activatePrototypeMap(MAX_ID); |
| 27 |
obj.setPrototype(getObjectPrototype(scope)); |
| 28 |
obj.setParentScope(scope); |
| 29 |
if (sealed) { obj.sealObject(); } |
| 30 |
ScriptableObject.defineProperty(scope, "Math", obj, |
| 31 |
ScriptableObject.DONTENUM); |
| 32 |
} |
| 33 |
|
| 34 |
private NativeMath() |
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public String getClassName() { return "Math"; } |
| 40 |
|
| 41 |
@Override |
| 42 |
protected void initPrototypeId(int id) |
| 43 |
{ |
| 44 |
if (id <= LAST_METHOD_ID) { |
| 45 |
String name; |
| 46 |
int arity; |
| 47 |
switch (id) { |
| 48 |
case Id_toSource: arity = 0; name = "toSource"; break; |
| 49 |
case Id_abs: arity = 1; name = "abs"; break; |
| 50 |
case Id_acos: arity = 1; name = "acos"; break; |
| 51 |
case Id_asin: arity = 1; name = "asin"; break; |
| 52 |
case Id_atan: arity = 1; name = "atan"; break; |
| 53 |
case Id_atan2: arity = 2; name = "atan2"; break; |
| 54 |
case Id_cbrt: arity = 1; name = "cbrt"; break; |
| 55 |
case Id_ceil: arity = 1; name = "ceil"; break; |
| 56 |
case Id_cos: arity = 1; name = "cos"; break; |
| 57 |
case Id_cosh: arity = 1; name = "cosh"; break; |
| 58 |
case Id_exp: arity = 1; name = "exp"; break; |
| 59 |
case Id_expm1: arity = 1; name = "expm1"; break; |
| 60 |
case Id_floor: arity = 1; name = "floor"; break; |
| 61 |
case Id_hypot: arity = 2; name = "hypot"; break; |
| 62 |
case Id_imul: arity = 2; name = "imul"; break; |
| 63 |
case Id_log: arity = 1; name = "log"; break; |
| 64 |
case Id_log1p: arity = 1; name = "log1p"; break; |
| 65 |
case Id_log10: arity = 1; name = "log10"; break; |
| 66 |
case Id_max: arity = 2; name = "max"; break; |
| 67 |
case Id_min: arity = 2; name = "min"; break; |
| 68 |
case Id_pow: arity = 2; name = "pow"; break; |
| 69 |
case Id_random: arity = 0; name = "random"; break; |
| 70 |
case Id_round: arity = 1; name = "round"; break; |
| 71 |
case Id_sin: arity = 1; name = "sin"; break; |
| 72 |
case Id_sinh: arity = 1; name = "sinh"; break; |
| 73 |
case Id_sqrt: arity = 1; name = "sqrt"; break; |
| 74 |
case Id_tan: arity = 1; name = "tan"; break; |
| 75 |
case Id_tanh: arity = 1; name = "tanh"; break; |
| 76 |
case Id_trunc: arity = 1; name = "trunc"; break; |
| 77 |
default: throw new IllegalStateException(String.valueOf(id)); |
| 78 |
} |
| 79 |
initPrototypeMethod(MATH_TAG, id, name, arity); |
| 80 |
} else { |
| 81 |
String name; |
| 82 |
double x; |
| 83 |
switch (id) { |
| 84 |
case Id_E: x = Math.E; name = "E"; break; |
| 85 |
case Id_PI: x = Math.PI; name = "PI"; break; |
| 86 |
case Id_LN10: x = 2.302585092994046; name = "LN10"; break; |
| 87 |
case Id_LN2: x = 0.6931471805599453; name = "LN2"; break; |
| 88 |
case Id_LOG2E: x = 1.4426950408889634; name = "LOG2E"; break; |
| 89 |
case Id_LOG10E: x = 0.4342944819032518; name = "LOG10E"; break; |
| 90 |
case Id_SQRT1_2: x = 0.7071067811865476; name = "SQRT1_2"; break; |
| 91 |
case Id_SQRT2: x = 1.4142135623730951; name = "SQRT2"; break; |
| 92 |
default: throw new IllegalStateException(String.valueOf(id)); |
| 93 |
} |
| 94 |
initPrototypeValue(id, name, ScriptRuntime.wrapNumber(x), |
| 95 |
DONTENUM | READONLY | PERMANENT); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 101 |
Scriptable thisObj, Object[] args) |
| 102 |
{ |
| 103 |
if (!f.hasTag(MATH_TAG)) { |
| 104 |
return super.execIdCall(f, cx, scope, thisObj, args); |
| 105 |
} |
| 106 |
double x; |
| 107 |
int methodId = f.methodId(); |
| 108 |
switch (methodId) { |
| 109 |
case Id_toSource: |
| 110 |
return "Math"; |
| 111 |
|
| 112 |
case Id_abs: |
| 113 |
x = ScriptRuntime.toNumber(args, 0); |
| 114 |
|
| 115 |
x = (x == 0.0) ? 0.0 : (x < 0.0) ? -x : x; |
| 116 |
break; |
| 117 |
|
| 118 |
case Id_acos: |
| 119 |
case Id_asin: |
| 120 |
x = ScriptRuntime.toNumber(args, 0); |
| 121 |
if (x == x && -1.0 <= x && x <= 1.0) { |
| 122 |
x = (methodId == Id_acos) ? Math.acos(x) : Math.asin(x); |
| 123 |
} else { |
| 124 |
x = Double.NaN; |
| 125 |
} |
| 126 |
break; |
| 127 |
|
| 128 |
case Id_atan: |
| 129 |
x = ScriptRuntime.toNumber(args, 0); |
| 130 |
x = Math.atan(x); |
| 131 |
break; |
| 132 |
|
| 133 |
case Id_atan2: |
| 134 |
x = ScriptRuntime.toNumber(args, 0); |
| 135 |
x = Math.atan2(x, ScriptRuntime.toNumber(args, 1)); |
| 136 |
break; |
| 137 |
|
| 138 |
case Id_cbrt: |
| 139 |
x = ScriptRuntime.toNumber(args, 0); |
| 140 |
x = Math.cbrt(x); |
| 141 |
break; |
| 142 |
|
| 143 |
case Id_ceil: |
| 144 |
x = ScriptRuntime.toNumber(args, 0); |
| 145 |
x = Math.ceil(x); |
| 146 |
break; |
| 147 |
|
| 148 |
case Id_cos: |
| 149 |
x = ScriptRuntime.toNumber(args, 0); |
| 150 |
x = (x == Double.POSITIVE_INFINITY |
| 151 |
|| x == Double.NEGATIVE_INFINITY) |
| 152 |
? Double.NaN : Math.cos(x); |
| 153 |
break; |
| 154 |
|
| 155 |
case Id_cosh: |
| 156 |
x = ScriptRuntime.toNumber(args, 0); |
| 157 |
x = Math.cosh(x); |
| 158 |
break; |
| 159 |
|
| 160 |
case Id_hypot: |
| 161 |
x = js_hypot(args); |
| 162 |
break; |
| 163 |
|
| 164 |
case Id_exp: |
| 165 |
x = ScriptRuntime.toNumber(args, 0); |
| 166 |
x = (x == Double.POSITIVE_INFINITY) ? x |
| 167 |
: (x == Double.NEGATIVE_INFINITY) ? 0.0 |
| 168 |
: Math.exp(x); |
| 169 |
break; |
| 170 |
|
| 171 |
case Id_expm1: |
| 172 |
x = ScriptRuntime.toNumber(args, 0); |
| 173 |
x = Math.expm1(x); |
| 174 |
break; |
| 175 |
|
| 176 |
case Id_floor: |
| 177 |
x = ScriptRuntime.toNumber(args, 0); |
| 178 |
x = Math.floor(x); |
| 179 |
break; |
| 180 |
|
| 181 |
case Id_imul: |
| 182 |
return js_imul(args); |
| 183 |
|
| 184 |
case Id_log: |
| 185 |
x = ScriptRuntime.toNumber(args, 0); |
| 186 |
|
| 187 |
x = (x < 0) ? Double.NaN : Math.log(x); |
| 188 |
break; |
| 189 |
|
| 190 |
case Id_log1p: |
| 191 |
x = ScriptRuntime.toNumber(args, 0); |
| 192 |
x = Math.log1p(x); |
| 193 |
break; |
| 194 |
|
| 195 |
case Id_log10: |
| 196 |
x = ScriptRuntime.toNumber(args, 0); |
| 197 |
x = Math.log10(x); |
| 198 |
break; |
| 199 |
|
| 200 |
case Id_max: |
| 201 |
case Id_min: |
| 202 |
x = (methodId == Id_max) |
| 203 |
? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; |
| 204 |
for (int i = 0; i != args.length; ++i) { |
| 205 |
double d = ScriptRuntime.toNumber(args[i]); |
| 206 |
if (d != d) { |
| 207 |
x = d; |
| 208 |
break; |
| 209 |
} |
| 210 |
if (methodId == Id_max) { |
| 211 |
|
| 212 |
x = Math.max(x, d); |
| 213 |
} else { |
| 214 |
x = Math.min(x, d); |
| 215 |
} |
| 216 |
} |
| 217 |
break; |
| 218 |
|
| 219 |
case Id_pow: |
| 220 |
x = ScriptRuntime.toNumber(args, 0); |
| 221 |
x = js_pow(x, ScriptRuntime.toNumber(args, 1)); |
| 222 |
break; |
| 223 |
|
| 224 |
case Id_random: |
| 225 |
x = Math.random(); |
| 226 |
break; |
| 227 |
|
| 228 |
case Id_round: |
| 229 |
x = ScriptRuntime.toNumber(args, 0); |
| 230 |
if (x == x && x != Double.POSITIVE_INFINITY |
| 231 |
&& x != Double.NEGATIVE_INFINITY) |
| 232 |
{ |
| 233 |
|
| 234 |
long l = Math.round(x); |
| 235 |
if (l != 0) { |
| 236 |
x = l; |
| 237 |
} else { |
| 238 |
|
| 239 |
if (x < 0.0) { |
| 240 |
x = ScriptRuntime.negativeZero; |
| 241 |
} else if (x != 0.0) { |
| 242 |
x = 0.0; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
break; |
| 247 |
|
| 248 |
case Id_sin: |
| 249 |
x = ScriptRuntime.toNumber(args, 0); |
| 250 |
x = (x == Double.POSITIVE_INFINITY |
| 251 |
|| x == Double.NEGATIVE_INFINITY) |
| 252 |
? Double.NaN : Math.sin(x); |
| 253 |
break; |
| 254 |
|
| 255 |
case Id_sinh: |
| 256 |
x = ScriptRuntime.toNumber(args, 0); |
| 257 |
x = Math.sinh(x); |
| 258 |
break; |
| 259 |
|
| 260 |
case Id_sqrt: |
| 261 |
x = ScriptRuntime.toNumber(args, 0); |
| 262 |
x = Math.sqrt(x); |
| 263 |
break; |
| 264 |
|
| 265 |
case Id_tan: |
| 266 |
x = ScriptRuntime.toNumber(args, 0); |
| 267 |
x = Math.tan(x); |
| 268 |
break; |
| 269 |
|
| 270 |
case Id_tanh: |
| 271 |
x = ScriptRuntime.toNumber(args, 0); |
| 272 |
x = Math.tanh(x); |
| 273 |
break; |
| 274 |
|
| 275 |
case Id_trunc: |
| 276 |
x = ScriptRuntime.toNumber(args, 0); |
| 277 |
x = js_trunc(x); |
| 278 |
break; |
| 279 |
|
| 280 |
default: throw new IllegalStateException(String.valueOf(methodId)); |
| 281 |
} |
| 282 |
return ScriptRuntime.wrapNumber(x); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
private double js_pow(double x, double y) { |
| 287 |
double result; |
| 288 |
if (y != y) { |
| 289 |
|
| 290 |
result = y; |
| 291 |
} else if (y == 0) { |
| 292 |
|
| 293 |
result = 1.0; |
| 294 |
} else if (x == 0) { |
| 295 |
|
| 296 |
if (1 / x > 0) { |
| 297 |
result = (y > 0) ? 0 : Double.POSITIVE_INFINITY; |
| 298 |
} else { |
| 299 |
|
| 300 |
long y_long = (long)y; |
| 301 |
if (y_long == y && (y_long & 0x1) != 0) { |
| 302 |
result = (y > 0) ? -0.0 : Double.NEGATIVE_INFINITY; |
| 303 |
} else { |
| 304 |
result = (y > 0) ? 0.0 : Double.POSITIVE_INFINITY; |
| 305 |
} |
| 306 |
} |
| 307 |
} else { |
| 308 |
result = Math.pow(x, y); |
| 309 |
if (result != result) { |
| 310 |
|
| 311 |
|
| 312 |
if (y == Double.POSITIVE_INFINITY) { |
| 313 |
if (x < -1.0 || 1.0 < x) { |
| 314 |
result = Double.POSITIVE_INFINITY; |
| 315 |
} else if (-1.0 < x && x < 1.0) { |
| 316 |
result = 0; |
| 317 |
} |
| 318 |
} else if (y == Double.NEGATIVE_INFINITY) { |
| 319 |
if (x < -1.0 || 1.0 < x) { |
| 320 |
result = 0; |
| 321 |
} else if (-1.0 < x && x < 1.0) { |
| 322 |
result = Double.POSITIVE_INFINITY; |
| 323 |
} |
| 324 |
} else if (x == Double.POSITIVE_INFINITY) { |
| 325 |
result = (y > 0) ? Double.POSITIVE_INFINITY : 0.0; |
| 326 |
} else if (x == Double.NEGATIVE_INFINITY) { |
| 327 |
long y_long = (long)y; |
| 328 |
if (y_long == y && (y_long & 0x1) != 0) { |
| 329 |
|
| 330 |
result = (y > 0) ? Double.NEGATIVE_INFINITY : -0.0; |
| 331 |
} else { |
| 332 |
result = (y > 0) ? Double.POSITIVE_INFINITY : 0.0; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
return result; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
private double js_hypot(Object[] args) |
| 342 |
{ |
| 343 |
if (args == null) { |
| 344 |
return 0.0; |
| 345 |
} |
| 346 |
double y = 0.0; |
| 347 |
|
| 348 |
for (Object o : args) { |
| 349 |
double d = ScriptRuntime.toNumber(o); |
| 350 |
if (d == ScriptRuntime.NaN) { |
| 351 |
return d; |
| 352 |
} |
| 353 |
if ((d == Double.POSITIVE_INFINITY) || (d == Double.NEGATIVE_INFINITY)) { |
| 354 |
return Double.POSITIVE_INFINITY; |
| 355 |
} |
| 356 |
y += d * d; |
| 357 |
} |
| 358 |
return Math.sqrt(y); |
| 359 |
} |
| 360 |
|
| 361 |
private double js_trunc(double d) |
| 362 |
{ |
| 363 |
return ((d < 0.0) ? Math.ceil(d) : Math.floor(d)); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
private Object js_imul(Object[] args) |
| 368 |
{ |
| 369 |
if ((args == null) || (args.length < 2)) { |
| 370 |
return ScriptRuntime.wrapNumber(ScriptRuntime.NaN); |
| 371 |
} |
| 372 |
|
| 373 |
long x = Conversions.toUint32(args[0]); |
| 374 |
long y = Conversions.toUint32(args[1]); |
| 375 |
long product = (x * y) % Conversions.THIRTYTWO_BIT; |
| 376 |
long result = (product >= (1L << 31L)) ? (product - Conversions.THIRTYTWO_BIT) : product; |
| 377 |
return ScriptRuntime.toNumber(result); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
@Override |
| 383 |
protected int findPrototypeId(String s) |
| 384 |
{ |
| 385 |
int id; |
| 386 |
|
| 387 |
L0: { id = 0; String X = null; int c; |
| 388 |
L: switch (s.length()) { |
| 389 |
case 1: if (s.charAt(0)=='E') {id=Id_E; break L0;} break L; |
| 390 |
case 2: if (s.charAt(0)=='P' && s.charAt(1)=='I') {id=Id_PI; break L0;} break L; |
| 391 |
case 3: switch (s.charAt(0)) { |
| 392 |
case 'L': if (s.charAt(2)=='2' && s.charAt(1)=='N') {id=Id_LN2; break L0;} break L; |
| 393 |
case 'a': if (s.charAt(2)=='s' && s.charAt(1)=='b') {id=Id_abs; break L0;} break L; |
| 394 |
case 'c': if (s.charAt(2)=='s' && s.charAt(1)=='o') {id=Id_cos; break L0;} break L; |
| 395 |
case 'e': if (s.charAt(2)=='p' && s.charAt(1)=='x') {id=Id_exp; break L0;} break L; |
| 396 |
case 'l': if (s.charAt(2)=='g' && s.charAt(1)=='o') {id=Id_log; break L0;} break L; |
| 397 |
case 'm': c=s.charAt(2); |
| 398 |
if (c=='n') { if (s.charAt(1)=='i') {id=Id_min; break L0;} } |
| 399 |
else if (c=='x') { if (s.charAt(1)=='a') {id=Id_max; break L0;} } |
| 400 |
break L; |
| 401 |
case 'p': if (s.charAt(2)=='w' && s.charAt(1)=='o') {id=Id_pow; break L0;} break L; |
| 402 |
case 's': if (s.charAt(2)=='n' && s.charAt(1)=='i') {id=Id_sin; break L0;} break L; |
| 403 |
case 't': if (s.charAt(2)=='n' && s.charAt(1)=='a') {id=Id_tan; break L0;} break L; |
| 404 |
} break L; |
| 405 |
case 4: switch (s.charAt(1)) { |
| 406 |
case 'N': X="LN10";id=Id_LN10; break L; |
| 407 |
case 'a': X="tanh";id=Id_tanh; break L; |
| 408 |
case 'b': X="cbrt";id=Id_cbrt; break L; |
| 409 |
case 'c': X="acos";id=Id_acos; break L; |
| 410 |
case 'e': X="ceil";id=Id_ceil; break L; |
| 411 |
case 'i': X="sinh";id=Id_sinh; break L; |
| 412 |
case 'm': X="imul";id=Id_imul; break L; |
| 413 |
case 'o': X="cosh";id=Id_cosh; break L; |
| 414 |
case 'q': X="sqrt";id=Id_sqrt; break L; |
| 415 |
case 's': X="asin";id=Id_asin; break L; |
| 416 |
case 't': X="atan";id=Id_atan; break L; |
| 417 |
} break L; |
| 418 |
case 5: switch (s.charAt(0)) { |
| 419 |
case 'L': X="LOG2E";id=Id_LOG2E; break L; |
| 420 |
case 'S': X="SQRT2";id=Id_SQRT2; break L; |
| 421 |
case 'a': X="atan2";id=Id_atan2; break L; |
| 422 |
case 'e': X="expm1";id=Id_expm1; break L; |
| 423 |
case 'f': X="floor";id=Id_floor; break L; |
| 424 |
case 'h': X="hypot";id=Id_hypot; break L; |
| 425 |
case 'l': c=s.charAt(4); |
| 426 |
if (c=='0') { X="log10";id=Id_log10; } |
| 427 |
else if (c=='p') { X="log1p";id=Id_log1p; } |
| 428 |
break L; |
| 429 |
case 'r': X="round";id=Id_round; break L; |
| 430 |
case 't': X="trunc";id=Id_trunc; break L; |
| 431 |
} break L; |
| 432 |
case 6: c=s.charAt(0); |
| 433 |
if (c=='L') { X="LOG10E";id=Id_LOG10E; } |
| 434 |
else if (c=='r') { X="random";id=Id_random; } |
| 435 |
break L; |
| 436 |
case 7: X="SQRT1_2";id=Id_SQRT1_2; break L; |
| 437 |
case 8: X="toSource";id=Id_toSource; break L; |
| 438 |
} |
| 439 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 440 |
break L0; |
| 441 |
} |
| 442 |
|
| 443 |
return id; |
| 444 |
} |
| 445 |
|
| 446 |
private static final int |
| 447 |
Id_toSource = 1, |
| 448 |
Id_abs = 2, |
| 449 |
Id_acos = 3, |
| 450 |
Id_asin = 4, |
| 451 |
Id_atan = 5, |
| 452 |
Id_atan2 = 6, |
| 453 |
Id_ceil = 7, |
| 454 |
Id_cos = 8, |
| 455 |
Id_exp = 9, |
| 456 |
Id_floor = 10, |
| 457 |
Id_log = 11, |
| 458 |
Id_max = 12, |
| 459 |
Id_min = 13, |
| 460 |
Id_pow = 14, |
| 461 |
Id_random = 15, |
| 462 |
Id_round = 16, |
| 463 |
Id_sin = 17, |
| 464 |
Id_sqrt = 18, |
| 465 |
Id_tan = 19, |
| 466 |
Id_cbrt = 20, |
| 467 |
Id_cosh = 21, |
| 468 |
Id_expm1 = 22, |
| 469 |
Id_hypot = 23, |
| 470 |
Id_log1p = 24, |
| 471 |
Id_log10 = 25, |
| 472 |
Id_sinh = 26, |
| 473 |
Id_tanh = 27, |
| 474 |
Id_imul = 28, |
| 475 |
Id_trunc = 29, |
| 476 |
|
| 477 |
LAST_METHOD_ID = Id_trunc; |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
private static final int |
| 489 |
Id_E = LAST_METHOD_ID + 1, |
| 490 |
Id_PI = LAST_METHOD_ID + 2, |
| 491 |
Id_LN10 = LAST_METHOD_ID + 3, |
| 492 |
Id_LN2 = LAST_METHOD_ID + 4, |
| 493 |
Id_LOG2E = LAST_METHOD_ID + 5, |
| 494 |
Id_LOG10E = LAST_METHOD_ID + 6, |
| 495 |
Id_SQRT1_2 = LAST_METHOD_ID + 7, |
| 496 |
Id_SQRT2 = LAST_METHOD_ID + 8, |
| 497 |
|
| 498 |
MAX_ID = LAST_METHOD_ID + 8; |
| 499 |
|
| 500 |
|
| 501 |
} |