Class PanamaHostFunction
- All Implemented Interfaces:
ai.tegmentum.wasmtime4j.WasmFunction
This class provides the foundation for callback upcall handles, allowing Java code to expose functions that can be imported and called by WebAssembly modules. It uses Panama's upcall stub mechanism for optimal performance while maintaining type safety and error handling.
The implementation uses a registry pattern to track active host functions and prevent premature garbage collection of callback stubs, ensuring memory safety across the FFI boundary.
- Since:
- 1.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for host function implementations. -
Constructor Summary
ConstructorsConstructorDescriptionPanamaHostFunction(String functionName, ai.tegmentum.wasmtime4j.type.FunctionType functionType, PanamaHostFunction.HostFunctionCallback callback, ai.tegmentum.wasmtime4j.func.HostFunction implementation, PanamaStore store, ArenaResourceManager arenaManager) Creates a new host function with the specified signature and implementation.PanamaHostFunction(String functionName, ai.tegmentum.wasmtime4j.type.FunctionType functionType, PanamaHostFunction.HostFunctionCallback callback, ai.tegmentum.wasmtime4j.func.HostFunction implementation, PanamaStore store, ArenaResourceManager arenaManager, boolean unchecked) Creates a new host function with the specified signature and implementation. -
Method Summary
Modifier and TypeMethodDescriptionai.tegmentum.wasmtime4j.WasmValue[]call(ai.tegmentum.wasmtime4j.WasmValue... params) CompletableFuture<ai.tegmentum.wasmtime4j.WasmValue[]> callAsync(ai.tegmentum.wasmtime4j.WasmValue... params) voidclose()Closes this host function and releases its resources.longGets the native function reference registry ID for table operations.Gets the native function handle for use in WebAssembly import operations.ai.tegmentum.wasmtime4j.type.FunctionTypegetName()Gets the upcall stub for direct FFI integration.booleanisClosed()Checks if this host function has been closed.longtoString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ai.tegmentum.wasmtime4j.WasmFunction
callF64ToF64, callI32I32ToI32, callI32ToI32, callI64ToI64, callSingleAsync, callToI32, callUnchecked, callVoid, getNativeHandle, matchesFuncType, matchesType, typed
-
Constructor Details
-
PanamaHostFunction
public PanamaHostFunction(String functionName, ai.tegmentum.wasmtime4j.type.FunctionType functionType, PanamaHostFunction.HostFunctionCallback callback, ai.tegmentum.wasmtime4j.func.HostFunction implementation, PanamaStore store, ArenaResourceManager arenaManager) throws ai.tegmentum.wasmtime4j.exception.WasmException Creates a new host function with the specified signature and implementation.- Parameters:
functionName- the name of the function (for debugging/logging)functionType- the WebAssembly function type signaturecallback- the Java implementation of the functionimplementation- the original HostFunction implementation (may be null for direct callbacks)store- the store this host function belongs to (may be null for direct callbacks)arenaManager- the arena resource manager for memory lifecycle- Throws:
ai.tegmentum.wasmtime4j.exception.WasmException- if host function creation failsIllegalArgumentException- if any parameter is null
-
PanamaHostFunction
public PanamaHostFunction(String functionName, ai.tegmentum.wasmtime4j.type.FunctionType functionType, PanamaHostFunction.HostFunctionCallback callback, ai.tegmentum.wasmtime4j.func.HostFunction implementation, PanamaStore store, ArenaResourceManager arenaManager, boolean unchecked) throws ai.tegmentum.wasmtime4j.exception.WasmException Creates a new host function with the specified signature and implementation.- Parameters:
functionName- the name of the function (for debugging/logging)functionType- the WebAssembly function type signaturecallback- the Java implementation of the functionimplementation- the original HostFunction implementation (may be null for direct callbacks)store- the store this host function belongs to (may be null for direct callbacks)arenaManager- the arena resource manager for memory lifecycleunchecked- if true, uses Func::new_unchecked for better performance- Throws:
ai.tegmentum.wasmtime4j.exception.WasmException- if host function creation failsIllegalArgumentException- if any parameter is null
-
-
Method Details
-
call
public ai.tegmentum.wasmtime4j.WasmValue[] call(ai.tegmentum.wasmtime4j.WasmValue... params) throws ai.tegmentum.wasmtime4j.exception.WasmException - Specified by:
callin interfaceai.tegmentum.wasmtime4j.WasmFunction- Throws:
ai.tegmentum.wasmtime4j.exception.WasmException
-
getFunctionType
public ai.tegmentum.wasmtime4j.type.FunctionType getFunctionType()- Specified by:
getFunctionTypein interfaceai.tegmentum.wasmtime4j.WasmFunction
-
getName
- Specified by:
getNamein interfaceai.tegmentum.wasmtime4j.WasmFunction
-
toRawFuncRef
public long toRawFuncRef() throws ai.tegmentum.wasmtime4j.exception.WasmException- Specified by:
toRawFuncRefin interfaceai.tegmentum.wasmtime4j.WasmFunction- Throws:
ai.tegmentum.wasmtime4j.exception.WasmException
-
callAsync
public CompletableFuture<ai.tegmentum.wasmtime4j.WasmValue[]> callAsync(ai.tegmentum.wasmtime4j.WasmValue... params) - Specified by:
callAsyncin interfaceai.tegmentum.wasmtime4j.WasmFunction
-
getFunctionHandle
Gets the native function handle for use in WebAssembly import operations.This handle can be passed to WebAssembly runtime functions to create function imports that reference this host function.
- Returns:
- the native function handle
- Throws:
IllegalStateException- if the host function has been closed
-
getFuncRefId
public long getFuncRefId()Gets the native function reference registry ID for table operations.This ID is used to look up the function in the native reference registry when setting table elements.
- Returns:
- the function reference ID, or 0 if not registered
-
getUpcallStub
Gets the upcall stub for direct FFI integration.This method is intended for internal use by Panama FFI components and should not be used by application code.
- Returns:
- the upcall stub memory segment
- Throws:
IllegalStateException- if the host function has been closed
-
isClosed
public boolean isClosed()Checks if this host function has been closed.- Returns:
- true if the host function has been closed
-
close
public void close() throws ai.tegmentum.wasmtime4j.exception.WasmExceptionCloses this host function and releases its resources.- Throws:
ai.tegmentum.wasmtime4j.exception.WasmException
-
toString
-