A question about weak()'s return value #13

Open
laurie71 opened this Issue Nov 20, 2012 · 3 comments

Projects

None yet

2 participants

@laurie71

The documentation says that the return value from weak(o) is an object that proxies all of o's properties and methods, so it seams I should be able to pass that around instead of 'o'. However, it doesn't look like it has o's prototype chain, which means it isn't a fully transparent replacement for o.

Is that true? And if so, is it by design, or is that something that can be fixed/added?

@TooTallNate
Owner

Can you show me a failing test case of what you would expect to be working?

@laurie71

Sure, code always makes things more concrete :)

var assert = require('assert')
,   weak = require('weak')
,   Ctor = function() {}
,   inst = new Ctor()
,   ref  = weak(inst, function() {})

assert.ok(inst instanceof Ctor)
assert.ok(ref instanceof Ctor) // FAILS
@laurie71

Motivation for this:

var ammo = require('ammo')
,   weak = require('weak')

exports.btVector3 = function(...args) {
    var v = new ammo.btVector3(...args)
    ,   r = weak(v, ammo.destroy)
    return r
}

The 'ammo' module is a port of the Bullet physics simulation engine, created via trans-compilation of the C++ source to JS via emscripten. Unfortunately, since emscripten's execution model requires objects allocated with new to be explicitly disposed with ammo.destroy(); if you don't call destroy(), the object will never be eligible for GC.

I'm therefore trying to wrap the constructors exposed by ammo with something like the code above, leveraging weak to automate memory management of ammo objects -- it seems like the perfect tool to solve this problem :-)

However, ammo chokes when I pass it objects that are wrapped as weak refs like this. Looking into why, I discovered the issue I reported here -- I don't know if it would solve the problem with ammo, but satisfying the invariant expressed in the test code in the previous comment seems like a nice thing to have :-)

Another one that would be nice would be to satisfy assert.ok(ref == inst) or even assert.ok(ref === inst), but from what little I know of the v8 API, I doubt those are possible...

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