@@ -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 , false , 0 ) }
30+ override fun toBytes (): ByteArray = value.owner.toLittleEndianBytes(false )
3131
3232 override fun dump (indent : String , startAddress : Int ): String {
3333 return " ${addressToString(startAddress)} : $indent i32 : ${value.owner} ;;\n "
@@ -39,7 +39,7 @@ class ConstantDataCharField(val value: WasmSymbol<Char>) : ConstantDataElement()
3939class ConstantDataIntField (val value : WasmSymbol <Int >) : ConstantDataElement() {
4040 constructor (value: Int ) : this (WasmSymbol (value))
4141
42- override fun toBytes (): ByteArray = ByteArray ( 4 ). apply { value.owner.toLittleEndianBytes(this , 0 ) }
42+ override fun toBytes (): ByteArray = value.owner.toLittleEndianBytes()
4343
4444 override fun dump (indent : String , startAddress : Int ): String {
4545 return " ${addressToString(startAddress)} : $indent i32 : ${value.owner} ;;\n "
@@ -67,11 +67,7 @@ class ConstantDataIntegerArray(val value: List<Long>, val integerSize: Int) : Co
6767
6868class ConstantDataIntArray (val value : List <WasmSymbol <Int >>) : ConstantDataElement() {
6969 override fun toBytes (): ByteArray {
70- return ByteArray (value.size * 4 ).apply {
71- for (index in value.indices) {
72- value[index].owner.toLittleEndianBytes(this , index * 4 )
73- }
74- }
70+ return value.fold(byteArrayOf()) { acc, el -> acc + el.owner.toLittleEndianBytes() }
7571 }
7672
7773 override fun dump (indent : String , startAddress : Int ): String {
@@ -82,16 +78,13 @@ class ConstantDataIntArray(val value: List<WasmSymbol<Int>>) : ConstantDataEleme
8278 override val sizeInBytes: Int = value.size * INT_SIZE_BYTES
8379}
8480
85- class ConstantDataCharArray (val value : List <WasmSymbol <Char >>) : ConstantDataElement() {
86- constructor (value: CharArray ) : this (value.map { WasmSymbol (it) })
87-
88- private val isLatin: Boolean
89- get() = value.all { it.owner.code in 0 .. 255 }
81+ class ConstantDataCharArray (val value : List <WasmSymbol <Char >>, val fitsOneByte : Int ) : ConstantDataElement() {
82+ constructor (value: CharArray , fitsOneByte: Int ) : this (value.map { WasmSymbol (it) }, fitsOneByte)
9083
9184 override fun toBytes (): ByteArray {
92- return ByteArray (sizeInBytes). apply {
93- value.forEachIndexed { index, symbol -> symbol .owner.toLittleEndianBytes(this , isLatin, index * 2 ) }
94- }
85+ return value
86+ .map { it .owner.toLittleEndianBytes(fitsOneByte != 0 ) }
87+ .fold(byteArrayOf(), ByteArray ::plus)
9588 }
9689
9790 override fun dump (indent : String , startAddress : Int ): String {
@@ -100,18 +93,12 @@ class ConstantDataCharArray(val value: List<WasmSymbol<Char>>) : ConstantDataEle
10093 }
10194
10295 override val sizeInBytes: Int = value.size *
103- if (isLatin ) BYTE_SIZE_BYTES else CHAR_SIZE_BYTES
96+ if (fitsOneByte != 0 ) BYTE_SIZE_BYTES else CHAR_SIZE_BYTES
10497}
10598
10699class ConstantDataStruct (val elements : List <ConstantDataElement >) : ConstantDataElement() {
107100 override fun toBytes (): ByteArray {
108- return buildList {
109- elements.forEach {
110- for (byte in it.toBytes()) {
111- add(byte)
112- }
113- }
114- }.toByteArray()
101+ return elements.fold(byteArrayOf()) { acc, el -> acc + el.toBytes() }
115102 }
116103
117104 override fun dump (indent : String , startAddress : Int ): String {
@@ -126,7 +113,7 @@ class ConstantDataStruct(val elements: List<ConstantDataElement>) : ConstantData
126113 return res
127114 }
128115
129- override val sizeInBytes: Int = elements.sumOf { it.sizeInBytes }
116+ override val sizeInBytes: Int = elements.map { it.sizeInBytes }.sum()
130117}
131118
132119fun Long.toLittleEndianBytesTo (to : ByteArray , offset : Int , size : Int ) {
@@ -135,16 +122,16 @@ fun Long.toLittleEndianBytesTo(to: ByteArray, offset: Int, size: Int) {
135122 }
136123}
137124
138- fun Int. toLittleEndianBytes ( to : ByteArray , offset : Int ) {
139- to[offset] = this .toByte()
140- to[offset + 1 ] = ( this ushr 8 ).toByte()
141- to[offset + 2 ] = (this ushr 16 ).toByte()
142- to[offset + 3 ] = ( this ushr 24 ).toByte()
125+
126+ fun Int. toLittleEndianBytes (): ByteArray {
127+ return ByteArray ( 4 ) {
128+ (this ushr (it * 8 ) ).toByte()
129+ }
143130}
144131
145- fun Char.toLittleEndianBytes (to : ByteArray , isLatin : Boolean , offset : Int ) {
146- to[offset] = (this .code and 0xFF ).toByte()
147- if (! isLatin) {
148- to[offset + 1 ] = (this .code ushr Byte .SIZE_BITS ).toByte()
132+ fun Char.toLittleEndianBytes (fitsOneByte : Boolean ): ByteArray {
133+ if (fitsOneByte) {
134+ return byteArrayOf((this .code and 0xFF ).toByte())
149135 }
136+ return byteArrayOf((this .code and 0xFF ).toByte(), (this .code ushr Byte .SIZE_BITS ).toByte())
150137}
0 commit comments