On Nov 30, 2004, at 11:45 AM, Dan Sugalski wrote:
In this example:
% cat continuation6.ruby def strange callcc {|continuation| $saved = continuation} end
def outer a = 0 strange() a = a + 1 print "a = ", a, "\n" end
Through the joys of reference types, a will continue to increase forevermore, assuming the compiler hasn't incorrectly put a in an int register. (Which'd be wrong)
Separate question, but then what would happen for languages which _do_ use primitive types? (Presumably, Perl6 would do that in the "my int" case.) If proper behavior requires basically never using the primitive I/N types, that seems like a waste.
Two potential options. One, they have a backing PMC for the lexicals (which they'll probably need anyway) and flush/restore at spots where things could get lost. Two, they don't actually get a low-level type but the compiler cheats and acts as if it was in spots where it's safe. (I admit, I'd always planned on having "my int $foo" use a PMC. The win would be for "my int @foo" which would also get a PMC, but one that had optimized backing store for the values)
The contents can change over and over without the register itself ever changing.
But in this Ruby case, "a = a + 1" actually creates a new Fixnum instance, so "a" ends up holding a different instance each time--you can verify that by printing out a.id in the print statement.
This is where the magic of objects comes in, for particularly loose values of "magic". Ruby uses a double-reference system for objects the same way that perl 5 does -- that is, the underlying structure for a doesn't hold the object, rather it holds a pointer to another structure that holds the object. So it looks like:
(name a) -> (a struct, addr 0x04) -> (object struct addr 0x08)
and after the store it looks like:
(name a) -> (a struct, addr 0x04) -> (object struct addr 0x0C)
The PMC register would hold the 0x04 struct pointer, basically a Reference PMC, and assignments to it just switch the thing it refers to.
Essentially things like I and N registers are value types, PMC and strings are (for us) reference types, and many objects (including ruby and perl's objects) are double-reference types.
Which, yeah, means we've been ignoring some important object things. Time to deal with that, too.
--
Dan
--------------------------------------it's like this------------------- Dan Sugalski even samurai [EMAIL PROTECTED] have teddy bears and even teddy bears get drunk