-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgenerate.kt
227 lines (194 loc) · 7.99 KB
/
generate.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright 2010-2024 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 org.jetbrains.kotlin.generators.helpers
import org.jetbrains.kotlin.tools.dukat.getHeader
import java.io.File
import java.io.FileWriter
fun main() {
generatePublicStdlibFunctions()
generateTests()
}
fun generatePublicStdlibFunctions() {
val conversions = listOf(
Conversion("Byte", "Int8"),
Conversion("UByte", "Uint8", isUnsigned = true),
Conversion("Short", "Int16"),
Conversion("UShort", "Uint16", isUnsigned = true),
Conversion("Int", "Int32"),
Conversion("UInt", "Uint32", isUnsigned = true),
Conversion("Float", "Float32"),
Conversion("Double", "Float64")
)
FileWriter(File("../src/browserMain/kotlin/arrayCopy.kt")).use { writer: FileWriter ->
with(writer) {
appendLine(getHeader(seeDetailsAt = "generator/src/main/kotlin/helpers/generate.kt"))
appendLine("package org.khronos.webgl")
conversions.forEach { (ktType, jsType, isUnsigned) ->
appendExpectConversionsForType(ktType, jsType, isUnsigned)
}
}
}
FileWriter(File("../src/jsMain/kotlin/arrayCopy.js.kt")).use { writer: FileWriter ->
with(writer) {
appendLine(getHeader(seeDetailsAt = "generator/src/main/kotlin/helpers/generate.kt"))
appendLine("package org.khronos.webgl")
conversions.forEach { (ktType, jsType, isUnsigned) ->
appendJsConversionsForType(ktType, jsType, isUnsigned)
}
}
}
FileWriter(File("../src/wasmJsMain/kotlin/arrayCopy.wasm.kt")).use { writer: FileWriter ->
with(writer) {
appendLine(getHeader(seeDetailsAt = "generator/src/main/kotlin/helpers/generate.kt"))
appendLine("package org.khronos.webgl")
conversions.forEach { (ktType, jsType, isUnsigned) ->
appendWasmConversionsForType(ktType, jsType, isUnsigned)
}
}
}
}
private fun FileWriter.appendExpectConversionsForType(
ktType: String,
jsType: String,
isUnsigned: Boolean = false,
) {
val kotlinArrayType = "${ktType}Array"
val jsArrayType = "${jsType}Array"
appendLine()
appendLine("/** Returns a new [$kotlinArrayType] containing all the elements of this [$jsArrayType]. */")
if (isUnsigned) {
appendLine("@ExperimentalUnsignedTypes")
}
appendLine("public expect fun $jsArrayType.to$kotlinArrayType(): $kotlinArrayType")
appendLine()
appendLine("/** Returns a new [$jsArrayType] containing all the elements of this [$kotlinArrayType]. */")
if (isUnsigned) {
appendLine("@ExperimentalUnsignedTypes")
}
appendLine("public expect fun $kotlinArrayType.to$jsArrayType(): $jsArrayType")
}
private fun FileWriter.appendJsConversionsForType(
ktType: String,
jsType: String,
isUnsigned: Boolean = false,
) {
val kotlinArrayType = "${ktType}Array"
val jsArrayType = "${jsType}Array"
appendLine()
appendLine("/** Returns a new [$kotlinArrayType] containing all the elements of this [$jsArrayType]. */")
if (isUnsigned) {
appendLine("@ExperimentalUnsignedTypes")
}
appendLine("public actual inline fun $jsArrayType.to$kotlinArrayType(): $kotlinArrayType =")
appendLine(" unsafeCast<$kotlinArrayType>()")
appendLine()
appendLine("/** Returns a new [$jsArrayType] containing all the elements of this [$kotlinArrayType]. */")
if (isUnsigned) {
appendLine("@ExperimentalUnsignedTypes")
}
appendLine("public actual inline fun $kotlinArrayType.to$jsArrayType(): $jsArrayType =")
appendLine(" unsafeCast<$jsArrayType>()")
}
private fun FileWriter.appendWasmConversionsForType(
ktType: String,
jsType: String,
isUnsigned: Boolean = false,
) {
val kotlinArrayType = "${ktType}Array"
val jsArrayType = "${jsType}Array"
appendLine()
appendLine("/** Returns a new [$kotlinArrayType] containing all the elements of this [$jsArrayType]. */")
if (isUnsigned) {
appendLine("@ExperimentalUnsignedTypes")
}
appendLine("public actual fun $jsArrayType.to$kotlinArrayType(): $kotlinArrayType =")
if (isUnsigned) {
appendLine(" $kotlinArrayType(this.length) { this[it].to$ktType() }")
} else {
appendLine(" $kotlinArrayType(this.length) { this[it] }")
}
appendLine()
appendLine("/** Returns a new [$jsArrayType] containing all the elements of this [$kotlinArrayType]. */")
if (isUnsigned) {
appendLine("@ExperimentalUnsignedTypes")
}
appendLine("public actual fun $kotlinArrayType.to$jsArrayType(): $jsArrayType {")
appendLine(" val result = $jsArrayType(this.size)")
appendLine(" for (index in this.indices) {")
if (isUnsigned) {
val signedType = ktType.drop(1)
appendLine(" result[index] = this[index].to$signedType()")
} else {
appendLine(" result[index] = this[index]")
}
appendLine(" }")
appendLine(" return result")
appendLine("}")
}
fun generateTests() {
FileWriter(File("../src/browserTest/kotlin/arrayCopyTest.kt")).use { writer: FileWriter ->
with(writer) {
appendLine(getHeader(seeDetailsAt = "generator/src/main/kotlin/helpers/generate.kt"))
appendLine("@file:OptIn(ExperimentalUnsignedTypes::class)")
appendLine("import org.khronos.webgl.*")
appendLine("import kotlin.test.*")
appendTestJsRoundTripHelper("Byte", "Int8")
appendTestJsRoundTripHelper("UByte", "Uint8", isUnsigned = true)
appendTestJsRoundTripHelper("Short", "Int16")
appendTestJsRoundTripHelper("UShort", "Uint16", isUnsigned = true)
appendTestJsRoundTripHelper("Int", "Int32")
appendTestJsRoundTripHelper("UInt", "Uint32", isUnsigned = true)
appendTestJsRoundTripHelper("Float", "Float32")
appendTestJsRoundTripHelper("Double", "Float64")
appendLine()
appendLine("class JsArrayConversionTest {")
appendTestFunction("Byte")
appendTestFunction("UByte")
appendTestFunction("Short")
appendTestFunction("UShort")
appendTestFunction("Int")
appendTestFunction("UInt")
appendTestFunction("Float")
appendTestFunction("Double")
appendLine("}")
}
}
}
private fun FileWriter.appendTestJsRoundTripHelper(
ktType: String,
jsType: String,
isUnsigned: Boolean = false,
) {
val kotlinArrayType = "${ktType}Array"
val jsArrayType = "${jsType}Array"
appendLine(
"""
private fun testJsRoundTrip(array: $kotlinArrayType) {
val jsArray = array.to$jsArrayType()
for (i in array.indices) {
assertEquals(array[i], jsArray[i]${if (isUnsigned) ".to$ktType()" else ""})
}
assertEquals(array.size, jsArray.length)
val roundTrippedArray = jsArray.to$kotlinArrayType()
assertContentEquals(array, roundTrippedArray)
}
""".trimIndent()
)
}
private fun FileWriter.appendTestFunction(
ktType: String,
) {
val kotlinArrayType = "${ktType}Array"
val ktArrayOf = "${ktType.lowercase()}ArrayOf"
appendLine()
appendLine(" @Test")
appendLine(" fun test$kotlinArrayType() {")
appendLine(" testJsRoundTrip($ktArrayOf())")
appendLine(" testJsRoundTrip($ktArrayOf(0.to$ktType(), (-42).to$ktType(), $ktType.MIN_VALUE, $ktType.MAX_VALUE))")
appendLine(" testJsRoundTrip($kotlinArrayType(1000) { it.to$ktType() })")
appendLine(" }")
}
data class Conversion(val jsType: String, val ktType: String, val isUnsigned: Boolean = false)
data class InteropCorrespondence(val interopType: String, val wasmType: String, val jsType: String)