Class UpcallFactory
java.lang.Object
express.mvp.roray.utils.functions.UpcallFactory
Factory for creating upcall stubs (Java callbacks callable from native code). All setup costs are
paid at stub creation time.
Upcall stubs allow native code to call back into Java methods. The stub's lifetime is tied to
the Arena used during creation.
Usage Example
// Define a callback interface
@FunctionalInterface
interface SignalHandler {
void handle(int signum);
}
// Create an upcall stub
try (Arena arena = Arena.ofConfined()) {
UpcallFactory factory = UpcallFactory.create();
MemorySegment stub = factory.upcallStub(
arena,
(int signum) -> System.out.println("Signal: " + signum),
SignalHandler.class,
FunctionDescriptor.ofVoid(ValueLayout.JAVA_INT)
);
// Pass 'stub' to native code as a function pointer
}
Warning: The upcall stub is only valid while the Arena is alive. Using the stub after the Arena is closed results in undefined behavior.
-
Method Summary
Modifier and TypeMethodDescriptionstatic UpcallFactorycreate()Create anUpcallFactoryusing the native linker.linker()Get the underlying linker.upcallStub(Arena arena, MethodHandle target, FunctionDescriptor descriptor) Creates an upcall stub from aMethodHandle.<T> MemorySegmentupcallStub(Arena arena, MethodHandles.Lookup lookup, T callback, Class<T> functionalInterface, FunctionDescriptor descriptor) Creates an upcall stub from a functional interface instance.<T> MemorySegmentupcallStub(Arena arena, T callback, Class<T> functionalInterface, FunctionDescriptor descriptor) Creates an upcall stub using the caller's lookup context.
-
Method Details
-
create
Create anUpcallFactoryusing the native linker. -
upcallStub
Creates an upcall stub from aMethodHandle.The returned
MemorySegmentcan be passed to native code as a function pointer. The stub remains valid for the lifetime of the arena.- Parameters:
arena- Arena controlling the stub's lifetimetarget- MethodHandle to the Java method to invokedescriptor- Native function signature matching the MethodHandle- Returns:
- MemorySegment representing a native function pointer
- Throws:
IllegalArgumentException- if the descriptor doesn't match the handle
-
upcallStub
public <T> MemorySegment upcallStub(Arena arena, MethodHandles.Lookup lookup, T callback, Class<T> functionalInterface, FunctionDescriptor descriptor) Creates an upcall stub from a functional interface instance.This is a convenience method that looks up the single abstract method (SAM) of the functional interface and creates an upcall stub for it.
- Type Parameters:
T- The functional interface type- Parameters:
arena- Arena controlling the stub's lifetimelookup- MethodHandles.Lookup with access to the interfacecallback- The callback instance (lambda or method reference)functionalInterface- The functional interface classdescriptor- Native function signature- Returns:
- MemorySegment representing a native function pointer
- Throws:
IllegalArgumentException- if the interface is not a functional interfaceRuntimeException- if the method cannot be found or accessed
-
upcallStub
public <T> MemorySegment upcallStub(Arena arena, T callback, Class<T> functionalInterface, FunctionDescriptor descriptor) Creates an upcall stub using the caller's lookup context.- Type Parameters:
T- The functional interface type- Parameters:
arena- Arena controlling the stub's lifetimecallback- The callback instancefunctionalInterface- The functional interface classdescriptor- Native function signature- Returns:
- MemorySegment representing a native function pointer
-
linker
Get the underlying linker.
-