Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert ByteString from/to Int8Array #386

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions bytestring/api/kotlinx-io-bytestring.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ final fun (kotlinx.io.bytestring/ByteStringBuilder).kotlinx.io.bytestring/append
final fun <#A: kotlin.text/Appendable> (kotlin.io.encoding/Base64).kotlinx.io.bytestring/encodeToAppendable(kotlinx.io.bytestring/ByteString, #A, kotlin/Int = ..., kotlin/Int = ...): #A // kotlinx.io.bytestring/encodeToAppendable|[email protected](kotlinx.io.bytestring.ByteString;0:0;kotlin.Int;kotlin.Int){0§<kotlin.text.Appendable>}[0]
final fun kotlinx.io.bytestring/ByteString(kotlin/ByteArray...): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/ByteString|ByteString(kotlin.ByteArray...){}[0]
final inline fun kotlinx.io.bytestring/buildByteString(kotlin/Int = ..., kotlin/Function1<kotlinx.io.bytestring/ByteStringBuilder, kotlin/Unit>): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/buildByteString|buildByteString(kotlin.Int;kotlin.Function1<kotlinx.io.bytestring.ByteStringBuilder,kotlin.Unit>){}[0]

// Targets: [js, wasmJs]
final fun (kotlinx.io.bytestring/ByteString).kotlinx.io.bytestring/toArrayBuffer(): org.khronos.webgl/ArrayBuffer // kotlinx.io.bytestring/toArrayBuffer|[email protected](){}[0]

// Targets: [js, wasmJs]
final fun (kotlinx.io.bytestring/ByteString).kotlinx.io.bytestring/toInt8Array(): org.khronos.webgl/Int8Array // kotlinx.io.bytestring/toInt8Array|[email protected](){}[0]

// Targets: [js, wasmJs]
final fun (org.khronos.webgl/ArrayBuffer).kotlinx.io.bytestring/toByteString(): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/toByteString|[email protected](){}[0]

// Targets: [js, wasmJs]
final fun (org.khronos.webgl/Int8Array).kotlinx.io.bytestring/toByteString(): kotlinx.io.bytestring/ByteString // kotlinx.io.bytestring/toByteString|[email protected](){}[0]
29 changes: 29 additions & 0 deletions bytestring/js/src/ByteStringsJs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/

package kotlinx.io.bytestring

import org.khronos.webgl.ArrayBuffer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something that should probably be addressed for all kotlinx libraries: should everyone use these built-in types, which are pretty weird considering we generally don't want anything to do with WebGL, or should they come from kotlin-wrappers, which tend to be the most used?

The question is important because the only "interop" between same types coming from different libs is unsafe-casting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an ArrayBuffer originating from kotlin-wrappers needs to be converted to/from ByteString, then it should be done either in kotlin-wrappers, or in a separate library.

Copy link
Contributor

@lppedd lppedd Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's reasonable. However I'm thinking that since most people on K/JS are using kotlin-wrappers (and most commonly its kotlin-js module), it would be a surprise to them that the code is red in the IDE when calling Int8Array.toByteString(), for example.

It's a bit of an unfortunate situation because it's all fragmented into multiple places.

  1. The org.khronos.webgl package is explicitly documented as a wrapper for WebGL, so the question "why am I using WebGL types?" would come up fairly naturally.

  2. The org.khronos.webgl package is a remnant of the extraction (KT-35973) of DOM APIs from the stdlib, and there is also an open issue about moving away from it (KT-57303). So maybe using kotlin-wrapper types ensures better stability on the long run.

I guess the right call would be to have the Kotlin team officially say "we will all use these types for JS", and I guess being kotlin-wrappers is the most complete and up to date collection, that could be the direction. If the stdlib needs a small interop layer, its externals could be made internal.

Anyway, from my perspective, it's all good anyway, I'd just unsafeCast<T>, but it's something I wanted to mention for more clarity.

import org.khronos.webgl.Int8Array

/**
* Returns a new [Int8Array] instance initialized with bytes copied from [this] ByteString.
*/
public fun ByteString.toInt8Array(): Int8Array = toByteArray().unsafeCast<Int8Array>()

/**
* Returns a new [ArrayBuffer] instance initialized with bytes copied from [this] ByteString.
*/
public fun ByteString.toArrayBuffer(): ArrayBuffer = toInt8Array().buffer

/**
* Return a new [ByteString] holding data copied from [this] Int8Array.
*/
public fun Int8Array.toByteString(): ByteString = ByteString(*unsafeCast<ByteArray>())

/**
* Returns a new [ByteString] holding data copied from [this] ArrayBuffer
*/
public fun ArrayBuffer.toByteString(): ByteString = Int8Array(this).toByteString()
70 changes: 70 additions & 0 deletions bytestring/js/test/ByteStringConversionsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/

package kotlinx.io.bytestring

