The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
/*
Copyright (C) 2007-2009, Parrot Foundation.

=head1 NAME

src/pmc/exporter.pmc - Export globals from one namespace to another

=head1 SYNOPSIS

You can use Exporter in PIR to import subs from a library. At its simplest:

 .sub main :main
     load_bytecode 'Test/More.pir'

     .local pmc exporter, src_ns, dest_ns
     src_ns   = get_namespace [ 'Test'; 'More' ]
     exporter = new 'Exporter'

     exporter.'import'( src_ns :named('source') 'plan ok' :named('globals') )
     plan(1)
     ok(1, 'exporter has imported the requested functions')
 .end

You can also specify the exporter attributes before making the import call,
for example to import into the alternate namespace 'Foo' you could use the
following code:

    src_ns   = get_namespace [ 'Test'; 'More' ]
    dest_ns  = get_namespace [ 'Foo' ]
    exporter.'source'(src_ns)
    exporter.'destination'(dest_ns)
    exporter.'import'('plan ok' :named('globals'))

You can even import subroutines under different names if globals is a hash:

    globals         = new 'Hash'
    globals['plan'] = 'steps'
    globals['ok']   = 'passed'
    exporter.'import'(globals :named('globals'))
    steps(1)
    passed(1)


=head1 DESCRIPTION

Exports globals from one namespace to another. Exporter always uses
the typed namespace interface, as outlined in
F<docs/pdds/pdd21_namespaces.pod>.

Exporter is not derived from any other PMC, and does not provide any
vtable interface--its interface consists solely of methods, not
vtable functions.

=head2 Structure

The Exporter PMC structure (C<Parrot_Exporter>) consists of three items:

=over 4

=item C<ns_src>

The source namespace -- a NameSpace PMC.
A Null PMC is allocated during initialization.

=item C<ns_dest>

The destination namespace -- a NameSpace PMC.
A PMC representing the current namespace is allocated upon initialization.

=item C<globals>

The globals to export -- a PMC that implements a hash, an array, a String
containing a list of space-separated subroutine names or Null.

A Null PMC is allocated during initialization.

=cut

*/

/*

=back

=head2 Functions

=over 4

=cut

*/

/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */

pmclass Exporter auto_attrs {

    ATTR PMC *ns_src;
    ATTR PMC *ns_dest;
    ATTR PMC *globals;

/*

=item C<void init()>

Initializes an Exporter PMC.

=cut

*/

    VTABLE void init() {
        /* Set up the object. */
        SET_ATTR_ns_src(INTERP, SELF, PMCNULL);
        SET_ATTR_ns_dest(INTERP, SELF, Parrot_pcc_get_namespace(INTERP, CURRENT_CONTEXT(INTERP)));
        SET_ATTR_globals(INTERP, SELF, PMCNULL);

        /* Set flags for custom GC mark and destroy. */
        PObj_custom_mark_SET(SELF);
    }

/*

=item C<void mark()>

Mark referenced strings and PMCs in the structure as live.

=cut

*/

    VTABLE void mark() {
        PMC *ns_src;
        PMC *ns_dest;
        PMC *globals;
        GET_ATTR_ns_src(INTERP, SELF, ns_src);
        GET_ATTR_ns_dest(INTERP, SELF, ns_dest);
        GET_ATTR_globals(INTERP, SELF, globals);

        Parrot_gc_mark_PMC_alive(INTERP, ns_src);
        Parrot_gc_mark_PMC_alive(INTERP, ns_dest);
        Parrot_gc_mark_PMC_alive(INTERP, globals);
    }


/*

=back

=head2 Methods

=over 4

=item C<METHOD
source(PMC *src :optional, int got_src :opt_flag)>

Accessor for the source NameSpace object (C<ns_src>.)
Sets the value if C<src> is passed, otherwise returns the value.
Throws an exception if a non-NameSpace PMC is passed.

=cut

*/

    METHOD source(PMC *src :optional, int got_src :opt_flag) {
        if (got_src) {
            if (src->vtable->base_type != enum_class_NameSpace)
                Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                        "source must be a NameSpace PMC");

            SET_ATTR_ns_src(INTERP, SELF, src);
        }
        else {
            PMC *tmp_ns_src;
            GET_ATTR_ns_src(INTERP, SELF, tmp_ns_src);
            RETURN(PMC *tmp_ns_src);
        }
    }


/*

=item C<METHOD
    destination(PMC *dest :optional, int got_dest :opt_flag)>

Accessor for the destination NameSpace object (C<ns_dest>.)
Sets the value if C<dest> is passed, otherwise returns the value.
Throws an exception if a non-NameSpace PMC is passed.

=cut

*/

    METHOD destination(PMC *dest :optional, int got_dest :opt_flag) {
        if (got_dest) {

            if (dest->vtable->base_type != enum_class_NameSpace)
                Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                        "destination must be a NameSpace PMC");

            SET_ATTR_ns_dest(INTERP, SELF, dest);
        }
        else {
            PMC *tmp_ns_dest;
            GET_ATTR_ns_dest(INTERP, SELF, tmp_ns_dest);
            RETURN(PMC *tmp_ns_dest);
        }
    }


