I would like for Parrot to have some way of creating Weak References; I
think that this is probably a vital feature.

The way I envision this is as follows.  The following typedef and new
function would be added:

typedef void (*pobject_died_cb)(INTERP, PMC* who_asked,
   Pobj* weakref, void *callback_info);
void pobject_weakref(INTERP, pobject_died_cb callback,
   Pobj* weakref, void *callback_info);

Inside of a PMC*'s mark method, it registers callbacks (using the above
function) so that it will be informed about what to do if the object to
which it weakly refers to is found to not be alive at the end of the DOD
sweep.

The pobject_weakref function first checks if the 'weakref' argument has
been marked as alive -- if so, nothing happens.  Then, it adds the Pobj*
to a lookup table, pointing from the Pobj*, to a list of registered
callbacks for that Pobj*.

After DOD finishes, the lookup table is walked; for each entry whose
Pobj* hasn't been marked as alive, the callbacks are called.

The effect of this of course is that a WeakRef has no cost except during
Dead Object Detection.


The first, perhaps most important use, would be to implement a
string-interning table.

You'd have a global (per-interpreter) pmc which contains a hashtable of
cstrings to perlstrings; if the cstring is present, the corresponding
perlstring is returned; otherwise a new perlstring would be created and
added to the table, then returned.  If any of the perlstrings go out of
scope, then their hash entries should disappear from the table.
Obivously, if the table contained normal references to the strings, then
they won't ever go out of scope.  And if the table simply doesn't mark
them as alive, it wouldn't know when they're dead.  But with weakrefs,
this is easy.

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "[EMAIL PROTECTED]
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}

Reply via email to