Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
248 lines (161 sloc) 3.62 KB
/*
Copyright (C) 2005-2009, Parrot Foundation.
$Id$
=head1 Lua Boolean
=head2 Description
C<LuaBoolean> extends C<LuaAny> to provide a class with the behaviour of
the Lua C<Boolean> type.
The value is stored as an Integer.
=head3 Methods
=over 4
=cut
*/
#include "lua_private.h"
pmclass LuaBoolean
extends LuaAny
provides scalar
provides boolean
provides integer
auto_attrs
dynpmc
group lua_group
hll lua
maps Boolean {
ATTR INTVAL iv;
/*
=item C<void init()>
Initializes the Boolean with a default value of C<false>.
=cut
*/
VTABLE void init() {
SET_ATTR_iv(INTERP, SELF, 0);
}
/*
=item C<STRING* name()>
Return the string "boolean".
=cut
*/
VTABLE STRING* name() {
return Parrot_str_new_constant(INTERP, "boolean");
}
/*
=item C<PMC *clone()>
Creates and returns a clone of the scalar.
=cut
*/
VTABLE PMC* clone() {
INTVAL iv;
PMC *dest = Parrot_pmc_new(INTERP, PMC_type(SELF));
GET_ATTR_iv(INTERP, SELF, iv);
SET_ATTR_iv(INTERP, dest, iv);
return dest;
}
/*
=item C<INTVAL get_integer()>
Returns the integer value of the Boolean.
=cut
*/
VTABLE INTVAL get_integer() {
INTVAL iv;
GET_ATTR_iv(INTERP, SELF, iv);
return iv;
}
/*
=item C<STRING* get_string()>
Return the string "true" or "false".
=cut
*/
VTABLE STRING* get_string() {
INTVAL iv;
GET_ATTR_iv(INTERP, SELF, iv);
if (iv)
return Parrot_str_new_constant(INTERP, "true");
else
return Parrot_str_new_constant(INTERP, "false");
}
/*
=item C<INTVAL get_bool()>
Returns the boolean value of the Boolean.
=cut
*/
VTABLE INTVAL get_bool() {
INTVAL iv;
GET_ATTR_iv(INTERP, SELF, iv);
return iv ? 1 : 0;
}
/*
=item C<void set_integer_native(INTVAL value)>
=item C<void set_bool(INTVAL value)>
=cut
*/
VTABLE void set_integer_native(INTVAL value) {
SET_ATTR_iv(INTERP, SELF, value != 0);
}
VTABLE void set_bool(INTVAL value) {
SET_ATTR_iv(INTERP, SELF, value != 0);
}
/*
=item C<void set_pmc(PMC *value)>
Sets the value of the boolean to the value in C<*value>.
=cut
*/
VTABLE void set_pmc(PMC *value) {
SET_ATTR_iv(INTERP, SELF, VTABLE_get_bool(INTERP, value));
}
/*
=item C<void freeze(PMC *info)>
Used to archive the boolean.
=cut
*/
VTABLE void freeze(PMC *info) {
SUPER(info);
VTABLE_push_integer(INTERP, info, SELF.get_integer());
}
/*
=item C<void thaw(PMC *info)>
Used to unarchive the boolean.
=cut
*/
VTABLE void thaw(PMC *info) {
SUPER(info);
SET_ATTR_iv(INTERP, SELF, VTABLE_shift_integer(INTERP, info));
}
/*
=back
=head3 non-Vtable Methods
=over 4
=item C<INTVAL is_equal(PMC *value)>
=cut
*/
MULTI INTVAL is_equal(LuaBoolean value) {
return (INTVAL)(VTABLE_get_integer(INTERP, SELF) == VTABLE_get_integer(INTERP, value));
}
MULTI INTVAL is_equal(DEFAULT value) {
return (INTVAL)0;
}
/*
=back
=head3 Specific Methods
=over 4
=item C<PMC* rawequal(PMC *value)>
=cut
*/
METHOD PMC* rawequal(PMC *value) {
const INTVAL b = (PMC_type(SELF) == PMC_type(value)
&& VTABLE_get_integer(INTERP, SELF) == VTABLE_get_integer(INTERP, value))
? 1 : 0;
PMC * const retval = Parrot_pmc_new(INTERP, dynpmc_LuaBoolean);
VTABLE_set_integer_native(INTERP, retval, b);
RETURN(PMC *retval);
}
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/