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 // Note: All Math function call valueOf for two arguments, whether those are
38 #define MATH_FUN(name) \
40 swfdec_as_math_ ## name (SwfdecAsContext *cx, SwfdecAsObject *object, \
41 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret) \
45 *ret = swfdec_as_value_from_number (cx, NAN); \
46 SWFDEC_AS_CHECK (0, NULL, "n|n", &d, &unused); \
49 *ret = swfdec_as_value_from_number (cx, d); \
52 SWFDEC_AS_NATIVE (200, 16, swfdec_as_math_acos
)
54 SWFDEC_AS_NATIVE (200, 15, swfdec_as_math_asin
)
56 SWFDEC_AS_NATIVE (200, 14, swfdec_as_math_atan
)
58 SWFDEC_AS_NATIVE (200, 13, swfdec_as_math_ceil
)
60 SWFDEC_AS_NATIVE (200, 4, swfdec_as_math_cos
)
62 SWFDEC_AS_NATIVE (200, 7, swfdec_as_math_exp
)
64 SWFDEC_AS_NATIVE (200, 12, swfdec_as_math_floor
)
66 SWFDEC_AS_NATIVE (200, 8, swfdec_as_math_log
)
68 SWFDEC_AS_NATIVE (200, 3, swfdec_as_math_sin
)
70 SWFDEC_AS_NATIVE (200, 9, swfdec_as_math_sqrt
)
72 SWFDEC_AS_NATIVE (200, 6, swfdec_as_math_tan
)
75 SWFDEC_AS_NATIVE (200, 0, swfdec_as_math_abs
)
77 swfdec_as_math_abs (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
78 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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
)
90 swfdec_as_math_atan2 (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
91 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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
)
103 swfdec_as_math_max (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
104 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
109 *ret
= swfdec_as_value_from_number (cx
, -HUGE_VAL
);
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
)
120 swfdec_as_math_min (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
121 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
126 *ret
= swfdec_as_value_from_number (cx
, HUGE_VAL
);
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
)
137 swfdec_as_math_pow (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
138 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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
)
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
)
163 swfdec_as_math_round (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
164 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
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));