[svn:parrot] r41378 - in trunk: src/pmc t/pmc
bacek at svn.parrot.org
bacek at svn.parrot.org
Sun Sep 20 13:46:36 UTC 2009
Author: bacek
Date: Sun Sep 20 13:46:35 2009
New Revision: 41378
URL: https://trac.parrot.org/parrot/changeset/41378
Log:
[core] Implement PackfileConstatTable.get_or_create_constant for PMC
values.
Modified:
trunk/src/pmc/packfileconstanttable.pmc
trunk/t/pmc/packfileconstanttable.t
Modified: trunk/src/pmc/packfileconstanttable.pmc
==============================================================================
--- trunk/src/pmc/packfileconstanttable.pmc Sun Sep 20 13:39:17 2009 (r41377)
+++ trunk/src/pmc/packfileconstanttable.pmc Sun Sep 20 13:46:35 2009 (r41378)
@@ -331,8 +331,6 @@
=cut
-TODO: Add same for PMCs.
-
*/
MULTI INTVAL get_or_create_constant(STRING *value) {
Parrot_PackfileConstantTable_attributes *attrs =
@@ -374,6 +372,30 @@
return i;
}
+ MULTI INTVAL get_or_create_constant(PMC *value) {
+ Parrot_PackfileConstantTable_attributes *attrs =
+ PARROT_PACKFILECONSTANTTABLE(SELF);
+ INTVAL num = VTABLE_elements(interp, attrs->types);
+ INTVAL i;
+ PMC *val;
+ INTVAL val_type = value->vtable->base_type == enum_class_Key
+ ? PFC_KEY
+ : PFC_PMC;
+
+ for (i=0; i < num; ++i) {
+ INTVAL type = VTABLE_get_integer_keyed_int(interp, attrs->types, i);
+ if (type != val_type)
+ continue;
+
+ val = VTABLE_get_pmc_keyed_int(interp, attrs->constants, i);
+ if (VTABLE_is_equal(INTERP, value, val))
+ return i;
+ }
+ /* Constant not found. Add new one */
+ VTABLE_set_pmc_keyed_int(interp, SELF, i, value);
+ return i;
+ }
+
/*
=item C<METHOD type()>
Modified: trunk/t/pmc/packfileconstanttable.t
==============================================================================
--- trunk/t/pmc/packfileconstanttable.t Sun Sep 20 13:39:17 2009 (r41377)
+++ trunk/t/pmc/packfileconstanttable.t Sun Sep 20 13:46:35 2009 (r41378)
@@ -26,7 +26,7 @@
.sub 'main' :main
.include 'test_more.pir'
.include 'packfile_constants.pasm'
- 'plan'(14)
+ 'plan'(16)
'test_sanity'()
'test_elements'()
@@ -159,6 +159,22 @@
$I2 = pfc.'get_or_create_constant'(42.1)
$I0 = $I1 != $I2
ok($I0, "get_or_create_constant returs different number values for different keys")
+
+ $P0 = new ['FixedIntegerArray']
+ $P0 = 1
+ $P0[0] = 42
+ $P1 = new ['FixedIntegerArray']
+ $P1 = 1
+ $P1[0] = 42
+ $P2 = new ['FixedIntegerArray']
+ $P2 = 1
+ $P2[0] = 84
+
+ $I0 = pfc.'get_or_create_constant'($P0)
+ $I1 = pfc.'get_or_create_constant'($P1)
+ is($I0, $I1, "get_or_create_constant returns same index for equal PMCs")
+ $I2 = pfc.'get_or_create_constant'($P2)
+ isnt($I0, $I2, "get_or_create_constant returns different index for different PMCs")
.end
.sub '_get_consttable'
More information about the parrot-commits
mailing list