From 197146a01cb79e27cb797a51b0129c8ba25c34b4 Mon Sep 17 00:00:00 2001 From: Filipp Zhinkin Date: Fri, 1 Sep 2023 10:18:47 +0200 Subject: [PATCH] Wasm target support (#188) * Enable Wasm target and updated Kotlin version to 1.9.10 --- README.md | 2 +- .../kotlinx-io-multiplatform.gradle.kts | 13 +++++++++ core/build.gradle.kts | 12 +++++++++ core/wasm/src/-PlatformWasm.kt | 19 +++++++++++++ core/wasm/src/RawSink.kt | 15 +++++++++++ core/wasm/src/SegmentPool.kt | 17 ++++++++++++ core/wasm/src/files/PathsWasm.kt | 27 +++++++++++++++++++ core/wasm/test/utils.kt | 14 ++++++++++ gradle/libs.versions.toml | 2 +- 9 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 core/wasm/src/-PlatformWasm.kt create mode 100644 core/wasm/src/RawSink.kt create mode 100644 core/wasm/src/SegmentPool.kt create mode 100644 core/wasm/src/files/PathsWasm.kt create mode 100644 core/wasm/test/utils.kt diff --git a/README.md b/README.md index 5479e3da7..6f50cd95b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![JetBrains incubator project](https://jb.gg/badges/incubator.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub) [![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0) [![Download](https://img.shields.io/maven-central/v/org.jetbrains.kotlinx/kotlinx-io-core?versionSuffix=0.2.1)](https://central.sonatype.com/artifact/org.jetbrains.kotlinx/kotlinx-io-core/0.2.1) -[![Kotlin](https://img.shields.io/badge/kotlin-1.9.0-blue.svg?logo=kotlin)](http://kotlinlang.org) +[![Kotlin](https://img.shields.io/badge/kotlin-1.9.10-blue.svg?logo=kotlin)](http://kotlinlang.org) [![TeamCity build](https://img.shields.io/teamcity/build/s/KotlinTools_KotlinxIo_BuildAggregated.svg?server=http%3A%2F%2Fteamcity.jetbrains.com)](https://teamcity.jetbrains.com/viewType.html?buildTypeId=KotlinTools_KotlinxIo_BuildAggregated&guest=1) [![KDoc link](https://img.shields.io/badge/API_reference-KDoc-blue)](https://fzhinkin.github.io/kotlinx-io-dokka-docs-preview/) diff --git a/build-logic/src/main/kotlin/kotlinx/io/conventions/kotlinx-io-multiplatform.gradle.kts b/build-logic/src/main/kotlin/kotlinx/io/conventions/kotlinx-io-multiplatform.gradle.kts index 53c008fdc..91c37c332 100644 --- a/build-logic/src/main/kotlin/kotlinx/io/conventions/kotlinx-io-multiplatform.gradle.kts +++ b/build-logic/src/main/kotlin/kotlinx/io/conventions/kotlinx-io-multiplatform.gradle.kts @@ -5,6 +5,7 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet +import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension import kotlin.jvm.optionals.getOrNull plugins { @@ -36,6 +37,14 @@ kotlin { } } + @OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl::class) + wasm { + nodejs() + // Disabled because we can't exclude some tests: https://youtrack.jetbrains.com/issue/KT-58291 + // browser() + binaries.executable() + } + sourceSets { commonTest { dependencies { @@ -163,3 +172,7 @@ fun androidTargets() = listOf( "androidNativeX64", "androidNativeX86" ) + +rootProject.the().apply { + nodeVersion = "20.4.0" +} diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 33de73700..0f24c8ed8 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -31,6 +31,18 @@ kotlin { } } + @OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl::class) + wasm { + nodejs { + testTask(Action { + useMocha { + timeout = "300s" + } + filter.setExcludePatterns("*SmokeFileTest*") + }) + } + } + sourceSets { commonMain { dependencies { diff --git a/core/wasm/src/-PlatformWasm.kt b/core/wasm/src/-PlatformWasm.kt new file mode 100644 index 000000000..732a2b4cc --- /dev/null +++ b/core/wasm/src/-PlatformWasm.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. + */ + +package kotlinx.io + +import kotlinx.io.internal.commonAsUtf8ToByteArray + +internal actual fun String.asUtf8ToByteArray(): ByteArray = commonAsUtf8ToByteArray() + +public actual open class IOException actual constructor( + message: String?, + cause: Throwable? +) : Exception(message, cause) { + public actual constructor(message: String?) : this(message, null) +} + +public actual open class EOFException actual constructor(message: String?) : IOException(message) diff --git a/core/wasm/src/RawSink.kt b/core/wasm/src/RawSink.kt new file mode 100644 index 000000000..3cef096ad --- /dev/null +++ b/core/wasm/src/RawSink.kt @@ -0,0 +1,15 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. + */ + +package kotlinx.io + +@OptIn(ExperimentalStdlibApi::class) +public actual interface RawSink : AutoCloseableAlias { + public actual fun write(source: Buffer, byteCount: Long) + + public actual fun flush() + + actual override fun close() +} diff --git a/core/wasm/src/SegmentPool.kt b/core/wasm/src/SegmentPool.kt new file mode 100644 index 000000000..c77d92ef9 --- /dev/null +++ b/core/wasm/src/SegmentPool.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. + */ + +package kotlinx.io + +internal actual object SegmentPool { + actual val MAX_SIZE: Int = 0 + + actual val byteCount: Int = 0 + + actual fun take(): Segment = Segment() + + actual fun recycle(segment: Segment) { + } +} diff --git a/core/wasm/src/files/PathsWasm.kt b/core/wasm/src/files/PathsWasm.kt new file mode 100644 index 000000000..3ff32f555 --- /dev/null +++ b/core/wasm/src/files/PathsWasm.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. + */ + +package kotlinx.io.files + +import kotlinx.io.Sink +import kotlinx.io.Source + + +public actual class Path internal constructor(private val path: String, + @Suppress("UNUSED_PARAMETER") any: Any?) { + override fun toString(): String = path +} + +public actual fun Path(path: String): Path { + return Path(path, null) +} + +public actual fun Path.source(): Source { + TODO("Paths are not supported for Wasm target") +} + +public actual fun Path.sink(): Sink { + TODO("Paths are not supported for Wasm target") +} diff --git a/core/wasm/test/utils.kt b/core/wasm/test/utils.kt new file mode 100644 index 000000000..409554fd2 --- /dev/null +++ b/core/wasm/test/utils.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. + */ + +package kotlinx.io + +actual fun createTempFile(): String { + TODO("Paths are not supported for Wasm target") +} + +actual fun deleteFile(path: String) { + TODO("Paths are not supported for Wasm target") +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 94f729c59..c70a25044 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -kotlin = "1.9.0" +kotlin = "1.9.10" java = "8" dokka = "1.8.20" kover = "0.7.3"