This repository has been archived by the owner. It is now read-only.

FR: WeakRef #631

ghost opened this Issue Feb 4, 2011 · 5 comments


None yet
3 participants

ghost commented Feb 4, 2011

Generic weak reference exposed to Javascript level would be handy.
Unlocks lots of possibilities not usable yet.


bnoordhuis commented Jul 25, 2011

@Herby, how should that work?


ghost commented Jul 26, 2011

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.

isaacs commented Jul 31, 2011

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.


bnoordhuis commented Aug 1, 2011

Turns out we can do this, a proof of concept is here:

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, node-weak. We even use this 3rd party module for the gc tests, and it seems to work out without being a core part of node.

Plus you can use harmony WeakMaps at this point if you really want to.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.