fix build for --disable-gtk-doc
[swfdec.git] / swfdec / swfdec_as_math.c
blobad3942aa22de8f2b8f7c987b180e841638fd97e5
1 /* Swfdec
2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <math.h>
26 #include "swfdec_as_object.h"
27 #include "swfdec_as_context.h"
28 #include "swfdec_as_strings.h"
29 #include "swfdec_as_internal.h"
30 #include "swfdec_as_native_function.h"
31 #include "swfdec_debug.h"
33 /*** AS CODE ***/
35 // Note: All Math function call valueOf for two arguments, whether those are
36 // needed or not
38 #define MATH_FUN(name) \
39 void \
40 swfdec_as_math_ ## name (SwfdecAsContext *cx, SwfdecAsObject *object, \
41 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret) \
42 { \
43 double d, unused; \
45 *ret = swfdec_as_value_from_number (cx, NAN); \
46 SWFDEC_AS_CHECK (0, NULL, "n|n", &d, &unused); \
48 d = name (d); \
49 *ret = swfdec_as_value_from_number (cx, d); \
52 SWFDEC_AS_NATIVE (200, 16, swfdec_as_math_acos)
53 MATH_FUN (acos)
54 SWFDEC_AS_NATIVE (200, 15, swfdec_as_math_asin)
55 MATH_FUN (asin)
56 SWFDEC_AS_NATIVE (200, 14, swfdec_as_math_atan)
57 MATH_FUN (atan)
58 SWFDEC_AS_NATIVE (200, 13, swfdec_as_math_ceil)
59 MATH_FUN (ceil)
60 SWFDEC_AS_NATIVE (200, 4, swfdec_as_math_cos)
61 MATH_FUN (cos)
62 SWFDEC_AS_NATIVE (200, 7, swfdec_as_math_exp)
63 MATH_FUN (exp)
64 SWFDEC_AS_NATIVE (200, 12, swfdec_as_math_floor)
65 MATH_FUN (floor)
66 SWFDEC_AS_NATIVE (200, 8, swfdec_as_math_log)
67 MATH_FUN (log)
68 SWFDEC_AS_NATIVE (200, 3, swfdec_as_math_sin)
69 MATH_FUN (sin)
70 SWFDEC_AS_NATIVE (200, 9, swfdec_as_math_sqrt)
71 MATH_FUN (sqrt)
72 SWFDEC_AS_NATIVE (200, 6, swfdec_as_math_tan)
73 MATH_FUN (tan)
75 SWFDEC_AS_NATIVE (200, 0, swfdec_as_math_abs)
76 void
77 swfdec_as_math_abs (SwfdecAsContext *cx, SwfdecAsObject *object,
78 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
80 double d, unused;
82 *ret = swfdec_as_value_from_number (cx, NAN);
83 SWFDEC_AS_CHECK (0, NULL, "n|n", &d, &unused);
85 *ret = swfdec_as_value_from_number (cx, fabs (d));
88 SWFDEC_AS_NATIVE (200, 5, swfdec_as_math_atan2)
89 void
90 swfdec_as_math_atan2 (SwfdecAsContext *cx, SwfdecAsObject *object,
91 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
93 double x, y;
95 *ret = swfdec_as_value_from_number (cx, NAN);
96 SWFDEC_AS_CHECK (0, NULL, "nn", &y, &x);
98 *ret = swfdec_as_value_from_number (cx, atan2 (y, x));
101 SWFDEC_AS_NATIVE (200, 2, swfdec_as_math_max)
102 void
103 swfdec_as_math_max (SwfdecAsContext *cx, SwfdecAsObject *object,
104 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
106 double x, y;
108 if (argc == 0) {
109 *ret = swfdec_as_value_from_number (cx, -HUGE_VAL);
110 } else {
111 *ret = swfdec_as_value_from_number (cx, NAN);
113 SWFDEC_AS_CHECK (0, NULL, "nn", &x, &y);
115 *ret = swfdec_as_value_from_number (cx, isnan (x) || isnan (y) ? NAN : MAX (x, y));
118 SWFDEC_AS_NATIVE (200, 1, swfdec_as_math_min)
119 void
120 swfdec_as_math_min (SwfdecAsContext *cx, SwfdecAsObject *object,
121 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
123 double x, y;
125 if (argc == 0) {
126 *ret = swfdec_as_value_from_number (cx, HUGE_VAL);
127 } else {
128 *ret = swfdec_as_value_from_number (cx, NAN);
130 SWFDEC_AS_CHECK (0, NULL, "nn", &x, &y);
132 *ret = swfdec_as_value_from_number (cx, isnan (x) || isnan (y) ? NAN : MIN (x, y));
135 SWFDEC_AS_NATIVE (200, 17, swfdec_as_math_pow)
136 void
137 swfdec_as_math_pow (SwfdecAsContext *cx, SwfdecAsObject *object,
138 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
140 double x, y;
142 *ret = swfdec_as_value_from_number (cx, NAN);
143 SWFDEC_AS_CHECK (0, NULL, "nn", &x, &y);
145 *ret = swfdec_as_value_from_number (cx, isfinite (x) ? pow (x, y): NAN);
148 SWFDEC_AS_NATIVE (200, 11, swfdec_as_math_random)
149 void
150 swfdec_as_math_random (SwfdecAsContext *cx, SwfdecAsObject *object,
151 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
153 double unused, unused2;
155 *ret = swfdec_as_value_from_number (cx, NAN);
156 SWFDEC_AS_CHECK (0, NULL, "|nn", &unused, &unused2);
158 *ret = swfdec_as_value_from_number (cx, g_rand_double (cx->rand));
161 SWFDEC_AS_NATIVE (200, 10, swfdec_as_math_round)
162 void
163 swfdec_as_math_round (SwfdecAsContext *cx, SwfdecAsObject *object,
164 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
166 double d, unused;
168 *ret = swfdec_as_value_from_number (cx, NAN);
169 SWFDEC_AS_CHECK (0, NULL, "n|n", &d, &unused);
171 *ret = swfdec_as_value_from_number (cx, floor (d + 0.5));