/*
Copyright (C) 2005, The Perl Foundation.
$Id: lexpad.pmc 17084 2007-02-20 20:03:37Z paultcochrane $

=head1 NAME

src/pmc/lexpad.pmc - LexPad PMC

=head1 DESCRIPTION

These are the vtable functions for the lexpad PMC.

=head2 Functions

=over 4

=cut

*/

#include "parrot/parrot.h"
#include <assert.h>

/*
 * LexPad provides a Hash interface for lexical fetch/store
 * needed
 *
 * struct_val ... parrot_context_t *ctx
 * pmc_val    ... LexInfo
 */

pmclass LexPad does hash no_ro {

    void init() {
        real_exception(INTERP, NULL, INVALID_OPERATION,
                "don't create me like this");
    }

/*

=item C<init_pmc(PMC *lexinfo)>

Initialize the LexPad PMC and remember the associate
lexinfo.

=item C<void set_pointer(void *)>

Initialize the LexPad PMC and remember the associate context.

=item C<INTVAL elements()>

Returns the number of elements in the hash.

=item C<INTVAL exists_keyed(PMC *name)>

=item C<INTVAL exists_keyed_str(STRING *name)>

Returns whether a lexical C<name> exists in the hash.

=item C<PMC *get_pmc_keyed_str(STRING *name)>

=item C<PMC *get_pmc_keyed(PMC *name)>

Return the lexical with the given name, or NULL (not PMCNULL), if the
lexical doesn't exist.

=item C<void set_pmc_keyed(PMC *name, PMC *value)>

=item C<void set_pmc_keyed_str(STRING *name, PMC *value)>

Set the lexical with the given name to value. If the lexical name
doesn't exist, it is created.

=item C<METHOD PMC* get_lexinfo()>

Return the LexInfo PMC, if any or a Null PMC.

=cut

*/
    void init_pmc(PMC* lexinfo) {
        PMC_pmc_val(SELF) = lexinfo;
    }

    void set_pointer(void* ctx) {
        PMC_struct_val(SELF) = ctx;
    }

    INTVAL elements() {
        PMC *info = PMC_pmc_val(SELF);
        return parrot_hash_size(INTERP, PMC_struct_val(info));
    }

    INTVAL exists_keyed_str(STRING* name) {
        PMC *info = PMC_pmc_val(SELF);
        return parrot_hash_get_bucket(INTERP,
            (Hash*) PMC_struct_val(info), name) != 0;
    }

    INTVAL exists_keyed(PMC* name) {
        STRING *s = key_string(INTERP, name);
        return SELF.exists_keyed_str(s);
    }

    PMC* get_pmc_keyed_str(STRING* name) {
        PMC *info = PMC_pmc_val(SELF);
        Hash *hash = PMC_struct_val(info);
        parrot_context_t *ctx = PMC_struct_val(SELF);
        HashBucket *b = parrot_hash_get_bucket(INTERP, hash, name);
        INTVAL regno;
        if (!b)
            return NULL;
        regno = (INTVAL) b->value;
        return CTX_REG_PMC(ctx, regno);
    }

    PMC* get_pmc_keyed(PMC* name) {
        STRING *s = key_string(INTERP, name);
        return SELF.get_pmc_keyed_str(s);
    }

    void set_pmc_keyed_str(STRING* name, PMC* value) {
        PMC *info = PMC_pmc_val(SELF);
        Hash *hash = PMC_struct_val(info);
        parrot_context_t *ctx = PMC_struct_val(SELF);
        HashBucket *b = parrot_hash_get_bucket(INTERP, hash, name);
        INTVAL regno;
        if (!b)
            real_exception(interp, NULL, LEX_NOT_FOUND,
                    "Lexical '%Ss' not found", name);
        regno = (INTVAL) b->value;
        CTX_REG_PMC(ctx, regno) = value;
    }

    void set_pmc_keyed(PMC* name, PMC* value) {
        STRING *s = key_string(INTERP, name);
        SELF.set_pmc_keyed_str(s, value);
    }

    METHOD PMC* get_lexinfo() {
        return PMC_pmc_val(SELF);
    }

}


/*

=back

=head1 SEE ALSO

F<docs/pdds/pdd20_lecical_vars.pod>, F<src/pmc/lexinfo.pmc>.

=cut

*/

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