-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38f7628
commit 5d7139d
Showing
27 changed files
with
443 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
compottie-dot/src/wasmJsMain/kotlin/io/github/alexzhirkevich/compottie/Conversion.wasmJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package io.github.alexzhirkevich.compottie | ||
|
||
import org.khronos.webgl.Int8Array | ||
import org.khronos.webgl.get | ||
import org.khronos.webgl.set | ||
import kotlin.wasm.unsafe.UnsafeWasmMemoryApi | ||
import kotlin.wasm.unsafe.withScopedMemoryAllocator | ||
|
||
internal fun createSourceStable(data : Int8Array) : JsAny = js(""" | ||
({ | ||
start(c){ | ||
console.log(data) | ||
c.enqueue(data) | ||
c.close() | ||
} | ||
}) | ||
""") | ||
|
||
internal fun createSourceUnstable(data : JsReference<ByteArray>) : JsAny = js(""" | ||
({ | ||
start(c){ | ||
const size = wasmExports.kotlinArraySize(data); | ||
const result = new Int8Array(size); | ||
for (let i = 0; i < size; i++) { | ||
result[i] = wasmExports.kotlinArrayGet(data, i); | ||
} | ||
c.enqueue(result) | ||
c.close() | ||
} | ||
}) | ||
""") | ||
|
||
@OptIn(ExperimentalCompottieApi::class) | ||
internal fun Int8Array.toByteArray(): Array<Byte> { | ||
if (L.useStableWasmMemoryManagement) { | ||
return Array(byteLength) { | ||
this[it] | ||
} | ||
} | ||
return jsInt8ArrayToKotlinByteArray(this) | ||
} | ||
|
||
@OptIn(ExperimentalCompottieApi::class) | ||
internal fun ByteArray.toInt8Array() : Int8Array { | ||
if (L.useStableWasmMemoryManagement) { | ||
val result = Int8Array(size) | ||
for (i in indices) { | ||
result[i] = this[i] | ||
} | ||
return result | ||
} | ||
return byteArrayToInt8ArrayImpl(toJsReference()) | ||
} | ||
|
||
@OptIn(ExperimentalJsExport::class) | ||
@JsExport | ||
private fun kotlinArrayGet(a: JsReference<ByteArray>, i: Int): Byte = a.get()[i] | ||
|
||
@OptIn(ExperimentalJsExport::class) | ||
@JsExport | ||
private fun kotlinArraySize(a: JsReference<ByteArray>): Int = a.get().size | ||
|
||
|
||
private fun byteArrayToInt8ArrayImpl(a: JsReference<ByteArray>): Int8Array = js("""{ | ||
const size = wasmExports.kotlinArraySize(a); | ||
const result = new Int8Array(size); | ||
for (let i = 0; i < size; i++) { | ||
result[i] = wasmExports.kotlinArrayGet(a, i); | ||
} | ||
return result; | ||
}""") | ||
|
||
|
||
|
||
@JsFun( | ||
""" (src, size, dstAddr) => { | ||
const mem8 = new Int8Array(wasmExports.memory.buffer, dstAddr, size); | ||
mem8.set(src); | ||
} | ||
""" | ||
) | ||
private external fun jsExportInt8ArrayToWasm(src: Int8Array, size: Int, dstAddr: Int) | ||
|
||
private fun jsInt8ArrayToKotlinByteArray(x: Int8Array): Array<Byte> { | ||
val size = x.length | ||
|
||
@OptIn(UnsafeWasmMemoryApi::class) | ||
return withScopedMemoryAllocator { allocator -> | ||
val memBuffer = allocator.allocate(size) | ||
val dstAddress = memBuffer.address.toInt() | ||
jsExportInt8ArrayToWasm(x, size, dstAddress) | ||
Array(size) { i -> (memBuffer + i).loadByte() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...tie-network/src/commonMain/kotlin/io/github/alexzhirkevich/compottie/DefaultHttpClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.