Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

FR: WeakRef #631

Closed
ghost opened this issue Feb 4, 2011 · 5 comments
Closed

FR: WeakRef #631

ghost opened this issue Feb 4, 2011 · 5 comments

Comments

@ghost
Copy link

ghost commented Feb 4, 2011

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

@bnoordhuis
Copy link
Member

@Herby, how should that work?

@ghost
Copy link
Author

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
Copy link

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. http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps

@bnoordhuis
Copy link
Member

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.

@TooTallNate
Copy link

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.
Projects
None yet
Development

No branches or pull requests

3 participants