Permalink
Browse files

[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

git-svn-id: https://svn.parrot.org/parrot/trunk@17773 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information...
particle committed Mar 26, 2007
1 parent 02dc535 commit 9318910f258987c49ccfbc76e912fab16ee2b791
Showing with 221 additions and 15 deletions.
  1. +65 −13 src/pmc/exporter.pmc
  2. +156 −2 t/pmc/exporter.t
View
@@ -60,15 +60,16 @@ Instantiates an Exporter.
/*
-=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;
@@ -83,20 +84,55 @@ Return the C<globals> array.
}
+/*
+
+=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,26 +146,42 @@ Add C<global> to the array of globals.
*/
- 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.
=cut
*/
- 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 */
+ }
}
/*
View
@@ -6,7 +6,7 @@ use strict;
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 @@ ok 2 - isa $P0, 'Exporter'
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

0 comments on commit 9318910

Please sign in to comment.