[svn:parrot] r42844 - in trunk: src/pmc t/pmc

dukeleto at svn.parrot.org dukeleto at svn.parrot.org
Wed Dec 2 08:35:27 UTC 2009


Author: dukeleto
Date: Wed Dec  2 08:35:26 2009
New Revision: 42844
URL: https://trac.parrot.org/parrot/changeset/42844

Log:
[TT #1148] Implement 'clone' method for CallSignature, flh++

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

Modified: trunk/src/pmc/callsignature.pmc
==============================================================================
--- trunk/src/pmc/callsignature.pmc	Wed Dec  2 00:43:43 2009	(r42843)
+++ trunk/src/pmc/callsignature.pmc	Wed Dec  2 08:35:26 2009	(r42844)
@@ -281,7 +281,11 @@
     Parrot_CallSignature_attributes * const attrs = PARROT_CALLSIGNATURE(SELF);
 
     if (!attrs->hash)
-        attrs->hash = parrot_new_hash(interp);
+        attrs->hash = parrot_create_hash(interp,
+            enum_type_ptr,
+            Hash_key_type_STRING,
+            STRING_compare,
+            (hash_hash_key_fn)key_hash_STRING);
 
     return attrs->hash;
 }
@@ -1177,6 +1181,70 @@
 
 /*
 
+=item C<PMC *clone()>
+
+Creates and returns a clone of the signature.
+
+=cut
+
+*/
+    VTABLE PMC *clone() {
+        struct Parrot_CallSignature_attributes *sig, *dest_sig;
+        PMC * const dest = pmc_new(INTERP, SELF->vtable->base_type);
+        Pcc_cell *cell;
+
+        sig = PARROT_CALLSIGNATURE(SELF);
+        dest_sig = PARROT_CALLSIGNATURE(dest);
+
+        /* Copy all positional cells (thanks to APPEND_CELL, this also
+         * sets num_positionals). */
+        for (cell = sig->positionals; cell; cell = NEXT_CELL(cell)) {
+            Pcc_cell *cloned_cell;
+
+            switch (CELL_TYPE_MASK(cell)) {
+              case INTCELL:
+                cloned_cell = CREATE_INTVAL_CELL(INTERP);
+                CELL_INT(cloned_cell) = CELL_INT(cell);
+                break;
+              case FLOATCELL:
+                cloned_cell = CREATE_FLOATVAL_CELL(INTERP);
+                CELL_FLOAT(cloned_cell) = CELL_FLOAT(cell);
+                break;
+              case STRINGCELL:
+                cloned_cell = CREATE_STRING_CELL(INTERP);
+                CELL_STRING(cloned_cell) = CELL_STRING(cell);
+                break;
+              case PMCCELL:
+                cloned_cell = CREATE_PMC_CELL(INTERP);
+                CELL_PMC(cloned_cell) = CELL_PMC(cell);
+                break;
+            }
+            APPEND_CELL(dest, cloned_cell);
+        }
+
+        if (!PMC_IS_NULL(sig->results))
+            dest_sig->results = VTABLE_clone(INTERP, sig->results);
+
+        if (!PMC_IS_NULL(sig->type_tuple))
+            dest_sig->type_tuple = VTABLE_clone(INTERP, sig->type_tuple);
+
+        if (sig->short_sig)
+            dest_sig->short_sig = Parrot_str_copy(INTERP, sig->short_sig);
+
+        if (!PMC_IS_NULL(sig->arg_flags))
+            dest_sig->arg_flags = VTABLE_clone(INTERP, sig->arg_flags);
+
+        if (!PMC_IS_NULL(sig->return_flags))
+            dest_sig->return_flags = VTABLE_clone(INTERP, sig->return_flags);
+
+        parrot_hash_clone(INTERP, get_hash(INTERP, SELF),
+            get_hash(INTERP, dest));
+
+        return dest;
+    }
+
+/*
+
 =back
 
 =cut

Modified: trunk/t/pmc/callsignature.t
==============================================================================
--- trunk/t/pmc/callsignature.t	Wed Dec  2 00:43:43 2009	(r42843)
+++ trunk/t/pmc/callsignature.t	Wed Dec  2 08:35:26 2009	(r42844)
@@ -19,7 +19,7 @@
 .sub 'main' :main
     .include 'test_more.pir'
 
-    plan(65)
+    plan(68)
 
     test_instantiate()
     test_get_set_attrs()
@@ -29,6 +29,7 @@
     test_indexed_boxing()
     test_keyed_access()
     test_exists()
+    test_clone()
 .end
 
 .sub 'test_instantiate'
@@ -285,6 +286,22 @@
     nok( $I0, 'exists_keyed_str -- non-existant' )
 .end
 
+.sub 'test_clone'
+    $P0 = new ['CallSignature']
+    $P0[0] = 42
+    $P0[1] = "Hello Parrot"
+    $P0['floatval'] = 3.14159
+
+    $P1 = clone $P0
+
+    $I2 = $P1[0]
+    is($I2, 42, 'clone - integer positional cloned')
+    $S2 = $P1[1]
+    is($S2, "Hello Parrot", 'clone - string positional cloned')
+    $N2 = $P1['floatval']
+    is($N2, 3.14159, 'clone - named number cloned')
+.end
+
 # Local Variables:
 #   mode: pir
 #   fill-column: 100


More information about the parrot-commits mailing list