Skip to content

Commit

Permalink
fix dotlottie decompression on wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhirkevich committed Jul 31, 2024
1 parent f76c62c commit 3b55511
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.alexzhirkevich.compottie

import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Int8Array
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
import org.khronos.webgl.set
import kotlin.wasm.unsafe.UnsafeWasmMemoryApi
Expand Down Expand Up @@ -32,11 +33,11 @@ internal fun createSourceUnstable(data : JsReference<ByteArray>) : JsAny = js(""
""")

internal fun ArrayBuffer.toByteArray(): ByteArray {
return Int8Array(this).toByteArray()
return Uint8Array(this).toByteArray()
}

@OptIn(ExperimentalCompottieApi::class)
internal fun Int8Array.toByteArray(): ByteArray {
internal fun Uint8Array.toByteArray(): ByteArray {

if (Compottie.useStableWasmMemoryManagement) {
return ByteArray(byteLength) {
Expand Down Expand Up @@ -81,14 +82,14 @@ private fun byteArrayToInt8ArrayImpl(a: JsReference<ByteArray>): Int8Array = js(

@JsFun(
""" (src, size, dstAddr) => {
const mem8 = new Int8Array(wasmExports.memory.buffer, dstAddr, size);
const mem8 = new Uint8Array(wasmExports.memory.buffer, dstAddr, size);
mem8.set(src);
}
"""
)
private external fun jsExportInt8ArrayToWasm(src: Int8Array, size: Int, dstAddr: Int)
private external fun jsExportInt8ArrayToWasm(src: Uint8Array, size: Int, dstAddr: Int)

private fun jsInt8ArrayToKotlinByteArray(x: Int8Array): ByteArray {
private fun jsInt8ArrayToKotlinByteArray(x: Uint8Array): ByteArray {
val size = x.length

@OptIn(UnsafeWasmMemoryApi::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.alexzhirkevich.compottie


import org.khronos.webgl.Int8Array
import org.khronos.webgl.Uint8Array

@OptIn(ExperimentalCompottieApi::class)
internal actual suspend fun decompress(array: ByteArray, decompressedSize : Int) : ByteArray {
Expand All @@ -28,10 +29,10 @@ internal actual suspend fun decompress(array: ByteArray, decompressedSize : Int)
break
}

val chunk = Int8Array(result.value.buffer).toByteArray()
val chunk = Uint8Array(result.value.buffer).toByteArray()

chunk.copyInto(decompressed, ind)
ind += array.size
ind += chunk.size
}

return decompressed
Expand Down

0 comments on commit 3b55511

Please sign in to comment.