From 70eb5b042c6e4cfea48f970a33eb9efa59215fa4 Mon Sep 17 00:00:00 2001 From: Alexey Illarionov Date: Thu, 20 Jun 2024 10:14:36 +0300 Subject: [PATCH] Refactor memory (#135) --- .../common/capi/Sqlite3StatementFunctions.kt | 5 +- .../kotlin/host/memory/ChasmMemoryAdapter.kt | 14 ++-- .../host/memory/ChicoryMemoryAdapter.kt | 11 ++-- .../memory/GraalvmWasmHostMemoryAdapter.kt | 18 ++++-- .../commonMain/kotlin/base/memory/Memory.kt | 64 +++++++++++++++++-- .../kotlin/base/memory/MemoryExt.kt | 50 --------------- .../kotlin/base/memory/WasiMemoryWriter.kt | 7 +- .../kotlin/emscripten/FcntlHandler.kt | 4 +- .../function/GetentropyFunctionHandle.kt | 1 - .../function/LocaltimeJsFunctionHandle.kt | 1 - .../function/SyscallFstat64FunctionHandle.kt | 1 - .../function/SyscallGetcwdFunctionHandle.kt | 1 - .../SyscallStatLstat64FunctionHandle.kt | 1 - .../function/TzsetJsFunctionHandle.kt | 1 - .../kotlin/ext/NullTerminatedStringExt.kt | 6 +- .../test/assertions/MemoryAssertions.kt | 4 +- .../kotlin/test/fixtures/TestMemory.kt | 17 +++-- 17 files changed, 109 insertions(+), 97 deletions(-) delete mode 100644 wasi-emscripten-host/src/commonMain/kotlin/base/memory/MemoryExt.kt diff --git a/sqlite-common/src/commonMain/kotlin/sqlite/common/capi/Sqlite3StatementFunctions.kt b/sqlite-common/src/commonMain/kotlin/sqlite/common/capi/Sqlite3StatementFunctions.kt index 0574cf1d..15da2a0f 100644 --- a/sqlite-common/src/commonMain/kotlin/sqlite/common/capi/Sqlite3StatementFunctions.kt +++ b/sqlite-common/src/commonMain/kotlin/sqlite/common/capi/Sqlite3StatementFunctions.kt @@ -12,7 +12,6 @@ import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.readNullableNullTerminatedString import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.readPtr -import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.write import ru.pixnews.wasm.sqlite.open.helper.host.base.plus import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.api.SqliteColumnType import ru.pixnews.wasm.sqlite.open.helper.sqlite.common.api.SqliteDb @@ -46,7 +45,7 @@ public class Sqlite3StatementFunctions internal constructor( ppStatement = memoryBindings.sqliteAllocOrThrow(WasmPtr.WASM_SIZEOF_PTR) memory.write(sqlBytesPtr, sqlEncoded) - memory.writeByte(sqlBytesPtr + sqlEncoded.size, 0) + memory.writeI8(sqlBytesPtr + sqlEncoded.size, 0) val errCode = sqliteExports.sqlite3_prepare_v2.executeForSqliteResultCode( sqliteDb.addr, @@ -244,6 +243,6 @@ public class Sqlite3StatementFunctions internal constructor( statement.addr, columnIndex, ) - return memory.readBytes(ptr, bytes) + return ByteArray(bytes).also { memory.read(ptr, it) } } } diff --git a/sqlite-embedder-chasm/src/commonMain/kotlin/host/memory/ChasmMemoryAdapter.kt b/sqlite-embedder-chasm/src/commonMain/kotlin/host/memory/ChasmMemoryAdapter.kt index 330a06d0..e67e37f3 100644 --- a/sqlite-embedder-chasm/src/commonMain/kotlin/host/memory/ChasmMemoryAdapter.kt +++ b/sqlite-embedder-chasm/src/commonMain/kotlin/host/memory/ChasmMemoryAdapter.kt @@ -43,11 +43,13 @@ internal class ChasmMemoryAdapter( return MemoryInstanceLongReaderImpl(memoryInstance, addr.addr, 8).getOrThrow() } - override fun readBytes(addr: WasmPtr<*>, length: Int): ByteArray { - return ByteArray(length) { readMemory(store, memoryAddress, addr.addr + it).orThrow() } + override fun read(addr: WasmPtr<*>, destination: ByteArray, destinationOffset: Int, readBytes: Int) { + repeat(readBytes) { offset -> + destination[destinationOffset + offset] = readMemory(store, memoryAddress, addr.addr + offset).orThrow() + } } - override fun writeByte(addr: WasmPtr<*>, data: Byte) { + override fun writeI8(addr: WasmPtr<*>, data: Byte) { writeMemory(store, memoryAddress, addr.addr, data).orThrow() } @@ -59,9 +61,9 @@ internal class ChasmMemoryAdapter( return MemoryInstanceLongWriterImpl(memoryInstance, data, addr.addr, 8).getOrThrow() } - override fun write(addr: WasmPtr<*>, data: ByteArray, offset: Int, size: Int) { - for (addrOffset in 0 until size) { - writeMemory(store, memoryAddress, addr.addr + addrOffset, data[offset + addrOffset]) + override fun write(addr: WasmPtr<*>, source: ByteArray, sourceOffset: Int, writeBytes: Int) { + for (addrOffset in 0 until writeBytes) { + writeMemory(store, memoryAddress, addr.addr + addrOffset, source[sourceOffset + addrOffset]) } } diff --git a/sqlite-embedder-chicory/src/jvmMain/kotlin/host/memory/ChicoryMemoryAdapter.kt b/sqlite-embedder-chicory/src/jvmMain/kotlin/host/memory/ChicoryMemoryAdapter.kt index 3617cef5..378ae96a 100644 --- a/sqlite-embedder-chicory/src/jvmMain/kotlin/host/memory/ChicoryMemoryAdapter.kt +++ b/sqlite-embedder-chicory/src/jvmMain/kotlin/host/memory/ChicoryMemoryAdapter.kt @@ -26,11 +26,12 @@ internal class ChicoryMemoryAdapter( return wasmMemory.readI64(addr.addr).asLong() } - override fun readBytes(addr: WasmPtr<*>, length: Int): ByteArray { - return wasmMemory.readBytes(addr.addr, length) + override fun read(addr: WasmPtr<*>, destination: ByteArray, destinationOffset: Int, readBytes: Int) { + val bytes = wasmMemory.readBytes(addr.addr, readBytes) + bytes.copyInto(destination, destinationOffset) } - override fun writeByte(addr: WasmPtr<*>, data: Byte) { + override fun writeI8(addr: WasmPtr<*>, data: Byte) { wasmMemory.writeByte(addr.addr, data) } @@ -42,7 +43,7 @@ internal class ChicoryMemoryAdapter( wasmMemory.writeLong(addr.addr, data) } - override fun write(addr: WasmPtr<*>, data: ByteArray, offset: Int, size: Int) { - wasmMemory.write(addr.addr, data, offset, size) + override fun write(addr: WasmPtr<*>, source: ByteArray, sourceOffset: Int, writeBytes: Int) { + wasmMemory.write(addr.addr, source, sourceOffset, writeBytes) } } diff --git a/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/memory/GraalvmWasmHostMemoryAdapter.kt b/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/memory/GraalvmWasmHostMemoryAdapter.kt index 93d42fbc..6258a54f 100644 --- a/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/memory/GraalvmWasmHostMemoryAdapter.kt +++ b/sqlite-embedder-graalvm/src/jvmMain/kotlin/host/memory/GraalvmWasmHostMemoryAdapter.kt @@ -39,13 +39,17 @@ internal class GraalvmWasmHostMemoryAdapter( return wasmMemory.load_i64(node, addr.addr.toLong()) } - override fun readBytes(addr: WasmPtr<*>, length: Int): ByteArray { - val bous = ByteArrayOutputStream(length) - wasmMemory.copyToStream(node, bous, addr.addr, length) - return bous.toByteArray() + override fun read(addr: WasmPtr<*>, destination: ByteArray, destinationOffset: Int, readBytes: Int) { + val bous = object : ByteArrayOutputStream(readBytes) { + fun copyInto(destination: ByteArray, destinationOffset: Int) { + buf.copyInto(destination, destinationOffset, 0, count) + } + } + wasmMemory.copyToStream(node, bous, addr.addr, readBytes) + bous.copyInto(destination, destinationOffset) } - override fun writeByte(addr: WasmPtr<*>, data: Byte) { + override fun writeI8(addr: WasmPtr<*>, data: Byte) { wasmMemory.store_i32_8(node, addr.addr.toLong(), data) } @@ -57,7 +61,7 @@ internal class GraalvmWasmHostMemoryAdapter( wasmMemory.store_i64(node, addr.addr.toLong(), data) } - override fun write(addr: WasmPtr<*>, data: ByteArray, offset: Int, size: Int) { - wasmMemory.initialize(data, offset, addr.addr.toLong(), size) + override fun write(addr: WasmPtr<*>, source: ByteArray, sourceOffset: Int, writeBytes: Int) { + wasmMemory.initialize(source, sourceOffset, addr.addr.toLong(), writeBytes) } } diff --git a/wasi-emscripten-host/src/commonMain/kotlin/base/memory/Memory.kt b/wasi-emscripten-host/src/commonMain/kotlin/base/memory/Memory.kt index 35c6159e..220c03de 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/base/memory/Memory.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/base/memory/Memory.kt @@ -6,20 +6,32 @@ package ru.pixnews.wasm.sqlite.open.helper.host.base.memory +import okio.Buffer import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr +import ru.pixnews.wasm.sqlite.open.helper.host.base.isSqlite3Null @InternalWasmSqliteHelperApi public interface Memory { public fun readI8(addr: WasmPtr<*>): Byte public fun readI32(addr: WasmPtr<*>): Int public fun readI64(addr: WasmPtr<*>): Long - public fun readBytes(addr: WasmPtr<*>, length: Int): ByteArray + public fun read( + addr: WasmPtr<*>, + destination: ByteArray, + destinationOffset: Int = 0, + readBytes: Int = destination.size, + ) - public fun writeByte(addr: WasmPtr<*>, data: Byte) + public fun writeI8(addr: WasmPtr<*>, data: Byte) public fun writeI32(addr: WasmPtr<*>, data: Int) public fun writeI64(addr: WasmPtr<*>, data: Long) - public fun write(addr: WasmPtr<*>, data: ByteArray, offset: Int, size: Int) + public fun write( + addr: WasmPtr<*>, + source: ByteArray, + sourceOffset: Int = 0, + writeBytes: Int = source.size, + ) } @InternalWasmSqliteHelperApi @@ -31,6 +43,15 @@ public fun Memory.readU32(addr: WasmPtr<*>): UInt = readI32(addr).toUInt() @InternalWasmSqliteHelperApi public fun Memory.readU64(addr: WasmPtr<*>): ULong = readI64(addr).toULong() +@InternalWasmSqliteHelperApi +public fun Memory.writeU8(addr: WasmPtr<*>, data: UByte): Unit = writeI8(addr, data.toByte()) + +@InternalWasmSqliteHelperApi +public fun Memory.writeU32(addr: WasmPtr<*>, data: UInt): Unit = writeI32(addr, data.toInt()) + +@InternalWasmSqliteHelperApi +public fun Memory.writeU64(addr: WasmPtr<*>, data: ULong): Unit = writeI64(addr, data.toLong()) + @Suppress("UNCHECKED_CAST") @InternalWasmSqliteHelperApi public fun > Memory.readPtr(addr: WasmPtr

): P = WasmPtr(readI32(addr)) as P @@ -39,4 +60,39 @@ public fun > Memory.readPtr(addr: WasmPtr

): P = WasmP public fun Memory.writePtr(addr: WasmPtr<*>, data: WasmPtr<*>): Unit = writeI32(addr, data.addr) @InternalWasmSqliteHelperApi -public fun Memory.write(addr: WasmPtr<*>, data: ByteArray): Unit = write(addr, data, 0, data.size) +public fun Memory.readNullableNullTerminatedString(offset: WasmPtr): String? { + return if (!offset.isSqlite3Null()) { + readNullTerminatedString(offset) + } else { + null + } +} + +@InternalWasmSqliteHelperApi +public fun Memory.readNullTerminatedString(offset: WasmPtr): String { + check(offset.addr != 0) + + val mem = Buffer() + var addr = offset.addr + do { + val byte = this.readI8(WasmPtr(addr)) + addr += 1 + if (byte == 0.toByte()) { + break + } + mem.writeByte(byte.toInt()) + } while (true) + + return mem.readByteString().utf8() +} + +@InternalWasmSqliteHelperApi +public fun Memory.writeNullTerminatedString( + offset: WasmPtr<*>, + value: String, +): Int { + val encoded = value.encodeToByteArray() + write(offset, encoded) + writeI8(WasmPtr(offset.addr + encoded.size), 0) + return encoded.size + 1 +} diff --git a/wasi-emscripten-host/src/commonMain/kotlin/base/memory/MemoryExt.kt b/wasi-emscripten-host/src/commonMain/kotlin/base/memory/MemoryExt.kt deleted file mode 100644 index 59957662..00000000 --- a/wasi-emscripten-host/src/commonMain/kotlin/base/memory/MemoryExt.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file - * for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - * SPDX-License-Identifier: Apache-2.0 - */ - -package ru.pixnews.wasm.sqlite.open.helper.host.base.memory - -import okio.Buffer -import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi -import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr -import ru.pixnews.wasm.sqlite.open.helper.host.base.isSqlite3Null - -@InternalWasmSqliteHelperApi -public fun Memory.readNullableNullTerminatedString(offset: WasmPtr): String? { - return if (!offset.isSqlite3Null()) { - readNullTerminatedString(offset) - } else { - null - } -} - -@InternalWasmSqliteHelperApi -public fun Memory.readNullTerminatedString(offset: WasmPtr): String { - check(offset.addr != 0) - - val mem = Buffer() - var addr = offset.addr - do { - val byte = this.readI8(WasmPtr(addr)) - addr += 1 - if (byte == 0.toByte()) { - break - } - mem.writeByte(byte.toInt()) - } while (true) - - return mem.readByteString().utf8() -} - -@InternalWasmSqliteHelperApi -public fun Memory.writeNullTerminatedString( - offset: WasmPtr<*>, - value: String, -): Int { - val encoded = value.encodeToByteArray() - write(offset, encoded) - writeByte(WasmPtr(offset.addr + encoded.size), 0) - return encoded.size + 1 -} diff --git a/wasi-emscripten-host/src/commonMain/kotlin/base/memory/WasiMemoryWriter.kt b/wasi-emscripten-host/src/commonMain/kotlin/base/memory/WasiMemoryWriter.kt index aa05036e..c300d26e 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/base/memory/WasiMemoryWriter.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/base/memory/WasiMemoryWriter.kt @@ -37,10 +37,9 @@ public class DefaultWasiMemoryWriter( memory: Memory, ): List = List(ciovecList.size) { idx -> val ciovec = ciovecList[idx] - val bytes = memory.readBytes( - ciovec.buf, - ciovec.bufLen.value.toInt(), - ) + val bytes = ByteArray(ciovec.bufLen.value.toInt()).also { + memory.read(ciovec.buf, it) + } FileSystemByteBuffer(bytes) } } diff --git a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/FcntlHandler.kt b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/FcntlHandler.kt index 673d6d7d..18672d5f 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/FcntlHandler.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/FcntlHandler.kt @@ -55,7 +55,9 @@ internal class FcntlHandler( varArgs: Int?, ): Int { val structStatPtr: WasmPtr = memory.readPtr(WasmPtr(checkNotNull(varArgs))) - val flockPacked = memory.readBytes(structStatPtr, StructFlock.STRUCT_FLOCK_SIZE) + val flockPacked = ByteArray(StructFlock.STRUCT_FLOCK_SIZE).also { + memory.read(structStatPtr, it) + } val flock = StructFlock.unpack(flockPacked) logger.v { "F_SETLK($fd, $flock)" } diff --git a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/GetentropyFunctionHandle.kt b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/GetentropyFunctionHandle.kt index 8cb08294..0eddfb9d 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/GetentropyFunctionHandle.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/GetentropyFunctionHandle.kt @@ -10,7 +10,6 @@ import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr import ru.pixnews.wasm.sqlite.open.helper.host.base.function.HostFunctionHandle import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory -import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.write import ru.pixnews.wasm.sqlite.open.helper.host.emscripten.EmscriptenHostFunction public class GetentropyFunctionHandle( diff --git a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/LocaltimeJsFunctionHandle.kt b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/LocaltimeJsFunctionHandle.kt index 9492a53f..b103d880 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/LocaltimeJsFunctionHandle.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/LocaltimeJsFunctionHandle.kt @@ -10,7 +10,6 @@ import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr import ru.pixnews.wasm.sqlite.open.helper.host.base.function.HostFunctionHandle import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory -import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.write import ru.pixnews.wasm.sqlite.open.helper.host.emscripten.EmscriptenHostFunction import ru.pixnews.wasm.sqlite.open.helper.host.include.StructTm import ru.pixnews.wasm.sqlite.open.helper.host.include.pack diff --git a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallFstat64FunctionHandle.kt b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallFstat64FunctionHandle.kt index ea68257e..1a440e04 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallFstat64FunctionHandle.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallFstat64FunctionHandle.kt @@ -10,7 +10,6 @@ import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr import ru.pixnews.wasm.sqlite.open.helper.host.base.function.HostFunctionHandle import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory -import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.write import ru.pixnews.wasm.sqlite.open.helper.host.emscripten.EmscriptenHostFunction import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.SysException import ru.pixnews.wasm.sqlite.open.helper.host.include.sys.StructStat diff --git a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallGetcwdFunctionHandle.kt b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallGetcwdFunctionHandle.kt index 7a2cc404..38eeea8f 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallGetcwdFunctionHandle.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallGetcwdFunctionHandle.kt @@ -10,7 +10,6 @@ import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr import ru.pixnews.wasm.sqlite.open.helper.host.base.function.HostFunctionHandle import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory -import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.write import ru.pixnews.wasm.sqlite.open.helper.host.emscripten.EmscriptenHostFunction import ru.pixnews.wasm.sqlite.open.helper.host.ext.encodeToNullTerminatedByteArray import ru.pixnews.wasm.sqlite.open.helper.host.wasi.preview1.type.Errno diff --git a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallStatLstat64FunctionHandle.kt b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallStatLstat64FunctionHandle.kt index 4ce0c019..4e4d4fa5 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallStatLstat64FunctionHandle.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/SyscallStatLstat64FunctionHandle.kt @@ -12,7 +12,6 @@ import ru.pixnews.wasm.sqlite.open.helper.host.base.function.HostFunction import ru.pixnews.wasm.sqlite.open.helper.host.base.function.HostFunctionHandle import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.readNullTerminatedString -import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.write import ru.pixnews.wasm.sqlite.open.helper.host.emscripten.EmscriptenHostFunction import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.SysException import ru.pixnews.wasm.sqlite.open.helper.host.include.sys.StructStat diff --git a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/TzsetJsFunctionHandle.kt b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/TzsetJsFunctionHandle.kt index 48ce5825..bda089a4 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/TzsetJsFunctionHandle.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/emscripten/function/TzsetJsFunctionHandle.kt @@ -10,7 +10,6 @@ import ru.pixnews.wasm.sqlite.open.helper.host.EmbedderHost import ru.pixnews.wasm.sqlite.open.helper.host.base.WasmPtr import ru.pixnews.wasm.sqlite.open.helper.host.base.function.HostFunctionHandle import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.Memory -import ru.pixnews.wasm.sqlite.open.helper.host.base.memory.write import ru.pixnews.wasm.sqlite.open.helper.host.emscripten.EmscriptenHostFunction import ru.pixnews.wasm.sqlite.open.helper.host.ext.encodeToNullTerminatedByteArray diff --git a/wasi-emscripten-host/src/commonMain/kotlin/ext/NullTerminatedStringExt.kt b/wasi-emscripten-host/src/commonMain/kotlin/ext/NullTerminatedStringExt.kt index d5747bbd..97ec424f 100644 --- a/wasi-emscripten-host/src/commonMain/kotlin/ext/NullTerminatedStringExt.kt +++ b/wasi-emscripten-host/src/commonMain/kotlin/ext/NullTerminatedStringExt.kt @@ -10,12 +10,12 @@ import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi @InternalWasmSqliteHelperApi public fun String.encodeToNullTerminatedByteArray( - maxDstSize: Int = Int.MAX_VALUE, + truncateAtSize: Int = Int.MAX_VALUE, ): ByteArray { - require(maxDstSize > 0) + require(truncateAtSize > 0) val raw = this.encodeToByteArray() - val rawSize = raw.size.coerceAtMost(maxDstSize - 1) + val rawSize = raw.size.coerceAtMost(truncateAtSize - 1) val os = ByteArray(rawSize + 1) { pos -> if (pos < rawSize) { raw[pos] diff --git a/wasi-emscripten-host/src/commonTest/kotlin/test/assertions/MemoryAssertions.kt b/wasi-emscripten-host/src/commonTest/kotlin/test/assertions/MemoryAssertions.kt index a0000ae7..e24afc30 100644 --- a/wasi-emscripten-host/src/commonTest/kotlin/test/assertions/MemoryAssertions.kt +++ b/wasi-emscripten-host/src/commonTest/kotlin/test/assertions/MemoryAssertions.kt @@ -15,8 +15,8 @@ import ru.pixnews.wasm.sqlite.open.helper.host.test.fixtures.TestMemory fun Assert.bytesAt( address: WasmPtr, size: Int, -) = transform(appendName("TestMemory{$address}", separator = ".")) { - it.readBytes(address, size) +) = transform(appendName("TestMemory{$address}", separator = ".")) { testMemory -> + ByteArray(size).also { testMemory.read(address, it) } } fun Assert.hasBytesAt( diff --git a/wasi-emscripten-host/src/commonTest/kotlin/test/fixtures/TestMemory.kt b/wasi-emscripten-host/src/commonTest/kotlin/test/fixtures/TestMemory.kt index 167b0a56..724d88ff 100644 --- a/wasi-emscripten-host/src/commonTest/kotlin/test/fixtures/TestMemory.kt +++ b/wasi-emscripten-host/src/commonTest/kotlin/test/fixtures/TestMemory.kt @@ -45,11 +45,16 @@ open class TestMemory( (bytes[addr.addr + 6].toLong() and 0xffL shl 48) or (bytes[addr.addr + 7].toLong() and 0xffL shl 56) - override fun readBytes(addr: WasmPtr<*>, length: Int): ByteArray = ByteArray(length) { - bytes[addr.addr + it] + override fun read(addr: WasmPtr<*>, destination: ByteArray, destinationOffset: Int, readBytes: Int) { + bytes.copyInto( + destination = destination, + destinationOffset = destinationOffset, + startIndex = addr.addr, + endIndex = addr.addr + readBytes, + ) } - override fun writeByte(addr: WasmPtr<*>, data: Byte) { + override fun writeI8(addr: WasmPtr<*>, data: Byte) { bytes[addr.addr] = data } @@ -71,9 +76,9 @@ open class TestMemory( bytes[addr.addr + 7] = (data ushr 56 and 0xff).toByte() } - override fun write(addr: WasmPtr<*>, data: ByteArray, offset: Int, size: Int) { - for (i in 0 until size) { - bytes[addr.addr + i] = data[offset + i] + override fun write(addr: WasmPtr<*>, source: ByteArray, sourceOffset: Int, writeBytes: Int) { + for (i in 0 until writeBytes) { + bytes[addr.addr + i] = source[sourceOffset + i] } }