Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

laurie71 opened this issue Nov 20, 2012 · 3 comments

Comments

@laurie71
Copy link

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

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

@laurie71
Copy link
Author

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

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

No branches or pull requests

2 participants