first step at rewriting the button code - nothing works, but it compiles
[swfdec.git] / libswfdec / swfdec_as_math.c
blob30b0c6e91805cb704f59a1036c2b718e120254b5
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 #define MATH_FUN(name) \
36 void \
37 swfdec_as_math_ ## name (SwfdecAsContext *cx, SwfdecAsObject *object, \
38 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret) \
39 { \
40 double d; \
42 SWFDEC_AS_CHECK (0, NULL, "n", &d); \
44 d = name (d); \
45 SWFDEC_AS_VALUE_SET_NUMBER (ret, d); \
48 SWFDEC_AS_NATIVE (200, 16, swfdec_as_math_acos)
49 MATH_FUN (acos)
50 SWFDEC_AS_NATIVE (200, 15, swfdec_as_math_asin)
51 MATH_FUN (asin)
52 SWFDEC_AS_NATIVE (200, 14, swfdec_as_math_atan)
53 MATH_FUN (atan)
54 SWFDEC_AS_NATIVE (200, 13, swfdec_as_math_ceil)
55 MATH_FUN (ceil)
56 SWFDEC_AS_NATIVE (200, 4, swfdec_as_math_cos)
57 MATH_FUN (cos)
58 SWFDEC_AS_NATIVE (200, 7, swfdec_as_math_exp)
59 MATH_FUN (exp)
60 SWFDEC_AS_NATIVE (200, 12, swfdec_as_math_floor)
61 MATH_FUN (floor)
62 SWFDEC_AS_NATIVE (200, 8, swfdec_as_math_log)
63 MATH_FUN (log)
64 SWFDEC_AS_NATIVE (200, 3, swfdec_as_math_sin)
65 MATH_FUN (sin)
66 SWFDEC_AS_NATIVE (200, 9, swfdec_as_math_sqrt)
67 MATH_FUN (sqrt)
68 SWFDEC_AS_NATIVE (200, 6, swfdec_as_math_tan)
69 MATH_FUN (tan)
71 SWFDEC_AS_NATIVE (200, 0, swfdec_as_math_abs)
72 void
73 swfdec_as_math_abs (SwfdecAsContext *cx, SwfdecAsObject *object,
74 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
76 double d;
78 SWFDEC_AS_CHECK (0, NULL, "n", &d);
80 SWFDEC_AS_VALUE_SET_NUMBER (ret, fabs (d));
83 SWFDEC_AS_NATIVE (200, 5, swfdec_as_math_atan2)
84 void
85 swfdec_as_math_atan2 (SwfdecAsContext *cx, SwfdecAsObject *object,
86 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
88 double x, y;
90 SWFDEC_AS_CHECK (0, NULL, "nn", &y, &x);
92 SWFDEC_AS_VALUE_SET_NUMBER (ret, atan2 (y, x));
95 SWFDEC_AS_NATIVE (200, 2, swfdec_as_math_max)
96 void
97 swfdec_as_math_max (SwfdecAsContext *cx, SwfdecAsObject *object,
98 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
100 double x, y;
102 SWFDEC_AS_CHECK (0, NULL, "nn", &x, &y);
104 SWFDEC_AS_VALUE_SET_NUMBER (ret, MAX (x, y));
107 SWFDEC_AS_NATIVE (200, 1, swfdec_as_math_min)
108 void
109 swfdec_as_math_min (SwfdecAsContext *cx, SwfdecAsObject *object,
110 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
112 double x, y;
114 SWFDEC_AS_CHECK (0, NULL, "nn", &x, &y);
116 SWFDEC_AS_VALUE_SET_NUMBER (ret, MIN (x, y));
119 SWFDEC_AS_NATIVE (200, 17, swfdec_as_math_pow)
120 void
121 swfdec_as_math_pow (SwfdecAsContext *cx, SwfdecAsObject *object,
122 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
124 double x, y;
126 SWFDEC_AS_CHECK (0, NULL, "nn", &x, &y);
128 SWFDEC_AS_VALUE_SET_NUMBER (ret, pow (x, y));
131 SWFDEC_AS_NATIVE (200, 11, swfdec_as_math_random)
132 void
133 swfdec_as_math_random (SwfdecAsContext *cx, SwfdecAsObject *object,
134 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
136 SWFDEC_AS_VALUE_SET_NUMBER (ret, g_rand_double (cx->rand));
139 SWFDEC_AS_NATIVE (200, 10, swfdec_as_math_round)
140 void
141 swfdec_as_math_round (SwfdecAsContext *cx, SwfdecAsObject *object,
142 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
144 double d;
146 SWFDEC_AS_CHECK (0, NULL, "n", &d);
148 SWFDEC_AS_VALUE_SET_NUMBER (ret, floor (d + 0.5));