On Sun, 2004-01-25 at 02:19, Leopold Toetsch wrote: > s. t/pmc/orderedhash.t The initializer of the struct could look like: > > new P1, .OrderedHash > set P1["a"], .DATATYPE_a > set P1[1], 0 > set P1[2], 0 > set P1["b"], .DATATYPE_b > set P1[4], 0 > set P1[5], 0 > new P2, .UnManagedStruct, P1 > > Now you can access P2[0] := P2["a"] or P2[1] := P2["b"] elements of the > structure by name or by struct item index. (The index is internally > multiplied by 3, so that it maps to the initializer item).
Ahh, I think I get it. Here's what a couple of hours of fumbling produced. It's still not quite right and I'm stuck, but I'm further than before. I'm currently getting the error "setting unhandled int type in struct" in keyedstruct.imcc, probably because UnManagedStruct's set_int() doesn't know how to handle INT16s. Of course, if I change them to INTVALs, it dumps core with a segfault. Clearly, there's something else I'm missing. I'd really like to get something like hashlike.imcc working, as that's very close to what I need for SDL_Rect objects. -- c
.include "datatypes.pasm" .sub _main new $P1, .OrderedHash push $P1, .DATATYPE_INT16 push $P1, 0 push $P1, 0 push $P1, .DATATYPE_INT16 push $P1, 0 push $P1, 0 new $P2, .UnManagedStruct, $P1 set $I0, 0 sizeof $I1, .DATATYPE_INT16 add $I0, $I1 add $I0, $I1 set $P1, $I0 print "Set x\n" set $I0, 2 set $P2[0], 2 print "Set y\n" set $I1, 16 set $P2[1], $I1 set $I2, $P1[0] set $I3, $P1[1] print "\nx: " print $I2 print "\ny: " print $I3 print "\n" end .end
.include "datatypes.pasm" .sub _main new $P1, .OrderedHash set $P1['x'], 0 push $P1, .DATATYPE_INT16 push $P1, 0 push $P1, 0 $I0 = $P1['x'] print "X index: " print $I0 print "\n" set $P1['y'], 1 push $P1, .DATATYPE_INT16 push $P1, 0 push $P1, 0 $I1 = $P1['y'] print "Y index: " print $I1 print "\n" new $P2, .UnManagedStruct, $P1 set $I0, 0 sizeof $I1, .DATATYPE_INT16 add $I0, $I1 add $I0, $I1 set $P1, $I0 set $I0, 2 set $P2["x"], $I0 print "Set x value\n" set $I1, 16 set $S0, "y" set $P2[$S0], $I1 print "Set y value by string\n" set $I2, $P1[0] set $I3, $P1[1] print "\nx: " print $I2 print "\ny: " print $I3 print "\n" end .end
Index: classes/orderedhash.pmc =================================================================== RCS file: /cvs/public/parrot/classes/orderedhash.pmc,v retrieving revision 1.13 diff -u -u -r1.13 orderedhash.pmc --- classes/orderedhash.pmc 26 Jan 2004 09:39:39 -0000 1.13 +++ classes/orderedhash.pmc 27 Jan 2004 07:26:40 -0000 @@ -61,6 +61,13 @@ return DYNSELF.get_pmc_keyed_int(n); } + INTVAL get_integer_keyed (PMC* key) + { + if (PObj_get_FLAGS(key) & KEY_string_FLAG) + return PerlHash.SELF.get_integer_keyed_str( PMC_str_val(key) ); + return SUPER( key ); + } + INTVAL get_integer_keyed_str (STRING* key) { INTVAL n = PerlHash.SELF.get_integer_keyed_str(key); return DYNSELF.get_integer_keyed_int(n); @@ -77,6 +84,18 @@ DYNSELF.set_pmc_keyed_int(n, value); PerlHash.SELF.set_integer_keyed_str(key, n); } + + void set_integer_keyed (PMC* key, INTVAL value) + { + STRING* str_key; + + if (PObj_get_FLAGS(key) & KEY_string_FLAG) + { + PerlHash.SELF.set_integer_keyed_str( PMC_str_val(key), value ); + return; + } + SUPER(key, value); + } void set_integer_keyed_str (STRING* key, INTVAL value) { INTVAL n = DYNSELF.elements(); Index: classes/unmanagedstruct.pmc =================================================================== RCS file: /cvs/public/parrot/classes/unmanagedstruct.pmc,v retrieving revision 1.23 diff -u -u -r1.23 unmanagedstruct.pmc --- classes/unmanagedstruct.pmc 26 Jan 2004 09:39:40 -0000 1.23 +++ classes/unmanagedstruct.pmc 27 Jan 2004 07:26:40 -0000 @@ -38,8 +38,17 @@ { size_t offs, n, count, size, max; PMC *next; - int ix = key_integer(interpreter, key); + int ix; char *p; + + if (PObj_get_FLAGS(key) & KEY_string_FLAG) + { + ix = (int)VTABLE_get_integer_keyed(interpreter, PMC_pmc_val(pmc), key); + } + else + { + ix = key_integer(interpreter, key); + } next = key_next(interpreter, key); p = char_offset_int(interpreter, pmc, ix, type); @@ -67,7 +76,7 @@ case enum_type_uchar: return *p; default: - internal_exception(1, "unhandled type in struct"); + internal_exception(1, "fetching unhandled int type in struct"); } return -1; } @@ -83,7 +92,7 @@ case enum_type_double: return (FLOATVAL) *(double*) p; default: - internal_exception(1, "unhandled type in struct"); + internal_exception(1, "fetching unhandled float type in struct"); } return -1.0; } @@ -106,7 +115,7 @@ *(short*)p = (short)value; break; default: - internal_exception(1, "unhandled type in struct"); + internal_exception(1, "setting unhandled int type in struct"); break; } }