Giter Site home page Giter Site logo

Comments (6)

gsathya avatar gsathya commented on July 30, 2024 2

It's not obviously clear that the key will be the target and value will be the holdings. Apart from being similar in terms of dealing with weak objects, WeakMap and FinalizationGroup seem pretty orthogonal in their use cases. It seems better to have separate API for different use cases. Your particular use case would be better solved with a FinalizationGroup rather than a WeakMap.

from proposal-weakrefs.

bergus avatar bergus commented on July 30, 2024 1

WeakSet/WeakMap provide ephemerons, not weak references - they cannot be followed. A finalisation callback would be one thing, the more important thing would be to make the collections iterable. And in many cases, you need only a single weak reference, not a collection of them, so this approach misses objective.

from proposal-weakrefs.

xaviergonz avatar xaviergonz commented on July 30, 2024

Real use case, I have a library that returns WASM objects with native memory that needs to be reclaimed, it offers a dispose() method on such objects to do so, but the library user might forget to call it (therefore produce a memory leak)

library side

const wasmObjects = new WeakSet([], obj => obj.dispose());

function gimmeAWasmObject() {
  const wasmObject = createWasmObjectWithDisposer();
  wasmObjects.add(wasmObject);
  return wasmObject;
}

user side

const obj = gimmeAWasmObject();
obj = undefined; // no worries, the callback in the WeakSet will be called and dispose it

from proposal-weakrefs.

fitzgen avatar fitzgen commented on July 30, 2024

The finalization callback can't be given the object itself. That would allow the finalizer to either revive the dead object or create memory unsafety, neither of which are permissible.

Broader point: wasm finalization is definitely something that this proposal intends to support :)

from proposal-weakrefs.

xaviergonz avatar xaviergonz commented on July 30, 2024

What about making it only available for WeakMap and being the value (rather than the key, which is the dead object) the one that is sent to the callback?

const wasmObjects = new WeakMap({}, valueWhichIsTheDisposer => valueWhichIsTheDisposer());

function gimmeAWasmObject() {
  const [wasmObject, disposer] = createWasmObjectWithDisposer();
  wasmObjects.set(wasmObject, disposer);
  return wasmObject;
}

If value keeps the object alive then the callback will never be called.
If the value doesn't keep the object alive (as it should be) then the callback will be called.

from proposal-weakrefs.

littledan avatar littledan commented on July 30, 2024

Let's add an explanation in the README about why this would not provide a solution. PRs welcome.

from proposal-weakrefs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.