import kotlinx.io.bytestring.unsafe.UnsafeByteStringApi
import kotlinx.io.bytestring.unsafe.UnsafeByteStringOperations
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Int8Array
import org.khronos.webgl.get
import org.khronos.webgl.set
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class ByteStringConversionsTest {
@Test
fun int8ArrayToByteString() {
assertTrue(Int8Array(0).toByteString().isEmpty())

val str = Int8Array(byteArrayOf(1, 2, 3, 4).toTypedArray()).toByteString()
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
}

@Test
fun arrayBufferToByteString() {
assertTrue(ArrayBuffer(0).toByteString().isEmpty())

val str = Int8Array(byteArrayOf(1, 2, 3, 4).toTypedArray()).buffer.toByteString()
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
}

@Test
fun byteStringToInt8Array() {
assertEquals(0, ByteString().toInt8Array().length)

val array = ByteString(1, 2, 3, 4).toInt8Array()
for (idx in 0..<3) {
assertEquals((idx + 1).toByte(), array[idx], "idx = $idx")
}
}

@Test
fun byteStringToArrayBuffer() {
assertEquals(0, ByteString().toArrayBuffer().byteLength)

val buffer = ByteString(1, 2, 3, 4).toArrayBuffer()
val array = Int8Array(buffer)
for (idx in 0..<3) {
assertEquals((idx + 1).toByte(), array[idx], "idx = $idx")
}
}

@OptIn(UnsafeByteStringApi::class)
@Test
fun integrityCheck() {
val array = Int8Array(byteArrayOf(1, 2, 3, 4).toTypedArray())
val str = array.toByteString()

array[0] = 42
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())

val array2 = str.toInt8Array()
UnsafeByteStringOperations.withByteArrayUnsafe(str) { it[1] = 42 }
assertEquals(2, array2[1])
}
}
46 changes: 46 additions & 0 deletions bytestring/wasmJs/src/ByteStringsWasmJs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/

package kotlinx.io.bytestring

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

/**
* Returns a new [Int8Array] instance initialized with bytes copied from [this] ByteString.
*/
public fun ByteString.toInt8Array(): Int8Array {
val res = Int8Array(size)
for (idx in this.indices) {
res[idx] = this[idx]
}
return res
}

/**
* Returns a new [ArrayBuffer] instance initialized with bytes copied from [this] ByteString.
*/
public fun ByteString.toArrayBuffer(): ArrayBuffer = toInt8Array().buffer

/**
* Return a new [ByteString] holding data copied from [this] Int8Array.
*/
public fun Int8Array.toByteString(): ByteString {
if (length == 0) {
return ByteString.EMPTY
}
val builder = ByteStringBuilder(length)
for (idx in 0..<length) {
builder.append(this[idx])
}
return builder.toByteString()
}

/**
* Returns a new [ByteString] holding data copied from [this] ArrayBuffer
*/
public fun ArrayBuffer.toByteString(): ByteString = Int8Array(this).toByteString()
78 changes: 78 additions & 0 deletions bytestring/wasmJs/test/ByteStringConversionsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
*/

package kotlinx.io.bytestring

import kotlinx.io.bytestring.unsafe.UnsafeByteStringApi
import kotlinx.io.bytestring.unsafe.UnsafeByteStringOperations
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Int8Array
import org.khronos.webgl.get
import org.khronos.webgl.set
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertTrue

private fun int8ArrayOf(vararg bytes: Byte): Int8Array {
val array = Int8Array(bytes.size)
for (i in bytes.indices) {
array[i] = bytes[i]
}
return array
}

class ByteStringConversionsTest {
@Test
fun int8ArrayToByteString() {
assertTrue(Int8Array(0).toByteString().isEmpty())

val str = int8ArrayOf(1, 2, 3, 4).toByteString()
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
}

@Test
fun arrayBufferToByteString() {
assertTrue(ArrayBuffer(0).toByteString().isEmpty())

val str = int8ArrayOf(1, 2, 3, 4).buffer.toByteString()
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())
}

@Test
fun byteStringToInt8Array() {
assertEquals(0, ByteString().toInt8Array().length)

val array = ByteString(1, 2, 3, 4).toInt8Array()
for (idx in 0..<3) {
assertEquals((idx + 1).toByte(), array[idx], "idx = $idx")
}
}

@Test
fun byteStringToArrayBuffer() {
assertEquals(0, ByteString().toArrayBuffer().byteLength)

val buffer = ByteString(1, 2, 3, 4).toArrayBuffer()
val array = Int8Array(buffer)
for (idx in 0..<3) {
assertEquals((idx + 1).toByte(), array[idx], "idx = $idx")
}
}

@OptIn(UnsafeByteStringApi::class)
@Test
fun integrityCheck() {
val array = int8ArrayOf(1, 2, 3, 4)
val str = array.toByteString()

array[0] = 42
assertContentEquals(byteArrayOf(1, 2, 3, 4), str.toByteArray())

val array2 = str.toInt8Array()
UnsafeByteStringOperations.withByteArrayUnsafe(str) { it[1] = 42 }
assertEquals(2, array2[1])
}
}