I have a little trouble understanding how host <-> guest data exchange works with Kotlin. Here is an example from the
kotlin-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?