2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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.
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
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"
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
46 explicit Boolean_as(bool val
)
52 bool value() const { return _val
; }
60 // extern (used by Global.cpp)
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
);
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);
92 attachBooleanInterface(as_object
& o
)
95 o
.init_member("valueOf", vm
.getNative(107, 0));
96 o
.init_member("toString", vm
.getNative(107, 1));
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");
109 boolean_valueof(const fn_call
& fn
)
111 Boolean_as
* obj
= ensure
<ThisIsNative
<Boolean_as
> >(fn
);
112 return as_value(obj
->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
));
132 } // anonymous namespace