Jason Zhao
04/05/2023, 5:26 PMSvyatoslav Kuzmich [JB]
04/05/2023, 5:43 PMJason Zhao
04/05/2023, 5:52 PMSvyatoslav Kuzmich [JB]
04/05/2023, 6:16 PMJason Zhao
04/05/2023, 6:19 PMJason Zhao
04/05/2023, 6:20 PMJason Zhao
04/05/2023, 6:29 PMSvyatoslav Kuzmich [JB]
04/05/2023, 6:31 PMis there a faster way to copy data from Kotlin/WASM arrays to Java number arrays than simply running a loop and setting numbers one by one?Unfortunately no, this is a current Wasm GC limitation, but I expect it to be addressed in the next iteration of the spec. The fastest way currently is to copy data to linear memory (one element at a time) and then use it JavaScript. I found an example of the opposite operation, but the idea is the same.
Svyatoslav Kuzmich [JB]
04/05/2023, 6:34 PMJason Zhao
04/05/2023, 6:38 PMJason Zhao
04/05/2023, 6:39 PMSvyatoslav Kuzmich [JB]
04/05/2023, 6:41 PMJason Zhao
04/05/2023, 6:42 PMSvyatoslav Kuzmich [JB]
04/05/2023, 6:42 PMSvyatoslav Kuzmich [JB]
04/05/2023, 6:52 PMWould it be reliable if i try without copying first?Use it only if you know for sure. For instance, if this behaviour is documented in the API. Otherwise you’ll get undefined behavior.
Jason Zhao
04/05/2023, 6:54 PMSvyatoslav Kuzmich [JB]
04/05/2023, 6:57 PMcan I make inline value classes with multiple parameters in Kotlin/WASM or is that planned for the future?Not yet. We don’t have concrete plans. I think this feature would need to be designed in the future together with other Kotlin platforms. At least together with Kotlin/Native which doesn’t have any technical limitations as well.
Jason Zhao
04/05/2023, 6:59 PMJason Zhao
04/05/2023, 7:00 PMSvyatoslav Kuzmich [JB]
04/05/2023, 7:00 PMJason Zhao
04/05/2023, 7:01 PM@JvmInline
annotation or is there a different multiplatform one?Jason Zhao
04/05/2023, 7:01 PMSvyatoslav Kuzmich [JB]
04/05/2023, 7:01 PMvalue class
should work fine on non-JVM platformsJason Zhao
04/05/2023, 7:02 PMSvyatoslav Kuzmich [JB]
04/05/2023, 7:02 PMJason Zhao
04/05/2023, 7:03 PMAlexander Girke
04/05/2023, 9:25 PMis there a faster way to copy data from Kotlin/WASM arrays to JavaScript number arrays than simply running a loop and setting numbers one by one?I am actually playing around with passing number arrays from K/Wasm to JS right now by doing something similar as the example of the opposite operation @Svyatoslav Kuzmich [JB] mentioned. The idea is the following: • pass a JS function reference to WebAssembly.Instance’s importObject with signature
(address, size, bytesPerElement) -> void
• write K/Wasm Array to linear memory utilising ScopedMemoryAllocator
• call external JS function within MemoryAllocator scope passing pointer address, array size & bytesPerElement
• use JS’s `Int8Array`/`Int16Array`/`Int32Array`/`BigInt64Array` to create a view on the buffer
As reading the allocated buffer is done within the JS callback, I think this should be safe to be used from ScopedMemoryAllocator. Copying from linear memory to a separate JS ArrayBuffer within JS callback should also be possible if needed.
Will try to push the example code asap for anyone interested to validate the idea, but I think this would work as an answer to your question @Jason Zhao .Jason Zhao
04/05/2023, 9:33 PMAdam S
04/05/2023, 10:08 PMShould I expect a native Kotlin/WASM 3D library to come out in the future similar to how Bevy engine works in Rust WASM?I’ve been mulling over using Raylib for this. I’m mucking about with Kotlin/Native bindings, and it work be relative easy (albeit boring and repetitive) to try writing Node/JS bindings…
Alexander Girke
04/07/2023, 11:29 AMonResult
callback - and since the JS implementation of onResult
(readJsArrayFromLinearMemory
in https://github.com/alxgrk/kotlin-nodejs-wasm-example/blob/main/src/wasmMain/kotlin/memoryUtil.kt) only creates a view on the buffer and consumes it within the scope of MemoryAllocator, it should be GCed correctly. If it would be copied to an JS array, of course the Node.js GC would take care of it.