From 1b4e672a4580e2d38edcd06510dabb45359f162b Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sat, 15 Mar 2025 22:47:10 -0300 Subject: Make flite work --- .../space/autistic/radio/reflection/MemoryReflection.kt | 14 ++++++++++++++ src/main/kotlin/space/autistic/radio/wasm/Bindings.kt | 5 +++++ .../kotlin/space/autistic/radio/wasm/WasmExitException.kt | 4 ++++ 3 files changed, 23 insertions(+) create mode 100644 src/main/kotlin/space/autistic/radio/reflection/MemoryReflection.kt create mode 100644 src/main/kotlin/space/autistic/radio/wasm/WasmExitException.kt (limited to 'src/main/kotlin') diff --git a/src/main/kotlin/space/autistic/radio/reflection/MemoryReflection.kt b/src/main/kotlin/space/autistic/radio/reflection/MemoryReflection.kt new file mode 100644 index 0000000..78961da --- /dev/null +++ b/src/main/kotlin/space/autistic/radio/reflection/MemoryReflection.kt @@ -0,0 +1,14 @@ +package space.autistic.radio.reflection + +import com.dylibso.chicory.runtime.ByteBufferMemory +import java.lang.invoke.MethodHandles +import java.nio.ByteBuffer + +fun ByteBufferMemory.getBuffer(): ByteBuffer { + return MemoryReflection.buffer.get(this) as ByteBuffer +} + +object MemoryReflection { + val buffer = MethodHandles.privateLookupIn(ByteBufferMemory::class.java, MethodHandles.lookup()) + .findVarHandle(ByteBufferMemory::class.java, "buffer", ByteBuffer::class.java) +} \ No newline at end of file diff --git a/src/main/kotlin/space/autistic/radio/wasm/Bindings.kt b/src/main/kotlin/space/autistic/radio/wasm/Bindings.kt index cc123a1..5ff4102 100644 --- a/src/main/kotlin/space/autistic/radio/wasm/Bindings.kt +++ b/src/main/kotlin/space/autistic/radio/wasm/Bindings.kt @@ -141,6 +141,11 @@ class Bindings { Value.EMPTY_VALUES } + Void::class.java -> HostFunction(module, name, wasmParameters, emptyList()) { instance, args -> + handle.invokeExact(instance, args) + throw IllegalStateException("unreachable") + } + Int::class.java -> HostFunction(module, name, wasmParameters, listOf(ValueType.I32)) { instance, args -> val result: Int = handle.invokeExact(instance, args) as Int longArrayOf(result.toLong()) diff --git a/src/main/kotlin/space/autistic/radio/wasm/WasmExitException.kt b/src/main/kotlin/space/autistic/radio/wasm/WasmExitException.kt new file mode 100644 index 0000000..43c08be --- /dev/null +++ b/src/main/kotlin/space/autistic/radio/wasm/WasmExitException.kt @@ -0,0 +1,4 @@ +package space.autistic.radio.wasm + +class WasmExitException(val status: Int) : Exception() { +} -- cgit 1.4.1