/*

=item C<METHOD
    globals(PMC *glb :optional, int got_glb :opt_flag)>

Accessor for the globals to export (C<globals>.)
Sets the value if C<glb> is passed, otherwise returns the value.
If C<glb> is a String, it is split on ascii whitespace, and each array member
is added as a hash key.
If C<glb> implements the array interface, each member is added as a hash key.
if C<glb> implements the hash interface, it is assigned to Exporter's
C<globals> attribute.
Throws an exception if an unknown PMC type is passed.

=cut

*/

    METHOD globals(PMC *glb :optional, int got_glb :opt_flag) {
        STRING * const s_str   = CONST_STRING(INTERP, "String");
        STRING * const s_arr   = CONST_STRING(INTERP, "array");
        STRING * const s_hash  = CONST_STRING(INTERP, "hash");
        STRING * const s_space = CONST_STRING(INTERP, " ");

        if (got_glb) {
            STRING * const s_empty      = CONST_STRING(INTERP, "");
            PMC           *temp_globals = Parrot_pmc_new(INTERP, enum_class_Hash);

            if (PMC_IS_NULL(glb)) {
                temp_globals = PMCNULL;
            }
            else if (VTABLE_isa(INTERP, glb, s_str) || (VTABLE_does(INTERP, glb, s_arr))) {
                PMC    *glb_array;
                INTVAL n, i;

                if (VTABLE_isa(INTERP, glb, s_str))
                    glb_array = Parrot_str_split(INTERP, s_space,
                        VTABLE_get_string(INTERP, glb));
                else
                    glb_array = glb;

                n = VTABLE_elements(INTERP, glb_array);

                if (n == 0)
                    temp_globals = PMCNULL;

                for (i = 0; i < n; ++i) {
                    STRING * const item = VTABLE_get_string_keyed_int(INTERP, glb_array, i);
                    VTABLE_set_string_keyed_str(INTERP, temp_globals, item, s_empty);
                }
            }
            else if (VTABLE_does(INTERP, glb, s_hash)) {
                if (VTABLE_elements(INTERP, glb) == 0)
                    temp_globals = PMCNULL;
                else
                    temp_globals = glb;
            }
            else {
                Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                        "Invalid type %d in globals()", glb->vtable->base_type);
            }

            SET_ATTR_globals(INTERP, SELF, temp_globals);
         }
         else {
            PMC *tmp_globals;
            GET_ATTR_globals(INTERP, SELF, tmp_globals);
            if (PMC_IS_NULL(tmp_globals)) {
                RETURN(PMC *PMCNULL);
            }
            else
                RETURN(PMC *tmp_globals);
        }
    }


/*

=item C<METHOD
    import(PMC *dest    :optional :named("destination"), int got_dest :opt_flag,
        PMC    *src     :optional :named("source"),      int got_src  :opt_flag,
        PMC    *globals :optional :named("globals"), int got_globals  :opt_flag)>

Import C<globals> from the C<src> namespace to the C<dest> namespace.
If C<src>, C<dest>, or C<globals> are passed, they override the current value.
C<import> follows the semantics of the C<export_to> method
of the C<NameSpace> PMC. in particular, if a NULL value is passed
for C<globals>, the default set of items will be imported.
Throws an exception upon error.

=cut

*/

    METHOD import(PMC *dest        :optional :named("destination"),
                  int  got_dest    :opt_flag,
                  PMC *src         :optional :named("source"),
                  int  got_src     :opt_flag,
                  PMC *globals     :optional :named("globals"),
                  int  got_globals :opt_flag) {

        PMC *ns_src, *ns_dest, *ns_globals;

        if (got_src)
            PCCINVOKE(INTERP, SELF, "source", PMC *src);

        if (got_dest)
            PCCINVOKE(INTERP, SELF, "destination", PMC *dest);

        if (got_globals)
            PCCINVOKE(INTERP, SELF, "globals", PMC *globals);

        GET_ATTR_ns_src(INTERP, SELF, ns_src);
        if (PMC_IS_NULL(ns_src))
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                    "source namespace not set");

        GET_ATTR_ns_dest(INTERP, SELF, ns_dest);

        /* This condition must never happen, destination is set during init and
         * attempts to change it with wrong values are rejected.
         * Even if it gets changed for unexpected reasons, "export_to" will
         * catch the problem. */
        PARROT_ASSERT(!PMC_IS_NULL(ns_dest));

        GET_ATTR_globals(INTERP, SELF, ns_globals);

        PCCINVOKE(INTERP, ns_src, "export_to", PMC *ns_dest, PMC *ns_globals);
    }


} /* end pmclass Exporter */

/*

=back

=head1 STABILITY

Unstable. This PMC is under active development; major portions of the
interface have not yet been completed.

=head1 SEE ALSO

F<docs/pdds/pdd17_basic_types.pod>, F<docs/pdds/pdd21_namespaces.pod>.

=cut

*/

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