1 // Number.cpp: ActionScript Number class, for Gnash.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "Number_as.h"
24 #include "smart_ptr.h"
26 #include "Global_as.h"
27 #include "as_object.h" // for inheritance
28 #include "as_value.h" // for doubleToString
29 #include "builtin_function.h"
30 #include "NativeFunction.h"
33 #include "namedStrings.h"
40 class Number_as
: public Relay
50 double value() const {
64 number_toString(const fn_call
& fn
)
66 // Number.toString must only work for number object, not generic ones.
67 // This is so trace(Number.prototype) doesn't return 0 ...
68 Number_as
* obj
= ensure
<ThisIsNative
<Number_as
> >(fn
);
70 double val
= obj
->value();
75 int userRadix
= toInt(fn
.arg(0), getVM(fn
));
76 if ( userRadix
>= 2 && userRadix
<= 36 ) radix
=userRadix
;
79 IF_VERBOSE_ASCODING_ERRORS(
80 log_aserror(_("Number.toString(%s): "
81 "radix must be in the 2..36 range (%d is invalid)"),
87 return doubleToString(val
, radix
);
91 number_valueOf(const fn_call
& fn
)
93 // Number.valueOf must only work for number object, not generic ones.
94 // This is so trace(Number.prototype == Object) return true in swf5 ?
95 Number_as
* obj
= ensure
<ThisIsNative
<Number_as
> >(fn
);
101 number_ctor(const fn_call
& fn
)
105 val
= toNumber(fn
.arg(0), getVM(fn
));
108 if (!fn
.isInstantiation()) {
109 return as_value(val
);
112 fn
.this_ptr
->setRelay(new Number_as(val
));
118 attachNumberInterface(as_object
& o
)
121 o
.init_member("valueOf", vm
.getNative(106, 0));
122 o
.init_member("toString", vm
.getNative(106, 1));
126 attachNumberStaticInterface(as_object
& o
)
129 const int cflags
= as_object::DefaultFlags
| PropFlags::readOnly
;
131 // Set __proto__ and constructor to constant.
132 as_value null
; null
.set_null();
133 o
.setPropFlags(null
, 0, cflags
);
135 o
.init_member("MAX_VALUE",
136 std::numeric_limits
<double>::max(), cflags
);
137 o
.init_member("MIN_VALUE",
138 std::numeric_limits
<double>::denorm_min(), cflags
);
139 o
.init_member("NaN", as_value(NaN
), cflags
);
140 o
.init_member("POSITIVE_INFINITY",
141 as_value(std::numeric_limits
<double>::infinity()), cflags
);
142 o
.init_member("NEGATIVE_INFINITY",
143 as_value(-std::numeric_limits
<double>::infinity()), cflags
);
146 } // anonymous namespace
149 // extern (used by Global.cpp)
151 number_class_init(as_object
& where
, const ObjectURI
& uri
)
153 VM
& vm
= getVM(where
);
154 Global_as
& gl
= getGlobal(where
);
156 as_object
* proto
= createObject(gl
);
157 as_object
* cl
= vm
.getNative(106, 2);
158 cl
->init_member(NSV::PROP_PROTOTYPE
, proto
);
159 proto
->init_member(NSV::PROP_CONSTRUCTOR
, cl
);
161 attachNumberInterface(*proto
);
162 attachNumberStaticInterface(*cl
);
164 // Register _global.Number
165 where
.init_member(uri
, cl
, as_object::DefaultFlags
);
170 registerNumberNative(as_object
& global
)
172 VM
& vm
= getVM(global
);
173 vm
.registerNative(number_valueOf
, 106, 0);
174 vm
.registerNative(number_toString
, 106, 1);
175 vm
.registerNative(number_ctor
, 106, 2);