On Sun, 4 Aug 2002, Mike Lambert wrote: > Unfortunately, this causes different semantics for whether you are storing > primitives or pointers (primitives copy, whereas pointers are shallow). Of > course, one could argue that the previous one didn't work at all. :) > > Thoughts?
Well, it's certainly wrong (though very efficiently so!) -- it needs to call string_copy for strings and vtable->clone for pmc's (attached). Are hashes the only (non-packed) containers we'll have to worry about holding things other than PMC's, or will arrays need this same snippet? /s
Index: hash.c =================================================================== RCS file: /cvs/public/parrot/hash.c,v retrieving revision 1.12 diff -p -u -w -r1.12 hash.c --- hash.c 3 Aug 2002 07:49:23 -0000 1.12 +++ hash.c 4 Aug 2002 07:38:20 -0000 @@ -436,8 +436,31 @@ hash_clone(struct Parrot_Interp * interp for (i = 0; i < hash->num_buckets; i++) { HASHBUCKET * b = table[i]; while (b) { - /* XXX: does b->key need to be copied? */ - hash_put(interp, ret, b->key, &(b->value)); + KEY_ATOM valtmp; + switch (b->value.type) { + case enum_key_undef: + case enum_key_int: + case enum_key_num: + valtmp = b->value; + break; + + case enum_key_string: + valtmp.type = enum_key_string; + valtmp.val.string_val + = string_copy(interp, b->value.val.string_val); + break; + + case enum_key_pmc: + valtmp.type = enum_key_pmc; + valtmp.val.pmc_val = b->value.val.pmc_val->vtable->clone( + interp, b->value.val.pmc_val); + break; + + default: + internal_exception(-1, "hash corruption: type = %d\n", + b->value.type); + }; + hash_put(interp, ret, b->key, &valtmp); b = b->next; } }