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

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix failure
Browse files Browse the repository at this point in the history
Dominaezzz committed Mar 1, 2020
1 parent 2c36ef2 commit ea84ad5
Showing 8 changed files with 39 additions and 26 deletions.
4 changes: 4 additions & 0 deletions kgl-core/src/commonMain/kotlin/com/kgl/core/BufferUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.kgl.core


expect inline fun <T> withByteBuffer(length: Long, block: (ByteBuffer) -> T): T
2 changes: 0 additions & 2 deletions kgl-core/src/commonMain/kotlin/com/kgl/core/ByteBuffer.kt
Original file line number Diff line number Diff line change
@@ -14,8 +14,6 @@ expect class ByteBuffer : Buffer {
fun slice(index: Long, length: Long): ByteBuffer
}

expect inline fun <T> withByteBuffer(length: Long, block: (ByteBuffer) -> T): T

@Suppress("NOTHING_TO_INLINE")
internal inline fun ByteBuffer.checkBounds(index: Long, length: Long) {
require(index in 0 until this.length)
8 changes: 8 additions & 0 deletions kgl-core/src/jsMain/kotlin/com/kgl/core/BufferUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kgl.core

import org.khronos.webgl.Int8Array

actual inline fun <T> withByteBuffer(length: Long, block: (ByteBuffer) -> T): T {
val array = ByteArray(length.toInt())
return block(ByteBuffer(array.unsafeCast<Int8Array>()))
}
9 changes: 2 additions & 7 deletions kgl-core/src/jsMain/kotlin/com/kgl/core/ByteBuffer.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.kgl.core

import org.khronos.webgl.Uint8Array
import org.khronos.webgl.Int8Array
import org.khronos.webgl.get
import org.khronos.webgl.set

actual class ByteBuffer(private val ptr: Uint8Array) : Buffer {
actual class ByteBuffer(private val ptr: Int8Array) : Buffer {
actual val length: Long get() = ptr.length.toLong()
actual override val size: ULong get() = length.toULong() * Byte.SIZE_BYTES.toUInt()

@@ -43,8 +43,3 @@ actual class ByteBuffer(private val ptr: Uint8Array) : Buffer {
return ByteBuffer(ptr.subarray(index.toInt(), (index + length).toInt()))
}
}

actual inline fun <T> withByteBuffer(length: Long, block: (ByteBuffer) -> T): T {
val array = Uint8Array(length.toInt())
return block(ByteBuffer(array))
}
13 changes: 13 additions & 0 deletions kgl-core/src/jvmMain/kotlin/com/kgl/core/BufferUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.kgl.core

import org.lwjgl.system.MemoryUtil


actual inline fun <T> withByteBuffer(length: Long, block: (ByteBuffer) -> T): T {
val jvmBuffer = MemoryUtil.memAlloc(length.toInt())
try {
return block(ByteBuffer(jvmBuffer))
} finally {
MemoryUtil.memFree(jvmBuffer)
}
}
9 changes: 0 additions & 9 deletions kgl-core/src/jvmMain/kotlin/com/kgl/core/ByteBuffer.kt
Original file line number Diff line number Diff line change
@@ -45,12 +45,3 @@ actual class ByteBuffer(private val ptr: Long, actual val length: Long) : Buffer
return ByteBuffer(ptr + (index * Byte.SIZE_BYTES), length)
}
}

actual inline fun <T> withByteBuffer(length: Long, block: (ByteBuffer) -> T): T {
val jvmBuffer = MemoryUtil.memAlloc(length.toInt())
try {
return block(ByteBuffer(jvmBuffer))
} finally {
MemoryUtil.memFree(jvmBuffer)
}
}
12 changes: 12 additions & 0 deletions kgl-core/src/nativeMain/kotlin/com/kgl/core/BufferUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kgl.core

import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned

actual inline fun <T> withByteBuffer(length: Long, block: (ByteBuffer) -> T): T {
val array = ByteArray(length.toInt())
return array.usePinned {
val buffer = ByteBuffer(it.addressOf(0), length)
block(buffer)
}
}
8 changes: 0 additions & 8 deletions kgl-core/src/nativeMain/kotlin/com/kgl/core/ByteBuffer.kt
Original file line number Diff line number Diff line change
@@ -43,11 +43,3 @@ actual class ByteBuffer(private val ptr: CPointer<ByteVar>, actual val length: L
return ByteBuffer((ptr + index)!!, length)
}
}

actual inline fun <T> withByteBuffer(length: Long, block: (ByteBuffer) -> T): T {
val array = ByteArray(length.toInt())
return array.usePinned {
val buffer = ByteBuffer(it.addressOf(0), length)
block(buffer)
}
}

0 comments on commit ea84ad5

Please sign in to comment.