Class PanamaFunctionReference
- All Implemented Interfaces:
ai.tegmentum.wasmtime4j.func.FunctionReference
Function references enable dynamic function dispatch and callback mechanisms in WebAssembly programs. They can be passed as parameters, stored in tables, and called indirectly through call_indirect instructions.
This implementation provides thread-safe management of function references with comprehensive defensive programming practices to prevent JVM crashes and resource leaks.
Key Features:
- Thread-safe function reference creation and management
- Support for both host functions and WebAssembly functions
- Automatic resource cleanup and lifecycle management
- Parameter and return value marshalling via Panama FFI
- Error handling and exception propagation
- Since:
- 1.0.0
-
Constructor Summary
ConstructorsConstructorDescriptionPanamaFunctionReference(ai.tegmentum.wasmtime4j.func.HostFunction hostFunction, ai.tegmentum.wasmtime4j.type.FunctionType functionType, PanamaStore store, ArenaResourceManager arenaManager) Creates a new function reference from a host function.PanamaFunctionReference(ai.tegmentum.wasmtime4j.WasmFunction wasmFunction, PanamaStore store, ArenaResourceManager arenaManager) Creates a new function reference from a WebAssembly function. -
Method Summary
Modifier and TypeMethodDescriptionai.tegmentum.wasmtime4j.WasmValue[]call(ai.tegmentum.wasmtime4j.WasmValue... params) voidclose()Closes this function reference and releases its resources.static ai.tegmentum.wasmtime4j.func.FunctionReferencegetFunctionReferenceById(long functionReferenceId) Retrieves a function reference from the registry by ID.ai.tegmentum.wasmtime4j.type.FunctionTypestatic MemorySegmentGets the global callback stub address for use with native function reference creation.longgetId()getName()longGets the native registry ID for this function reference.Gets the upcall stub for this function reference.static intinvokeNativeCallback(long callbackId, MemorySegment paramsPtr, int paramsLen, MemorySegment resultsPtr, int resultsLen, MemorySegment errorMsgPtr, int errorMsgLen) Native callback entry point for function reference invocation.booleanChecks if this function reference is a host function.booleanisValid()booleanChecks if this function reference is a WebAssembly function.longReturns the registry ID as a long value for storage in globals or tables.toString()
-
Constructor Details
-
PanamaFunctionReference
public PanamaFunctionReference(ai.tegmentum.wasmtime4j.func.HostFunction hostFunction, ai.tegmentum.wasmtime4j.type.FunctionType functionType, PanamaStore store, ArenaResourceManager arenaManager) throws ai.tegmentum.wasmtime4j.exception.WasmException Creates a new function reference from a host function.- Parameters:
hostFunction- the host function implementationfunctionType- the WebAssembly function type signaturestore- the store this function reference belongs toarenaManager- the arena resource manager for memory lifecycle- Throws:
ai.tegmentum.wasmtime4j.exception.WasmException- if function reference creation failsIllegalArgumentException- if any required parameter is null
-
PanamaFunctionReference
public PanamaFunctionReference(ai.tegmentum.wasmtime4j.WasmFunction wasmFunction, PanamaStore store, ArenaResourceManager arenaManager) throws ai.tegmentum.wasmtime4j.exception.WasmException Creates a new function reference from a WebAssembly function.- Parameters:
wasmFunction- the WebAssembly functionstore- the store this function reference belongs toarenaManager- the arena resource manager for memory lifecycle- Throws:
ai.tegmentum.wasmtime4j.exception.WasmException- if function reference creation failsIllegalArgumentException- if any required parameter is null
-
-
Method Details
-
getFunctionType
public ai.tegmentum.wasmtime4j.type.FunctionType getFunctionType()- Specified by:
getFunctionTypein interfaceai.tegmentum.wasmtime4j.func.FunctionReference
-
call
public ai.tegmentum.wasmtime4j.WasmValue[] call(ai.tegmentum.wasmtime4j.WasmValue... params) throws ai.tegmentum.wasmtime4j.exception.WasmException - Specified by:
callin interfaceai.tegmentum.wasmtime4j.func.FunctionReference- Throws:
ai.tegmentum.wasmtime4j.exception.WasmException
-
getName
- Specified by:
getNamein interfaceai.tegmentum.wasmtime4j.func.FunctionReference
-
isValid
public boolean isValid()- Specified by:
isValidin interfaceai.tegmentum.wasmtime4j.func.FunctionReference
-
getId
public long getId()- Specified by:
getIdin interfaceai.tegmentum.wasmtime4j.func.FunctionReference
-
isHostFunction
public boolean isHostFunction()Checks if this function reference is a host function.- Returns:
- true if this is a host function reference
-
isWasmFunction
public boolean isWasmFunction()Checks if this function reference is a WebAssembly function.- Returns:
- true if this is a WebAssembly function reference
-
getUpcallStub
Gets the upcall stub for this function reference.This is used when passing the function reference to native code.
- Returns:
- the upcall stub memory segment, or null for WebAssembly functions
-
longValue
public long longValue()Returns the registry ID as a long value for storage in globals or tables.- Returns:
- the registry ID
-
close
public void close() throws ai.tegmentum.wasmtime4j.exception.WasmExceptionCloses this function reference and releases its resources.- Throws:
ai.tegmentum.wasmtime4j.exception.WasmException- if closing fails
-
getNativeRegistryId
public long getNativeRegistryId()Gets the native registry ID for this function reference.This ID is used when setting funcref globals or storing in tables.
- Returns:
- the native registry ID, or -1 if not registered
-
getFunctionReferenceById
public static ai.tegmentum.wasmtime4j.func.FunctionReference getFunctionReferenceById(long functionReferenceId) Retrieves a function reference from the registry by ID.This is the public accessor for looking up function references during value unmarshalling.
- Parameters:
functionReferenceId- the function reference ID- Returns:
- the function reference, or null if not found
-
getGlobalCallbackStub
Gets the global callback stub address for use with native function reference creation.This method returns a memory segment pointing to a static callback function that can be passed to native code. When the native code calls this callback, it dispatches to the appropriate Java function reference based on the callback ID.
- Returns:
- the global callback stub memory segment
-
invokeNativeCallback
public static int invokeNativeCallback(long callbackId, MemorySegment paramsPtr, int paramsLen, MemorySegment resultsPtr, int resultsLen, MemorySegment errorMsgPtr, int errorMsgLen) Native callback entry point for function reference invocation.This static method is called by native code through the global callback stub. It unmarshals parameters, invokes the function reference, and marshals results back.
- Parameters:
callbackId- the function reference IDparamsPtr- pointer to the parameter arrayparamsLen- number of parametersresultsPtr- pointer to the result bufferresultsLen- expected number of resultserrorMsgPtr- pointer to error message buffer (for writing on failure)errorMsgLen- size of error message buffer- Returns:
- 0 on success, non-zero on error
-
toString
-