Null Callbacks
Jorn Vernee
jbvernee at xs4all.nl
Sat Feb 16 15:31:12 UTC 2019
Hi Mark,
An API for getting a null callback is currently in the process of being
added [1].
In the meantime, there is a way to work around this, but it's tricky
since there doesn't seem to be a public API for parsing a Function
descriptor, or a public API to easily get a layout for a NativeCallback
class (will make a note of that as well). AFAICT it currently requires
manually constructing the Function descriptor for the callback. But, you
should be able to use something like this:
```
@NativeCallback("()v")
public interface FI1 {
void run();
}
public static <T> T pointerLiteral(LayoutType<T> type, long value) {
try(Scope scope = Scope.newNativeScope()) {
Pointer<Long> ptr = scope.allocate(NativeTypes.UINT64);
ptr.set(value);
return ptr.cast(NativeTypes.VOID).cast(type).get();
}
}
public static void main(String[] args) {
Function f = Function.ofVoid(false, NativeTypes.VOID.layout());
Address a = Address.ofFunction(64, f);
Callback<FI1> nullCB = pointerLiteral(LayoutType.ofFunction(a,
FI1.class), 0);
System.out.println(nullCB.entryPoint().isNull()); // prints
'true'
}
```
Hope that helps,
Jorn
[1] :
https://mail.openjdk.java.net/pipermail/panama-dev/2019-February/004373.html
Mark Hammons schreef op 2019-02-16 15:53:
> Hi,
>
> I'm back with a new wayland problem. More specifically, my problem is
> with a helper library called wlroots. One of its functions,
> wlr_backend_autocreate takes a function pointer as an argument. The
> big problem here is that jextract represents this as a Callback<Fi1>,
> and part of the valid input for this parameter is the null pointer. So
> the question is, how do I create a null callback? Can it even be done
> with foreign as it's currently made?
>
> Thanks,
>
> Mark Hammons
More information about the panama-dev
mailing list