33 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
44 */
55
6- package org.jetbrains.kotlin.generators.wasm.js
6+ package org.jetbrains.kotlin.generators.helpers
77
88import org.jetbrains.kotlin.tools.dukat.getHeader
99import java.io.File
@@ -15,25 +15,100 @@ fun main() {
1515}
1616
1717fun 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/webMain/kotlin/arrayCopy.kt" )).use { writer: FileWriter ->
1930 with (writer) {
2031 appendLine(getHeader(seeDetailsAt = " generator/src/main/kotlin/helpers/generate.kt" ))
32+ appendLine(" package org.khronos.webgl" )
2133
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" ))
2253 appendLine(" package org.khronos.webgl" )
2354
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+ }
3258 }
3359 }
3460}
3561
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 (
37112 ktType : String ,
38113 jsType : String ,
39114 isUnsigned : Boolean = false,
@@ -46,7 +121,7 @@ private fun FileWriter.appendConversionsForType(
46121 if (isUnsigned) {
47122 appendLine(" @ExperimentalUnsignedTypes" )
48123 }
49- appendLine(" public fun $jsArrayType .to$kotlinArrayType (): $kotlinArrayType =" )
124+ appendLine(" public actual fun $jsArrayType .to$kotlinArrayType (): $kotlinArrayType =" )
50125 if (isUnsigned) {
51126 appendLine(" $kotlinArrayType (this.length) { this[it].to$ktType () }" )
52127 } else {
@@ -58,7 +133,7 @@ private fun FileWriter.appendConversionsForType(
58133 if (isUnsigned) {
59134 appendLine(" @ExperimentalUnsignedTypes" )
60135 }
61- appendLine(" public fun $kotlinArrayType .to$jsArrayType (): $jsArrayType {" )
136+ appendLine(" public actual fun $kotlinArrayType .to$jsArrayType (): $jsArrayType {" )
62137 appendLine(" val result = $jsArrayType (this.size)" )
63138 appendLine(" for (index in this.indices) {" )
64139 if (isUnsigned) {
@@ -73,7 +148,7 @@ private fun FileWriter.appendConversionsForType(
73148}
74149
75150fun generateTests () {
76- FileWriter (File (" ../src/wasmJsTest /kotlin/arrayCopyTest.kt" )).use { writer: FileWriter ->
151+ FileWriter (File (" ../src/webtest /kotlin/arrayCopyTest.kt" )).use { writer: FileWriter ->
77152 with (writer) {
78153 appendLine(getHeader(seeDetailsAt = " generator/src/main/kotlin/helpers/generate.kt" ))
79154
@@ -146,4 +221,7 @@ private fun FileWriter.appendTestFunction(
146221 appendLine(" testJsRoundTrip($ktArrayOf (0.to$ktType (), (-42).to$ktType (), $ktType .MIN_VALUE, $ktType .MAX_VALUE))" )
147222 appendLine(" testJsRoundTrip($kotlinArrayType (1000) { it.to$ktType () })" )
148223 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