Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #115 from tanishiking/toCharArray
Browse files Browse the repository at this point in the history
Refactor: use `toCharArray` to encode string to WTF-16
  • Loading branch information
sjrd authored Apr 21, 2024
2 parents 7465e39 + 5ec2628 commit 6c65fc2
Showing 1 changed file with 3 additions and 29 deletions.
32 changes: 3 additions & 29 deletions wasm/src/main/scala/wasm4s/WasmContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import org.scalajs.linker.interface.ModuleInitializer
import org.scalajs.linker.interface.unstable.ModuleInitializerImpl
import org.scalajs.linker.standard.LinkedTopLevelExport

import java.nio.charset.StandardCharsets

abstract class ReadOnlyWasmContext {
import WasmContext._

Expand Down Expand Up @@ -188,7 +186,9 @@ abstract class TypeDefinableWasmContext extends ReadOnlyWasmContext { this: Wasm
data

case None =>
val bytes = encodeStringToWTF16LE(str)
val bytes = str.toCharArray.flatMap { char =>
Array((char & 0xFF).toByte, (char >> 8).toByte)
}
val offset = nextConstatnStringOffset
val data = StringData(nextConstantStringIndex, offset)
constantStringGlobals(str) = data
Expand Down Expand Up @@ -243,32 +243,6 @@ abstract class TypeDefinableWasmContext extends ReadOnlyWasmContext { this: Wasm
if (typeRef.dimensions > 1) IRTypes.ArrayType(typeRef.copy(dimensions = typeRef.dimensions - 1))
else inferTypeFromTypeRef(typeRef.base)
}

/** http://simonsapin.github.io/wtf-8/#encoding-ill-formed-utf-16
*/
private def encodeStringToWTF16LE(input: String): Array[Byte] = {
val result = scala.collection.mutable.ArrayBuffer[Int]()
var i = 0
while (i < input.length) {
val codePoint = input.codePointAt(i)
if (codePoint < 0x10000) {
// BMP code point
result += codePoint
i += Character.charCount(codePoint)
} else {
// Supplementary code point
val highSurrogate = ((codePoint - 0x10000) >> 10) + 0xD800
val lowSurrogate = ((codePoint - 0x10000) & 0x3FF) + 0xDC00
result += highSurrogate
result += lowSurrogate
i += 2
}
}

result
.flatMap(codeUnit => Seq((codeUnit & 0xFF).toByte, ((codeUnit >> 8) & 0xFF).toByte))
.toArray
}
}

class WasmContext(val module: WasmModule) extends TypeDefinableWasmContext {
Expand Down

0 comments on commit 6c65fc2

Please sign in to comment.