All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.polywrap.wasm.WasmWrapper.kt Maven / Gradle / Ivy

There is a newer version: 0.10.4
Show newest version
package io.polywrap.wasm

import io.polywrap.core.Invoker
import io.polywrap.core.Wrapper
import uniffi.polywrap_native.FfiInvoker
import uniffi.polywrap_native.FfiWrapper
import uniffi.polywrap_native.ffiWrapperFromBytecode

/**
 * Represents a WebAssembly (Wasm) wrapper for executing Wasm code.
 *
 * @param ffiWrapper The underlying FFI wrapper instance.
 */
@OptIn(ExperimentalUnsignedTypes::class)
data class WasmWrapper(val ffiWrapper: FfiWrapper) : Wrapper, AutoCloseable {

    /**
     * Creates a new [WasmWrapper] instance with the given Wasm module.
     *
     * @param wasmModule The WebAssembly module as a ByteArray.
     */
    constructor(wasmModule: ByteArray) : this(
        ffiWrapperFromBytecode(wasmModule.asUByteArray().asList())
    )

    override fun invoke(
        method: String,
        args: List?,
        env: List?,
        invoker: FfiInvoker
    ): List = ffiWrapper.invoke(method, args, env, invoker)

    override fun invoke(
        method: String,
        args: ByteArray?,
        env: ByteArray?,
        invoker: Invoker
    ): Result = runCatching {
        ffiWrapper.invoke(
            method = method,
            args = args?.asUByteArray()?.toList(),
            env = env?.asUByteArray()?.toList(),
            invoker = invoker.ffiInvoker
        ).toUByteArray().asByteArray()
    }

    override fun close() = ffiWrapper.close()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy