Class PanamaInstance

java.lang.Object
ai.tegmentum.wasmtime4j.panama.PanamaInstance
All Implemented Interfaces:
ai.tegmentum.wasmtime4j.Instance, Closeable, AutoCloseable

public final class PanamaInstance extends Object implements ai.tegmentum.wasmtime4j.Instance
Panama FFI implementation of WebAssembly Instance.
Since:
1.0.0
  • Constructor Details

    • PanamaInstance

      public PanamaInstance(PanamaModule module, PanamaStore store) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Creates a new Panama instance.
      Parameters:
      module - the module to instantiate
      store - the store to create the instance in
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if instance creation fails
  • Method Details

    • clearCallContext

      public static void clearCallContext()
      Releases the current thread's call context arena, freeing its off-heap memory.

      This is useful when a thread-pool worker is about to be returned to the pool and will not make further WASM calls for a while. A new context will be lazily created on the next call.

    • getFunction

      public Optional<ai.tegmentum.wasmtime4j.WasmFunction> getFunction(String name)
      Specified by:
      getFunction in interface ai.tegmentum.wasmtime4j.Instance
    • getGlobal

      public Optional<ai.tegmentum.wasmtime4j.WasmGlobal> getGlobal(String name)
      Specified by:
      getGlobal in interface ai.tegmentum.wasmtime4j.Instance
    • getTag

      public Optional<ai.tegmentum.wasmtime4j.memory.Tag> getTag(String name)
      Specified by:
      getTag in interface ai.tegmentum.wasmtime4j.Instance
    • getMemory

      public Optional<ai.tegmentum.wasmtime4j.WasmMemory> getMemory(String name)
      Specified by:
      getMemory in interface ai.tegmentum.wasmtime4j.Instance
    • getTable

      public Optional<ai.tegmentum.wasmtime4j.WasmTable> getTable(String name)
      Specified by:
      getTable in interface ai.tegmentum.wasmtime4j.Instance
    • getSharedMemory

      public Optional<ai.tegmentum.wasmtime4j.WasmMemory> getSharedMemory(String name)
      Specified by:
      getSharedMemory in interface ai.tegmentum.wasmtime4j.Instance
    • getExportNames

      public String[] getExportNames()
      Specified by:
      getExportNames in interface ai.tegmentum.wasmtime4j.Instance
    • debugFunction

      public Optional<ai.tegmentum.wasmtime4j.WasmFunction> debugFunction(int functionIndex)
      Specified by:
      debugFunction in interface ai.tegmentum.wasmtime4j.Instance
    • debugGlobal

      public Optional<ai.tegmentum.wasmtime4j.WasmGlobal> debugGlobal(int globalIndex)
      Specified by:
      debugGlobal in interface ai.tegmentum.wasmtime4j.Instance
    • debugMemory

      public Optional<ai.tegmentum.wasmtime4j.WasmMemory> debugMemory(int memoryIndex)
      Specified by:
      debugMemory in interface ai.tegmentum.wasmtime4j.Instance
    • debugSharedMemory

      public Optional<ai.tegmentum.wasmtime4j.WasmMemory> debugSharedMemory(int memoryIndex)
      Specified by:
      debugSharedMemory in interface ai.tegmentum.wasmtime4j.Instance
    • debugTable

      public Optional<ai.tegmentum.wasmtime4j.WasmTable> debugTable(int tableIndex)
      Specified by:
      debugTable in interface ai.tegmentum.wasmtime4j.Instance
    • debugTag

      public Optional<ai.tegmentum.wasmtime4j.memory.Tag> debugTag(int tagIndex)
      Specified by:
      debugTag in interface ai.tegmentum.wasmtime4j.Instance
    • hasExport

      public boolean hasExport(String name)
      Specified by:
      hasExport in interface ai.tegmentum.wasmtime4j.Instance
    • getExport

      public Optional<ai.tegmentum.wasmtime4j.Extern> getExport(String name)
      Specified by:
      getExport in interface ai.tegmentum.wasmtime4j.Instance
    • getExport

      public Optional<ai.tegmentum.wasmtime4j.Extern> getExport(ai.tegmentum.wasmtime4j.Store store, ai.tegmentum.wasmtime4j.ModuleExport moduleExport) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Specified by:
      getExport in interface ai.tegmentum.wasmtime4j.Instance
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException
    • getModule

      public ai.tegmentum.wasmtime4j.Module getModule()
      Specified by:
      getModule in interface ai.tegmentum.wasmtime4j.Instance
    • getStore

      public ai.tegmentum.wasmtime4j.Store getStore()
      Specified by:
      getStore in interface ai.tegmentum.wasmtime4j.Instance
    • getNativeInstance

      public MemorySegment getNativeInstance()
      Gets the native instance pointer.
      Returns:
      the native instance pointer
    • callFunction

      public ai.tegmentum.wasmtime4j.WasmValue[] callFunction(String functionName, ai.tegmentum.wasmtime4j.WasmValue... params) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Specified by:
      callFunction in interface ai.tegmentum.wasmtime4j.Instance
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException
    • callFunctionAsync

      public ai.tegmentum.wasmtime4j.WasmValue[] callFunctionAsync(String functionName, ai.tegmentum.wasmtime4j.WasmValue... params) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Calls a function using Wasmtime's native async call path.

      Gets the function handle by name, calls it via Func::call_async, and frees the handle. This enables proper async host function interleaving for stores configured with async support.

      Parameters:
      functionName - the function name
      params - the parameters to pass
      Returns:
      the results
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if the call fails
    • isValid

      public boolean isValid()
      Specified by:
      isValid in interface ai.tegmentum.wasmtime4j.Instance
    • callI32I32ToI32Fast

      public int callI32I32ToI32Fast(String functionName, int arg1, int arg2) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Fast-path call for functions with two i32 parameters returning i32. Bypasses WasmValue boxing entirely by writing primitives directly to native memory.
      Parameters:
      functionName - the name of the function to call
      arg1 - the first i32 argument
      arg2 - the second i32 argument
      Returns:
      the i32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI32ToI32Fast

      public int callI32ToI32Fast(String functionName, int arg) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Fast-path call for functions with one i32 parameter returning i32. Bypasses WasmValue boxing entirely by writing primitives directly to native memory.
      Parameters:
      functionName - the name of the function to call
      arg - the i32 argument
      Returns:
      the i32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callToI32Fast

      public int callToI32Fast(String functionName) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Fast-path call for functions with no parameters returning i32. Bypasses WasmValue boxing entirely.
      Parameters:
      functionName - the name of the function to call
      Returns:
      the i32 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callVoidFast

      public void callVoidFast(String functionName) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Fast-path call for functions with no parameters and no return value. Bypasses WasmValue boxing entirely.
      Parameters:
      functionName - the name of the function to call
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callI64ToI64Fast

      public long callI64ToI64Fast(String functionName, long arg) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Fast-path call for functions with one i64 parameter returning i64. Bypasses WasmValue boxing entirely.
      Parameters:
      functionName - the name of the function to call
      arg - the i64 argument
      Returns:
      the i64 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • callF64ToF64Fast

      public double callF64ToF64Fast(String functionName, double arg) throws ai.tegmentum.wasmtime4j.exception.WasmException
      Fast-path call for functions with one f64 parameter returning f64. Bypasses WasmValue boxing entirely.
      Parameters:
      functionName - the name of the function to call
      arg - the f64 argument
      Returns:
      the f64 result
      Throws:
      ai.tegmentum.wasmtime4j.exception.WasmException - if function execution fails
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface ai.tegmentum.wasmtime4j.Instance