Skip to content

Commit

Permalink
Bump WEH and Chasm (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
illarionov authored Jan 14, 2025
1 parent 9942591 commit 151d051
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 47 deletions.
8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ androidx-test-rules = "1.6.1"
atomicfu = "0.26.1"
arrow = "2.0.0"
assertk = "0.28.1"
chasm = "0.9.2"
chasm = "0.9.4"
chicory = "1.0.0"
desugar_jdk_libs = "2.1.4"
detekt = "1.23.7"
diktat = "2.0.0"
dokka = "1.9.20"
dokka = "2.0.0"
graalvm = "24.1.1"
gradle-maven-publish-plugin = "0.30.0"
kotlin = "2.1.0"
Expand All @@ -33,7 +33,7 @@ ksp = "2.1.0-1.0.29"
mockk = "1.13.14"
spotless = "7.0.0"
sqlite-wasm-binary = "0.3"
wasi-emscripten-host = "0.1"
wasi-emscripten-host = "0.2"

[libraries]
androidx-collection = { group = "androidx.collection", name = "collection", version.ref = "androidx-collection" }
Expand All @@ -55,7 +55,7 @@ androidx-test-rules = { group = "androidx.test", name = "rules", version.ref = "
arrow-core = { group = "io.arrow-kt", name = "arrow-core", version.ref = "arrow" }
assertk = { group = "com.willowtreeapps.assertk", name = "assertk", version.ref = "assertk" }
chasm = { group = "io.github.charlietap.chasm", name = "chasm", version.ref = "chasm" }
chasm-runtime = { group = "io.github.charlietap.chasm", name = "runtime", version.ref = "chasm" }
chasm-runtime-internal = { group = "io.github.charlietap.chasm", name = "runtime-internal", version.ref = "chasm" }
chicory-runtime = { group = "com.dylibso.chicory", name = "runtime", version.ref = "chicory" }
chicory-aot = { group = "com.dylibso.chicory", name = "aot-experimental", version.ref = "chicory" }
chicory-log = { group = "com.dylibso.chicory", name = "log", version.ref = "chicory" }
Expand Down
8 changes: 2 additions & 6 deletions sqlite-embedder-chasm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ kotlin {
linuxX64()
macosArm64()
macosX64()
mingwX64 {
binaries.all {
linkerOpts("-lntdll", "-lole32")
}
}

sourceSets {
commonMain.dependencies {
api(projects.sqliteCommon)
api(libs.wsoh.binary.reader)
implementation(libs.chasm.runtime)
implementation(libs.chasm)
compileOnly(libs.chasm.runtime.internal)
implementation(libs.wasi.emscripten.host.chasm.emscripten)
implementation(libs.wasi.emscripten.host.chasm.wasip1)
implementation(libs.wasi.emscripten.host.emscripten.runtime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import io.github.charlietap.chasm.embedding.error.ChasmError
import io.github.charlietap.chasm.embedding.invoke
import io.github.charlietap.chasm.embedding.shapes.Instance
import io.github.charlietap.chasm.embedding.shapes.Store
import io.github.charlietap.chasm.embedding.shapes.Value
import io.github.charlietap.chasm.embedding.shapes.fold
import io.github.charlietap.chasm.executor.runtime.value.ExecutionValue
import io.github.charlietap.chasm.executor.runtime.value.NumberValue
import ru.pixnews.wasm.sqlite.open.helper.chasm.ext.asInt
import ru.pixnews.wasm.sqlite.open.helper.chasm.ext.asLong
import ru.pixnews.wasm.sqlite.open.helper.chasm.ext.orThrow
Expand Down Expand Up @@ -41,17 +42,15 @@ internal class ChasmFunctionBinding(
override fun executeForFloat(vararg args: Any?): Float = invoke(store, instance, name, args.argsToValues())
.fold(
{
@Suppress("UNCHECKED_CAST")
(it[0] as Value.Number<Float>).value
(it[0] as NumberValue.F32).value
},
::throwOnError,
)

override fun executeForDouble(vararg args: Any?): Double = invoke(store, instance, name, args.argsToValues())
.fold(
{
@Suppress("UNCHECKED_CAST")
(it[0] as Value.Number<Double>).value
(it[0] as NumberValue.F64).value
},
::throwOnError,
)
Expand All @@ -63,18 +62,18 @@ internal class ChasmFunctionBinding(
)
}

private fun Array<out Any?>.argsToValues(): List<Value> {
private fun Array<out Any?>.argsToValues(): List<ExecutionValue> {
return if (this.isEmpty()) {
emptyList()
} else {
List(this.size) { idx ->
when (val arg = this[idx]) {
is Int -> Value.Number.I32(arg)
is UInt -> Value.Number.I32(arg.toInt())
is Long -> Value.Number.I64(arg.toLong())
is ULong -> Value.Number.I64(arg.toLong())
is Float -> Value.Number.F32(arg.toFloat())
is Double -> Value.Number.F64(arg.toDouble())
is Int -> NumberValue.I32(arg)
is UInt -> NumberValue.I32(arg.toInt())
is Long -> NumberValue.I64(arg.toLong())
is ULong -> NumberValue.I64(arg.toLong())
is Float -> NumberValue.F32(arg.toFloat())
is Double -> NumberValue.F64(arg.toDouble())
else -> error("Unsupported argument type $arg")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ import at.released.weh.wasm.core.WasmValueTypes.F32
import at.released.weh.wasm.core.WasmValueTypes.F64
import at.released.weh.wasm.core.WasmValueTypes.I32
import at.released.weh.wasm.core.WasmValueTypes.I64
import io.github.charlietap.chasm.embedding.shapes.ValueType
import io.github.charlietap.chasm.embedding.shapes.FunctionType as ChasmFunctionType
import io.github.charlietap.chasm.ast.type.NumberType
import io.github.charlietap.chasm.ast.type.ResultType
import io.github.charlietap.chasm.ast.type.ValueType
import io.github.charlietap.chasm.ast.type.FunctionType as ChasmFunctionType

internal fun List<HostFunctionType>.toChasmFunctionTypes(): Map<HostFunctionType, ChasmFunctionType> = associateWith(
HostFunctionType::toChasmFunctionType,
)

internal fun HostFunctionType.toChasmFunctionType(): ChasmFunctionType = ChasmFunctionType(
paramTypes.map(::toChasmValueTypes),
returnTypes.map(::toChasmValueTypes),
ResultType(paramTypes.map(::toChasmValueTypes)),
ResultType(returnTypes.map(::toChasmValueTypes)),
)

internal fun toChasmValueTypes(@WasmValueType type: Int): ValueType = when (type) {
I32 -> ValueType.Number.I32
I64 -> ValueType.Number.I64
F32 -> ValueType.Number.F32
F64 -> ValueType.Number.F64
I32 -> ValueType.Number(NumberType.I32)
I64 -> ValueType.Number(NumberType.I64)
F32 -> ValueType.Number(NumberType.F32)
F64 -> ValueType.Number(NumberType.F64)
else -> error("Unsupported WASM value type `$type`")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

@file:Suppress("UNCHECKED_CAST")

package ru.pixnews.wasm.sqlite.open.helper.chasm.ext

import io.github.charlietap.chasm.embedding.shapes.Value
import io.github.charlietap.chasm.embedding.shapes.Value.Number
import io.github.charlietap.chasm.executor.runtime.value.ExecutionValue
import io.github.charlietap.chasm.executor.runtime.value.NumberValue
import ru.pixnews.wasm.sqlite.open.helper.WasmPtr

internal fun Value.asInt(): Int = (this as Number<Int>).value
internal fun Value.asUInt(): UInt = (this as Number<Int>).value.toUInt()
internal fun Value.asLong(): Long = (this as Number<Long>).value
internal fun Value.asULong(): ULong = (this as Number<Long>).value.toULong()
internal fun ExecutionValue.asInt(): Int = (this as NumberValue.I32).value
internal fun ExecutionValue.asUInt(): UInt = (this as NumberValue.I32).value.toUInt()
internal fun ExecutionValue.asLong(): Long = (this as NumberValue.I64).value
internal fun ExecutionValue.asULong(): ULong = (this as NumberValue.I64).value.toULong()

internal fun <P : Any?> Value.asWasmAddr(): WasmPtr<P> = WasmPtr(asInt())
internal fun <P : Any?> ExecutionValue.asWasmAddr(): WasmPtr<P> = WasmPtr(asInt())
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import at.released.weh.wasm.core.WasmModules.ENV_MODULE_NAME
import at.released.weh.wasm.core.memory.WASM_MEMORY_DEFAULT_MAX_PAGES
import at.released.weh.wasm.core.memory.WASM_MEMORY_PAGE_SIZE
import com.github.michaelbull.result.getOrThrow
import io.github.charlietap.chasm.ast.type.Limits
import io.github.charlietap.chasm.ast.type.SharedStatus.Unshared
import io.github.charlietap.chasm.embedding.error.ChasmError.DecodeError
import io.github.charlietap.chasm.embedding.instance
import io.github.charlietap.chasm.embedding.memory
import io.github.charlietap.chasm.embedding.module
import io.github.charlietap.chasm.embedding.shapes.Function
import io.github.charlietap.chasm.embedding.shapes.Import
import io.github.charlietap.chasm.embedding.shapes.Instance
import io.github.charlietap.chasm.embedding.shapes.Limits
import io.github.charlietap.chasm.embedding.shapes.Module
import io.github.charlietap.chasm.embedding.shapes.Store
import io.github.charlietap.chasm.embedding.shapes.fold
Expand All @@ -29,6 +30,7 @@ import io.github.charlietap.chasm.executor.runtime.ext.asRange
import io.github.charlietap.chasm.executor.runtime.ext.table
import io.github.charlietap.chasm.executor.runtime.instance.TableInstance
import io.github.charlietap.chasm.executor.runtime.store.Address
import io.github.charlietap.chasm.executor.runtime.store.Address.Table
import io.github.charlietap.chasm.executor.runtime.value.ReferenceValue
import io.github.charlietap.chasm.stream.SourceReader
import kotlinx.io.RawSource
Expand All @@ -45,9 +47,9 @@ import ru.pixnews.wasm.sqlite.open.helper.embedder.callback.SqliteCallbackStore
import ru.pixnews.wasm.sqlite.open.helper.embedder.functiontable.IndirectFunctionTableIndex
import ru.pixnews.wasm.sqlite.open.helper.embedder.functiontable.SqliteCallbackFunctionIndexes
import ru.pixnews.wasm.sqlite.open.helper.embedder.sqlitecb.SqliteCallbacksModuleFunction
import io.github.charlietap.chasm.ast.type.MemoryType as ChasmMemoryType
import io.github.charlietap.chasm.embedding.shapes.Import as ChasmImport
import io.github.charlietap.chasm.embedding.shapes.Memory as ChasmMemoryImportable
import io.github.charlietap.chasm.embedding.shapes.MemoryType as ChasmMemoryType

internal class ChasmInstanceBuilder(
private val host: EmbedderHost,
Expand Down Expand Up @@ -113,6 +115,7 @@ internal class ChasmInstanceBuilder(
min = (minMemorySize / WASM_MEMORY_PAGE_SIZE).toUInt(),
max = WASM_MEMORY_DEFAULT_MAX_PAGES.count.toUInt(),
),
shared = Unshared,
)
val memory: ChasmMemoryImportable = memory(store, memoryType)
val import = ChasmImport(ENV_MODULE_NAME, "memory", memory)
Expand All @@ -125,8 +128,8 @@ internal class ChasmInstanceBuilder(
instance: Instance,
callbackHostFunctions: List<ChasmImport>,
): SqliteCallbackFunctionIndexes {
val tableAddress = instance.instance.tableAddresses[0]
val table = store.store.table(tableAddress).getOrThrow {
val tableAddress: Table = instance.instance.tableAddresses[0]
val table: TableInstance = store.store.table(tableAddress).getOrThrow {
ChasmException("Can not get table $tableAddress")
}
val oldSize = table.elements.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package ru.pixnews.wasm.sqlite.open.helper.chasm.host.module.sqlitecb.function

import at.released.weh.host.EmbedderHost
import io.github.charlietap.chasm.embedding.shapes.Value
import io.github.charlietap.chasm.executor.runtime.value.NumberValue
import ru.pixnews.wasm.sqlite.open.helper.WasmPtr
import ru.pixnews.wasm.sqlite.open.helper.chasm.ext.asWasmAddr
import ru.pixnews.wasm.sqlite.open.helper.embedder.sqlitecb.function.Sqlite3ProgressFunctionHandle
Expand All @@ -22,6 +22,6 @@ internal class Sqlite3ProgressAdapter(
private val handle = Sqlite3ProgressFunctionHandle(host, progressCallbackStore)
val function: ChasmHostFunction = { args ->
val result = handle.execute(args[0].asWasmAddr())
listOf(Value.Number.I32(result))
listOf(NumberValue.I32(result))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package ru.pixnews.wasm.sqlite.open.helper.chasm.host.module.sqlitecb.function

import at.released.weh.host.EmbedderHost
import at.released.weh.wasm.core.memory.ReadOnlyMemory
import io.github.charlietap.chasm.embedding.shapes.Value
import io.github.charlietap.chasm.executor.runtime.value.NumberValue
import ru.pixnews.wasm.sqlite.open.helper.WasmPtr
import ru.pixnews.wasm.sqlite.open.helper.chasm.ext.asInt
import ru.pixnews.wasm.sqlite.open.helper.chasm.ext.asUInt
Expand All @@ -33,6 +33,6 @@ internal class Sqlite3TraceAdapter(
args[2].asWasmAddr(),
args[3].asInt().toLong(),
)
listOf(Value.Number.I32(result))
listOf(NumberValue.I32(result))
}
}

0 comments on commit 151d051

Please sign in to comment.