|
|
|
|
|
by Vanit
662 days ago
|
|
I don't understand the use case, can someone enlighten me? I've always used them as a caching mechanism for derived / expensive data, and they work perfectly for that as-is. If you want to enumerate them I can't help but think your mental model is wrong and you actually don't want the keys to ever be released from memory. |
|
The use case for Andy here is building backtraces for Scheme compiled to WebAssembly. To print a meaningful backtrace, a Wasm funcref for a compiled Scheme function needs to be mapped to a code origin (file name, line, column). Unfortunately, Wasm can't compare funcrefs, so the host needs to do it. Andy's solution is to have each Scheme Wasm module maintain a WeakMap of funcrefs to code origins. However, this alone is not enough because there may be many Scheme modules that are sharing resources and any given funcref could come from any of the modules. So, the runtime needs to maintain a weak set of all modules so that you can query for the code origin for any funcref and the runtime can search all the modules to find it. A WeakSet would be enough for this task... if it were iterable. To work around this, Andy made an IterableWeakSet which has the drawbacks noted in the blog post.
I also ran into another use case recently while trying to port a library written in a language with iterable weak maps to Wasm with JS providing the weak map support. I'll have to do a very similar, unsatisfying workaround there, too.