Class NativeWasiBindings
Provides type-safe wrappers for all WASI context native functions including context lifecycle management, environment and argument configuration, stdio redirection, directory pre-opening, output capture, and linker integration.
-
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 NativeWasiBindingsGets the singleton instance.booleanChecks if the bindings are initialized and available.intlinkerAddWasi(MemorySegment linkerHandle) Adds WASI Preview 1 imports to a linker.protected voidMarks this binding set as initialized.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.Creates a new WASI context.voidwasiContextDestroy(MemorySegment contextHandle) Destroys a WASI context.intwasiContextEnableOutputCapture(MemorySegment contextHandle) Enables output capture for stdout and stderr.intwasiContextGetArguments(MemorySegment contextHandle, MemorySegment outPtr, MemorySegment outLen) Gets the arguments from the WASI context.intwasiContextGetEnvironment(MemorySegment contextHandle, MemorySegment outPtr, MemorySegment outLen) Gets the environment variables from the WASI context.wasiContextGetStderrCapture(MemorySegment contextHandle, MemorySegment lengthOut) Gets captured stderr data.wasiContextGetStdoutCapture(MemorySegment contextHandle, MemorySegment lengthOut) Gets captured stdout data.intwasiContextHasStderrCapture(MemorySegment contextHandle) Checks if stderr capture is enabled.intwasiContextHasStdoutCapture(MemorySegment contextHandle) Checks if stdout capture is enabled.intwasiContextInheritArgs(MemorySegment contextHandle) Inherits host command-line arguments.intwasiContextInheritEnv(MemorySegment contextHandle) Inherits all host environment variables.intwasiContextInheritStdio(MemorySegment contextHandle) Inherits host stdio streams.intwasiContextPreopenDir(MemorySegment contextHandle, MemorySegment hostPath, MemorySegment guestPath) Adds a pre-opened directory with full permissions.intwasiContextPreopenDirReadonly(MemorySegment contextHandle, MemorySegment hostPath, MemorySegment guestPath) Adds a pre-opened directory with read-only permissions.intwasiContextPreopenDirWithPerms(MemorySegment contextHandle, MemorySegment hostPath, MemorySegment guestPath, int canRead, int canWrite, int canCreate) Adds a pre-opened directory with custom permissions.intwasiContextRebuild(MemorySegment contextHandle) Rebuilds the WASI context after configuration changes.intwasiContextSetAllowBlocking(MemorySegment contextHandle, int allow) Sets whether blocking the current thread is allowed.intwasiContextSetArgv(MemorySegment contextHandle, MemorySegment args, long argCount) Sets command line arguments for the WASI context.intwasiContextSetEnv(MemorySegment contextHandle, MemorySegment key, MemorySegment value) Sets an environment variable in the WASI context.intwasiContextSetInsecureRandomSeed(MemorySegment contextHandle, long seedLo, long seedHi) Sets the insecure random seed for deterministic testing.intwasiContextSetNetworkConfig(MemorySegment contextHandle, int allowNetwork, int allowTcp, int allowUdp, int allowIpNameLookup) Sets network configuration on a WASI context.intwasiContextSetStderr(MemorySegment contextHandle, MemorySegment path) Sets stderr to write to a file.intwasiContextSetStderrAppend(MemorySegment contextHandle, MemorySegment path) Sets stderr to append to a file.intwasiContextSetStdin(MemorySegment contextHandle, MemorySegment path) Sets stdin to read from a file.intwasiContextSetStdinBytes(MemorySegment contextHandle, MemorySegment dataPtr, long dataLen) Sets stdin from binary data buffer (supports binary data with null bytes).intwasiContextSetStdout(MemorySegment contextHandle, MemorySegment path) Sets stdout to write to a file.intwasiContextSetStdoutAppend(MemorySegment contextHandle, MemorySegment path) Sets stdout to append to a file.intwasiCtxAddToStore(MemorySegment contextHandle, MemorySegment storeHandle) Adds a WASI context to a Store.voidwasiFreeCaptureBuffer(MemorySegment buffer, long length) Frees a capture buffer allocated bywasiContextGetStdoutCapture(MemorySegment, MemorySegment)orwasiContextGetStderrCapture(MemorySegment, MemorySegment).
-
Method Details
-
getInstance
Gets the singleton instance.- Returns:
- the singleton instance
- Throws:
RuntimeException- if initialization fails
-
wasiContextCreate
Creates a new WASI context.- Returns:
- pointer to the WASI context, or null on failure
-
wasiContextDestroy
Destroys a WASI context.- Parameters:
contextHandle- the WASI context handle
-
wasiContextSetArgv
Sets command line arguments for the WASI context.- Parameters:
contextHandle- the WASI context handleargs- pointer to array of C stringsargCount- number of arguments- Returns:
- 0 on success, non-zero on error
-
wasiContextSetEnv
Sets an environment variable in the WASI context.- Parameters:
contextHandle- the WASI context handlekey- the environment variable name (C string)value- the environment variable value (C string)- Returns:
- 0 on success, non-zero on error
-
wasiContextInheritEnv
Inherits all host environment variables.- Parameters:
contextHandle- the WASI context handle- Returns:
- 0 on success, non-zero on error
-
wasiContextInheritArgs
Inherits host command-line arguments.- Parameters:
contextHandle- the WASI context handle- Returns:
- 0 on success, non-zero on error
-
wasiContextGetEnvironment
public int wasiContextGetEnvironment(MemorySegment contextHandle, MemorySegment outPtr, MemorySegment outLen) Gets the environment variables from the WASI context.Returns environment as "key=value\n" pairs in the output buffer. The caller must free the buffer using
wasiFreeCaptureBuffer(MemorySegment, long).- Parameters:
contextHandle- the WASI context handleoutPtr- output pointer for the data bufferoutLen- output pointer for the data length- Returns:
- 0 on success, non-zero on error
-
wasiContextGetArguments
public int wasiContextGetArguments(MemorySegment contextHandle, MemorySegment outPtr, MemorySegment outLen) Gets the arguments from the WASI context.Returns arguments as "arg1\narg2\n..." in the output buffer. The caller must free the buffer using
wasiFreeCaptureBuffer(MemorySegment, long).- Parameters:
contextHandle- the WASI context handleoutPtr- output pointer for the data bufferoutLen- output pointer for the data length- Returns:
- 0 on success, non-zero on error
-
wasiContextInheritStdio
Inherits host stdio streams.- Parameters:
contextHandle- the WASI context handle- Returns:
- 0 on success, non-zero on error
-
wasiContextSetStdin
Sets stdin to read from a file.- Parameters:
contextHandle- the WASI context handlepath- the file path (C string)- Returns:
- 0 on success, non-zero on error
-
wasiContextSetStdinBytes
public int wasiContextSetStdinBytes(MemorySegment contextHandle, MemorySegment dataPtr, long dataLen) Sets stdin from binary data buffer (supports binary data with null bytes).- Parameters:
contextHandle- the WASI context handledataPtr- pointer to the binary datadataLen- length of the data in bytes- Returns:
- 0 on success, non-zero on error
-
wasiContextSetStdout
Sets stdout to write to a file.- Parameters:
contextHandle- the WASI context handlepath- the file path (C string)- Returns:
- 0 on success, non-zero on error
-
wasiContextSetStderr
Sets stderr to write to a file.- Parameters:
contextHandle- the WASI context handlepath- the file path (C string)- Returns:
- 0 on success, non-zero on error
-
wasiContextSetStdoutAppend
Sets stdout to append to a file.- Parameters:
contextHandle- the WASI context handlepath- the file path (C string)- Returns:
- 0 on success, non-zero on error
-
wasiContextSetStderrAppend
Sets stderr to append to a file.- Parameters:
contextHandle- the WASI context handlepath- the file path (C string)- Returns:
- 0 on success, non-zero on error
-
wasiContextPreopenDir
public int wasiContextPreopenDir(MemorySegment contextHandle, MemorySegment hostPath, MemorySegment guestPath) Adds a pre-opened directory with full permissions.- Parameters:
contextHandle- the WASI context handlehostPath- the host filesystem path (C string)guestPath- the guest path visible to WASI module (C string)- Returns:
- 0 on success, non-zero on error
-
wasiContextPreopenDirReadonly
public int wasiContextPreopenDirReadonly(MemorySegment contextHandle, MemorySegment hostPath, MemorySegment guestPath) Adds a pre-opened directory with read-only permissions.- Parameters:
contextHandle- the WASI context handlehostPath- the host filesystem path (C string)guestPath- the guest path visible to WASI module (C string)- Returns:
- 0 on success, non-zero on error
-
wasiContextPreopenDirWithPerms
public int wasiContextPreopenDirWithPerms(MemorySegment contextHandle, MemorySegment hostPath, MemorySegment guestPath, int canRead, int canWrite, int canCreate) Adds a pre-opened directory with custom permissions.- Parameters:
contextHandle- the WASI context handlehostPath- the host filesystem path (C string)guestPath- the guest path visible to WASI module (C string)canRead- whether reading is allowedcanWrite- whether writing is allowedcanCreate- whether file creation is allowed- Returns:
- 0 on success, non-zero on error
-
wasiContextEnableOutputCapture
Enables output capture for stdout and stderr.This configures the WASI context to capture stdout and stderr to internal buffers instead of inheriting from the host process.
- Parameters:
contextHandle- the WASI context handle- Returns:
- 0 on success, non-zero on error
-
wasiContextGetStdoutCapture
public MemorySegment wasiContextGetStdoutCapture(MemorySegment contextHandle, MemorySegment lengthOut) Gets captured stdout data.Returns a pointer to the captured stdout data and sets the length in the output parameter. The caller must free the returned buffer using
wasiFreeCaptureBuffer(MemorySegment, long).- Parameters:
contextHandle- the WASI context handlelengthOut- output parameter for the data length- Returns:
- pointer to captured data, or NULL if capture is not enabled or empty
-
wasiContextGetStderrCapture
public MemorySegment wasiContextGetStderrCapture(MemorySegment contextHandle, MemorySegment lengthOut) Gets captured stderr data.Returns a pointer to the captured stderr data and sets the length in the output parameter. The caller must free the returned buffer using
wasiFreeCaptureBuffer(MemorySegment, long).- Parameters:
contextHandle- the WASI context handlelengthOut- output parameter for the data length- Returns:
- pointer to captured data, or NULL if capture is not enabled or empty
-
wasiFreeCaptureBuffer
Frees a capture buffer allocated bywasiContextGetStdoutCapture(MemorySegment, MemorySegment)orwasiContextGetStderrCapture(MemorySegment, MemorySegment).- Parameters:
buffer- pointer to the buffer to free (can be NULL)length- the length of the buffer in bytes (must match the length returned at allocation)
-
wasiContextHasStdoutCapture
Checks if stdout capture is enabled.- Parameters:
contextHandle- the WASI context handle- Returns:
- 1 if capture is enabled, 0 if not, -1 on error
-
wasiContextHasStderrCapture
Checks if stderr capture is enabled.- Parameters:
contextHandle- the WASI context handle- Returns:
- 1 if capture is enabled, 0 if not, -1 on error
-
wasiCtxAddToStore
Adds a WASI context to a Store.This must be called before instantiating WASI-enabled modules. The context will be used by WASI imports when they are called.
- Parameters:
contextHandle- the WASI context handlestoreHandle- the Store handle- Returns:
- 0 on success, non-zero on error
-
linkerAddWasi
Adds WASI Preview 1 imports to a linker.Configures the linker to extract WASI context from store data when instantiating modules. The store must have a WASI context attached before instantiating WASI-enabled modules.
- Parameters:
linkerHandle- pointer to the linker- Returns:
- 0 on success, non-zero on failure
-
wasiContextSetNetworkConfig
public int wasiContextSetNetworkConfig(MemorySegment contextHandle, int allowNetwork, int allowTcp, int allowUdp, int allowIpNameLookup) Sets network configuration on a WASI context.- Parameters:
contextHandle- the WASI context handleallowNetwork- 1 to allow network access (inherit_network), 0 to denyallowTcp- 1 to allow TCP socket creation, 0 to denyallowUdp- 1 to allow UDP socket creation, 0 to denyallowIpNameLookup- 1 to allow IP name lookups (DNS), 0 to deny- Returns:
- 0 on success, non-zero on failure
-
wasiContextSetAllowBlocking
Sets whether blocking the current thread is allowed.- Parameters:
contextHandle- the WASI context handleallow- 1 to allow blocking, 0 to deny- Returns:
- 0 on success, non-zero on failure
-
wasiContextSetInsecureRandomSeed
Sets the insecure random seed for deterministic testing.- Parameters:
contextHandle- the WASI context handleseedLo- low 64 bits of the u128 seedseedHi- high 64 bits of the u128 seed- Returns:
- 0 on success, non-zero on failure
-
wasiContextRebuild
Rebuilds the WASI context after configuration changes.Must be called after setting network config, blocking, or random seed to apply the changes. This avoids redundant rebuilds when multiple settings are configured at once.
- Parameters:
contextHandle- the WASI context handle- Returns:
- 0 on success, non-zero on failure
-
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]
-