Skip to content

Commit 9618132

Browse files
alex28shSpace Team
authored andcommitted
[Wasm] add boolean serialization for serialization of ConstantDataCharArray (KT-79357)
1 parent f969e61 commit 9618132

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/ConstantData.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ class ConstantDataIntArray(val value: List<WasmSymbol<Int>>) : ConstantDataEleme
7878
override val sizeInBytes: Int = value.size * INT_SIZE_BYTES
7979
}
8080

81-
class ConstantDataCharArray(val value: List<WasmSymbol<Char>>, val fitsLatin1: Int) : ConstantDataElement() {
82-
constructor(value: CharArray, fitsLatin1: Int) : this(value.map { WasmSymbol(it) }, fitsLatin1)
81+
class ConstantDataCharArray(val value: List<WasmSymbol<Char>>, val fitsLatin1: Boolean) : ConstantDataElement() {
82+
constructor(value: CharArray, fitsLatin1: Boolean) : this(value.map { WasmSymbol(it) }, fitsLatin1)
8383

8484
override fun toBytes(): ByteArray {
8585
return value
86-
.map { it.owner.toLittleEndianBytes(fitsLatin1 != 0) }
86+
.map { it.owner.toLittleEndianBytes(fitsLatin1) }
8787
.fold(byteArrayOf(), ByteArray::plus)
8888
}
8989

@@ -93,7 +93,7 @@ class ConstantDataCharArray(val value: List<WasmSymbol<Char>>, val fitsLatin1: I
9393
}
9494

9595
override val sizeInBytes: Int = value.size *
96-
if (fitsLatin1 != 0) BYTE_SIZE_BYTES else CHAR_SIZE_BYTES
96+
if (fitsLatin1) BYTE_SIZE_BYTES else CHAR_SIZE_BYTES
9797
}
9898

9999
class ConstantDataStruct(val elements: List<ConstantDataElement>) : ConstantDataElement() {

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/WasmCompiledModuleFragment.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,7 @@ class WasmCompiledModuleFragment(
561561
stringAddressAndId[string] = currentStringAddress to currentStringId
562562
addressesAndLengths.add(currentStringAddress.toLong() or (string.length.toLong() shl 32))
563563

564-
val fitsLatin1 = if (string.fitsLatin1) 1 else 0
565-
566-
val constData = ConstantDataCharArray(string.toCharArray(), fitsLatin1)
564+
val constData = ConstantDataCharArray(string.toCharArray(), string.fitsLatin1)
567565
stringDataSectionBytes += constData.toBytes().toList()
568566
stringDataSectionStart += constData.sizeInBytes
569567
} else {

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/serialization/WasmDeserializer.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class WasmDeserializer(inputStream: InputStream, private val skipLocalNames: Boo
156156
private fun deserializeStructFieldDeclaration(): WasmStructFieldDeclaration {
157157
val name = deserializeString()
158158
val type = deserializeType()
159-
val isMutable = b.readUByte().toBoolean()
159+
val isMutable = deserializeBoolean()
160160
return WasmStructFieldDeclaration(name, type, isMutable)
161161
}
162162

@@ -213,7 +213,7 @@ class WasmDeserializer(inputStream: InputStream, private val skipLocalNames: Boo
213213
name = deserializeString()
214214
}
215215
val type = deserializeType()
216-
val isParameter = b.readUByte().toBoolean()
216+
val isParameter = deserializeBoolean()
217217
return WasmLocal(id, name, type, isParameter)
218218
}
219219

@@ -489,7 +489,7 @@ class WasmDeserializer(inputStream: InputStream, private val skipLocalNames: Boo
489489

490490
private fun deserializeConstantDataCharArray(): ConstantDataCharArray {
491491
val value = deserializeList { deserializeSymbol { Char(deserializeInt()) } }
492-
val fitsLatin1 = deserializeInt()
492+
val fitsLatin1 = deserializeBoolean()
493493
return ConstantDataCharArray(value, fitsLatin1)
494494
}
495495

@@ -555,6 +555,8 @@ class WasmDeserializer(inputStream: InputStream, private val skipLocalNames: Boo
555555

556556
private fun deserializeInt() = b.readUInt32().toInt()
557557

558+
private fun deserializeBoolean() = b.readUByte().toBoolean()
559+
558560
private fun skipInt() {
559561
b.skip(4)
560562
}

compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/serialization/WasmSerializer.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class WasmSerializer(outputStream: OutputStream) {
223223
private fun serializeWasmStructFieldDeclaration(structFieldDecl: WasmStructFieldDeclaration) {
224224
serializeString(structFieldDecl.name)
225225
serializeWasmType(structFieldDecl.type)
226-
b.writeByte(structFieldDecl.isMutable.toByte())
226+
serializeBoolean(structFieldDecl.isMutable)
227227
}
228228

229229
private fun serializeWasmType(type: WasmType) =
@@ -267,7 +267,7 @@ class WasmSerializer(outputStream: OutputStream) {
267267
b.writeUInt32(local.id.toUInt())
268268
serializeString(local.name)
269269
serializeWasmType(local.type)
270-
b.writeByte(local.isParameter.toByte())
270+
serializeBoolean(local.isParameter)
271271
}
272272

273273
private fun serializeWasmInstr(instr: WasmInstr) {
@@ -515,7 +515,7 @@ class WasmSerializer(outputStream: OutputStream) {
515515

516516
private fun serializeConstantDataCharArray(constantDataCharArray: ConstantDataCharArray) {
517517
serializeList(constantDataCharArray.value) { serializeWasmSymbolReadOnly(it) { b.writeUInt32(it.code.toUInt()) } }
518-
serializeInt(constantDataCharArray.fitsLatin1)
518+
serializeBoolean(constantDataCharArray.fitsLatin1)
519519
}
520520

521521
private fun serializeConstantDataCharField(constantDataCharField: ConstantDataCharField) {
@@ -578,6 +578,10 @@ class WasmSerializer(outputStream: OutputStream) {
578578
b.writeUInt32(int.toUInt())
579579
}
580580

581+
private fun serializeBoolean(bool: Boolean) {
582+
b.writeByte(bool.toByte())
583+
}
584+
581585
private fun serializeLong(long: Long) {
582586
b.writeUInt64(long.toULong())
583587
}

0 commit comments

Comments
 (0)