[svn:parrot] r39687 - in branches/tt761_keys_revamp: config/gen include/parrot src/pmc t/pmc

bacek at svn.parrot.org bacek at svn.parrot.org
Sun Jun 21 11:24:18 UTC 2009


Author: bacek
Date: Sun Jun 21 11:24:16 2009
New Revision: 39687
URL: https://trac.parrot.org/parrot/changeset/39687

Log:
[pmc] Start removing dangerous Hash.set_pointer tn favour of
Hash.set_key_type.
Add failing test for desired behavior.

Modified:
   branches/tt761_keys_revamp/config/gen/parrot_include.pm
   branches/tt761_keys_revamp/include/parrot/hash.h
   branches/tt761_keys_revamp/src/pmc/hash.pmc
   branches/tt761_keys_revamp/t/pmc/hash.t

Modified: branches/tt761_keys_revamp/config/gen/parrot_include.pm
==============================================================================
--- branches/tt761_keys_revamp/config/gen/parrot_include.pm	Sun Jun 21 10:44:46 2009	(r39686)
+++ branches/tt761_keys_revamp/config/gen/parrot_include.pm	Sun Jun 21 11:24:16 2009	(r39687)
@@ -33,6 +33,7 @@
         include/parrot/events.h
         include/parrot/scheduler.h
         include/parrot/exceptions.h
+        include/parrot/hash.h
         include/parrot/interpreter.h
         include/parrot/io.h
         include/parrot/longopt.h

Modified: branches/tt761_keys_revamp/include/parrot/hash.h
==============================================================================
--- branches/tt761_keys_revamp/include/parrot/hash.h	Sun Jun 21 10:44:46 2009	(r39686)
+++ branches/tt761_keys_revamp/include/parrot/hash.h	Sun Jun 21 11:24:16 2009	(r39687)
@@ -46,6 +46,7 @@
 typedef void (*hash_mark_key_fn)(PARROT_INTERP, PObj *);
 typedef size_t (*hash_hash_key_fn)(PARROT_INTERP, ARGIN(const void *), size_t seed);
 
+/* &gen_from_enum(hash_key_type.pasm) */
 typedef enum {
     Hash_key_type_int,
     Hash_key_type_cstring,
@@ -53,6 +54,7 @@
     Hash_key_type_PMC,
     Hash_key_type_ptr
 } Hash_key_type;
+/* &end_gen */
 
 typedef struct _hashbucket {
     struct _hashbucket *next;

Modified: branches/tt761_keys_revamp/src/pmc/hash.pmc
==============================================================================
--- branches/tt761_keys_revamp/src/pmc/hash.pmc	Sun Jun 21 10:44:46 2009	(r39686)
+++ branches/tt761_keys_revamp/src/pmc/hash.pmc	Sun Jun 21 11:24:16 2009	(r39687)
@@ -228,6 +228,40 @@
     }
 
 /*
+=item C<void set_integer(INTVAL type)>
+
+Reset Hash to use non-string keys. See enum C<Hash_key_type> for possible values.
+
+=cut
+*/
+    VTABLE void set_integer_native(INTVAL type) {
+        Hash *old_hash = (Hash *)SELF.get_pointer();
+        Hash *new_hash = 0;
+
+        if (type == Hash_key_type_STRING)
+            new_hash = parrot_new_hash(INTERP);
+        else if (type == Hash_key_type_int)
+            new_hash = parrot_new_intval_hash(INTERP);
+        else
+            /*
+                We probably will not implement other types of keys. They are way
+                too dangerous to use from PIR
+            */
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_UNIMPLEMENTED,
+                "Hash: Unknown key type");
+
+
+        PARROT_HASH(SELF)->hash = new_hash;
+        new_hash->container     = SELF;
+
+        if (old_hash)
+            parrot_hash_destroy(INTERP, old_hash);
+    }
+
+    METHOD set_key_type(INTVAL type) {
+        SELF.set_integer_native(type);
+    }
+/*
 
 =item C<void *get_pointer()>
 
@@ -308,7 +342,7 @@
         /* called from iterator */
         if (VTABLE_type(INTERP, key) == enum_class_HashIteratorKey) {
             PMC *val = (PMC*)PARROT_HASHITERATORKEY(key)->bucket->value;
-            return VTABLE_get_string(INTERP, val);
+            return VTABLE_get_integer(INTERP, val);
         }
 
         keystr  = make_ro_hash_key(INTERP, key);

Modified: branches/tt761_keys_revamp/t/pmc/hash.t
==============================================================================
--- branches/tt761_keys_revamp/t/pmc/hash.t	Sun Jun 21 10:44:46 2009	(r39686)
+++ branches/tt761_keys_revamp/t/pmc/hash.t	Sun Jun 21 11:24:16 2009	(r39687)
@@ -22,7 +22,7 @@
     .include 'test_more.pir'
     .include 'except_types.pasm'
 
-    plan(146)
+    plan(147)
 
     initial_hash_tests()
     more_than_one_hash()
@@ -67,6 +67,7 @@
     unicode_keys_register_rt_39249()
     unicode_keys_literal_rt_39249()
 
+    integer_keys()
 .end
 
 .sub initial_hash_tests
@@ -1271,6 +1272,22 @@
   is( $S1, 'ok', 'literal unicode key lookup via var' )
 .end
 
+# Switch to use integer keys instead of strings.
+.sub integer_keys
+    .include "hash_key_type.pasm"
+    .local pmc hash
+    hash = new ['Hash']
+    hash = .Hash_key_type_int
+
+    hash[0]  = 'foo'
+    hash[42] = 'bar'
+    hash['foo'] = 'BAZ'
+
+    # 'foo' numifies to '0'. So check it
+    $P0 = hash[0]
+    is($P0, 'BAZ', 'Key was numified')
+.end
+
 # Local Variables:
 #   mode: pir
 #   fill-column: 100


More information about the parrot-commits mailing list