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.
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
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"
35 #define MATH_FUN(name) \
37 swfdec_as_math_ ## name (SwfdecAsContext *cx, SwfdecAsObject *object, \
38 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret) \
42 SWFDEC_AS_CHECK (0, NULL, "n", &d); \
45 SWFDEC_AS_VALUE_SET_NUMBER (ret, d); \
48 SWFDEC_AS_NATIVE (200, 16, swfdec_as_math_acos
)
50 SWFDEC_AS_NATIVE (200, 15, swfdec_as_math_asin
)
52 SWFDEC_AS_NATIVE (200, 14, swfdec_as_math_atan
)
54 SWFDEC_AS_NATIVE (200, 13, swfdec_as_math_ceil
)
56 SWFDEC_AS_NATIVE (200, 4, swfdec_as_math_cos
)
58 SWFDEC_AS_NATIVE (200, 7, swfdec_as_math_exp
)
60 SWFDEC_AS_NATIVE (200, 12, swfdec_as_math_floor
)
62 SWFDEC_AS_NATIVE (200, 8, swfdec_as_math_log
)
64 SWFDEC_AS_NATIVE (200, 3, swfdec_as_math_sin
)
66 SWFDEC_AS_NATIVE (200, 9, swfdec_as_math_sqrt
)
68 SWFDEC_AS_NATIVE (200, 6, swfdec_as_math_tan
)
71 SWFDEC_AS_NATIVE (200, 0, swfdec_as_math_abs
)
73 swfdec_as_math_abs (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
74 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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
)
85 swfdec_as_math_atan2 (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
86 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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
)
97 swfdec_as_math_max (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
98 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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
)
109 swfdec_as_math_min (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
110 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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
)
121 swfdec_as_math_pow (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
122 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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
)
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
)
141 swfdec_as_math_round (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
142 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
146 SWFDEC_AS_CHECK (0, NULL
, "n", &d
);
148 SWFDEC_AS_VALUE_SET_NUMBER (ret
, floor (d
+ 0.5));