Merge branch 'master' of git.sv.gnu.org:/srv/git/gnash
[gnash.git] / libcore / asobj / Boolean_as.cpp
blobf00fa576bbf9eb1ed6e94194d7332f1ab930b860
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "namedStrings.h"
21 #include "Boolean_as.h"
22 #include "as_object.h" // for inheritance
23 #include "log.h"
24 #include "fn_call.h"
25 #include "Global_as.h"
26 #include "smart_ptr.h" // for boost intrusive_ptr
27 #include "builtin_function.h" // need builtin_function
28 #include "NativeFunction.h"
29 #include "GnashException.h"
30 #include "VM.h"
32 namespace gnash {
34 namespace {
35 as_value boolean_tostring(const fn_call& fn);
36 as_value boolean_valueof(const fn_call& fn);
37 as_value boolean_ctor(const fn_call& fn);
38 void attachBooleanInterface(as_object& o);
41 class Boolean_as: public Relay
44 public:
46 explicit Boolean_as(bool val)
48 _val(val)
52 bool value() const { return _val; }
54 private:
56 bool _val;
60 // extern (used by Global.cpp)
61 void
62 boolean_class_init(as_object& where, const ObjectURI& uri)
64 VM& vm = getVM(where);
65 Global_as& gl = getGlobal(where);
67 as_object* proto = createObject(gl);
68 as_object* cl = vm.getNative(107, 2);
69 cl->init_member(NSV::PROP_PROTOTYPE, proto);
70 proto->init_member(NSV::PROP_CONSTRUCTOR, cl);
72 attachBooleanInterface(*proto);
74 // Register _global.Boolean
75 where.init_member(uri, cl, as_object::DefaultFlags);
79 void
80 registerBooleanNative(as_object& global)
82 VM& vm = getVM(global);
83 vm.registerNative(boolean_valueof, 107, 0);
84 vm.registerNative(boolean_tostring, 107, 1);
85 vm.registerNative(boolean_ctor, 107, 2);
88 namespace {
91 void
92 attachBooleanInterface(as_object& o)
94 VM& vm = getVM(o);
95 o.init_member("valueOf", vm.getNative(107, 0));
96 o.init_member("toString", vm.getNative(107, 1));
99 as_value
100 boolean_tostring(const fn_call& fn)
102 Boolean_as* obj = ensure<ThisIsNative<Boolean_as> >(fn);
103 if (obj->value()) return as_value("true");
104 return as_value("false");
108 as_value
109 boolean_valueof(const fn_call& fn)
111 Boolean_as* obj = ensure<ThisIsNative<Boolean_as> >(fn);
112 return as_value(obj->value());
115 as_value
116 boolean_ctor(const fn_call& fn)
119 if (!fn.isInstantiation()) {
120 if (!fn.nargs) return as_value();
121 return as_value(toBool(fn.arg(0), getVM(fn)));
124 const bool val = fn.nargs ? toBool(fn.arg(0), getVM(fn)) : false;
126 as_object* obj = fn.this_ptr;
127 obj->setRelay(new Boolean_as(val));
128 return as_value();
132 } // anonymous namespace
133 } // gnash namespace