Skip to content

Commit

Permalink
Add wasi-emscripten-host module (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
illarionov authored Mar 13, 2024
1 parent 28d6a8e commit 1d8d845
Show file tree
Hide file tree
Showing 73 changed files with 3,725 additions and 0 deletions.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ include("common-api")
include("sqlite-common-api")
include("sqlite-open-helper")
include("sqlite-wasm")
include("wasi-emscripten-host")
1 change: 1 addition & 0 deletions wasi-emscripten-host/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
25 changes: 25 additions & 0 deletions wasi-emscripten-host/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

plugins {
id("ru.pixnews.sqlite.open.helper.gradle.multiplatform.kotlin")
}

group = "ru.pixnews.sqlite.open.helper.host"

kotlin {
jvm()

sourceSets {
commonMain.dependencies {
api(projects.commonApi)
}
commonTest.dependencies {
implementation(kotlin("test"))
implementation(libs.assertk)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

package ru.pixnews.sqlite.open.helper.host

// https://webassembly.github.io/spec/core/appendix/index-types.html
@JvmInline
public value class WasmValueType(
public val opcode: Byte?,
) {
@Suppress("VARIABLE_NAME_INCORRECT")
public companion object WebAssemblyTypes {
public val I32: WasmValueType = WasmValueType(0x7f)
public val I64: WasmValueType = WasmValueType(0x7e)
public val F32: WasmValueType = WasmValueType(0x7d)
public val F64: WasmValueType = WasmValueType(0x7c)
public val V128: WasmValueType = WasmValueType(0x7b)
public val FuncRef: WasmValueType = WasmValueType(0x70)
public val ExternRef: WasmValueType = WasmValueType(0x6f)
public val FunctionType: WasmValueType = WasmValueType(0x60)
public val ResultType: WasmValueType = WasmValueType(0x40)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

package ru.pixnews.sqlite.open.helper.host

import ru.pixnews.sqlite.open.helper.host.WasmValueType.WebAssemblyTypes.I32

public val POINTER: WasmValueType get() = I32

public val WasmValueType.pointer: WasmValueType
get() = POINTER
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

package ru.pixnews.sqlite.open.helper.host.emscripten

public class AssertionFailedException(
condition: String?,
filename: String?,
line: Int,
func: String?,
) : RuntimeException(
formatErrMsg(
condition,
filename,
line,
func,
),
) {
private companion object {
fun formatErrMsg(
condition: String?,
filename: String?,
line: Int,
func: String?,
): String = buildString {
append("Assertion failed: ")
append(condition ?: "``")
append(", at ")
listOf(
filename ?: "unknown filename",
line.toString(),
func ?: "unknown function",
).joinTo(this, ", ", prefix = "[", postfix = "]")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

package ru.pixnews.sqlite.open.helper.host.filesystem

public enum class ReadWriteStrategy {
DO_NOT_CHANGE_POSITION,
CHANGE_POSITION,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

package ru.pixnews.sqlite.open.helper.host.filesystem

import ru.pixnews.sqlite.open.helper.host.wasi.preview1.type.Errno

public class SysException(
public val errNo: Errno,
msg: String? = null,
cause: Throwable? = null,
) : RuntimeException(msg, cause)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

package ru.pixnews.sqlite.open.helper.host.functiontable

public sealed interface IndirectFunctionRef {
public val ref: Int

@JvmInline
public value class FuncRef(
override val ref: Int,
) : IndirectFunctionRef

@JvmInline
public value class ExternalRef(
override val ref: Int,
) : IndirectFunctionRef
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

package ru.pixnews.sqlite.open.helper.host.functiontable

@JvmInline
public value class IndirectFunctionTableIndex(
public val funcId: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

@file:Suppress("VARIABLE_HAS_PREFIX", "BLANK_LINE_BETWEEN_PROPERTIES")

package ru.pixnews.sqlite.open.helper.host.include

public object Fcntl {
public const val O_RDONLY: UInt = 0x0U
public const val O_WRONLY: UInt = 0x1U
public const val O_RDWR: UInt = 0x2U
public const val O_ACCMODE: UInt = 0x3U

public const val O_CREAT: UInt = 0x40U
public const val O_EXCL: UInt = 0x80U
public const val O_NOCTTY: UInt = 0x100U
public const val O_TRUNC: UInt = 0x200U
public const val O_APPEND: UInt = 0x400U
public const val O_NONBLOCK: UInt = 0x800U
public const val O_NDELAY: UInt = O_NONBLOCK
public const val O_DSYNC: UInt = 0x1000U
public const val O_ASYNC: UInt = 0x2000U
public const val O_DIRECT: UInt = 0x4000U
public const val O_LARGEFILE: UInt = 0x8000U
public const val O_DIRECTORY: UInt = 0x10000U
public const val O_NOFOLLOW: UInt = 0x20000U
public const val O_NOATIME: UInt = 0x40000U
public const val O_CLOEXEC: UInt = 0x80000U
public const val O_SYNC: UInt = 0x101000U
public const val O_PATH: UInt = 0x200000U
public const val O_TMPFILE: UInt = 0x410000U
public const val O_SEARCH: UInt = O_PATH

public const val S_ISUID: UInt = 0x800U
public const val S_ISGID: UInt = 0x400U
public const val S_ISVTX: UInt = 0x200U
public const val S_IRUSR: UInt = 0x100U
public const val S_IWUSR: UInt = 0x80U
public const val S_IXUSR: UInt = 0x40U
public const val S_IRWXU: UInt = 0x1c0U
public const val S_IRGRP: UInt = 0x20U
public const val S_IWGRP: UInt = 0x10U
public const val S_IXGRP: UInt = 0x08U
public const val S_IRWXG: UInt = 0x38U
public const val S_IROTH: UInt = 0x04U
public const val S_IWOTH: UInt = 0x02U
public const val S_IXOTH: UInt = 0x01U
public const val S_IRWXO: UInt = 0x07U

public const val AT_FDCWD: Int = -100
public const val AT_SYMLINK_NOFOLLOW: UInt = 0x100U
public const val AT_REMOVEDIR: UInt = 0x200U
public const val AT_SYMLINK_FOLLOW: UInt = 0x400U
public const val AT_EACCESS: UInt = 0x200U
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

@file:Suppress("MagicNumber")

package ru.pixnews.sqlite.open.helper.host.include

import kotlin.reflect.KProperty0

public fun Fcntl.oMaskToString(mask: UInt): String {
val startNames = if (mask.and(O_ACCMODE) == 0U) {
listOf(Fcntl::O_RDONLY.name)
} else {
emptyList()
}

return maskToString(
mask,
listOf(
Fcntl::O_WRONLY,
Fcntl::O_RDWR,
Fcntl::O_CREAT,
Fcntl::O_EXCL,
Fcntl::O_NOCTTY,
Fcntl::O_TRUNC,
Fcntl::O_APPEND,
Fcntl::O_NONBLOCK,
Fcntl::O_SYNC,
Fcntl::O_TMPFILE,
Fcntl::O_DSYNC,
Fcntl::O_ASYNC,
Fcntl::O_DIRECT,
Fcntl::O_LARGEFILE,
Fcntl::O_DIRECTORY,
Fcntl::O_NOFOLLOW,
Fcntl::O_NOATIME,
Fcntl::O_CLOEXEC,
Fcntl::O_PATH,
),
startNames,
)
}

public fun Fcntl.sMaskToString(mask: UInt): String = "0${mask.toString(8)}"

public fun Fcntl.sMaskToStringLong(mask: UInt): String = maskToString(
mask,
listOf(
Fcntl::S_ISUID,
Fcntl::S_ISGID,
Fcntl::S_ISVTX,
Fcntl::S_IRUSR,
Fcntl::S_IWUSR,
Fcntl::S_IXUSR,
Fcntl::S_IRWXU,
Fcntl::S_IRGRP,
Fcntl::S_IWGRP,
Fcntl::S_IXGRP,
Fcntl::S_IRWXG,
Fcntl::S_IROTH,
Fcntl::S_IWOTH,
Fcntl::S_IXOTH,
Fcntl::S_IRWXO,
),
)

private fun maskToString(
mask: UInt,
maskProps: List<KProperty0<UInt>>,
startNames: List<String> = emptyList(),
): String {
var left = mask
val names = startNames.toMutableList()
maskProps.forEach { prop: KProperty0<UInt> ->
val propMask: UInt = prop.get()
if (left.and(propMask) != 0U) {
names.add(prop.name)
left = left.and(propMask.inv())
}
}
return buildString {
names.joinTo(this, ",")
if (left != 0U) {
append("0")
append(left.toString(8))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

package ru.pixnews.sqlite.open.helper.host.include

@Suppress("PropertyName", "ConstructorParameterNaming")
public data class StructTimespec(
val tv_sec: time_t,
val tv_nsec: ULong,
)

@Suppress("TYPEALIAS_NAME_INCORRECT_CASE")
public typealias time_t = ULong

public val StructTimespec.timeMillis: ULong
get(): ULong = tv_sec * 1000U + tv_nsec / 1_000_000U
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/

package ru.pixnews.sqlite.open.helper.host.wasi.preview1.ext

import ru.pixnews.sqlite.open.helper.common.api.WasmPtr
import ru.pixnews.sqlite.open.helper.common.api.plus
import ru.pixnews.sqlite.open.helper.host.memory.Memory
import ru.pixnews.sqlite.open.helper.host.memory.readPtr
import ru.pixnews.sqlite.open.helper.host.wasi.preview1.type.Iovec
import ru.pixnews.sqlite.open.helper.host.wasi.preview1.type.IovecArray
import ru.pixnews.sqlite.open.helper.host.wasi.preview1.type.Size

@Suppress("VARIABLE_HAS_PREFIX")
public object FdReadExt {
public fun readIovecs(
memory: Memory,
pIov: WasmPtr<Iovec>,
iovCnt: Int,
): IovecArray {
@Suppress("UNCHECKED_CAST", "MagicNumber")
val iovecs = MutableList(iovCnt) { idx ->
val pIovec: WasmPtr<*> = pIov + 8 * idx
Iovec(
buf = memory.readPtr(pIovec as WasmPtr<WasmPtr<Byte>>),
bufLen = Size(memory.readI32(pIovec + 4).toUInt()),
)
}
return IovecArray(iovecs)
}
}
Loading

0 comments on commit 1d8d845

Please sign in to comment.