1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software 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.
14 //
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
18 //
19
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 "NativeFunction.h"
27 #include "GnashException.h"
28 #include "VM.h"
29
30 namespace gnash {
31
32 namespace {
33 as_value boolean_tostring(const fn_call& fn);
34 as_value boolean_valueof(const fn_call& fn);
35 as_value boolean_ctor(const fn_call& fn);
36 void attachBooleanInterface(as_object& o);
37 }
38
39 class Boolean_as: public Relay
40 {
41
42 public:
43
Boolean_as(bool val)44 explicit Boolean_as(bool val)
45 :
46 _val(val)
47 {
48 }
49
value() const50 bool value() const { return _val; }
51
52 private:
53
54 bool _val;
55
56 };
57
58 // extern (used by Global.cpp)
59 void
boolean_class_init(as_object & where,const ObjectURI & uri)60 boolean_class_init(as_object& where, const ObjectURI& uri)
61 {
62 VM& vm = getVM(where);
63 Global_as& gl = getGlobal(where);
64
65 as_object* proto = createObject(gl);
66 as_object* cl = vm.getNative(107, 2);
67 cl->init_member(NSV::PROP_PROTOTYPE, proto);
68 proto->init_member(NSV::PROP_CONSTRUCTOR, cl);
69
70 attachBooleanInterface(*proto);
71
72 // Register _global.Boolean
73 where.init_member(uri, cl, as_object::DefaultFlags);
74
75 }
76
77 void
registerBooleanNative(as_object & global)78 registerBooleanNative(as_object& global)
79 {
80 VM& vm = getVM(global);
81 vm.registerNative(boolean_valueof, 107, 0);
82 vm.registerNative(boolean_tostring, 107, 1);
83 vm.registerNative(boolean_ctor, 107, 2);
84 }
85
86 namespace {
87
88
89 void
attachBooleanInterface(as_object & o)90 attachBooleanInterface(as_object& o)
91 {
92 VM& vm = getVM(o);
93 o.init_member("valueOf", vm.getNative(107, 0));
94 o.init_member("toString", vm.getNative(107, 1));
95 }
96
97 as_value
boolean_tostring(const fn_call & fn)98 boolean_tostring(const fn_call& fn)
99 {
100 Boolean_as* obj = ensure<ThisIsNative<Boolean_as> >(fn);
101 if (obj->value()) return as_value("true");
102 return as_value("false");
103 }
104
105
106 as_value
boolean_valueof(const fn_call & fn)107 boolean_valueof(const fn_call& fn)
108 {
109 Boolean_as* obj = ensure<ThisIsNative<Boolean_as> >(fn);
110 return as_value(obj->value());
111 }
112
113 as_value
boolean_ctor(const fn_call & fn)114 boolean_ctor(const fn_call& fn)
115 {
116
117 if (!fn.isInstantiation()) {
118 if (!fn.nargs) return as_value();
119 return as_value(toBool(fn.arg(0), getVM(fn)));
120 }
121
122 const bool val = fn.nargs ? toBool(fn.arg(0), getVM(fn)) : false;
123
124 as_object* obj = fn.this_ptr;
125 obj->setRelay(new Boolean_as(val));
126 return as_value();
127
128 }
129
130 } // anonymous namespace
131 } // gnash namespace
132
133