/[jscoverage]/trunk/js/jsmath.cpp
ViewVC logotype

Contents of /trunk/js/jsmath.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 507 - (show annotations)
Sun Jan 10 07:23:34 2010 UTC (9 years, 9 months ago) by siliconforks
File size: 20028 byte(s)
Update SpiderMonkey from Firefox 3.6rc1.

1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
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/
10 *
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.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
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.
23 *
24 * Contributor(s):
25 *
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.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 /*
41 * JS math package.
42 */
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 "jsobj.h"
58
59 extern jsdouble js_NaN;
60
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
85
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}}
96 };
97
98 JSClass js_MathClass = {
99 js_Math_str,
100 JSCLASS_HAS_CACHED_PROTO(JSProto_Math),
101 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
102 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
103 JSCLASS_NO_OPTIONAL_MEMBERS
104 };
105
106 static JSBool
107 math_abs(JSContext *cx, uintN argc, jsval *vp)
108 {
109 jsdouble x, z;
110
111 if (argc == 0) {
112 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
113 return JS_TRUE;
114 }
115 x = js_ValueToNumber(cx, &vp[2]);
116 if (JSVAL_IS_NULL(vp[2]))
117 return JS_FALSE;
118 z = fabs(x);
119 return js_NewNumberInRootedValue(cx, z, vp);
120 }
121
122 static JSBool
123 math_acos(JSContext *cx, uintN argc, jsval *vp)
124 {
125 jsdouble x, z;
126
127 if (argc == 0) {
128 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
129 return JS_TRUE;
130 }
131 x = js_ValueToNumber(cx, &vp[2]);
132 if (JSVAL_IS_NULL(vp[2]))
133 return JS_FALSE;
134 #if defined(SOLARIS) && defined(__GNUC__)
135 if (x < -1 || 1 < x) {
136 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
137 return JS_TRUE;
138 }
139 #endif
140 z = acos(x);
141 return js_NewNumberInRootedValue(cx, z, vp);
142 }
143
144 static JSBool
145 math_asin(JSContext *cx, uintN argc, jsval *vp)
146 {
147 jsdouble x, z;
148
149 if (argc == 0) {
150 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
151 return JS_TRUE;
152 }
153 x = js_ValueToNumber(cx, &vp[2]);
154 if (JSVAL_IS_NULL(vp[2]))
155 return JS_FALSE;
156 #if defined(SOLARIS) && defined(__GNUC__)
157 if (x < -1 || 1 < x) {
158 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
159 return JS_TRUE;
160 }
161 #endif
162 z = asin(x);
163 return js_NewNumberInRootedValue(cx, z, vp);
164 }
165
166 static JSBool
167 math_atan(JSContext *cx, uintN argc, jsval *vp)
168 {
169 jsdouble x, z;
170
171 if (argc == 0) {
172 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
173 return JS_TRUE;
174 }
175 x = js_ValueToNumber(cx, &vp[2]);
176 if (JSVAL_IS_NULL(vp[2]))
177 return JS_FALSE;
178 z = atan(x);
179 return js_NewNumberInRootedValue(cx, z, vp);
180 }
181
182 static inline jsdouble JS_FASTCALL
183 math_atan2_kernel(jsdouble x, jsdouble y)
184 {
185 #if defined(_MSC_VER)
186 /*
187 * MSVC's atan2 does not yield the result demanded by ECMA when both x
188 * and y are infinite.
189 * - The result is a multiple of pi/4.
190 * - The sign of x determines the sign of the result.
191 * - The sign of y determines the multiplicator, 1 or 3.
192 */
193 if (JSDOUBLE_IS_INFINITE(x) && JSDOUBLE_IS_INFINITE(y)) {
194 jsdouble z = js_copysign(M_PI / 4, x);
195 if (y < 0)
196 z *= 3;
197 return z;
198 }
199 #endif
200
201 #if defined(SOLARIS) && defined(__GNUC__)
202 if (x == 0) {
203 if (JSDOUBLE_IS_NEGZERO(y))
204 return js_copysign(M_PI, x);
205 if (y == 0)
206 return x;
207 }
208 #endif
209 return atan2(x, y);
210 }
211
212 static JSBool
213 math_atan2(JSContext *cx, uintN argc, jsval *vp)
214 {
215 jsdouble x, y;
216
217 if (argc <= 1) {
218 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
219 return JS_TRUE;
220 }
221 x = js_ValueToNumber(cx, &vp[2]);
222 if (JSVAL_IS_NULL(vp[2]))
223 return JS_FALSE;
224 y = js_ValueToNumber(cx, &vp[3]);
225 if (JSVAL_IS_NULL(vp[3]))
226 return JS_FALSE;
227 return js_NewNumberInRootedValue(cx, math_atan2_kernel (x, y), vp);
228 }
229
230 static inline jsdouble JS_FASTCALL
231 math_ceil_kernel(jsdouble x)
232 {
233 #ifdef __APPLE__
234 if (x < 0 && x > -1.0)
235 return js_copysign(0, -1);
236 #endif
237 return ceil(x);
238 }
239
240 JSBool
241 js_math_ceil(JSContext *cx, uintN argc, jsval *vp)
242 {
243 jsdouble x, z;
244
245 if (argc == 0) {
246 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
247 return JS_TRUE;
248 }
249 x = js_ValueToNumber(cx, &vp[2]);
250 if (JSVAL_IS_NULL(vp[2]))
251 return JS_FALSE;
252 z = math_ceil_kernel(x);
253 return js_NewNumberInRootedValue(cx, z, vp);
254 }
255
256 static JSBool
257 math_cos(JSContext *cx, uintN argc, jsval *vp)
258 {
259 jsdouble x, z;
260
261 if (argc == 0) {
262 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
263 return JS_TRUE;
264 }
265 x = js_ValueToNumber(cx, &vp[2]);
266 if (JSVAL_IS_NULL(vp[2]))
267 return JS_FALSE;
268 z = cos(x);
269 return js_NewNumberInRootedValue(cx, z, vp);
270 }
271
272 static JSBool
273 math_exp(JSContext *cx, uintN argc, jsval *vp)
274 {
275 jsdouble x, z;
276
277 if (argc == 0) {
278 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
279 return JS_TRUE;
280 }
281 x = js_ValueToNumber(cx, &vp[2]);
282 if (JSVAL_IS_NULL(vp[2]))
283 return JS_FALSE;
284 #ifdef _WIN32
285 if (!JSDOUBLE_IS_NaN(x)) {
286 if (x == *cx->runtime->jsPositiveInfinity) {
287 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsPositiveInfinity);
288 return JS_TRUE;
289 }
290 if (x == *cx->runtime->jsNegativeInfinity) {
291 *vp = JSVAL_ZERO;
292 return JS_TRUE;
293 }
294 }
295 #endif
296 z = exp(x);
297 return js_NewNumberInRootedValue(cx, z, vp);
298 }
299
300 JSBool
301 js_math_floor(JSContext *cx, uintN argc, jsval *vp)
302 {
303 jsdouble x, z;
304
305 if (argc == 0) {
306 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
307 return JS_TRUE;
308 }
309 x = js_ValueToNumber(cx, &vp[2]);
310 if (JSVAL_IS_NULL(vp[2]))
311 return JS_FALSE;
312 z = floor(x);
313 return js_NewNumberInRootedValue(cx, z, vp);
314 }
315
316 static JSBool
317 math_log(JSContext *cx, uintN argc, jsval *vp)
318 {
319 jsdouble x, z;
320
321 if (argc == 0) {
322 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
323 return JS_TRUE;
324 }
325 x = js_ValueToNumber(cx, &vp[2]);
326 if (JSVAL_IS_NULL(vp[2]))
327 return JS_FALSE;
328 #if defined(SOLARIS) && defined(__GNUC__)
329 if (x < 0) {
330 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
331 return JS_TRUE;
332 }
333 #endif
334 z = log(x);
335 return js_NewNumberInRootedValue(cx, z, vp);
336 }
337
338 JSBool
339 js_math_max(JSContext *cx, uintN argc, jsval *vp)
340 {
341 jsdouble x, z = *cx->runtime->jsNegativeInfinity;
342 jsval *argv;
343 uintN i;
344
345 if (argc == 0) {
346 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNegativeInfinity);
347 return JS_TRUE;
348 }
349 argv = vp + 2;
350 for (i = 0; i < argc; i++) {
351 x = js_ValueToNumber(cx, &argv[i]);
352 if (JSVAL_IS_NULL(argv[i]))
353 return JS_FALSE;
354 if (JSDOUBLE_IS_NaN(x)) {
355 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
356 return JS_TRUE;
357 }
358 if (x == 0 && x == z) {
359 if (js_copysign(1.0, z) == -1)
360 z = x;
361 } else {
362 z = (x > z) ? x : z;
363 }
364 }
365 return js_NewNumberInRootedValue(cx, z, vp);
366 }
367
368 JSBool
369 js_math_min(JSContext *cx, uintN argc, jsval *vp)
370 {
371 jsdouble x, z = *cx->runtime->jsPositiveInfinity;
372 jsval *argv;
373 uintN i;
374
375 if (argc == 0) {
376 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsPositiveInfinity);
377 return JS_TRUE;
378 }
379 argv = vp + 2;
380 for (i = 0; i < argc; i++) {
381 x = js_ValueToNumber(cx, &argv[i]);
382 if (JSVAL_IS_NULL(argv[i]))
383 return JS_FALSE;
384 if (JSDOUBLE_IS_NaN(x)) {
385 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
386 return JS_TRUE;
387 }
388 if (x == 0 && x == z) {
389 if (js_copysign(1.0, x) == -1)
390 z = x;
391 } else {
392 z = (x < z) ? x : z;
393 }
394 }
395 return js_NewNumberInRootedValue(cx, z, vp);
396 }
397
398 static JSBool
399 math_pow(JSContext *cx, uintN argc, jsval *vp)
400 {
401 jsdouble x, y, z;
402
403 if (argc <= 1) {
404 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
405 return JS_TRUE;
406 }
407 x = js_ValueToNumber(cx, &vp[2]);
408 if (JSVAL_IS_NULL(vp[2]))
409 return JS_FALSE;
410 y = js_ValueToNumber(cx, &vp[3]);
411 if (JSVAL_IS_NULL(vp[3]))
412 return JS_FALSE;
413 /*
414 * Because C99 and ECMA specify different behavior for pow(),
415 * we need to wrap the libm call to make it ECMA compliant.
416 */
417 if (!JSDOUBLE_IS_FINITE(y) && (x == 1.0 || x == -1.0)) {
418 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
419 return JS_TRUE;
420 }
421 /* pow(x, +-0) is always 1, even for x = NaN. */
422 if (y == 0) {
423 *vp = JSVAL_ONE;
424 return JS_TRUE;
425 }
426 z = pow(x, y);
427 return js_NewNumberInRootedValue(cx, z, vp);
428 }
429
430 static const int64 RNG_MULTIPLIER = 0x5DEECE66DLL;
431 static const int64 RNG_ADDEND = 0xBLL;
432 static const int64 RNG_MASK = (1LL << 48) - 1;
433 static const jsdouble RNG_DSCALE = jsdouble(1LL << 53);
434
435 /*
436 * Math.random() support, lifted from java.util.Random.java.
437 */
438 static inline void
439 random_setSeed(JSThreadData *data, int64 seed)
440 {
441 data->rngSeed = (seed ^ RNG_MULTIPLIER) & RNG_MASK;
442 }
443
444 void
445 js_InitRandom(JSThreadData *data)
446 {
447 /* Finally, set the seed from current time. */
448 random_setSeed(data, PRMJ_Now() / 1000);
449 }
450
451 static inline uint64
452 random_next(JSThreadData *data, int bits)
453 {
454 uint64 nextseed = data->rngSeed * RNG_MULTIPLIER;
455 nextseed += RNG_ADDEND;
456 nextseed &= RNG_MASK;
457 data->rngSeed = nextseed;
458 return nextseed >> (48 - bits);
459 }
460
461 static inline jsdouble
462 random_nextDouble(JSThreadData *data)
463 {
464 return jsdouble((random_next(data, 26) << 27) + random_next(data, 27)) / RNG_DSCALE;
465 }
466
467 static JSBool
468 math_random(JSContext *cx, uintN argc, jsval *vp)
469 {
470 jsdouble z = random_nextDouble(JS_THREAD_DATA(cx));
471 return js_NewNumberInRootedValue(cx, z, vp);
472 }
473
474 #if defined _WIN32 && !defined WINCE && _MSC_VER < 1400
475 /* Try to work around apparent _copysign bustage in VC6 and VC7. */
476 double
477 js_copysign(double x, double y)
478 {
479 jsdpun xu, yu;
480
481 xu.d = x;
482 yu.d = y;
483 xu.s.hi &= ~JSDOUBLE_HI32_SIGNBIT;
484 xu.s.hi |= yu.s.hi & JSDOUBLE_HI32_SIGNBIT;
485 return xu.d;
486 }
487 #endif
488
489 JSBool
490 js_math_round(JSContext *cx, uintN argc, jsval *vp)
491 {
492 jsdouble x, z;
493
494 if (argc == 0) {
495 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
496 return JS_TRUE;
497 }
498 x = js_ValueToNumber(cx, &vp[2]);
499 if (JSVAL_IS_NULL(vp[2]))
500 return JS_FALSE;
501 z = js_copysign(floor(x + 0.5), x);
502 return js_NewNumberInRootedValue(cx, z, vp);
503 }
504
505 static JSBool
506 math_sin(JSContext *cx, uintN argc, jsval *vp)
507 {
508 jsdouble x, z;
509
510 if (argc == 0) {
511 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
512 return JS_TRUE;
513 }
514 x = js_ValueToNumber(cx, &vp[2]);
515 if (JSVAL_IS_NULL(vp[2]))
516 return JS_FALSE;
517 z = sin(x);
518 return js_NewNumberInRootedValue(cx, z, vp);
519 }
520
521 static JSBool
522 math_sqrt(JSContext *cx, uintN argc, jsval *vp)
523 {
524 jsdouble x, z;
525
526 if (argc == 0) {
527 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
528 return JS_TRUE;
529 }
530 x = js_ValueToNumber(cx, &vp[2]);
531 if (JSVAL_IS_NULL(vp[2]))
532 return JS_FALSE;
533 z = sqrt(x);
534 return js_NewNumberInRootedValue(cx, z, vp);
535 }
536
537 static JSBool
538 math_tan(JSContext *cx, uintN argc, jsval *vp)
539 {
540 jsdouble x, z;
541
542 if (argc == 0) {
543 *vp = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
544 return JS_TRUE;
545 }
546 x = js_ValueToNumber(cx, &vp[2]);
547 if (JSVAL_IS_NULL(vp[2]))
548 return JS_FALSE;
549 z = tan(x);
550 return js_NewNumberInRootedValue(cx, z, vp);
551 }
552
553 #if JS_HAS_TOSOURCE
554 static JSBool
555 math_toSource(JSContext *cx, uintN argc, jsval *vp)
556 {
557 *vp = ATOM_KEY(CLASS_ATOM(cx, Math));
558 return JS_TRUE;
559 }
560 #endif
561
562 #ifdef JS_TRACER
563
564 #define MATH_BUILTIN_1(name) MATH_BUILTIN_CFUN_1(name, name)
565 #define MATH_BUILTIN_CFUN_1(name, cfun) \
566 static jsdouble FASTCALL math_##name##_tn(jsdouble d) { return cfun(d); } \
567 JS_DEFINE_TRCINFO_1(math_##name, \
568 (1, (static, DOUBLE, math_##name##_tn, DOUBLE, 1, 1)))
569
570 MATH_BUILTIN_CFUN_1(abs, fabs)
571 MATH_BUILTIN_1(atan)
572 MATH_BUILTIN_1(sin)
573 MATH_BUILTIN_1(cos)
574 MATH_BUILTIN_1(sqrt)
575 MATH_BUILTIN_1(tan)
576
577 static jsdouble FASTCALL
578 math_acos_tn(jsdouble d)
579 {
580 #if defined(SOLARIS) && defined(__GNUC__)
581 if (d < -1 || 1 < d) {
582 return js_NaN;
583 }
584 #endif
585 return acos(d);
586 }
587
588 static jsdouble FASTCALL
589 math_asin_tn(jsdouble d)
590 {
591 #if defined(SOLARIS) && defined(__GNUC__)
592 if (d < -1 || 1 < d) {
593 return js_NaN;
594 }
595 #endif
596 return asin(d);
597 }
598
599 #ifdef _WIN32
600
601 static jsdouble FASTCALL
602 math_exp_tn(JSContext *cx, jsdouble d)
603 {
604 if (!JSDOUBLE_IS_NaN(d)) {
605 if (d == *cx->runtime->jsPositiveInfinity) {
606 return *cx->runtime->jsPositiveInfinity;
607 }
608 if (d == *cx->runtime->jsNegativeInfinity) {
609 return 0.0;
610 }
611 }
612 return exp(d);
613 }
614
615 JS_DEFINE_TRCINFO_1(math_exp,
616 (2, (static, DOUBLE, math_exp_tn, CONTEXT, DOUBLE, 1, 1)))
617
618 #else
619
620 MATH_BUILTIN_1(exp)
621
622 #endif
623
624 static jsdouble FASTCALL
625 math_log_tn(jsdouble d)
626 {
627 #if defined(SOLARIS) && defined(__GNUC__)
628 if (d < 0)
629 return js_NaN;
630 #endif
631 return log(d);
632 }
633
634 static jsdouble FASTCALL
635 math_max_tn(jsdouble d, jsdouble p)
636 {
637 if (JSDOUBLE_IS_NaN(d) || JSDOUBLE_IS_NaN(p))
638 return js_NaN;
639
640 if (p == 0 && p == d) {
641 // Max prefers 0.0 to -0.0.
642 if (js_copysign(1.0, d) == -1)
643 return p;
644 return d;
645 }
646 return (p > d) ? p : d;
647 }
648
649 static jsdouble FASTCALL
650 math_min_tn(jsdouble d, jsdouble p)
651 {
652 if (JSDOUBLE_IS_NaN(d) || JSDOUBLE_IS_NaN(p))
653 return js_NaN;
654
655 if (p == 0 && p == d) {
656 // Min prefers -0.0 to 0.0.
657 if (js_copysign (1.0, p) == -1)
658 return p;
659 return d;
660 }
661 return (p < d) ? p : d;
662 }
663
664 static jsdouble FASTCALL
665 math_pow_tn(jsdouble d, jsdouble p)
666 {
667 if (!JSDOUBLE_IS_FINITE(p) && (d == 1.0 || d == -1.0))
668 return js_NaN;
669 if (p == 0)
670 return 1.0;
671 return pow(d, p);
672 }
673
674 static jsdouble FASTCALL
675 math_random_tn(JSContext *cx)
676 {
677 return random_nextDouble(JS_THREAD_DATA(cx));
678 }
679
680 static jsdouble FASTCALL
681 math_round_tn(jsdouble x)
682 {
683 return js_copysign(floor(x + 0.5), x);
684 }
685
686 static jsdouble FASTCALL
687 math_ceil_tn(jsdouble x)
688 {
689 return math_ceil_kernel(x);
690 }
691
692 static jsdouble FASTCALL
693 math_floor_tn(jsdouble x)
694 {
695 return floor(x);
696 }
697
698 JS_DEFINE_TRCINFO_1(math_acos,
699 (1, (static, DOUBLE, math_acos_tn, DOUBLE, 1, 1)))
700 JS_DEFINE_TRCINFO_1(math_asin,
701 (1, (static, DOUBLE, math_asin_tn, DOUBLE, 1, 1)))
702 JS_DEFINE_TRCINFO_1(math_atan2,
703 (2, (static, DOUBLE, math_atan2_kernel, DOUBLE, DOUBLE, 1, 1)))
704 JS_DEFINE_TRCINFO_1(js_math_floor,
705 (1, (static, DOUBLE, math_floor_tn, DOUBLE, 1, 1)))
706 JS_DEFINE_TRCINFO_1(math_log,
707 (1, (static, DOUBLE, math_log_tn, DOUBLE, 1, 1)))
708 JS_DEFINE_TRCINFO_1(js_math_max,
709 (2, (static, DOUBLE, math_max_tn, DOUBLE, DOUBLE, 1, 1)))
710 JS_DEFINE_TRCINFO_1(js_math_min,
711 (2, (static, DOUBLE, math_min_tn, DOUBLE, DOUBLE, 1, 1)))
712 JS_DEFINE_TRCINFO_1(math_pow,
713 (2, (static, DOUBLE, math_pow_tn, DOUBLE, DOUBLE, 1, 1)))
714 JS_DEFINE_TRCINFO_1(math_random,
715 (1, (static, DOUBLE, math_random_tn, CONTEXT, 0, 0)))
716 JS_DEFINE_TRCINFO_1(js_math_round,
717 (1, (static, DOUBLE, math_round_tn, DOUBLE, 1, 1)))
718 JS_DEFINE_TRCINFO_1(js_math_ceil,
719 (1, (static, DOUBLE, math_ceil_tn, DOUBLE, 1, 1)))
720
721 #endif /* JS_TRACER */
722
723 static JSFunctionSpec math_static_methods[] = {
724 #if JS_HAS_TOSOURCE
725 JS_FN(js_toSource_str, math_toSource, 0, 0),
726 #endif
727 JS_TN("abs", math_abs, 1, 0, &math_abs_trcinfo),
728 JS_TN("acos", math_acos, 1, 0, &math_acos_trcinfo),
729 JS_TN("asin", math_asin, 1, 0, &math_asin_trcinfo),
730 JS_TN("atan", math_atan, 1, 0, &math_atan_trcinfo),
731 JS_TN("atan2", math_atan2, 2, 0, &math_atan2_trcinfo),
732 JS_TN("ceil", js_math_ceil, 1, 0, &js_math_ceil_trcinfo),
733 JS_TN("cos", math_cos, 1, 0, &math_cos_trcinfo),
734 JS_TN("exp", math_exp, 1, 0, &math_exp_trcinfo),
735 JS_TN("floor", js_math_floor, 1, 0, &js_math_floor_trcinfo),
736 JS_TN("log", math_log, 1, 0, &math_log_trcinfo),
737 JS_TN("max", js_math_max, 2, 0, &js_math_max_trcinfo),
738 JS_TN("min", js_math_min, 2, 0, &js_math_min_trcinfo),
739 JS_TN("pow", math_pow, 2, 0, &math_pow_trcinfo),
740 JS_TN("random", math_random, 0, 0, &math_random_trcinfo),
741 JS_TN("round", js_math_round, 1, 0, &js_math_round_trcinfo),
742 JS_TN("sin", math_sin, 1, 0, &math_sin_trcinfo),
743 JS_TN("sqrt", math_sqrt, 1, 0, &math_sqrt_trcinfo),
744 JS_TN("tan", math_tan, 1, 0, &math_tan_trcinfo),
745 JS_FS_END
746 };
747
748 JSObject *
749 js_InitMathClass(JSContext *cx, JSObject *obj)
750 {
751 JSObject *Math;
752
753 Math = JS_NewObject(cx, &js_MathClass, NULL, obj);
754 if (!Math)
755 return NULL;
756 if (!JS_DefineProperty(cx, obj, js_Math_str, OBJECT_TO_JSVAL(Math),
757 JS_PropertyStub, JS_PropertyStub, 0)) {
758 return NULL;
759 }
760
761 if (!JS_DefineFunctions(cx, Math, math_static_methods))
762 return NULL;
763 if (!JS_DefineConstDoubles(cx, Math, math_constants))
764 return NULL;
765 return Math;
766 }

  ViewVC Help
Powered by ViewVC 1.1.24