Class PanamaTypedFunc

java.lang.Object
ai.tegmentum.wasmtime4j.panama.PanamaTypedFunc
All Implemented Interfaces:
ai.tegmentum.wasmtime4j.func.TypedFunc, AutoCloseable

public final class PanamaTypedFunc extends Object implements ai.tegmentum.wasmtime4j.func.TypedFunc
Panama FFI implementation of typed WebAssembly function calls.

This class provides zero-cost typed function calls by caching type information and avoiding runtime type checking on each invocation. This is a significant performance optimization for frequently called functions with known signatures.

TypedFunc wraps a regular WasmFunction with compile-time type information, enabling:

  • Zero-cost abstraction - no runtime type checking overhead
  • Direct memory layout matching between Java and WebAssembly
  • Optimized parameter/result marshalling for common type combinations
  • Type safety through signature validation at creation time

Supported signatures:

  • () -> void
  • (i32) -> i32
  • (i32, i32) -> i32
  • (i32, i32, i32) -> i32
  • (i64) -> i64
  • (i64, i64) -> i64
  • (i64, i64, i64) -> i64
  • (f32) -> f32
  • (f32, f32) -> f32
  • (f64) -> f64
  • (f64, f64) -> f64

Example usage:


 WasmFunction func = instance.getFunction("add").get();
 PanamaTypedFunc typedAdd = new PanamaTypedFunc(func, "ii->i");
 int result = typedAdd.callI32I32ToI32(10, 20); // Returns 30
 
