[Wasm] Use Wasm GC arrays instead of JS arrays

JS arrays was a workaround for lack of arrays in Firefox Wasm GC prototype
Now with V8 as a test runner we can use proper arrays
This commit is contained in:
Svyatoslav Kuzmich
2020-12-07 15:42:55 +03:00
parent d15af70a3e
commit d4233f3f0e
15 changed files with 400 additions and 276 deletions
@@ -15,7 +15,7 @@ import kotlin.wasm.internal.*
* for more information on arrays.
*/
public class Array<T> constructor(size: Int) {
private var jsArray: WasmExternRef = JsArray_new(size)
private var storage: WasmAnyArray = WasmAnyArray(size)
/**
* Creates a new array with the specified [size], where each element is calculated by calling the specified
@@ -26,7 +26,7 @@ public class Array<T> constructor(size: Int) {
*/
@Suppress("TYPE_PARAMETER_AS_REIFIED")
public constructor(size: Int, init: (Int) -> T) : this(size) {
JsArray_fill_T(jsArray, size, init)
storage.fill(size, init)
}
/**
@@ -41,7 +41,7 @@ public class Array<T> constructor(size: Int) {
*/
@Suppress("UNCHECKED_CAST")
public operator fun get(index: Int): T =
WasmExternRefToAny(JsArray_get_WasmExternRef(jsArray, index)) as T
storage.get(index) as T
/**
* Sets the array element at the specified [index] to the specified [value]. This method can
@@ -54,14 +54,14 @@ public class Array<T> constructor(size: Int) {
* where the behavior is unspecified.
*/
public operator fun set(index: Int, value: T) {
JsArray_set_WasmExternRef(jsArray, index, value.toWasmExternRef())
storage.set(index, value)
}
/**
* Returns the number of elements in the array.
*/
public val size: Int
get() = JsArray_getSize(jsArray)
get() = storage.len()
/**
* Creates an iterator for iterating over the elements of the array.
+40 -79
View File
@@ -8,27 +8,21 @@ package kotlin
import kotlin.wasm.internal.*
public class ByteArray(size: Int) {
private var jsArray: WasmExternRef = JsArray_new(size)
init {
JsArray_fill_Byte(jsArray, size) { 0 }
}
private var storage = WasmByteArray(size)
public constructor(size: Int, init: (Int) -> Byte) : this(size) {
jsArray = JsArray_new(size)
JsArray_fill_Byte(jsArray, size, init)
storage.fill(size, init)
}
public operator fun get(index: Int): Byte =
JsArray_get_Byte(jsArray, index)
storage.get(index)
public operator fun set(index: Int, value: Byte) {
JsArray_set_Byte(jsArray, index, value)
storage.set(index, value)
}
public val size: Int
get() = JsArray_getSize(jsArray)
get() = storage.len()
public operator fun iterator(): ByteIterator = byteArrayIterator(this)
}
@@ -41,26 +35,21 @@ internal fun byteArrayIterator(array: ByteArray) = object : ByteIterator() {
public class CharArray(size: Int) {
private var jsArray: WasmExternRef = JsArray_new(size)
init {
JsArray_fill_Char(jsArray, size) { 0.toChar() }
}
private var storage = WasmShortArray(size)
public constructor(size: Int, init: (Int) -> Char) : this(size) {
jsArray = JsArray_new(size)
JsArray_fill_Char(jsArray, size, init)
storage.fill(size) { init(it).toShort() }
}
public operator fun get(index: Int): Char =
JsArray_get_Char(jsArray, index)
storage.get(index).toChar()
public operator fun set(index: Int, value: Char) {
JsArray_set_Char(jsArray, index, value)
storage.set(index, value.toShort())
}
public val size: Int
get() = JsArray_getSize(jsArray)
get() = storage.len()
public operator fun iterator(): CharIterator = charArrayIterator(this)
@@ -74,25 +63,21 @@ internal fun charArrayIterator(array: CharArray) = object : CharIterator() {
public class ShortArray(size: Int) {
private var jsArray: WasmExternRef = JsArray_new(size)
init {
JsArray_fill_Short(jsArray, size) { 0 }
}
private var storage = WasmShortArray(size)
public constructor(size: Int, init: (Int) -> Short) : this(size) {
JsArray_fill_Short(jsArray, size, init)
storage.fill(size, init)
}
public operator fun get(index: Int): Short =
JsArray_get_Short(jsArray, index)
storage.get(index)
public operator fun set(index: Int, value: Short) {
JsArray_set_Short(jsArray, index, value)
storage.set(index, value)
}
public val size: Int
get() = JsArray_getSize(jsArray)
get() = storage.len()
public operator fun iterator(): ShortIterator = shortArrayIterator(this)
@@ -106,25 +91,21 @@ internal fun shortArrayIterator(array: ShortArray) = object : ShortIterator() {
public class IntArray(size: Int) {
private var jsArray: WasmExternRef = JsArray_new(size)
init {
JsArray_fill_Int(jsArray, size) { 0 }
}
private var storage = WasmIntArray(size)
public constructor(size: Int, init: (Int) -> Int) : this(size) {
JsArray_fill_Int(jsArray, size, init)
storage.fill(size, init)
}
public operator fun get(index: Int): Int =
JsArray_get_Int(jsArray, index)
storage.get(index)
public operator fun set(index: Int, value: Int) {
JsArray_set_Int(jsArray, index, value)
storage.set(index, value)
}
public val size: Int
get() = JsArray_getSize(jsArray)
get() = storage.len()
public operator fun iterator(): IntIterator = intArrayIterator(this)
@@ -138,26 +119,21 @@ internal fun intArrayIterator(array: IntArray) = object : IntIterator() {
public class LongArray(size: Int) {
private var jsArray: WasmExternRef = JsArray_new(size)
init {
JsArray_fill_Long(jsArray, size) { 0L }
}
private var storage = WasmLongArray (size)
public constructor(size: Int, init: (Int) -> Long) : this(size) {
JsArray_fill_Long(jsArray, size, init)
storage.fill(size, init)
}
public operator fun get(index: Int): Long =
JsArray_get_Long(jsArray, index)
storage.get(index)
public operator fun set(index: Int, value: Long) {
JsArray_set_Long(jsArray, index, value)
storage.set(index, value)
}
public val size: Int
get() = JsArray_getSize(jsArray)
get() = storage.len()
public operator fun iterator(): LongIterator = longArrayIterator(this)
}
@@ -170,26 +146,21 @@ internal fun longArrayIterator(array: LongArray) = object : LongIterator() {
public class FloatArray(size: Int) {
private var jsArray: WasmExternRef = JsArray_new(size)
init {
JsArray_fill_Float(jsArray, size) { 0.0f }
}
private var storage = WasmFloatArray(size)
public constructor(size: Int, init: (Int) -> Float) : this(size) {
JsArray_fill_Float(jsArray, size, init)
storage.fill(size, init)
}
public operator fun get(index: Int): Float =
JsArray_get_Float(jsArray, index)
storage.get(index)
public operator fun set(index: Int, value: Float) {
JsArray_set_Float(jsArray, index, value)
storage.set(index, value)
}
public val size: Int
get() = JsArray_getSize(jsArray)
get() = storage.len()
public operator fun iterator(): FloatIterator = floatArrayIterator(this)
}
@@ -202,26 +173,21 @@ internal fun floatArrayIterator(array: FloatArray) = object : FloatIterator() {
public class DoubleArray(size: Int) {
private var jsArray: WasmExternRef = JsArray_new(size)
init {
JsArray_fill_Double(jsArray, size) { 0.0 }
}
private var storage = WasmDoubleArray(size)
public constructor(size: Int, init: (Int) -> Double) : this(size) {
JsArray_fill_Double(jsArray, size, init)
storage.fill(size, init)
}
public operator fun get(index: Int): Double =
JsArray_get_Double(jsArray, index)
storage.get(index)
public operator fun set(index: Int, value: Double) {
JsArray_set_Double(jsArray, index, value)
storage.set(index, value)
}
public val size: Int
get() = JsArray_getSize(jsArray)
get() = storage.len()
public operator fun iterator(): DoubleIterator = doubleArrayIterator(this)
}
@@ -234,26 +200,21 @@ internal fun doubleArrayIterator(array: DoubleArray) = object : DoubleIterator()
public class BooleanArray(size: Int) {
private var jsArray: WasmExternRef = JsArray_new(size)
init {
JsArray_fill_Boolean(jsArray, size) { false }
}
private var storage = WasmBooleanArray(size)
public constructor(size: Int, init: (Int) -> Boolean) : this(size) {
JsArray_fill_Boolean(jsArray, size, init)
storage.fill(size, init)
}
public operator fun get(index: Int): Boolean =
JsArray_get_Boolean(jsArray, index)
storage.get(index)
public operator fun set(index: Int, value: Boolean) {
JsArray_set_Boolean(jsArray, index, value)
storage.set(index, value)
}
public val size: Int
get() = JsArray_getSize(jsArray)
get() = storage.len()
public operator fun iterator(): BooleanIterator = booleanArrayIterator(this)
}
@@ -1,153 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.wasm.internal
@ExcludedFromCodegen
@WasmForeign
internal class WasmExternRef
@WasmImport("runtime", "JsArray_new")
internal fun JsArray_new(size: Int): WasmExternRef =
implementedAsIntrinsic
@WasmImport("runtime", "JsArray_get")
internal fun JsArray_get_Byte(array: WasmExternRef, index: Int): Byte = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_set")
internal fun JsArray_set_Byte(array: WasmExternRef, index: Int, value: Byte): Unit = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_get")
internal fun JsArray_get_Char(array: WasmExternRef, index: Int): Char = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_set")
internal fun JsArray_set_Char(array: WasmExternRef, index: Int, value: Char): Unit = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_get")
internal fun JsArray_get_Short(array: WasmExternRef, index: Int): Short = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_set")
internal fun JsArray_set_Short(array: WasmExternRef, index: Int, value: Short): Unit = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_get")
internal fun JsArray_get_Int(array: WasmExternRef, index: Int): Int = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_set")
internal fun JsArray_set_Int(array: WasmExternRef, index: Int, value: Int): Unit = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_get")
internal fun JsArray_get_Long(array: WasmExternRef, index: Int): Long = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_set")
internal fun JsArray_set_Long(array: WasmExternRef, index: Int, value: Long): Unit = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_get")
internal fun JsArray_get_Float(array: WasmExternRef, index: Int): Float = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_set")
internal fun JsArray_set_Float(array: WasmExternRef, index: Int, value: Float): Unit = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_get")
internal fun JsArray_get_Double(array: WasmExternRef, index: Int): Double = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_set")
internal fun JsArray_set_Double(array: WasmExternRef, index: Int, value: Double): Unit = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_get")
internal fun JsArray_get_Boolean(array: WasmExternRef, index: Int): Boolean = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_set")
internal fun JsArray_set_Boolean(array: WasmExternRef, index: Int, value: Boolean): Unit = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_get")
internal fun JsArray_get_WasmExternRef(array: WasmExternRef, index: Int): WasmExternRef = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_set")
internal fun JsArray_set_WasmExternRef(array: WasmExternRef, index: Int, value: WasmExternRef): Unit = implementedAsIntrinsic
@WasmImport("runtime", "JsArray_getSize")
internal fun JsArray_getSize(array: WasmExternRef): Int =
implementedAsIntrinsic
@JsFun("(x) => x")
internal fun Any?.toWasmExternRef(): WasmExternRef =
implementedAsIntrinsic
@JsFun("(x) => x")
internal fun WasmExternRefToAny(ref: WasmExternRef): Any? =
implementedAsIntrinsic
internal inline fun JsArray_fill_Byte(array: WasmExternRef, size: Int, init: (Int) -> Byte) {
var i = 0
while (i < size) {
JsArray_set_Byte(array, i, init(i))
i++
}
}
internal inline fun JsArray_fill_Char(array: WasmExternRef, size: Int, init: (Int) -> Char) {
var i = 0
while (i < size) {
JsArray_set_Char(array, i, init(i))
i++
}
}
internal inline fun JsArray_fill_Short(array: WasmExternRef, size: Int, init: (Int) -> Short) {
var i = 0
while (i < size) {
JsArray_set_Short(array, i, init(i))
i++
}
}
internal inline fun JsArray_fill_Int(array: WasmExternRef, size: Int, init: (Int) -> Int) {
var i = 0
while (i < size) {
JsArray_set_Int(array, i, init(i))
i++
}
}
internal inline fun JsArray_fill_Long(array: WasmExternRef, size: Int, init: (Int) -> Long) {
var i = 0
while (i < size) {
JsArray_set_Long(array, i, init(i))
i++
}
}
internal inline fun JsArray_fill_Float(array: WasmExternRef, size: Int, init: (Int) -> Float) {
var i = 0
while (i < size) {
JsArray_set_Float(array, i, init(i))
i++
}
}
internal inline fun JsArray_fill_Double(array: WasmExternRef, size: Int, init: (Int) -> Double) {
var i = 0
while (i < size) {
JsArray_set_Double(array, i, init(i))
i++
}
}
internal inline fun JsArray_fill_Boolean(array: WasmExternRef, size: Int, init: (Int) -> Boolean) {
var i = 0
while (i < size) {
JsArray_set_Boolean(array, i, init(i))
i++
}
}
internal inline fun <T> JsArray_fill_T(array: WasmExternRef, size: Int, init: (Int) -> T) {
var i = 0
while (i < size) {
JsArray_set_WasmExternRef(array, i, init(i).toWasmExternRef())
i++
}
}
@@ -6,6 +6,7 @@
package kotlin.wasm.internal
import kotlin.annotation.AnnotationTarget.*
import kotlin.reflect.KClass
// Exclude declaration or file from lowering and code generation
@Target(FILE, CLASS, FUNCTION, CONSTRUCTOR, PROPERTY)
@@ -20,6 +21,13 @@ internal annotation class WasmImport(val module: String, val name: String)
@Retention(AnnotationRetention.BINARY)
internal annotation class WasmForeign
@Target(CLASS)
@Retention(AnnotationRetention.BINARY)
internal annotation class WasmArrayOf(
val type: KClass<*>,
val isNullable: Boolean,
)
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
internal annotation class WasmReinterpret
@@ -0,0 +1,196 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.wasm.internal
//
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
@WasmArrayOf(Any::class, isNullable = true)
internal class WasmAnyArray(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
fun get(index: Int): Any? =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: Any?): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun WasmAnyArray.fill(size: Int, init: (Int) -> Any?) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}
@WasmArrayOf(Boolean::class, isNullable = false)
internal class WasmBooleanArray(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
fun get(index: Int): Boolean =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: Boolean): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun WasmBooleanArray.fill(size: Int, init: (Int) -> Boolean) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}
@WasmArrayOf(Byte::class, isNullable = false)
internal class WasmByteArray(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
fun get(index: Int): Byte =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: Byte): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun WasmByteArray.fill(size: Int, init: (Int) -> Byte) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}
@WasmArrayOf(Short::class, isNullable = false)
internal class WasmShortArray(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
fun get(index: Int): Short =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: Short): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun WasmShortArray.fill(size: Int, init: (Int) -> Short) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}
@WasmArrayOf(Int::class, isNullable = false)
internal class WasmIntArray(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
fun get(index: Int): Int =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: Int): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun WasmIntArray.fill(size: Int, init: (Int) -> Int) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}
@WasmArrayOf(Long::class, isNullable = false)
internal class WasmLongArray(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
fun get(index: Int): Long =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: Long): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun WasmLongArray.fill(size: Int, init: (Int) -> Long) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}
@WasmArrayOf(Float::class, isNullable = false)
internal class WasmFloatArray(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
fun get(index: Int): Float =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: Float): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun WasmFloatArray.fill(size: Int, init: (Int) -> Float) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}
@WasmArrayOf(Double::class, isNullable = false)
internal class WasmDoubleArray(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
fun get(index: Int): Double =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: Double): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun WasmDoubleArray.fill(size: Int, init: (Int) -> Double) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}