3
3
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4
4
*/
5
5
6
- package org.jetbrains.kotlin.generators.wasm.js
6
+ package org.jetbrains.kotlin.generators.helpers
7
7
8
8
import org.jetbrains.kotlin.tools.dukat.getHeader
9
9
import java.io.File
@@ -15,25 +15,100 @@ fun main() {
15
15
}
16
16
17
17
fun generatePublicStdlibFunctions () {
18
- FileWriter (File (" ../src/wasmJsMain/kotlin/arrayCopy.kt" )).use { writer: FileWriter ->
18
+ val conversions = listOf (
19
+ Conversion (" Byte" , " Int8" ),
20
+ Conversion (" UByte" , " Uint8" , isUnsigned = true ),
21
+ Conversion (" Short" , " Int16" ),
22
+ Conversion (" UShort" , " Uint16" , isUnsigned = true ),
23
+ Conversion (" Int" , " Int32" ),
24
+ Conversion (" UInt" , " Uint32" , isUnsigned = true ),
25
+ Conversion (" Float" , " Float32" ),
26
+ Conversion (" Double" , " Float64" )
27
+ )
28
+
29
+ FileWriter (File (" ../src/browserMain/kotlin/arrayCopy.kt" )).use { writer: FileWriter ->
19
30
with (writer) {
20
31
appendLine(getHeader(seeDetailsAt = " generator/src/main/kotlin/helpers/generate.kt" ))
32
+ appendLine(" package org.khronos.webgl" )
21
33
34
+ conversions.forEach { (ktType, jsType, isUnsigned) ->
35
+ appendExpectConversionsForType(ktType, jsType, isUnsigned)
36
+ }
37
+ }
38
+ }
39
+ FileWriter (File (" ../src/jsMain/kotlin/arrayCopy.js.kt" )).use { writer: FileWriter ->
40
+ with (writer) {
41
+ appendLine(getHeader(seeDetailsAt = " generator/src/main/kotlin/helpers/generate.kt" ))
42
+
43
+ appendLine(" package org.khronos.webgl" )
44
+
45
+ conversions.forEach { (ktType, jsType, isUnsigned) ->
46
+ appendJsConversionsForType(ktType, jsType, isUnsigned)
47
+ }
48
+ }
49
+ }
50
+ FileWriter (File (" ../src/wasmJsMain/kotlin/arrayCopy.wasm.kt" )).use { writer: FileWriter ->
51
+ with (writer) {
52
+ appendLine(getHeader(seeDetailsAt = " generator/src/main/kotlin/helpers/generate.kt" ))
22
53
appendLine(" package org.khronos.webgl" )
23
54
24
- appendConversionsForType(" Byte" , " Int8" )
25
- appendConversionsForType(" UByte" , " Uint8" , isUnsigned = true )
26
- appendConversionsForType(" Short" , " Int16" )
27
- appendConversionsForType(" UShort" , " Uint16" , isUnsigned = true )
28
- appendConversionsForType(" Int" , " Int32" )
29
- appendConversionsForType(" UInt" , " Uint32" , isUnsigned = true )
30
- appendConversionsForType(" Float" , " Float32" )
31
- appendConversionsForType(" Double" , " Float64" )
55
+ conversions.forEach { (ktType, jsType, isUnsigned) ->
56
+ appendWasmConversionsForType(ktType, jsType, isUnsigned)
57
+ }
32
58
}
33
59
}
34
60
}
35
61
36
- private fun FileWriter.appendConversionsForType (
62
+
63
+ private fun FileWriter.appendExpectConversionsForType (
64
+ ktType : String ,
65
+ jsType : String ,
66
+ isUnsigned : Boolean = false,
67
+ ) {
68
+ val kotlinArrayType = " ${ktType} Array"
69
+ val jsArrayType = " ${jsType} Array"
70
+
71
+ appendLine()
72
+ appendLine(" /** Returns a new [$kotlinArrayType ] containing all the elements of this [$jsArrayType ]. */" )
73
+ if (isUnsigned) {
74
+ appendLine(" @ExperimentalUnsignedTypes" )
75
+ }
76
+ appendLine(" public expect fun $jsArrayType .to$kotlinArrayType (): $kotlinArrayType " )
77
+ appendLine()
78
+ appendLine(" /** Returns a new [$jsArrayType ] containing all the elements of this [$kotlinArrayType ]. */" )
79
+ if (isUnsigned) {
80
+ appendLine(" @ExperimentalUnsignedTypes" )
81
+ }
82
+ appendLine(" public expect fun $kotlinArrayType .to$jsArrayType (): $jsArrayType " )
83
+ }
84
+
85
+
86
+ private fun FileWriter.appendJsConversionsForType (
87
+ ktType : String ,
88
+ jsType : String ,
89
+ isUnsigned : Boolean = false,
90
+ ) {
91
+ val kotlinArrayType = " ${ktType} Array"
92
+ val jsArrayType = " ${jsType} Array"
93
+
94
+ appendLine()
95
+ appendLine(" /** Returns a new [$kotlinArrayType ] containing all the elements of this [$jsArrayType ]. */" )
96
+ if (isUnsigned) {
97
+ appendLine(" @ExperimentalUnsignedTypes" )
98
+ }
99
+ appendLine(" public actual inline fun $jsArrayType .to$kotlinArrayType (): $kotlinArrayType =" )
100
+ appendLine(" unsafeCast<$kotlinArrayType >()" )
101
+
102
+ appendLine()
103
+ appendLine(" /** Returns a new [$jsArrayType ] containing all the elements of this [$kotlinArrayType ]. */" )
104
+ if (isUnsigned) {
105
+ appendLine(" @ExperimentalUnsignedTypes" )
106
+ }
107
+ appendLine(" public actual inline fun $kotlinArrayType .to$jsArrayType (): $jsArrayType =" )
108
+ appendLine(" unsafeCast<$jsArrayType >()" )
109
+ }
110
+
111
+ private fun FileWriter.appendWasmConversionsForType (
37
112
ktType : String ,
38
113
jsType : String ,
39
114
isUnsigned : Boolean = false,
@@ -46,7 +121,7 @@ private fun FileWriter.appendConversionsForType(
46
121
if (isUnsigned) {
47
122
appendLine(" @ExperimentalUnsignedTypes" )
48
123
}
49
- appendLine(" public fun $jsArrayType .to$kotlinArrayType (): $kotlinArrayType =" )
124
+ appendLine(" public actual fun $jsArrayType .to$kotlinArrayType (): $kotlinArrayType =" )
50
125
if (isUnsigned) {
51
126
appendLine(" $kotlinArrayType (this.length) { this[it].to$ktType () }" )
52
127
} else {
@@ -58,7 +133,7 @@ private fun FileWriter.appendConversionsForType(
58
133
if (isUnsigned) {
59
134
appendLine(" @ExperimentalUnsignedTypes" )
60
135
}
61
- appendLine(" public fun $kotlinArrayType .to$jsArrayType (): $jsArrayType {" )
136
+ appendLine(" public actual fun $kotlinArrayType .to$jsArrayType (): $jsArrayType {" )
62
137
appendLine(" val result = $jsArrayType (this.size)" )
63
138
appendLine(" for (index in this.indices) {" )
64
139
if (isUnsigned) {
@@ -73,7 +148,7 @@ private fun FileWriter.appendConversionsForType(
73
148
}
74
149
75
150
fun generateTests () {
76
- FileWriter (File (" ../src/wasmJsTest /kotlin/arrayCopyTest.kt" )).use { writer: FileWriter ->
151
+ FileWriter (File (" ../src/browserTest /kotlin/arrayCopyTest.kt" )).use { writer: FileWriter ->
77
152
with (writer) {
78
153
appendLine(getHeader(seeDetailsAt = " generator/src/main/kotlin/helpers/generate.kt" ))
79
154
@@ -146,4 +221,7 @@ private fun FileWriter.appendTestFunction(
146
221
appendLine(" testJsRoundTrip($ktArrayOf (0.to$ktType (), (-42).to$ktType (), $ktType .MIN_VALUE, $ktType .MAX_VALUE))" )
147
222
appendLine(" testJsRoundTrip($kotlinArrayType (1000) { it.to$ktType () })" )
148
223
appendLine(" }" )
149
- }
224
+ }
225
+
226
+ data class Conversion (val jsType : String , val ktType : String , val isUnsigned : Boolean = false )
227
+ data class InteropCorrespondence (val interopType : String , val wasmType : String , val jsType : String )
0 commit comments