Since:
1.0.0
  • Nested Class Summary

    Nested classes/interfaces inherited from interface ai.tegmentum.wasmtime4j.func.TypedFunc

    ai.tegmentum.wasmtime4j.func.TypedFunc.TypedFunctionSupport
  • Constructor Summary

    Constructors
    Constructor
    Description
    PanamaTypedFunc(ai.tegmentum.wasmtime4j.WasmFunction func, String signature)
    Creates a typed function wrapper from a regular function.
  • Method Summary

    Modifier and Type
    Method
    Description
    float
    callF32F32F32ToF32(float param1, float param2, float param3)
    Calls a typed function with (f32, f32, f32) parameters and f32 result: (f32, f32, f32) -> f32.
    float
    callF32F32ToF32(float param1, float param2)
    Calls a typed function with (f32, f32) parameters and f32 result: (f32, f32) -> f32.
    float
    callF32I32ToF32(float param1, int param2)
    Calls a typed function with (f32, i32) parameters and f32 result: (f32, i32) -> f32.
    float
    callF32ToF32(float param)
    Calls a typed function with f32 parameter and f32 result: (f32) -> f32.
    double
    callF64F64F64ToF64(double param1, double param2, double param3)
    Calls a typed function with (f64, f64, f64) parameters and f64 result: (f64, f64, f64) -> f64.
    double
    callF64F64ToF64(double param1, double param2)
    Calls a typed function with (f64, f64) parameters and f64 result: (f64, f64) -> f64.
    double
    callF64ToF64(double param)
    Calls a typed function with f64 parameter and f64 result: (f64) -> f64.
    float
    callI32F32ToF32(int param1, float param2)
    Calls a typed function with (i32, f32) parameters and f32 result: (i32, f32) -> f32.
    int
    callI32I32I32ToI32(int param1, int param2, int param3)
    Calls a typed function with (i32, i32, i32) parameters and i32 result: (i32, i32, i32) -> i32.
    int
    callI32I32ToI32(int param1, int param2)
    Calls a typed function with (i32, i32) parameters and i32 result: (i32, i32) -> i32.
    long
    callI32I32ToI64(int param1, int param2)
    Calls a typed function with (i32, i32) parameters and i64 result: (i32, i32) -> i64.
    void
    callI32I32ToVoid(int param1, int param2)
    Calls a typed function with (i32, i32) parameters and void result: (i32, i32) -> ().
    int
    callI32ToI32(int param)
    Calls a typed function with i32 parameter and i32 result: (i32) -> i32.
    void
    callI32ToVoid(int param)
    Calls a typed function with i32 parameter and void result: (i32) -> ().
    long
    callI64I64I64ToI64(long param1, long param2, long param3)
    Calls a typed function with (i64, i64, i64) parameters and i64 result: (i64, i64, i64) -> i64.
    long
    callI64I64ToI64(long param1, long param2)
    Calls a typed function with (i64, i64) parameters and i64 result: (i64, i64) -> i64.
    void
    callI64I64ToVoid(long param1, long param2)
    Calls a typed function with (i64, i64) parameters and void result: (i64, i64) -> ().
    int
    callI64ToI32(long param)
    Calls a typed function with i64 parameter and i32 result: (i64) -> i32.
    long
    callI64ToI64(long param)
    Calls a typed function with i64 parameter and i64 result: (i64) -> i64.
    void
    callI64ToVoid(long param)
    Calls a typed function with i64 parameter and void result: (i64) -> ().
    void
    Calls a typed function with no parameters and no return value: () -> ().
    void
    Closes the typed function and releases resources.
    ai.tegmentum.wasmtime4j.WasmFunction
     
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface ai.tegmentum.wasmtime4j.func.TypedFunc

    callAsyncF32F32F32ToF32, callAsyncF32F32ToF32, callAsyncF32I32ToF32, callAsyncF32ToF32, callAsyncF64F64F64ToF64, callAsyncF64F64ToF64, callAsyncF64ToF64, callAsyncI32F32ToF32, callAsyncI32I32I32ToI32, callAsyncI32I32ToI32, callAsyncI32I32ToI64, callAsyncI32I32ToVoid, callAsyncI32ToI32, callAsyncI32ToVoid, callAsyncI64I64I64ToI64, callAsyncI64I64ToI64, callAsyncI64I64ToVoid, callAsyncI64ToI32, callAsyncI64ToI64, callAsyncI64ToVoid, callAsyncVoidToVoid
  • Constructor Details

    • PanamaTypedFunc

      public PanamaTypedFunc(ai.tegmentum.wasmtime4j.WasmFunction func, String signature)
      Creates a typed function wrapper from a regular function.

      The signature string encodes parameter and return types:

      • "i" = i32
      • "I" = i64
      • "f" = f32
      • "F" = f64
      • "v" = void/none

      Format: "params->results", e.g., "ii->i" for (i32, i32) -> i32

      Parameters:
      func - the function to wrap with type information
      signature - the signature string encoding parameter and return types
      Throws:
      IllegalArgumentException - if function or signature is invalid
  • Method Details

    • callVoidToVoid

      public void callVoidToVoid() throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with no parameters and no return value: () -> ().
      Specified by:
      callVoidToVoid in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI32ToVoid

      public void callI32ToVoid(int param) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with i32 parameter and void result: (i32) -> ().
      Specified by:
      callI32ToVoid in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param - the i32 parameter
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI32I32ToVoid

      public void callI32I32ToVoid(int param1, int param2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (i32, i32) parameters and void result: (i32, i32) -> ().
      Specified by:
      callI32I32ToVoid in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first i32 parameter
      param2 - the second i32 parameter
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI64ToVoid

      public void callI64ToVoid(long param) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with i64 parameter and void result: (i64) -> ().
      Specified by:
      callI64ToVoid in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param - the i64 parameter
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI64I64ToVoid

      public void callI64I64ToVoid(long param1, long param2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (i64, i64) parameters and void result: (i64, i64) -> ().
      Specified by:
      callI64I64ToVoid in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first i64 parameter
      param2 - the second i64 parameter
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI32ToI32

      public int callI32ToI32(int param) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with i32 parameter and i32 result: (i32) -> i32.
      Specified by:
      callI32ToI32 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param - the i32 parameter
      Returns:
      the i32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI32I32ToI32

      public int callI32I32ToI32(int param1, int param2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (i32, i32) parameters and i32 result: (i32, i32) -> i32.
      Specified by:
      callI32I32ToI32 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first i32 parameter
      param2 - the second i32 parameter
      Returns:
      the i32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI64ToI64

      public long callI64ToI64(long param) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with i64 parameter and i64 result: (i64) -> i64.
      Specified by:
      callI64ToI64 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param - the i64 parameter
      Returns:
      the i64 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI64I64ToI64

      public long callI64I64ToI64(long param1, long param2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (i64, i64) parameters and i64 result: (i64, i64) -> i64.
      Specified by:
      callI64I64ToI64 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first i64 parameter
      param2 - the second i64 parameter
      Returns:
      the i64 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callF32ToF32

      public float callF32ToF32(float param) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with f32 parameter and f32 result: (f32) -> f32.
      Specified by:
      callF32ToF32 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param - the f32 parameter
      Returns:
      the f32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callF64ToF64

      public double callF64ToF64(double param) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with f64 parameter and f64 result: (f64) -> f64.
      Specified by:
      callF64ToF64 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param - the f64 parameter
      Returns:
      the f64 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callF32F32ToF32

      public float callF32F32ToF32(float param1, float param2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (f32, f32) parameters and f32 result: (f32, f32) -> f32.
      Specified by:
      callF32F32ToF32 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first f32 parameter
      param2 - the second f32 parameter
      Returns:
      the f32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callF64F64ToF64

      public double callF64F64ToF64(double param1, double param2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (f64, f64) parameters and f64 result: (f64, f64) -> f64.
      Specified by:
      callF64F64ToF64 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first f64 parameter
      param2 - the second f64 parameter
      Returns:
      the f64 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI32I32I32ToI32

      public int callI32I32I32ToI32(int param1, int param2, int param3) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (i32, i32, i32) parameters and i32 result: (i32, i32, i32) -> i32.
      Specified by:
      callI32I32I32ToI32 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first i32 parameter
      param2 - the second i32 parameter
      param3 - the third i32 parameter
      Returns:
      the i32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI64I64I64ToI64

      public long callI64I64I64ToI64(long param1, long param2, long param3) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (i64, i64, i64) parameters and i64 result: (i64, i64, i64) -> i64.
      Specified by:
      callI64I64I64ToI64 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first i64 parameter
      param2 - the second i64 parameter
      param3 - the third i64 parameter
      Returns:
      the i64 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • getSignature

      public String getSignature()
      Specified by:
      getSignature in interface ai.tegmentum.wasmtime4j.func.TypedFunc
    • getFunction

      public ai.tegmentum.wasmtime4j.WasmFunction getFunction()
      Specified by:
      getFunction in interface ai.tegmentum.wasmtime4j.func.TypedFunc
    • callF32F32F32ToF32

      public float callF32F32F32ToF32(float param1, float param2, float param3) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (f32, f32, f32) parameters and f32 result: (f32, f32, f32) -> f32.
      Specified by:
      callF32F32F32ToF32 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first f32 parameter
      param2 - the second f32 parameter
      param3 - the third f32 parameter
      Returns:
      the f32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callF64F64F64ToF64

      public double callF64F64F64ToF64(double param1, double param2, double param3) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (f64, f64, f64) parameters and f64 result: (f64, f64, f64) -> f64.
      Specified by:
      callF64F64F64ToF64 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first f64 parameter
      param2 - the second f64 parameter
      param3 - the third f64 parameter
      Returns:
      the f64 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI32I32ToI64

      public long callI32I32ToI64(int param1, int param2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (i32, i32) parameters and i64 result: (i32, i32) -> i64.
      Specified by:
      callI32I32ToI64 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the first i32 parameter
      param2 - the second i32 parameter
      Returns:
      the i64 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI64ToI32

      public int callI64ToI32(long param) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with i64 parameter and i32 result: (i64) -> i32.
      Specified by:
      callI64ToI32 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param - the i64 parameter
      Returns:
      the i32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI32F32ToF32

      public float callI32F32ToF32(int param1, float param2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (i32, f32) parameters and f32 result: (i32, f32) -> f32.
      Specified by:
      callI32F32ToF32 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the i32 parameter
      param2 - the f32 parameter
      Returns:
      the f32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callF32I32ToF32

      public float callF32I32ToF32(float param1, int param2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a typed function with (f32, i32) parameters and f32 result: (f32, i32) -> f32.
      Specified by:
      callF32I32ToF32 in interface ai.tegmentum.wasmtime4j.func.TypedFunc
      Parameters:
      param1 - the f32 parameter
      param2 - the i32 parameter
      Returns:
      the f32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • close

      public void close()
      Closes the typed function and releases resources.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface ai.tegmentum.wasmtime4j.func.TypedFunc