related to the above, but different question: `wit...
# webassembly
b
related to the above, but different question:
withScopedMemoryAllocator
states that it frees all allocated memory after running the block. in my situation it would be beneficial to keep some intermediate-results in wasm-memory and use them in various subsequent js->wasm calls, rather than passing them out and back in. is there a way to achieve that with kotlin (perhaupt the wasm-module can be changed to export a second memory or something like that..) ?
s
All memory is currently managed by
withScopedMemoryAllocator
, and it does release allocated memory at the end of the block. You can reuse allocated memory if you can lift
withScopedMemoryAllocator
block up the stack to include all usages within it. Not possible otherwise.
In the future we consider adding an ability to define more memories. It would enable building and sharing custom allocators.
🙌 1
b
ah ok. i'm not sure if that's feasible in my case. is it possible to make a long-running call that keeps the memory allocated, and multiple ones that use it at the same time ?
not sure how i'd even keep the allocation-call active, i guess there's no sleep/suspend or such..
s
Yes, even though it wasn’t originally designed for this purpose, you can make long-running calls like:
Copy code
fun main() { 
   withScopedMemoryAllocator { allocator -> 
      MyWholeApp.run(allocator)
   }
}
Be aware that all allocations will remain active until the end of the block.
b
interesting. since my app is mostly running in js, i guess i'd need something like
Copy code
fun precomputeAndWait() {
    withScopedMemoryAllocator {
        allocateMemoryAndComputeStuff()
        callSomeJSFunctionThatKeepsMeWaiting()
    }
}

fun usePrecomputedStuff() {
    doOtherStuffWithAllocatedMemory()
}
i'm not that experienced with wasm, so i'll probably not do that for now, but might try at some point. can you think of something to just keep a js->wasm call running for some seconds without expensive busy-waiting ?
do you also consider adding threading-related apis at some point ? there seem to be mutex/wait/notify-instructions already available in wasm: https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md#atomic-memory-accesses