/*
Copyright (C) 2005-2008, The Perl Foundation.
$Id: /mirror/trunk/languages/lua/src/pmc/luanil.pmc 35819 2009-01-20T05:19:27.074079Z chromatic  $

=head1 NAME

pmc/luanil.pmc - Lua Nil

=head1 DESCRIPTION

C<LuaNil> extends C<LuaAny> to provide a class with the behaviour of
the Lua C<Nil> type.

=for rational

LuaNil is no longer a singleton;
this would be a problem, as all uninitialized values in Lua are LuaNil.
If some value is assigned, the LuaNil should morph into the correct type,
and so a new PMC is constructed anyway. Therefore, we may as well create
a new PMC right away. Also, creating a new PMC from a singleton class is
troublesome (if not possible?).

=head2 Methods

=over 4

=cut

*/

#include "lua_private.h"


pmclass LuaNil
    extends  LuaAny
    provides scalar
    dynpmc
    group    lua_group
    hll      lua {

/*

=item C<PMC* instantiate_str(STRING *rep, INTVAL flags)>

Allow :

    .const .LuaNil nil = "dummy"

=cut

*/
    VTABLE PMC* instantiate_str(STRING *rep, INTVAL flags) {
        PMC *res;
        INTVAL type = PMC_type(SELF);
        if (flags & PObj_constant_FLAG)
            res = constant_pmc_new(INTERP, type);
        else
            res = pmc_new(INTERP, type);
        return res;
    }

/*

=item C<STRING* name()>

Return the string "nil".

=cut

*/
    VTABLE STRING* name() {
        return const_string(INTERP, "nil");
    }

/*

=item C<PMC *clone()>

Creates and returns a clone of the scalar.

=cut

*/
    VTABLE PMC* clone() {
        PMC *dest = pmc_new(INTERP, PMC_type(SELF));
        STRUCT_COPY(&PMC_union(dest), &PMC_union(SELF));
        return dest;
    }

/*

=item C<STRING* get_string()>

Return the string "nil".

=cut

*/
    VTABLE STRING* get_string() {
        return const_string(INTERP, "nil");
    }

/*

=item C<INTVAL get_bool()>

Return always C<false>.

=cut

*/
    VTABLE INTVAL get_bool() {
        return (INTVAL)0;
    }

/*

=item C<INTVAL defined()>

"nil" in Lua is always undefined.

=cut

*/
    VTABLE INTVAL defined() {
        return (INTVAL)0;
    }

/*

=back

=head2 non-Vtable Methods

=over 4

=item C<INTVAL is_equal(PMC* value)>

=cut

*/
    MULTI INTVAL is_equal(LuaNil value) {
        return (INTVAL)1;
    }

    MULTI INTVAL is_equal(DEFAULT value) {
        return (INTVAL)0;
    }

/*

=back

=head2 Specific Methods

=over 4

=item C<PMC* rawequal(PMC* value)>

=cut

*/
    METHOD PMC *rawequal(PMC *value) {
        PMC *retval = pmc_new(INTERP, dynpmc_LuaBoolean);
        if (PMC_type(SELF) == PMC_type(value))
            VTABLE_set_integer_native(INTERP, retval, 1);
        else
            VTABLE_set_integer_native(INTERP, retval, 0);

        RETURN(PMC *retval);
    }

}

/*

=back

=cut

*/

/*
 * Local variables:
 *   c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
