kotlinx-io
kotlinx-io copied to clipboard
The kotlinx.io.core.String(ByteArray, Int, Int, Charset) function ignores the length and offset arguments on JS
Description
Looks like the kotlinx.io.core.String(ByteArray, Int, Int, Charset)
function just ignores the length
and offset
arguments on JS.
For example, the String(byteArrayOf(64, 64, 67, 67), length = 2, offset = 1)
expression will return @@CC
instead of @C
.
Steps to Reproduce:
- Create a new Kotlin/JavaScript + Gradle project using IntelliJ IDEA.
- Make the
build.gradle
file look like that:
plugins {
id 'java'
id 'kotlin2js' version '1.3.20-dev-592'
}
group 'com.company.test'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
jcenter()
maven { url 'http://dl.bintray.com/kotlin/kotlin-dev' }
maven { url 'http://dl.bintray.com/kotlin/kotlinx' }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js"
compile "org.jetbrains.kotlin:kotlin-test-js"
compile "org.jetbrains.kotlinx:kotlinx-io-js:0.1.0-alpha-17-rc13"
}
compileKotlin2Js {
kotlinOptions.moduleKind = 'umd'
}
- Make the
settings.gradle
file look like that:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "kotlin2js") {
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
}
}
}
repositories {
jcenter()
maven { url 'http://dl.bintray.com/kotlin/kotlin-dev' }
}
}
rootProject.name = 'test'
- Create a new package named "com.company.test" and a "Test.kt" file inside it with the following contents:
package com.company.test
import kotlinx.io.core.String
fun main(args: Array<String>) {
println(String(byteArrayOf(64, 64, 67, 67), length = 2, offset = 1))
}
- Build the project and run it using Node.JS (I've been using v10.11.0).
- The
@@CC
will be printed instead of@C
:disappointed:.
Looks like an array returned by the TypedArray#subarray(...)
method uses the same buffer as the original array: The subarray() method returns a new TypedArray on the same ArrayBuffer store and with the same element types as for this TypedArray object
. So, i8.subarray(offset, offset + length).buffer
will return the original ArrayBuffer and will ignore the offset and length parameters :/.
We're rebooting the kotlinx-io development (see https://github.com/Kotlin/kotlinx-io/issues/131), all issues related to the previous versions will be closed. Consider reopening it if the issue remains (or the feature is still missing) in a new version.