@@ -27,7 +27,7 @@ private fun addressToString(address: Int): String =
2727class ConstantDataCharField (val value : WasmSymbol <Char >) : ConstantDataElement() {
2828 constructor (value: Char ) : this (WasmSymbol (value))
2929
30- override fun toBytes (): ByteArray = ByteArray (2 ).apply { value.owner.toLittleEndianBytes(this , 0 ) }
30+ override fun toBytes (): ByteArray = ByteArray (2 ).apply { value.owner.toLittleEndianBytes(this , false , 0 ) }
3131
3232 override fun dump (indent : String , startAddress : Int ): String {
3333 return " ${addressToString(startAddress)} : $indent i32 : ${value.owner} ;;\n "
@@ -85,9 +85,12 @@ class ConstantDataIntArray(val value: List<WasmSymbol<Int>>) : ConstantDataEleme
8585class ConstantDataCharArray (val value : List <WasmSymbol <Char >>) : ConstantDataElement() {
8686 constructor (value: CharArray ) : this (value.map { WasmSymbol (it) })
8787
88+ private val isLatin: Boolean
89+ get() = value.all { it.owner.code in 0 .. 255 }
90+
8891 override fun toBytes (): ByteArray {
89- return ByteArray (value.size * 2 ).apply {
90- value.forEachIndexed { index, symbol -> symbol.owner.toLittleEndianBytes(this , index * 2 ) }
92+ return ByteArray (sizeInBytes ).apply {
93+ value.forEachIndexed { index, symbol -> symbol.owner.toLittleEndianBytes(this , isLatin, index * 2 ) }
9194 }
9295 }
9396
@@ -96,7 +99,8 @@ class ConstantDataCharArray(val value: List<WasmSymbol<Char>>) : ConstantDataEle
9699 return " ${addressToString(startAddress)} : $indent i16[] : ${value.map { it.owner }.toCharArray().contentToString()} ;;\n "
97100 }
98101
99- override val sizeInBytes: Int = value.size * CHAR_SIZE_BYTES
102+ override val sizeInBytes: Int = value.size *
103+ if (isLatin) BYTE_SIZE_BYTES else CHAR_SIZE_BYTES
100104}
101105
102106class ConstantDataStruct (val elements : List <ConstantDataElement >) : ConstantDataElement() {
@@ -138,7 +142,9 @@ fun Int.toLittleEndianBytes(to: ByteArray, offset: Int) {
138142 to[offset + 3 ] = (this ushr 24 ).toByte()
139143}
140144
141- fun Char.toLittleEndianBytes (to : ByteArray , offset : Int ) {
145+ fun Char.toLittleEndianBytes (to : ByteArray , isLatin : Boolean , offset : Int ) {
142146 to[offset] = (this .code and 0xFF ).toByte()
143- to[offset + 1 ] = (this .code ushr Byte .SIZE_BITS ).toByte()
147+ if (! isLatin) {
148+ to[offset + 1 ] = (this .code ushr Byte .SIZE_BITS ).toByte()
149+ }
144150}
0 commit comments