[WASM] Add array copy intrinsic

This commit is contained in:
Igor Yakovlev
2022-06-29 18:51:31 +02:00
committed by teamcity
parent 8306b1bd71
commit 0ea7e8b70a
17 changed files with 218 additions and 203 deletions
@@ -15,7 +15,7 @@ import kotlin.wasm.internal.*
* for more information on arrays.
*/
public class Array<T> constructor(size: Int) {
private var storage: WasmAnyArray = WasmAnyArray(size)
internal val storage: WasmAnyArray = WasmAnyArray(size)
/**
* Creates a new array with the specified [size], where each element is calculated by calling the specified
@@ -8,7 +8,7 @@ package kotlin
import kotlin.wasm.internal.*
public class ByteArray(size: Int) {
private var storage = WasmByteArray(size)
internal val storage = WasmByteArray(size)
public constructor(size: Int, init: (Int) -> Byte) : this(size) {
storage.fill(size, init)
@@ -38,7 +38,7 @@ internal fun byteArrayIterator(array: ByteArray) = object : ByteIterator() {
public class CharArray(size: Int) {
private var storage = WasmCharArray(size)
internal val storage = WasmCharArray(size)
public constructor(size: Int, init: (Int) -> Char) : this(size) {
storage.fill(size) { init(it) }
@@ -69,7 +69,7 @@ internal fun charArrayIterator(array: CharArray) = object : CharIterator() {
public class ShortArray(size: Int) {
private var storage = WasmShortArray(size)
internal val storage = WasmShortArray(size)
public constructor(size: Int, init: (Int) -> Short) : this(size) {
storage.fill(size, init)
@@ -100,7 +100,7 @@ internal fun shortArrayIterator(array: ShortArray) = object : ShortIterator() {
public class IntArray(size: Int) {
private var storage = WasmIntArray(size)
internal val storage = WasmIntArray(size)
public constructor(size: Int, init: (Int) -> Int) : this(size) {
storage.fill(size, init)
@@ -131,7 +131,7 @@ internal fun intArrayIterator(array: IntArray) = object : IntIterator() {
public class LongArray(size: Int) {
private var storage = WasmLongArray (size)
internal val storage = WasmLongArray (size)
public constructor(size: Int, init: (Int) -> Long) : this(size) {
storage.fill(size, init)
@@ -161,7 +161,7 @@ internal fun longArrayIterator(array: LongArray) = object : LongIterator() {
public class FloatArray(size: Int) {
private var storage = WasmFloatArray(size)
internal val storage = WasmFloatArray(size)
public constructor(size: Int, init: (Int) -> Float) : this(size) {
storage.fill(size, init)
@@ -191,7 +191,7 @@ internal fun floatArrayIterator(array: FloatArray) = object : FloatIterator() {
public class DoubleArray(size: Int) {
private var storage = WasmDoubleArray(size)
internal val storage = WasmDoubleArray(size)
public constructor(size: Int, init: (Int) -> Double) : this(size) {
storage.fill(size, init)
@@ -221,7 +221,7 @@ internal fun doubleArrayIterator(array: DoubleArray) = object : DoubleIterator()
public class BooleanArray(size: Int) {
private var storage = WasmByteArray(size)
internal val storage = WasmByteArray(size)
public constructor(size: Int, init: (Int) -> Boolean) : this(size) {
storage.fill(size) { init(it).toInt().reinterpretAsByte() }
+29 -20
View File
@@ -22,12 +22,15 @@ public class String private constructor(internal val chars: WasmCharArray) : Com
* Returns a string obtained by concatenating this string with the string representation of the given [other] object.
*/
public operator fun plus(other: Any?): String {
val thisChars = chars
val otherChars = if (other is String) other.chars else other.toString().chars
val newCharsLen = chars.len() + otherChars.len()
val newChars = WasmCharArray(newCharsLen)
newChars.fill(newCharsLen) { i ->
if (i < chars.len()) chars.get(i) else otherChars.get(i - chars.len())
}
val thisLen = thisChars.len()
val otherLen = otherChars.len()
if (otherLen == 0) return String(thisChars)
val newChars = WasmCharArray(thisLen + otherLen)
thisChars.copyTo(newChars, 0, 0, thisLen)
otherChars.copyTo(newChars, 0, thisLen, otherLen)
return String(newChars)
}
@@ -47,42 +50,48 @@ public class String private constructor(internal val chars: WasmCharArray) : Com
public override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
val actualStartIndex = startIndex.coerceAtLeast(0)
val actualEndIndex = endIndex.coerceAtMost(chars.len())
val thisChars = chars
val actualEndIndex = endIndex.coerceAtMost(thisChars.len())
val newCharsLen = actualEndIndex - actualStartIndex
val newChars = WasmCharArray(newCharsLen)
newChars.fill(newCharsLen) { i ->
chars.get(actualStartIndex + i)
}
thisChars.copyTo(newChars, actualStartIndex, 0, newCharsLen)
return String(newChars)
}
public override fun compareTo(other: String): Int {
val len = min(this.length, other.length)
val thisChars = this.chars
val otherChars = other.chars
val thisLength = thisChars.len()
val otherLength = otherChars.len()
val len = min(thisLength, otherLength)
for (i in 0 until len) {
val l = this[i]
val r = other[i]
val l = thisChars.get(i)
val r = otherChars.get(i)
if (l != r)
return l - r
}
return this.length - other.length
return thisLength - otherLength
}
public override fun equals(other: Any?): Boolean {
if (other is String)
return this.compareTo(other) == 0
return false
}
public override fun equals(other: Any?): Boolean =
other != null &&
other is String &&
(this.length == other.length) &&
this.compareTo(other) == 0
public override fun toString(): String = this
public override fun hashCode(): Int {
if (_hashCode != 0 || this.isEmpty())
val thisLength = length
if (_hashCode != 0 || thisLength == null)
return _hashCode
var hash = 0
var i = 0
while (i < chars.len()) {
while (i < thisLength) {
hash = 31 * hash + chars.get(i).toInt()
i++
}