[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
@@ -72,7 +72,7 @@ fun main(args: Array<String>) {
targetDir.resolve("_${source.name.capitalize()}$platformSuffix.kt")
}
targetBaseDirs[KotlinTarget.WASM]?.let { generateWasmOps(it) }
targetBaseDirs[KotlinTarget.WASM]?.let { generateWasmBuiltIns(it) }
}
fun File.resolveExistingDir(subpath: String) = resolve(subpath).also { it.requireExistingDir() }
@@ -8,16 +8,27 @@ package generators
import org.jetbrains.kotlin.wasm.ir.WasmOp
import templates.COMMON_AUTOGENERATED_WARNING
import templates.COPYRIGHT_NOTICE
import templates.PrimitiveType
import java.io.File
import java.io.FileWriter
fun generateWasmBuiltIns(targetDir: File) {
generateWasmOps(targetDir)
generateWasmArrays(targetDir)
}
fun FileWriter.generateStandardWasmInternalHeader() {
appendLine(COPYRIGHT_NOTICE)
appendLine("package kotlin.wasm.internal")
appendLine()
appendLine(COMMON_AUTOGENERATED_WARNING)
appendLine()
}
fun generateWasmOps(targetDir: File) {
FileWriter(targetDir.resolve("_WasmOp.kt")).use { writer ->
writer.appendLine(COPYRIGHT_NOTICE)
writer.appendLine("package kotlin.wasm.internal")
writer.appendLine()
writer.appendLine(COMMON_AUTOGENERATED_WARNING)
writer.appendLine()
writer.generateStandardWasmInternalHeader()
writer.appendLine(
"""
@ExcludedFromCodegen
@@ -38,4 +49,46 @@ fun generateWasmOps(targetDir: File) {
""".trimIndent()
)
}
}
fun generateWasmArrays(targetDir: File) {
FileWriter(targetDir.resolve("_WasmArrays.kt")).use { writer ->
writer.generateStandardWasmInternalHeader()
writer.appendLine(wasmArrayForType("Any", true))
writer.appendLine(wasmArrayForType("Boolean", false))
PrimitiveType.numericPrimitives.sortedBy { it.capacity }.forEach { primitive ->
writer.appendLine(wasmArrayForType(primitive.name, false))
}
}
}
fun wasmArrayForType(klass: String, isNullable: Boolean): String {
val type = klass + if (isNullable) "?" else ""
val name = "Wasm${klass}Array"
return """
@WasmArrayOf($klass::class, isNullable = $isNullable)
internal class $name(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
fun get(index: Int): $type =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: $type): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun $name.fill(size: Int, init: (Int) -> $type) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}
""".trimIndent()
}