Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / js / src / jsmath.cpp
blob352dead4e5c4e4b4978dfdffaf11799452ab1b0b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 * JS math package.
43 #include <stdlib.h>
44 #include "jstypes.h"
45 #include "jsstdint.h"
46 #include "jslong.h"
47 #include "prmjtime.h"
48 #include "jsapi.h"
49 #include "jsatom.h"
50 #include "jsbuiltins.h"
51 #include "jscntxt.h"
52 #include "jsversion.h"
53 #include "jslock.h"
54 #include "jsmath.h"
55 #include "jsnum.h"
56 #include "jslibmath.h"
57 #include "jscompartment.h"
59 using namespace js;
61 #ifndef M_E
62 #define M_E 2.7182818284590452354
63 #endif
64 #ifndef M_LOG2E
65 #define M_LOG2E 1.4426950408889634074
66 #endif
67 #ifndef M_LOG10E
68 #define M_LOG10E 0.43429448190325182765
69 #endif
70 #ifndef M_LN2
71 #define M_LN2 0.69314718055994530942
72 #endif
73 #ifndef M_LN10
74 #define M_LN10 2.30258509299404568402
75 #endif
76 #ifndef M_PI
77 #define M_PI 3.14159265358979323846
78 #endif
79 #ifndef M_SQRT2
80 #define M_SQRT2 1.41421356237309504880
81 #endif
82 #ifndef M_SQRT1_2
83 #define M_SQRT1_2 0.70710678118654752440
84 #endif
86 static JSConstDoubleSpec math_constants[] = {
87 {M_E, "E", 0, {0,0,0}},
88 {M_LOG2E, "LOG2E", 0, {0,0,0}},
89 {M_LOG10E, "LOG10E", 0, {0,0,0}},
90 {M_LN2, "LN2", 0, {0,0,0}},
91 {M_LN10, "LN10", 0, {0,0,0}},
92 {M_PI, "PI", 0, {0,0,0}},
93 {M_SQRT2, "SQRT2", 0, {0,0,0}},
94 {M_SQRT1_2, "SQRT1_2", 0, {0,0,0}},
95 {0,0,0,{0,0,0}}
98 MathCache::MathCache() {
99 memset(table, 0, sizeof(table));
101 /* See comments in lookup(). */
102 JS_ASSERT(JSDOUBLE_IS_NEGZERO(-0.0));
103 JS_ASSERT(!JSDOUBLE_IS_NEGZERO(+0.0));
104 JS_ASSERT(hash(-0.0) != hash(+0.0));
107 Class js_MathClass = {
108 js_Math_str,
109 JSCLASS_HAS_CACHED_PROTO(JSProto_Math),
110 PropertyStub, /* addProperty */
111 PropertyStub, /* delProperty */
112 PropertyStub, /* getProperty */
113 StrictPropertyStub, /* setProperty */
114 EnumerateStub,
115 ResolveStub,
116 ConvertStub
119 JSBool
120 js_math_abs(JSContext *cx, uintN argc, Value *vp)
122 jsdouble x, z;
124 if (argc == 0) {
125 vp->setDouble(js_NaN);
126 return JS_TRUE;
128 if (!ValueToNumber(cx, vp[2], &x))
129 return JS_FALSE;
130 z = fabs(x);
131 vp->setNumber(z);
132 return JS_TRUE;
135 static JSBool
136 math_acos(JSContext *cx, uintN argc, Value *vp)
138 jsdouble x, z;
140 if (argc == 0) {
141 vp->setDouble(js_NaN);
142 return JS_TRUE;
144 if (!ValueToNumber(cx, vp[2], &x))
145 return JS_FALSE;
146 #if defined(SOLARIS) && defined(__GNUC__)
147 if (x < -1 || 1 < x) {
148 vp->setDouble(js_NaN);
149 return JS_TRUE;
151 #endif
152 MathCache *mathCache = GetMathCache(cx);
153 if (!mathCache)
154 return JS_FALSE;
155 z = mathCache->lookup(acos, x);
156 vp->setDouble(z);
157 return JS_TRUE;
160 static JSBool
161 math_asin(JSContext *cx, uintN argc, Value *vp)
163 jsdouble x, z;
165 if (argc == 0) {
166 vp->setDouble(js_NaN);
167 return JS_TRUE;
169 if (!ValueToNumber(cx, vp[2], &x))
170 return JS_FALSE;
171 #if defined(SOLARIS) && defined(__GNUC__)
172 if (x < -1 || 1 < x) {
173 vp->setDouble(js_NaN);
174 return JS_TRUE;
176 #endif
177 MathCache *mathCache = GetMathCache(cx);
178 if (!mathCache)
179 return JS_FALSE;
180 z = mathCache->lookup(asin, x);
181 vp->setDouble(z);
182 return JS_TRUE;
185 static JSBool
186 math_atan(JSContext *cx, uintN argc, Value *vp)
188 jsdouble x, z;
190 if (argc == 0) {
191 vp->setDouble(js_NaN);
192 return JS_TRUE;
194 if (!ValueToNumber(cx, vp[2], &x))
195 return JS_FALSE;
196 MathCache *mathCache = GetMathCache(cx);
197 if (!mathCache)
198 return JS_FALSE;
199 z = mathCache->lookup(atan, x);
200 vp->setDouble(z);
201 return JS_TRUE;
204 static inline jsdouble JS_FASTCALL
205 math_atan2_kernel(jsdouble x, jsdouble y)
207 #if defined(_MSC_VER)
209 * MSVC's atan2 does not yield the result demanded by ECMA when both x
210 * and y are infinite.
211 * - The result is a multiple of pi/4.
212 * - The sign of x determines the sign of the result.
213 * - The sign of y determines the multiplicator, 1 or 3.
215 if (JSDOUBLE_IS_INFINITE(x) && JSDOUBLE_IS_INFINITE(y)) {
216 jsdouble z = js_copysign(M_PI / 4, x);
217 if (y < 0)
218 z *= 3;
219 return z;
221 #endif
223 #if defined(SOLARIS) && defined(__GNUC__)
224 if (x == 0) {
225 if (JSDOUBLE_IS_NEGZERO(y))
226 return js_copysign(M_PI, x);
227 if (y == 0)
228 return x;
230 #endif
231 return atan2(x, y);
234 static JSBool
235 math_atan2(JSContext *cx, uintN argc, Value *vp)
237 jsdouble x, y, z;
239 if (argc <= 1) {
240 vp->setDouble(js_NaN);
241 return JS_TRUE;
243 if (!ValueToNumber(cx, vp[2], &x))
244 return JS_FALSE;
245 if (!ValueToNumber(cx, vp[3], &y))
246 return JS_FALSE;
247 z = math_atan2_kernel(x, y);
248 vp->setDouble(z);
249 return JS_TRUE;
252 jsdouble
253 js_math_ceil_impl(jsdouble x)
255 #ifdef __APPLE__
256 if (x < 0 && x > -1.0)
257 return js_copysign(0, -1);
258 #endif
259 return ceil(x);
262 JSBool
263 js_math_ceil(JSContext *cx, uintN argc, Value *vp)
265 jsdouble x, z;
267 if (argc == 0) {
268 vp->setDouble(js_NaN);
269 return JS_TRUE;
271 if (!ValueToNumber(cx, vp[2], &x))
272 return JS_FALSE;
273 z = js_math_ceil_impl(x);
274 vp->setNumber(z);
275 return JS_TRUE;
278 static JSBool
279 math_cos(JSContext *cx, uintN argc, Value *vp)
281 jsdouble x, z;
283 if (argc == 0) {
284 vp->setDouble(js_NaN);
285 return JS_TRUE;
287 if (!ValueToNumber(cx, vp[2], &x))
288 return JS_FALSE;
289 MathCache *mathCache = GetMathCache(cx);
290 if (!mathCache)
291 return JS_FALSE;
292 z = mathCache->lookup(cos, x);
293 vp->setDouble(z);
294 return JS_TRUE;
297 static double
298 math_exp_body(double d)
300 #ifdef _WIN32
301 if (!JSDOUBLE_IS_NaN(d)) {
302 if (d == js_PositiveInfinity)
303 return js_PositiveInfinity;
304 if (d == js_NegativeInfinity)
305 return 0.0;
307 #endif
308 return exp(d);
311 static JSBool
312 math_exp(JSContext *cx, uintN argc, Value *vp)
314 jsdouble x, z;
316 if (argc == 0) {
317 vp->setDouble(js_NaN);
318 return JS_TRUE;
320 if (!ValueToNumber(cx, vp[2], &x))
321 return JS_FALSE;
322 MathCache *mathCache = GetMathCache(cx);
323 if (!mathCache)
324 return JS_FALSE;
325 z = mathCache->lookup(math_exp_body, x);
326 vp->setNumber(z);
327 return JS_TRUE;
330 jsdouble
331 js_math_floor_impl(jsdouble x)
333 return floor(x);
336 JSBool
337 js_math_floor(JSContext *cx, uintN argc, Value *vp)
339 jsdouble x, z;
341 if (argc == 0) {
342 vp->setDouble(js_NaN);
343 return JS_TRUE;
345 if (!ValueToNumber(cx, vp[2], &x))
346 return JS_FALSE;
347 z = js_math_floor_impl(x);
348 vp->setNumber(z);
349 return JS_TRUE;
352 static JSBool
353 math_log(JSContext *cx, uintN argc, Value *vp)
355 jsdouble x, z;
357 if (argc == 0) {
358 vp->setDouble(js_NaN);
359 return JS_TRUE;
361 if (!ValueToNumber(cx, vp[2], &x))
362 return JS_FALSE;
363 #if defined(SOLARIS) && defined(__GNUC__)
364 if (x < 0) {
365 vp->setDouble(js_NaN);
366 return JS_TRUE;
368 #endif
369 MathCache *mathCache = GetMathCache(cx);
370 if (!mathCache)
371 return JS_FALSE;
372 z = mathCache->lookup(log, x);
373 vp->setNumber(z);
374 return JS_TRUE;
377 JSBool
378 js_math_max(JSContext *cx, uintN argc, Value *vp)
380 jsdouble x, z = js_NegativeInfinity;
381 Value *argv;
382 uintN i;
384 if (argc == 0) {
385 vp->setDouble(js_NegativeInfinity);
386 return JS_TRUE;
388 argv = vp + 2;
389 for (i = 0; i < argc; i++) {
390 if (!ValueToNumber(cx, argv[i], &x))
391 return JS_FALSE;
392 if (JSDOUBLE_IS_NaN(x)) {
393 vp->setDouble(js_NaN);
394 return JS_TRUE;
396 if (x == 0 && x == z) {
397 if (js_copysign(1.0, z) == -1)
398 z = x;
399 } else {
400 z = (x > z) ? x : z;
403 vp->setNumber(z);
404 return JS_TRUE;
407 JSBool
408 js_math_min(JSContext *cx, uintN argc, Value *vp)
410 jsdouble x, z = js_PositiveInfinity;
411 Value *argv;
412 uintN i;
414 if (argc == 0) {
415 vp->setDouble(js_PositiveInfinity);
416 return JS_TRUE;
418 argv = vp + 2;
419 for (i = 0; i < argc; i++) {
420 if (!ValueToNumber(cx, argv[i], &x))
421 return JS_FALSE;
422 if (JSDOUBLE_IS_NaN(x)) {
423 vp->setDouble(js_NaN);
424 return JS_TRUE;
426 if (x == 0 && x == z) {
427 if (js_copysign(1.0, x) == -1)
428 z = x;
429 } else {
430 z = (x < z) ? x : z;
433 vp->setNumber(z);
434 return JS_TRUE;
437 static jsdouble
438 powi(jsdouble x, jsint y)
440 jsuint n = (y < 0) ? -y : y;
441 jsdouble m = x;
442 jsdouble p = 1;
443 while (true) {
444 if ((n & 1) != 0) p *= m;
445 n >>= 1;
446 if (n == 0) {
447 if (y < 0) {
448 // Unfortunately, we have to be careful when p has reached
449 // infinity in the computation, because sometimes the higher
450 // internal precision in the pow() implementation would have
451 // given us a finite p. This happens very rarely.
453 jsdouble result = 1.0 / p;
454 return (result == 0 && JSDOUBLE_IS_INFINITE(p))
455 ? pow(x, static_cast<jsdouble>(y)) // Avoid pow(double, int).
456 : result;
459 return p;
461 m *= m;
465 static JSBool
466 math_pow(JSContext *cx, uintN argc, Value *vp)
468 jsdouble x, y, z;
470 if (argc <= 1) {
471 vp->setDouble(js_NaN);
472 return JS_TRUE;
474 if (!ValueToNumber(cx, vp[2], &x))
475 return JS_FALSE;
476 if (!ValueToNumber(cx, vp[3], &y))
477 return JS_FALSE;
479 * Special case for square roots. Note that pow(x, 0.5) != sqrt(x)
480 * when x = -0.0, so we have to guard for this.
482 if (JSDOUBLE_IS_FINITE(x) && x != 0.0) {
483 if (y == 0.5) {
484 vp->setNumber(sqrt(x));
485 return JS_TRUE;
487 if (y == -0.5) {
488 vp->setNumber(1.0/sqrt(x));
489 return JS_TRUE;
493 * Because C99 and ECMA specify different behavior for pow(),
494 * we need to wrap the libm call to make it ECMA compliant.
496 if (!JSDOUBLE_IS_FINITE(y) && (x == 1.0 || x == -1.0)) {
497 vp->setDouble(js_NaN);
498 return JS_TRUE;
500 /* pow(x, +-0) is always 1, even for x = NaN. */
501 if (y == 0) {
502 vp->setInt32(1);
503 return JS_TRUE;
506 if (vp[3].isInt32())
507 z = powi(x, vp[3].toInt32());
508 else
509 z = pow(x, y);
511 vp->setNumber(z);
512 return JS_TRUE;
515 static const int64 RNG_MULTIPLIER = 0x5DEECE66DLL;
516 static const int64 RNG_ADDEND = 0xBLL;
517 static const int64 RNG_MASK = (1LL << 48) - 1;
518 static const jsdouble RNG_DSCALE = jsdouble(1LL << 53);
521 * Math.random() support, lifted from java.util.Random.java.
523 static inline void
524 random_setSeed(JSContext *cx, int64 seed)
526 cx->rngSeed = (seed ^ RNG_MULTIPLIER) & RNG_MASK;
529 void
530 js_InitRandom(JSContext *cx)
533 * Set the seed from current time. Since we have a RNG per context and we often bring
534 * up several contexts at the same time, we xor in some additional values, namely
535 * the context and its successor. We don't just use the context because it might be
536 * possible to reverse engineer the context pointer if one guesses the time right.
538 random_setSeed(cx,
539 (PRMJ_Now() / 1000) ^
540 int64(cx) ^
541 int64(cx->link.next));
544 static inline uint64
545 random_next(JSContext *cx, int bits)
547 uint64 nextseed = cx->rngSeed * RNG_MULTIPLIER;
548 nextseed += RNG_ADDEND;
549 nextseed &= RNG_MASK;
550 cx->rngSeed = nextseed;
551 return nextseed >> (48 - bits);
554 static inline jsdouble
555 random_nextDouble(JSContext *cx)
557 return jsdouble((random_next(cx, 26) << 27) + random_next(cx, 27)) / RNG_DSCALE;
560 static JSBool
561 math_random(JSContext *cx, uintN argc, Value *vp)
563 jsdouble z = random_nextDouble(cx);
564 vp->setDouble(z);
565 return JS_TRUE;
568 #if defined _WIN32 && !defined WINCE && _MSC_VER < 1400
569 /* Try to work around apparent _copysign bustage in VC7.x. */
570 double
571 js_copysign(double x, double y)
573 jsdpun xu, yu;
575 xu.d = x;
576 yu.d = y;
577 xu.s.hi &= ~JSDOUBLE_HI32_SIGNBIT;
578 xu.s.hi |= yu.s.hi & JSDOUBLE_HI32_SIGNBIT;
579 return xu.d;
581 #endif
583 jsdouble
584 js_math_round_impl(jsdouble x)
586 return js_copysign(floor(x + 0.5), x);
589 JSBool
590 js_math_round(JSContext *cx, uintN argc, Value *vp)
592 jsdouble x, z;
594 if (argc == 0) {
595 vp->setDouble(js_NaN);
596 return JS_TRUE;
598 if (!ValueToNumber(cx, vp[2], &x))
599 return JS_FALSE;
600 z = js_copysign(floor(x + 0.5), x);
601 vp->setNumber(z);
602 return JS_TRUE;
605 static JSBool
606 math_sin(JSContext *cx, uintN argc, Value *vp)
608 jsdouble x, z;
610 if (argc == 0) {
611 vp->setDouble(js_NaN);
612 return JS_TRUE;
614 if (!ValueToNumber(cx, vp[2], &x))
615 return JS_FALSE;
616 MathCache *mathCache = GetMathCache(cx);
617 if (!mathCache)
618 return JS_FALSE;
619 z = mathCache->lookup(sin, x);
620 vp->setDouble(z);
621 return JS_TRUE;
624 static JSBool
625 math_sqrt(JSContext *cx, uintN argc, Value *vp)
627 jsdouble x, z;
629 if (argc == 0) {
630 vp->setDouble(js_NaN);
631 return JS_TRUE;
633 if (!ValueToNumber(cx, vp[2], &x))
634 return JS_FALSE;
635 MathCache *mathCache = GetMathCache(cx);
636 if (!mathCache)
637 return JS_FALSE;
638 z = mathCache->lookup(sqrt, x);
639 vp->setDouble(z);
640 return JS_TRUE;
643 static JSBool
644 math_tan(JSContext *cx, uintN argc, Value *vp)
646 jsdouble x, z;
648 if (argc == 0) {
649 vp->setDouble(js_NaN);
650 return JS_TRUE;
652 if (!ValueToNumber(cx, vp[2], &x))
653 return JS_FALSE;
654 MathCache *mathCache = GetMathCache(cx);
655 if (!mathCache)
656 return JS_FALSE;
657 z = mathCache->lookup(tan, x);
658 vp->setDouble(z);
659 return JS_TRUE;
662 #if JS_HAS_TOSOURCE
663 static JSBool
664 math_toSource(JSContext *cx, uintN argc, Value *vp)
666 vp->setString(ATOM_TO_STRING(CLASS_ATOM(cx, Math)));
667 return JS_TRUE;
669 #endif
671 #ifdef JS_TRACER
673 #define MATH_BUILTIN_1(name, cfun) \
674 static jsdouble FASTCALL name##_tn(MathCache *cache, jsdouble d) { \
675 return cache->lookup(cfun, d); \
677 JS_DEFINE_TRCINFO_1(name, \
678 (2, (static, DOUBLE, name##_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
680 MATH_BUILTIN_1(js_math_abs, fabs)
681 MATH_BUILTIN_1(math_atan, atan)
682 MATH_BUILTIN_1(math_sin, sin)
683 MATH_BUILTIN_1(math_cos, cos)
684 MATH_BUILTIN_1(math_sqrt, sqrt)
685 MATH_BUILTIN_1(math_tan, tan)
687 static jsdouble FASTCALL
688 math_acos_tn(MathCache *cache, jsdouble d)
690 #if defined(SOLARIS) && defined(__GNUC__)
691 if (d < -1 || 1 < d) {
692 return js_NaN;
694 #endif
695 return cache->lookup(acos, d);
698 static jsdouble FASTCALL
699 math_asin_tn(MathCache *cache, jsdouble d)
701 #if defined(SOLARIS) && defined(__GNUC__)
702 if (d < -1 || 1 < d) {
703 return js_NaN;
705 #endif
706 return cache->lookup(asin, d);
709 static jsdouble FASTCALL
710 math_exp_tn(MathCache *cache, jsdouble d)
712 return cache->lookup(math_exp_body, d);
715 JS_DEFINE_TRCINFO_1(math_exp,
716 (2, (static, DOUBLE, math_exp_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
718 static jsdouble FASTCALL
719 math_log_tn(MathCache *cache, jsdouble d)
721 #if defined(SOLARIS) && defined(__GNUC__)
722 if (d < 0)
723 return js_NaN;
724 #endif
725 return cache->lookup(log, d);
728 static jsdouble FASTCALL
729 math_max_tn(jsdouble d, jsdouble p)
731 if (JSDOUBLE_IS_NaN(d) || JSDOUBLE_IS_NaN(p))
732 return js_NaN;
734 if (p == 0 && p == d) {
735 // Max prefers 0.0 to -0.0.
736 if (js_copysign(1.0, d) == -1)
737 return p;
738 return d;
740 return (p > d) ? p : d;
743 static jsdouble FASTCALL
744 math_min_tn(jsdouble d, jsdouble p)
746 if (JSDOUBLE_IS_NaN(d) || JSDOUBLE_IS_NaN(p))
747 return js_NaN;
749 if (p == 0 && p == d) {
750 // Min prefers -0.0 to 0.0.
751 if (js_copysign (1.0, p) == -1)
752 return p;
753 return d;
755 return (p < d) ? p : d;
758 static jsdouble FASTCALL
759 math_pow_tn(jsdouble d, jsdouble p)
762 * Special case for square roots. Note that pow(x, 0.5) != sqrt(x)
763 * when x = -0.0, so we have to guard for this.
765 if (JSDOUBLE_IS_FINITE(d) && d != 0.0) {
766 if (p == 0.5)
767 return sqrt(d);
769 if (p == -0.5)
770 return 1.0/sqrt(d);
772 if (!JSDOUBLE_IS_FINITE(p) && (d == 1.0 || d == -1.0))
773 return js_NaN;
774 if (p == 0)
775 return 1.0;
776 int32_t i;
777 if (JSDOUBLE_IS_INT32(p, &i))
778 return powi(d, i);
780 return pow(d, p);
783 static jsdouble FASTCALL
784 math_random_tn(JSContext *cx)
786 return random_nextDouble(cx);
789 static jsdouble FASTCALL
790 math_round_tn(jsdouble x)
792 return js_math_round_impl(x);
795 static jsdouble FASTCALL
796 math_ceil_tn(jsdouble x)
798 return js_math_ceil_impl(x);
801 static jsdouble FASTCALL
802 math_floor_tn(jsdouble x)
804 return js_math_floor_impl(x);
807 JS_DEFINE_TRCINFO_1(math_acos,
808 (2, (static, DOUBLE, math_acos_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
809 JS_DEFINE_TRCINFO_1(math_asin,
810 (2, (static, DOUBLE, math_asin_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
811 JS_DEFINE_TRCINFO_1(math_atan2,
812 (2, (static, DOUBLE, math_atan2_kernel, DOUBLE, DOUBLE, 1, nanojit::ACCSET_NONE)))
813 JS_DEFINE_TRCINFO_1(js_math_floor,
814 (1, (static, DOUBLE, math_floor_tn, DOUBLE, 1, nanojit::ACCSET_NONE)))
815 JS_DEFINE_TRCINFO_1(math_log,
816 (2, (static, DOUBLE, math_log_tn, MATHCACHE, DOUBLE, 1, nanojit::ACCSET_NONE)))
817 JS_DEFINE_TRCINFO_1(js_math_max,
818 (2, (static, DOUBLE, math_max_tn, DOUBLE, DOUBLE, 1, nanojit::ACCSET_NONE)))
819 JS_DEFINE_TRCINFO_1(js_math_min,
820 (2, (static, DOUBLE, math_min_tn, DOUBLE, DOUBLE, 1, nanojit::ACCSET_NONE)))
821 JS_DEFINE_TRCINFO_1(math_pow,
822 (2, (static, DOUBLE, math_pow_tn, DOUBLE, DOUBLE, 1, nanojit::ACCSET_NONE)))
823 JS_DEFINE_TRCINFO_1(math_random,
824 (1, (static, DOUBLE, math_random_tn, CONTEXT, 0, nanojit::ACCSET_STORE_ANY)))
825 JS_DEFINE_TRCINFO_1(js_math_round,
826 (1, (static, DOUBLE, math_round_tn, DOUBLE, 1, nanojit::ACCSET_NONE)))
827 JS_DEFINE_TRCINFO_1(js_math_ceil,
828 (1, (static, DOUBLE, math_ceil_tn, DOUBLE, 1, nanojit::ACCSET_NONE)))
830 #endif /* JS_TRACER */
832 static JSFunctionSpec math_static_methods[] = {
833 #if JS_HAS_TOSOURCE
834 JS_FN(js_toSource_str, math_toSource, 0, 0),
835 #endif
836 JS_TN("abs", js_math_abs, 1, 0, &js_math_abs_trcinfo),
837 JS_TN("acos", math_acos, 1, 0, &math_acos_trcinfo),
838 JS_TN("asin", math_asin, 1, 0, &math_asin_trcinfo),
839 JS_TN("atan", math_atan, 1, 0, &math_atan_trcinfo),
840 JS_TN("atan2", math_atan2, 2, 0, &math_atan2_trcinfo),
841 JS_TN("ceil", js_math_ceil, 1, 0, &js_math_ceil_trcinfo),
842 JS_TN("cos", math_cos, 1, 0, &math_cos_trcinfo),
843 JS_TN("exp", math_exp, 1, 0, &math_exp_trcinfo),
844 JS_TN("floor", js_math_floor, 1, 0, &js_math_floor_trcinfo),
845 JS_TN("log", math_log, 1, 0, &math_log_trcinfo),
846 JS_TN("max", js_math_max, 2, 0, &js_math_max_trcinfo),
847 JS_TN("min", js_math_min, 2, 0, &js_math_min_trcinfo),
848 JS_TN("pow", math_pow, 2, 0, &math_pow_trcinfo),
849 JS_TN("random", math_random, 0, 0, &math_random_trcinfo),
850 JS_TN("round", js_math_round, 1, 0, &js_math_round_trcinfo),
851 JS_TN("sin", math_sin, 1, 0, &math_sin_trcinfo),
852 JS_TN("sqrt", math_sqrt, 1, 0, &math_sqrt_trcinfo),
853 JS_TN("tan", math_tan, 1, 0, &math_tan_trcinfo),
854 JS_FS_END
857 bool
858 js_IsMathFunction(JSNative native)
860 for (size_t i=0; math_static_methods[i].name != NULL; i++) {
861 if (native == math_static_methods[i].call)
862 return true;
864 return false;
867 JSObject *
868 js_InitMathClass(JSContext *cx, JSObject *obj)
870 JSObject *Math;
872 Math = JS_NewObject(cx, Jsvalify(&js_MathClass), NULL, obj);
873 if (!Math)
874 return NULL;
875 if (!JS_DefineProperty(cx, obj, js_Math_str, OBJECT_TO_JSVAL(Math),
876 JS_PropertyStub, JS_StrictPropertyStub, 0)) {
877 return NULL;
880 if (!JS_DefineFunctions(cx, Math, math_static_methods))
881 return NULL;
882 if (!JS_DefineConstDoubles(cx, Math, math_constants))
883 return NULL;
884 return Math;