Another question: is it possible to cast from/into...
# webassembly
c
Another question: is it possible to cast from/into
ByteArray
<->
Int8Array
directly as it was possible in JS? to avoid copying?
b
it’s impossible, and unfortunately there is no fast way to copy yet 😞
c
okay, no problem 🙂 then I will do a copy. Will it be possible to get the Buffer of the WASM memory and the address so we can create a Int8Array view from that chunk of memory? It will be nice for webgl and webgpu
a
@Carlos Ballesteros Velasco, I experimented with that a little and think it is possible if you provide a callback that consumes address & array size. Reading the memory must happen in the callback though since Kotlin will free the memory after the function call. Alternatively, you can of course copy the chunk to JS memory during the callback. See this example: https://github.com/alxgrk/kotlin-nodejs-wasm-example/blob/main/src/wasmMain/kotlin/memoryUtil.kt & https://github.com/alxgrk/kotlin-nodejs-wasm-example/blob/main/node-app/wasmLibraries/primeFactorization.mjs
c
So from a JsFun chunk we can use
wasmExports.memory.buffer
to access to wasm's linear memory?
Also checked this: https://github.com/JetBrains/kotlin/blob/ee1608ec99767976d307b605120f93fce03d3720/libraries/stdlib/wasm/src/kotlin/wasm/unsafe/MemoryAllocation.kt Not sure if wasm would have limitations with that, but would be awesome if there could be a
ByteArray.usePin { pin -> pin.getAddress() }
eventually if it is possible.
BTW. I have migrated my libraries already: https://github.com/korlibs/korge/pull/1628 there are some errors, but there are also a lot of things working already.
For now I'm copying ByteArray<->Int8Array manually. Once everything compiles and somehow works we can start doing bencharks and figuring out how to improve stuff