mmomtchev/swig

Reuse existing JavaScript proxy objects instead of creating duplicates

Opened this issue · 0 comments

Currently the following pattern:

class Object;

Object *fn() {
  static Object o;
  return &o;
}

will always create a new JavaScript proxy object for each invocation. Not only this is wasteful, but also operations such as

fn() === fn()

would not have the expected result.

This problem exists in most languages supported by SWIG.

It can be solved by storing weak references to all returned objects in a hash table indexed by the pointer address.

However there are practical problems with handling the destruction of these objects in JavaScript - namely the V8 delayed destruction which cancels the weak references without immediately destroying the objects.