Alexey Zolotarev
12/03/2024, 7:31 AMkotlin-wasm-wasi-template
https://github.com/Kotlin/kotlin-wasm-wasi-template/blob/main/src/wasmWasiMain/kotlin/MonotonicTime.kt#L19-L30:
@OptIn(UnsafeWasmMemoryApi::class)
fun wasiGetTime(clockId: Int): Long = withScopedMemoryAllocator { allocator ->
val rp0 = allocator.allocate(8)
val ret = wasiRawClockTimeGet(
clockId = clockId, precision = 1,
resultPtr = rp0.address.toInt()
)
// ...
(Pointer(rp0.address.toInt().toUInt())).loadLong()
}
withScopedMemoryAllocator
relies on Wasm GC to allocate the memory as far as I can understand. Wasm GC has its own heap according to the Wasm GC proposal which is separate from the guest linear memory. But then the host in wasiRawClockTimeGet
seems to write the response data at rp0.address
in the guest linear memory and the guest can somehow read it with (Pointer(rp0.address.toInt().toUInt())).loadLong()
.
So what I don't understand is that if the memory allocated in allocator.allocate(8)
is in Wasm GC heap then why the host is writing the data to the linear memory at this address? Does Wasm GC heap overlap with the linear memory?Igor Yakovlev
12/03/2024, 8:39 AMIgor Yakovlev
12/03/2024, 8:42 AMAlexey Zolotarev
12/03/2024, 9:25 AMwithScopedMemoryAllocator
? And objects automatically allocated by Kotlin during the lifetime of the module are stored in the Wasm GC heap and rely on Wasm GC for garbage collection?Igor Yakovlev
12/03/2024, 9:54 AM