Author: particle
Date: Mon Mar 26 09:50:19 2007
New Revision: 17773

Modified:
   trunk/src/pmc/exporter.pmc
   trunk/t/pmc/exporter.t

Log:
[pmc] Exporter updates
~ added 'destination', 'globals', 'add_global' methods
~ renamed 'src' method to 'source'
~ added *some* code to 'import' method, still TODO
~ added tests for 'source', 'destination', and 'globals' methods

Modified: trunk/src/pmc/exporter.pmc
==============================================================================
--- trunk/src/pmc/exporter.pmc  (original)
+++ trunk/src/pmc/exporter.pmc  Mon Mar 26 09:50:19 2007
@@ -60,15 +60,16 @@
 
 /*
 
-=item C<void src(PMC *)>
+=item C<void source(PMC *src)>
 
-Return the C<globals> array.
+Accessor for the source NameSpace object. Sets the value if C<src> is passed,
+otherwise returns the value.
 
 =cut
 
 */
 
-    PCCMETHOD void src(PMC *src :optional, int got_src :opt_flag) {
+    PCCMETHOD void source(PMC *src :optional, int got_src :opt_flag) {
         Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
         PMC *ret_ns_src;
 
@@ -85,18 +86,53 @@
 
 /*
 
+=item C<void destination(PMC *dest)>
+
+Accessor for the destination NameSpace object. Sets the value if C<dest> is 
passed,
+otherwise returns the value.
+
+=cut
+
+*/
+
+    PCCMETHOD void destination(PMC *dest :optional, int got_dest :opt_flag) {
+        Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
+        PMC *ret_ns_dest;
+
+        /* TODO deal with non-namespace pmcs */
+        if (got_dest) {
+            exp->ns_dest = VTABLE_clone(interp, dest);
+        }
+        else {
+            ret_ns_dest = VTABLE_clone(interp, exp->ns_dest);
+            PCCRETURN(PMC *ret_ns_dest);
+        }
+    }
+
+
+/*
+
 =item C<PMC *globals(void)>
 
-Return the C<globals> array.
+Accessor for the array of globals to export. Sets the array if C<glb_array>
+is passed, otherwise returns the value.
 
 =cut
 
 */
 
-    PCCMETHOD void globals() {
+    PCCMETHOD void globals(PMC *glb_array :optional, int got_glb_array 
:opt_flag) {
         Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
-        PMC *ret_globals = VTABLE_clone(interp, exp->globals);
-        PCCRETURN(PMC *ret_globals);
+        PMC *ret_globals;
+
+        /* TODO deal properly with non-array pmcs */
+        if (got_glb_array) {
+            exp->globals = VTABLE_clone(interp, glb_array);
+        }
+        else {
+            ret_globals = VTABLE_clone(interp, exp->globals);
+            PCCRETURN(PMC *ret_globals);
+        }
     }
 
 
@@ -110,15 +146,16 @@
 
 */
 
-    void add_global(PMC *global) {
+    PCCMETHOD void add_global(PMC *global) {
         Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
-        PMC *globals = exp->globals;
+        VTABLE_push_string(interp, exp->globals,
+                VTABLE_get_string(interp, global));
     }
 
 
 /*
 
-=item C<PMC *import(PMC *dest, PMC *src, PMC *globals)>
+=item C<void import(PMC *dest, PMC *src, PMC *globals)>
 
 Import C<globals> from the C<src> namespace to the C<dest> namespace.
 
@@ -126,10 +163,25 @@
 
 */
 
-    PMC *import(PMC *dest, PMC *src, PMC *globals) {
+    PCCMETHOD void import(PMC *dest, PMC *src, PMC *globals) {
         Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
-        /*  */
-        return PMCNULL;
+        int size_globals;
+
+        if (exp->globals == PMCNULL) {
+            real_exception(interp, NULL, 0, "no globals to import");
+        }
+        else if (exp->ns_src == PMCNULL) {
+            real_exception(interp, NULL, 0, "source namespace not set");
+        }
+        else if (exp->ns_dest == PMCNULL) {
+            real_exception(interp, NULL, 0, "destination namespace not set");
+        }
+        else {
+            size_globals = VTABLE_get_integer(interp, exp->globals);
+            if (!size_globals)
+                return;
+            /* TODO for each global, look up in source and alias to dest */
+        }
     }
 
 /*

Modified: trunk/t/pmc/exporter.t
==============================================================================
--- trunk/t/pmc/exporter.t      (original)
+++ trunk/t/pmc/exporter.t      Mon Mar 26 09:50:19 2007
@@ -6,7 +6,7 @@
 use warnings;

 use lib qw( . lib ../lib ../../lib );

 use Test::More;

-use Parrot::Test tests => 2;

+use Parrot::Test tests => 4;

 

 =head1 NAME

 

@@ -41,17 +41,171 @@
 OUT

 

 

+pir_output_is( <<'CODE', <<'OUT', 'source', todo => 'broken' );

+.sub 'test' :main

+    new $P0, .Exporter

+    $P1 = $P0.'source'()

+    if $P1 == '' goto ok_1

+    print 'not '

+  ok_1:

+    say 'ok 1 - source() with no args returns source namespace, which is empty 
at first'

+

+    # get a NameSpace PMC for testing

+    # TODO replace with make_namespace, when implemented

+    .local pmc ns

+    ns = get_namespace ['Eponymous']

+

+    $P0.'source'(ns)

+    $P1 = $P0.'source'()

+    if $P1 == 'Eponymous' goto ok_2

+    print 'not '

+  ok_2:

+    say 'ok 2 - source() with args sets source namespace'

+

+    $P1 = clone ns

+

+    push_eh ok_3

+    $P0.'source'(ns, $P1)

+    clear_eh

+

+    print 'not '

+  ok_3:

+    say 'ok 3 - source() with too many args fails'

+

+.end

+CODE

+ok 1 - source() with no args returns source namespace, which is empty at first

+ok 2 - source() with args sets source namespace

+ok 3 - source() with too many args fails

+OUT

+# TODO test passing non-namespace pmc

+

+

+pir_output_is( <<'CODE', <<'OUT', 'destination', todo => 'broken' );

+.sub 'test' :main

+    new $P0, .Exporter

+    $P1 = $P0.'destination'()

+    if $P1 == '' goto ok_1

+    print 'not '

+  ok_1:

+    say 'ok 1 - destination() with no args returns destination namespace, 
which is empty at first'

+

+    # get a NameSpace PMC for testing

+    # TODO replace with make_namespace, when implemented

+    .local pmc ns

+    ns = get_namespace ['Eponymous']

+

+    $P0.'destination'(ns)

+    $P1 = $P0.'destination'()

+    if $P1 == 'Eponymous' goto ok_2

+    print 'not '

+  ok_2:

+    say 'ok 2 - destination() with args sets destination namespace'

+

+    $P1 = clone ns

+

+    push_eh ok_3

+    $P0.'destination'(ns, $P1)

+    clear_eh

+

+    print 'not '

+  ok_3:

+    say 'ok 3 - destination() with too many args fails'

+

+.end

+

+

+# TODO replace with make_namespace, when implemented

+.namespace ['Eponymous']

+.sub 'Eponymous' :anon

+.end

+CODE

+ok 1 - destination() with no args returns destination namespace, which is 
empty at first

+ok 2 - destination() with args sets destination namespace

+ok 3 - destination() with too many args fails

+OUT

+# TODO test passing non-namespace pmc

+

+

 pir_output_is( <<'CODE', <<'OUT', 'globals' );

 .sub 'test' :main

     $P0 = new .Exporter

 

     $P1 = $P0.'globals'()

+    $I0 = does $P1, 'array'

+    if $I0 goto ok_1

+    print 'not '

+  ok_1:

+    say 'ok 1 - globals() with no args returns globals array'

+

+    $I0 = $P1

+    if $I0 == 0 goto ok_2

+    print 'not '

+  ok_2:

+    say 'ok 2 - ...which is empty at first'

+

+    # create an array to store globals in

+    $P99 = new .ResizableStringArray

+

+    $P0.'globals'($P99)

+    $P1 = $P0.'globals'()

+    $I99 = $P99

+    $I1 = $P1

+    unless $I1 == $I99 goto nok_3

+    unless $I1 == 0 goto ok_3

+    goto ok_3

+  nok_3:

+    print 'not '

+  ok_3:

+    say 'ok 3 - globals() with args sets globals array (empty array)'

+

+    $P99 = push 'Alex'

+    $P99 = push 'Prince'

+

+    $P0.'globals'($P99)

+    $P1 = $P0.'globals'()

+    $I99 = $P99

+    $I1 = $P1

+    unless $I1 == $I99 goto nok_4

+    unless $I1 == 2 goto nok_4

+    $S0 = pop $P1

+    unless $S0 == 'Prince' goto nok_4

+    $S0 = pop $P1

+    unless $S0 == 'Alex' goto nok_4

+    goto ok_4

+  nok_4:

+    print 'not '

+  ok_4:

+    say 'ok 4 - globals() with args sets globals array (array with two values)'

+

+

+    $P98 = clone $P99

+

+    push_eh ok_5

+    $P0.'globals'($P99, $P98)

+    clear_eh

+

+    print 'not '

+  ok_5:

+    say 'ok 5 - globals() with too many args fails'

+

 .end

 CODE

+ok 1 - globals() with no args returns globals array

+ok 2 - ...which is empty at first

+ok 3 - globals() with args sets globals array (empty array)

+ok 4 - globals() with args sets globals array (array with two values)

+ok 5 - globals() with too many args fails

 OUT

 

 

-## TODO add more tests as this is documented and implemented

+## TODO add_global

+

+

+## TODO import

+

+

+

 

 # Local Variables:

 #   mode: cperl

Reply via email to