Skip to content

Kotlin library for high level playing with the at WASM runtime.

Notifications You must be signed in to change notification settings

NOctu1412/AxionEngine

Repository files navigation

Axion Engine

This library is used to get a good interopability between Kotlin and WebAssembly (especially with Rust but I think you can port the wasm_utils.rs to other languages).

Features

  • Loading WASM modules into Kotlin
  • Calling WASM exports function with Kotlin types
  • WASM Imports for calling Kotlin from WASM
  • Support for all primitive types + structures
  • Cross platform

Usage/Examples

Rust:

mod wasm_utils;

use wasm_utils::*;

extern "C" {
    pub fn kotlin_print(str: *mut String);
}

#[no_mangle]
pub unsafe fn test(x: [i64; 4]) -> f32 {
    kotlin_print(into_mut_ptr("Hello from Rust !".to_string()));
    x.len() as f32
}

Kotlin:

class KotlinPrintImport : WasmImport(
    "kotlin_print",
    EnumWasmType.VOID,             //return type
    arrayOf(EnumWasmType.STRING),  //args types
    { axionEngine, args ->         //callback
        val firstArgument = args[0].value as String

        println(firstArgument)

        null                        //for void functions, returning null is good
    }
)

fun main() {
    val wasmBin = File("rust.wasm").readBytes()

    val axionEngine = AxionEngine(wasmBin,
        KotlinPrintImport(),
        //...  (add all your imports)
    )

    val callResult = axionEngine.callExport<FloatWasmType>  //for void functions, do not put a type
        (
        "test",  //function name
        listOf(1L, 2L, 3L, 4L).toWasmType(axionEngine),
        //...   (add all your arguments)
        )

    println(callResult.value)
}

(see more examples in the kotlin test directory and in the rust example)

Authors

Thanks to

About

Kotlin library for high level playing with the at WASM runtime.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published