Author: leo
Date: Sat Oct 29 10:39:55 2005
New Revision: 9633

Modified:
   trunk/classes/eval.pmc
   trunk/t/pmc/eval.t
Log:
add get_pmc_keyed_int to Eval.pmc to extract subs

Modified: trunk/classes/eval.pmc
==============================================================================
--- trunk/classes/eval.pmc      (original)
+++ trunk/classes/eval.pmc      Sat Oct 29 10:39:55 2005
@@ -51,6 +51,37 @@ clear_fixups(Interp* interpreter, PMC* s
     }
 }
 
+static PMC*
+get_sub(Interp* interpreter, PMC* self, int idx)
+{
+    opcode_t i, n, ci;
+    struct PackFile_ByteCode *seg;
+    struct PackFile_FixupTable *ft;
+    struct PackFile_ConstTable *ct;
+    PMC *sub;
+
+    seg = PMC_sub(self)->seg;
+    if (!seg)
+        return PMCNULL;
+    ft = seg->fixups;
+    if (!ft)
+        return PMCNULL;
+    ct = seg->const_table;
+    if (!ct)
+        return PMCNULL;
+    for (i = n = 0; i < ft->fixup_count; i++) {
+        switch (ft->fixups[i]->type) {
+            case enum_fixup_sub:
+                if (n++ != idx)
+                    continue;
+                ci = ft->fixups[i]->offset;
+                sub = ct->constants[ci]->u.key;
+                return sub;
+        }
+    }
+    return PMCNULL;
+}
+
 static void
 mark_subs(Interp* interpreter, PMC* self)
 {
@@ -197,6 +228,20 @@ for writing to disc and later loading vi
     }
 
 /*
+
+=item C<PMC *get_pmc_keyed_int(INTVAL key)>
+
+Returns the Sub PMC of the element at index C<key> or PMCNULL. 
+
+=cut
+
+*/
+
+    PMC* get_pmc_keyed_int (INTVAL key) {
+        return get_sub(INTERP, SELF, key);
+    }
+
+/*
 
 =item C<void freeze(visit_info *info)>
 

Modified: trunk/t/pmc/eval.t
==============================================================================
--- trunk/t/pmc/eval.t  (original)
+++ trunk/t/pmc/eval.t  Sat Oct 29 10:39:55 2005
@@ -17,7 +17,7 @@ Tests on-the-fly PASM, PIR and PAST comp
 
 =cut
 
-use Parrot::Test tests => 14;
+use Parrot::Test tests => 15;
 use Test::More;
 
 output_is(<<'CODE', <<'OUTPUT', "eval_sc");
@@ -463,3 +463,29 @@ CODE
 hello from foo_1
 hello from foo_1
 OUTPUT
+
+pir_output_is(<<'CODE', <<'OUTPUT', "get_pmc_keyed_int");
+.sub main :main
+    .local string code
+    .local pmc e, s, compi
+    code = <<"EOC"
+    .sub foo
+       noop
+    .end
+    .sub bar
+       noop
+    .end
+EOC
+    compi = compreg "PIR"
+    e  = compi(code)
+    s = e[0]
+    print s
+    print "\n"
+    s = e[1]
+    print s
+    print "\n"
+.end
+CODE
+foo
+bar
+OUTPUT

Reply via email to