Skip to content
This repository
tag: RELEASE_0_5_1
Fetching contributors…

Cannot retrieve contributors at this time

file 385 lines (265 sloc) 9.711 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
/*
Copyright (C) 2007, The Perl Foundation.
$Id$

=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 non-vtable methods.

=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

*/

#include "parrot/parrot.h"
#define PARROT_EXPORTER(e) ((Parrot_Exporter *) PMC_data(e))

typedef struct Parrot_Exporter {
    PMC *ns_src;
    PMC *ns_dest;
    PMC *globals;
} Parrot_Exporter;


/*

=back

=head2 Functions

=over 4

=cut

*/

pmclass Exporter
    need_ext {


/*

=item C<void init()>

Initializes an Exporter PMC.

=cut

*/

    void init() {
        Parrot_Exporter *exp = NULL;

        /* Set up the object. */
        exp = mem_allocate_zeroed_typed(Parrot_Exporter);
        exp->ns_src = PMCNULL;
        exp->ns_dest = CONTEXT(interp->ctx)->current_namespace;
        exp->globals = PMCNULL;
        PMC_data(SELF) = exp;

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


/*

=item C<void destroy()>

Free the memory associated with the object's underlying struct.

=cut

*/

    void destroy() {
        mem_sys_free(PMC_data(SELF));
    }


/*

=item C<void mark()>

Mark referenced strings and PMCs in the structure as live.

=cut

*/

    void mark() {
        Parrot_Exporter *exp = PARROT_EXPORTER(SELF);

        if (exp->ns_src)
            pobject_lives(interp, (PObj *)exp->ns_src);
        if (exp->ns_dest)
            pobject_lives(interp, (PObj *)exp->ns_dest);
        if (exp->globals)
            pobject_lives(interp, (PObj *)exp->globals);
    }


/*

=back

=head2 Methods

=over 4

=item C<PCCMETHOD
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

*/

    PCCMETHOD source(PMC *src :optional, int got_src :opt_flag) {
        Parrot_Exporter *exp = PARROT_EXPORTER(SELF);

        if (got_src) {
            if (src->vtable->base_type != enum_class_NameSpace) {
                real_exception(interp, NULL, 0,
                        "source must be a NameSpace PMC");
            }
            exp->ns_src = src;
        }
        else {
            PMC *tmp_ns_src = exp->ns_src;
            PCCRETURN(PMC *tmp_ns_src);
        }
    }


/*

=item C<PCCMETHOD
    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

*/

    PCCMETHOD destination(PMC *dest :optional, int got_dest :opt_flag) {
        Parrot_Exporter *exp = PARROT_EXPORTER(SELF);

        if (got_dest) {
            if (dest->vtable->base_type != enum_class_NameSpace) {
                real_exception(interp, NULL, 0,
                        "destination must be a NameSpace PMC");
            }
            exp->ns_dest = dest;
        }
        else {
            PMC *tmp_ns_dest = exp->ns_dest;
            PCCRETURN(PMC *tmp_ns_dest);
        }
    }


/*

=item C<PCCMETHOD
    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

*/

    PCCMETHOD globals(PMC *glb :optional, int got_glb :opt_flag) {
        Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
        STRING *s_str = CONST_STRING(interp, "String");
        STRING *s_arr = CONST_STRING(interp, "array");
        STRING *s_hash = CONST_STRING(interp, "hash");

        if (got_glb) {
            STRING * const s_empty = CONST_STRING(interp, "");
            PMC *temp_globals = 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 = string_split(interp,
                            CONST_STRING(interp, " "),
                            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 {
                real_exception(interp, NULL, 0,
                    "Invalid type %d in globals()", glb->vtable->base_type);
            }

            exp->globals = temp_globals;
         }
         else {
            if (PMC_IS_NULL(exp->globals)) {
                PCCRETURN(PMC *PMCNULL);
            }
            else {
                PMC *tmp_globals = exp->globals;
                PCCRETURN(PMC *tmp_globals);
            }
        }
    }


/*

=item C<PCCMETHOD
    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

*/

    PCCMETHOD 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) {

        Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
        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);

        if (PMC_IS_NULL(exp->ns_src))
            real_exception(interp, NULL, 0, "source namespace not set");

        if (PMC_IS_NULL(exp->ns_dest))
            real_exception(interp, NULL, 0, "destination namespace not set");

        ns_src = exp->ns_src;
        ns_dest = exp->ns_dest;
        ns_globals = exp->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:
 */
Something went wrong with that request. Please try again.