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

Basic filesystems support for Wasm: reuse JS implementation for wasmJs target #256

Merged
merged 4 commits into from
Feb 14, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ kotlin {
createSourceSet("linuxTest", parent = unixTest, children = linuxTargets())
createSourceSet("androidMain", parent = unixMain, children = androidTargets())
createSourceSet("androidTest", parent = unixTest, children = androidTargets())
createSourceSet("nodeFilesystemSharedMain", parent = commonMain.get(), children = nodeTargets())
createSourceSet("nodeFilesystemSharedTest", parent = commonTest.get(), children = nodeTargets())
createSourceSet("wasmMain", parent = commonMain.get(), children = wasmTargets())
createSourceSet("wasmTest", parent = commonTest.get(), children = wasmTargets())
}
Expand Down Expand Up @@ -200,6 +202,8 @@ fun androidTargets() = listOf(

fun wasmTargets() = listOf("wasmJs", "wasmWasi")

fun nodeTargets() = listOf("js", "wasmJs")

rootProject.the<NodeJsRootExtension>().apply {
nodeVersion = "21.0.0-v8-canary202310177990572111"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
Expand Down
5 changes: 0 additions & 5 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ kotlin {
}
}

@OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl::class)
wasmJs {
filterSmokeTests()
}

@OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl::class)
wasmWasi {
filterSmokeTests()
Expand Down
15 changes: 15 additions & 0 deletions core/js/src/-PlatfromJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ public actual open class IOException actual constructor(
public actual open class EOFException actual constructor(message: String?) : IOException(message) {
public actual constructor() : this(null)
}

@Suppress("ACTUAL_WITHOUT_EXPECT")
internal actual typealias CommonJsModule = JsModule

@Suppress("ACTUAL_WITHOUT_EXPECT")
internal actual typealias CommonJsNonModule = JsNonModule

internal actual fun withCaughtException(block: () -> Unit): Throwable? {
try {
block()
return null
} catch (t: Throwable) {
return t
}
}
42 changes: 0 additions & 42 deletions core/js/src/imports.kt

This file was deleted.

49 changes: 0 additions & 49 deletions core/js/test/utils.kt

This file was deleted.

22 changes: 22 additions & 0 deletions core/nodeFilesystemShared/src/annotations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2024 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

// The source set is shared by js and wasmJs targets that don't have a single common JS-source set.
// As a result, JsModule and JsNonModule annotations are unavailable. To overcome that issue,
// the following expects are declared in this module and actualized in the leaf source sets.

/**
* Actualized with JsModule on both JS and WasmJs.
*/
@Target(AnnotationTarget.FILE)
internal expect annotation class CommonJsModule(val import: String)

/**
* Actualized with JsNonModule on JS, left "empty" on WasmJs.
*/
@Target(AnnotationTarget.FILE)
internal expect annotation class CommonJsNonModule()
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@

package kotlinx.io.files

import kotlinx.io.*
import kotlinx.io.IOException
import kotlinx.io.RawSink
import kotlinx.io.RawSource
import kotlinx.io.node.fs.*
import kotlinx.io.node.os.platform
import kotlinx.io.node.os.tmpdir
import kotlinx.io.withCaughtException

public actual val SystemFileSystem: FileSystem = object : SystemFileSystemImpl() {
override fun exists(path: Path): Boolean {
check(fs !== null) { "Module 'fs' was not found" }
return fs.existsSync(path.path) as Boolean
return existsSync(path.path)
}

override fun delete(path: Path, mustExist: Boolean) {
check(fs !== null) { "Module 'fs' was not found" }
if (!exists(path)) {
if (mustExist) {
throw FileNotFoundException("File does not exist: ${path.path}")
throw FileNotFoundException("File does not exist: $path")
}
return
}
try {
val stats = fs.statSync(path.path)
if (stats.isDirectory() as Boolean) {
fs.rmdirSync(path.path)
withCaughtException {
val stats = statSync(path.path) ?: throw FileNotFoundException("File does not exist: $path")
if (stats.isDirectory()) {
rmdirSync(path.path)
} else {
fs.rmSync(path.path)
rmSync(path.path)
}
} catch (t: Throwable) {
throw IOException("Delete failed for $path", t)
}?.also {
throw IOException("Delete failed for $path", it)
}
}

Expand All @@ -52,65 +56,60 @@ public actual val SystemFileSystem: FileSystem = object : SystemFileSystemImpl()
p = p.parent
}
parts.asReversed().forEach {
fs.mkdirSync(it)
mkdirSync(it)
}
}

override fun atomicMove(source: Path, destination: Path) {
check(fs !== null) { "Module 'fs' was not found" }
if (!exists(source)) {
throw FileNotFoundException("Source does not exist: ${source.path}")
}
try {
fs.renameSync(source.path, destination.path)
} catch (t: Throwable) {
throw IOException("Move failed from $source to $destination", t)
withCaughtException {
renameSync(source.path, destination.path)
}?.also {
throw IOException("Move failed from $source to $destination", it)
}
}

override fun metadataOrNull(path: Path): FileMetadata? {
check(fs !== null) { "Module 'fs' was not found" }
return try {
val stat = fs.statSync(path.path)
val mode = stat.mode as Int
val isFile = (mode and fs.constants.S_IFMT as Int) == fs.constants.S_IFREG
FileMetadata(
if (!exists(path)) return null
var metadata: FileMetadata? = null
withCaughtException {
val stat = statSync(path.path) ?: return@withCaughtException
val mode = stat.mode
val isFile = (mode and constants.S_IFMT) == constants.S_IFREG
metadata = FileMetadata(
isRegularFile = isFile,
isDirectory = (mode and fs.constants.S_IFMT as Int) == fs.constants.S_IFDIR,
if (isFile) (stat.size as Int).toLong() else -1L
isDirectory = (mode and constants.S_IFMT) == constants.S_IFDIR,
if (isFile) stat.size.toLong() else -1L
)
} catch (t: Throwable) {
if (exists(path)) throw IOException("Stat failed for $path", t)
return null
}?.also {
throw IOException("Stat failed for $path", it)
}
return metadata
}

override fun source(path: Path): RawSource {
check(fs !== null) { "Module 'fs' was not found" }
return FileSource(path)
}

override fun sink(path: Path, append: Boolean): RawSink {
check(fs !== null) { "Module 'fs' was not found" }
check(buffer !== null) { "Module 'buffer' was not found" }
return FileSink(path, append)
}

override fun resolve(path: Path): Path {
check(fs !== null) { "Module 'fs' was not found" }
if (!exists(path)) throw FileNotFoundException(path.path)
return Path(fs.realpathSync.native(path.path) as String)
return Path(realpathSync.native(path.path))
}
}

public actual val SystemTemporaryDirectory: Path
get() {
check(os !== null) { "Module 'os' was not found" }
return Path(os.tmpdir() as? String ?: "")
return Path(tmpdir() ?: "")
}

public actual open class FileNotFoundException actual constructor(
message: String?,
) : IOException(message)

internal actual val isWindows = os.platform() == "win32"
internal actual val isWindows = platform() == "win32"
Loading