-
Notifications
You must be signed in to change notification settings - Fork 7.3k
FR: WeakRef #631
Comments
@Herby, how should that work? |
Well, any form of weak reference. For example WeakRef class containing weak reference and returning it when toValue() is called (or nil, if expired). Or WeakObject, derived from object which will have all its own properties weak. Or anything else, but having able to use some form of weak reference programmatically. |
If we do this, it should be by either waiting for v8 to implement the es-next WeakMap spec, or we should mock it to that specification. http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps |
Turns out we can do this, a proof of concept is here: https://github.com/bnoordhuis/node-weakref Here is what usage looks like: var obj = weakref.weaken({val:42});
assert.equal(obj.val, 42);
assert.deepEqual(obj, {val:42});
force_gc(); // force weak object to be GC'ed
assert.equal(obj.val, undefined);
assert.deepEqual(obj, {}); For the record: it doesn't pretend to be a WeakMap implementation (which is an awful proposal if you ask me but that's beside the point), it simply showcases that weak references are indeed possible in V8. |
This doesn't belong in core. I took @bnoordhuis' proof of concept a long time ago and decided to package it up into an npm module, Plus you can use harmony WeakMaps at this point if you really want to. |
Generic weak reference exposed to Javascript level would be handy.
Unlocks lots of possibilities not usable yet.
The text was updated successfully, but these errors were encountered: