remove revno.h for distclean
[gnash.git] / libcore / asobj / Number_as.cpp
blob1c0ec577ed9e0a85bd88c4cbc4485240aadfdeeb
1 // Number.cpp: ActionScript Number class, for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // Foundation, Inc
5 //
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.
10 //
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
22 #include "log.h"
23 #include "Number_as.h"
24 #include "smart_ptr.h"
25 #include "fn_call.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"
31 #include "VM.h"
33 #include "namedStrings.h"
34 #include <sstream>
35 #include <cmath>
38 namespace gnash {
40 class Number_as : public Relay
42 public:
44 Number_as(double val)
46 _val(val)
50 double value() const {
51 return _val;
54 private:
56 // the number value
57 double _val;
61 namespace {
63 as_value
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();
71 unsigned radix = 10;
73 if ( fn.nargs )
75 int userRadix = toInt(fn.arg(0), getVM(fn));
76 if ( userRadix >= 2 && userRadix <= 36 ) radix=userRadix;
77 else
79 IF_VERBOSE_ASCODING_ERRORS(
80 log_aserror(_("Number.toString(%s): "
81 "radix must be in the 2..36 range (%d is invalid)"),
82 fn.arg(0), userRadix)
87 return doubleToString(val, radix);
90 as_value
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);
97 return obj->value();
100 as_value
101 number_ctor(const fn_call& fn)
103 double val = 0;
104 if (fn.nargs > 0) {
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));
114 return as_value();
117 void
118 attachNumberInterface(as_object& o)
120 VM& vm = getVM(o);
121 o.init_member("valueOf", vm.getNative(106, 0));
122 o.init_member("toString", vm.getNative(106, 1));
125 void
126 attachNumberStaticInterface(as_object& o)
128 // constant flags
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)
150 void
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);
169 void
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);
178 } // namespace gnash