Class NativeExecutionBindings
java.lang.Object
ai.tegmentum.wasmtime4j.panama.NativeExecutionBindings
Native function bindings for execution-related operations.
Provides type-safe wrappers for pooling allocator native functions.
-
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddFunctionBinding(String functionName, FunctionDescriptor descriptor) Registers a native function binding with a given descriptor.<T> TcallNativeFunction(String functionName, Class<T> returnType, Object... args) Calls a native function with type safety.protected Object[]extractValueForNative(ai.tegmentum.wasmtime4j.WasmValue value) Extracts value components from WasmValue for passing to native code.protected ai.tegmentum.wasmtime4j.panama.NativeBindingsBase.FunctionBindinggetFunctionBinding(String functionName) Gets a registered function binding by name.static NativeExecutionBindingsGets the singleton instance.booleanChecks if the bindings are initialized and available.protected voidMarks this binding set as initialized.booleanpoolingAllocatorAllocateInstance(MemorySegment allocatorPtr, MemorySegment instanceIdOut) Allocates an instance from the pool.poolingAllocatorCreateFromJson(byte[] jsonBytes) Creates a pooling allocator from JSON configuration bytes.poolingAllocatorCreateWithConfig(int instancePoolSize, long maxMemoryPerInstance, int stackSize, int maxStacks, int maxTablesPerInstance, int maxTables, boolean memoryDecommitEnabled, boolean poolWarmingEnabled, float poolWarmingPercentage) Creates a new pooling allocator with custom configuration.voidpoolingAllocatorDestroy(MemorySegment allocatorPtr) Destroys a pooling allocator.booleanpoolingAllocatorGetStatistics(MemorySegment allocatorPtr, MemorySegment statsOut) Gets pool statistics.booleanpoolingAllocatorPerformMaintenance(MemorySegment allocatorPtr) Performs maintenance operations on the pools.booleanpoolingAllocatorReleaseInstance(MemorySegment allocatorPtr, long instanceId) Releases an instance back to the pool.booleanpoolingAllocatorResetStatistics(MemorySegment allocatorPtr) Resets pool statistics.booleanpoolingAllocatorReuseInstance(MemorySegment allocatorPtr, long instanceId) Reuses an existing instance from the pool.booleanpoolingAllocatorWarmPools(MemorySegment allocatorPtr) Warms up the pools by pre-allocating resources.protected voidvalidatePointer(MemorySegment pointer, String name) Validates that a pointer is not null.protected voidvalidateSize(long size, String name) Validates that a size is positive.
-
Method Details
-
getInstance
Gets the singleton instance.- Returns:
- the singleton instance
- Throws:
RuntimeException- if initialization fails
-
poolingAllocatorCreateWithConfig
public MemorySegment poolingAllocatorCreateWithConfig(int instancePoolSize, long maxMemoryPerInstance, int stackSize, int maxStacks, int maxTablesPerInstance, int maxTables, boolean memoryDecommitEnabled, boolean poolWarmingEnabled, float poolWarmingPercentage) Creates a new pooling allocator with custom configuration.- Parameters:
instancePoolSize- the number of instances in the poolmaxMemoryPerInstance- maximum memory per instance in bytesstackSize- stack size for WebAssembly executionmaxStacks- maximum number of stacksmaxTablesPerInstance- maximum tables per instancemaxTables- maximum total tablesmemoryDecommitEnabled- whether memory decommit is enabledpoolWarmingEnabled- whether pool warming is enabledpoolWarmingPercentage- pool warming percentage (0.0 to 1.0)- Returns:
- pointer to the allocator, or null on failure
-
poolingAllocatorCreateFromJson
Creates a pooling allocator from JSON configuration bytes.All fields in the JSON map directly to wasmtime's PoolingAllocationConfig API. This is the preferred creation path as it wires all configuration options.
- Parameters:
jsonBytes- UTF-8 JSON configuration bytes- Returns:
- pointer to the allocator, or null on failure
-
poolingAllocatorAllocateInstance
public boolean poolingAllocatorAllocateInstance(MemorySegment allocatorPtr, MemorySegment instanceIdOut) Allocates an instance from the pool.- Parameters:
allocatorPtr- pointer to the allocatorinstanceIdOut- pointer to store the instance ID- Returns:
- true on success, false on failure
-
poolingAllocatorReuseInstance
Reuses an existing instance from the pool.- Parameters:
allocatorPtr- pointer to the allocatorinstanceId- the instance ID to reuse- Returns:
- true on success, false on failure
-
poolingAllocatorReleaseInstance
Releases an instance back to the pool.- Parameters:
allocatorPtr- pointer to the allocatorinstanceId- the instance ID to release- Returns:
- true on success, false on failure
-
poolingAllocatorGetStatistics
Gets pool statistics.- Parameters:
allocatorPtr- pointer to the allocatorstatsOut- pointer to store the statistics- Returns:
- true on success, false on failure
-
poolingAllocatorResetStatistics
Resets pool statistics.- Parameters:
allocatorPtr- pointer to the allocator- Returns:
- true on success, false on failure
-
poolingAllocatorWarmPools
Warms up the pools by pre-allocating resources.- Parameters:
allocatorPtr- pointer to the allocator- Returns:
- true on success, false on failure
-
poolingAllocatorPerformMaintenance
Performs maintenance operations on the pools.- Parameters:
allocatorPtr- pointer to the allocator- Returns:
- true on success, false on failure
-
poolingAllocatorDestroy
Destroys a pooling allocator.- Parameters:
allocatorPtr- pointer to the allocator
-
markInitialized
protected void markInitialized()Marks this binding set as initialized. Subclasses should call this after registering all function bindings and initializing hot-path MethodHandles. -
isInitialized
public boolean isInitialized()Checks if the bindings are initialized and available.- Returns:
- true if initialized, false otherwise
-
addFunctionBinding
Registers a native function binding with a given descriptor.- Parameters:
functionName- the native function symbol namedescriptor- the Panama FunctionDescriptor for the native function
-
getFunctionBinding
protected ai.tegmentum.wasmtime4j.panama.NativeBindingsBase.FunctionBinding getFunctionBinding(String functionName) Gets a registered function binding by name.- Parameters:
functionName- the native function symbol name- Returns:
- the function binding, or null if not registered
-
callNativeFunction
Calls a native function with type safety.- Parameters:
functionName- the name of the functionreturnType- the expected return typeargs- the function arguments- Returns:
- the function result
- Throws:
IllegalStateException- if the function call fails
-
validatePointer
Validates that a pointer is not null.- Parameters:
pointer- the pointer to validatename- the name of the parameter for error messages- Throws:
IllegalArgumentException- if the pointer is null
-
validateSize
Validates that a size is positive.- Parameters:
size- the size to validatename- the name of the parameter for error messages- Throws:
IllegalArgumentException- if the size is not positive
-
extractValueForNative
Extracts value components from WasmValue for passing to native code.- Parameters:
value- the WasmValue to extract components from- Returns:
- array containing [i32Value, i64Value, f32Value, f64Value, refValue]
-