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_number.h"
27 #include "swfdec_as_context.h"
28 #include "swfdec_as_frame.h"
29 #include "swfdec_as_internal.h"
30 #include "swfdec_as_native_function.h"
31 #include "swfdec_as_strings.h"
32 #include "swfdec_debug.h"
34 G_DEFINE_TYPE (SwfdecAsNumber
, swfdec_as_number
, SWFDEC_TYPE_AS_RELAY
)
37 swfdec_as_number_class_init (SwfdecAsNumberClass
*klass
)
42 swfdec_as_number_init (SwfdecAsNumber
*number
)
48 SWFDEC_AS_NATIVE (106, 2, swfdec_as_number_construct
)
50 swfdec_as_number_construct (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
51 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
56 d
= swfdec_as_value_to_number (cx
, &argv
[0]);
61 if (swfdec_as_context_is_constructing (cx
)) {
62 SwfdecAsNumber
*num
= g_object_new (SWFDEC_TYPE_AS_NUMBER
, "context", cx
, NULL
);
64 swfdec_as_object_set_relay (object
, SWFDEC_AS_RELAY (num
));
65 SWFDEC_AS_VALUE_SET_OBJECT (ret
, object
);
67 swfdec_as_value_set_number (cx
, ret
, d
);
71 // code adapted from Tamarin's convertDoubleToStringRadix in MathUtils.cpp
74 swfdec_as_number_toStringRadix (SwfdecAsContext
*context
, double value
,
79 double left
= floor (value
);
81 g_return_val_if_fail (SWFDEC_IS_AS_CONTEXT (context
), SWFDEC_AS_STR_NaN
);
82 g_return_val_if_fail (radix
>= 2 && radix
<= 36, SWFDEC_AS_STR_NaN
);
83 g_return_val_if_fail (!isinf (value
) && !isnan (value
), SWFDEC_AS_STR_NaN
);
93 return SWFDEC_AS_STR_0
;
95 str
= g_string_new ("");
102 left
= floor (left
/ radix
);
103 val
-= (left
* radix
);
105 g_string_prepend_c (str
,
106 (val
< 10 ? ((int)val
+ '0') : ((int)val
+ ('a' - 10))));
110 g_string_prepend_c (str
, '-');
112 return swfdec_as_context_give_string (context
, g_string_free (str
, FALSE
));
115 SWFDEC_AS_NATIVE (106, 1, swfdec_as_number_toString
)
117 swfdec_as_number_toString (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
118 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
125 SWFDEC_AS_CHECK (SWFDEC_TYPE_AS_NUMBER
, &num
, "|i", &radix
);
127 if (radix
== 10 || radix
< 2 || radix
> 36 || isinf (num
->number
) ||
128 isnan (num
->number
)) {
129 swfdec_as_value_set_number (cx
, &val
, num
->number
);
130 s
= swfdec_as_value_to_string (cx
, val
);
132 s
= swfdec_as_number_toStringRadix (cx
, num
->number
, radix
);
134 SWFDEC_AS_VALUE_SET_STRING (ret
, s
);
137 SWFDEC_AS_NATIVE (106, 0, swfdec_as_number_valueOf
)
139 swfdec_as_number_valueOf (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
140 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
144 SWFDEC_AS_CHECK (SWFDEC_TYPE_AS_NUMBER
, &num
, "");
146 swfdec_as_value_set_number (cx
, ret
, num
->number
);
149 // only available as ASnative
150 SWFDEC_AS_NATIVE (3, 1, swfdec_as_number_old_constructor
)
152 swfdec_as_number_old_constructor (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
153 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
155 SWFDEC_STUB ("old 'Number' function (only available as ASnative)");
158 // only available as ASnative
159 SWFDEC_AS_NATIVE (3, 4, swfdec_as_number_old_toString
)
161 swfdec_as_number_old_toString (SwfdecAsContext
*cx
, SwfdecAsObject
*object
,
162 guint argc
, SwfdecAsValue
*argv
, SwfdecAsValue
*ret
)
164 SWFDEC_STUB ("old 'Number.prototype.toString' function (only available as ASnative)");