|
| 1 | +/* |
| 2 | + * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. |
| 3 | + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
| 4 | + */ |
| 5 | +@file:OptIn(UnsafeWasmMemoryApi::class) |
| 6 | + |
| 7 | +package kotlinx.io |
| 8 | + |
| 9 | +import kotlin.wasm.unsafe.MemoryAllocator |
| 10 | +import kotlin.wasm.unsafe.Pointer |
| 11 | +import kotlin.wasm.unsafe.UnsafeWasmMemoryApi |
| 12 | + |
| 13 | +internal fun Pointer.loadInt(offset: Int): Int = (this + offset).loadInt() |
| 14 | +internal fun Pointer.loadLong(offset: Int): Long = (this + offset).loadLong() |
| 15 | +internal fun Pointer.loadShort(offset: Int): Short = (this + offset).loadShort() |
| 16 | +internal fun Pointer.loadByte(offset: Int): Byte = (this + offset).loadByte() |
| 17 | + |
| 18 | +internal fun Pointer.loadBytes(length: Int): ByteArray { |
| 19 | + val buffer = ByteArray(length) |
| 20 | + for (offset in 0 until length) { |
| 21 | + buffer[offset] = this.loadByte(offset) |
| 22 | + } |
| 23 | + return buffer |
| 24 | +} |
| 25 | + |
| 26 | +internal fun Pointer.storeInt(offset: Int, value: Int): Unit = (this + offset).storeInt(value) |
| 27 | +internal fun Pointer.storeLong(offset: Int, value: Long): Unit = (this + offset).storeLong(value) |
| 28 | +internal fun Pointer.storeShort(offset: Int, value: Short): Unit = (this + offset).storeShort(value) |
| 29 | +internal fun Pointer.storeByte(offset: Int, value: Byte): Unit = (this + offset).storeByte(value) |
| 30 | + |
| 31 | +internal fun Pointer.storeBytes(bytes: ByteArray) { |
| 32 | + for (offset in bytes.indices) { |
| 33 | + this.storeByte(offset, bytes[offset]) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +@OptIn(UnsafeWasmMemoryApi::class) |
| 38 | +internal fun Buffer.readToLinearMemory(pointer: Pointer, bytes: Int) { |
| 39 | + checkBounds(size, 0L, bytes.toLong()) |
| 40 | + var current: Segment? = head |
| 41 | + var remaining = bytes |
| 42 | + var currentPtr = pointer |
| 43 | + do { |
| 44 | + current!! |
| 45 | + val data = current.data |
| 46 | + val pos = current.pos |
| 47 | + val limit = current.limit |
| 48 | + val read = minOf(remaining, limit - pos) |
| 49 | + for (offset in 0 until read) { |
| 50 | + currentPtr.storeByte(offset, data[pos + offset]) |
| 51 | + } |
| 52 | + currentPtr += read |
| 53 | + remaining -= read |
| 54 | + current = current.next |
| 55 | + } while (current != head && remaining > 0) |
| 56 | + check(remaining == 0) |
| 57 | + skip(bytes.toLong()) |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +internal fun Buffer.writeFromLinearMemory(pointer: Pointer, bytes: Int) { |
| 62 | + var remaining = bytes |
| 63 | + var currentPtr = pointer |
| 64 | + while (remaining > 0) { |
| 65 | + val segment = writableSegment(1) |
| 66 | + val limit = segment.limit |
| 67 | + val data = segment.data |
| 68 | + val toWrite = minOf(data.size - limit, remaining) |
| 69 | + |
| 70 | + for (offset in 0 until toWrite) { |
| 71 | + data[limit + offset] = currentPtr.loadByte(offset) |
| 72 | + } |
| 73 | + |
| 74 | + currentPtr += toWrite |
| 75 | + remaining -= toWrite |
| 76 | + segment.limit += toWrite |
| 77 | + size += toWrite |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Encoding [value] into a NULL-terminated byte sequence using UTF-8 encoding |
| 83 | + * and writes it to a memory region allocated to fit the sequence. |
| 84 | + * Return a pointer to the beginning of the written byte sequence and its length. |
| 85 | + */ |
| 86 | +@OptIn(UnsafeWasmMemoryApi::class) |
| 87 | +internal fun MemoryAllocator.storeString(value: String): Pair<Pointer, Int> { |
| 88 | + val bytes = value.encodeToByteArray() |
| 89 | + val ptr = allocate(bytes.size + 1) |
| 90 | + ptr.storeBytes(bytes) |
| 91 | + ptr.storeByte(bytes.size, 0) |
| 92 | + return ptr to (bytes.size + 1) |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Encodes [value] into a NULL-terminated byte sequence using UTF-8 encoding, |
| 97 | + * stores it in memory starting at the position this pointer points to, |
| 98 | + * and returns the length of the stored bytes sequence. |
| 99 | + */ |
| 100 | +internal fun Pointer.allocateString(value: String): Int { |
| 101 | + val bytes = value.encodeToByteArray() |
| 102 | + storeBytes(bytes) |
| 103 | + storeByte(bytes.size, 0) |
| 104 | + return bytes.size + 1 |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Allocates memory to hold a single integer value. |
| 109 | + */ |
| 110 | +@UnsafeWasmMemoryApi |
| 111 | +internal fun MemoryAllocator.allocateInt(): Pointer = allocate(Int.SIZE_BYTES) |
0 commit comments