[foreign-jextract] Bindings to functions declared in header file

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Aug 10 10:02:47 UTC 2020


This is indeed possible

Maurizio

On 07/08/2020 19:13, Paul Sandoz wrote:
> Would it make sense to have a property/attribute on Declaration.Function declaring it's inline (or whatever the correct term is)?
>
> If that’s possible it's easier fir jextract to present warnings to the user that they may have additional work to bind those functions, if required, into an additional auxiliary library.
>
> Paul.
>   
>
>> On Aug 7, 2020, at 10:54 AM, Maurizio Cimadamore <Maurizio.Cimadamore at Oracle.COM> wrote:
>>
>> That's the expected behavior. We need to polish it a bit so that a better exception is generated.
>>
>> Another thing which clients might do, using the API, could be to filter out bindings not available (by looking at the library statically, at extraction time).
>>
>> We used to have such a filtering capability, but we have removed it since we switched to the more minimalistic jextract.
>>
>> Maurizio
>>
>> On 07/08/2020 18:47, Paul Sandoz wrote:
>>> Hi,
>>>
>>> The jextract tool generates bindings to functions declared in a header file.
>>>
>>> MethodHandle lookup will return null, since the function does not exist in the corresponding shared library.
>>>
>>> $ jshell --class-path . --add-modules jdk.incubator.foreign -R-Dforeign.restricted=permit -R-Djava.library.path=.
>>> |  Welcome to JShell -- Version 16-internal
>>> |  For an introduction type: /help intro
>>>
>>> jshell> import a.a_h
>>>
>>> jshell> a_h.f_in_source()
>>>
>>> jshell> a_h.f_in_header()
>>> |  Exception java.lang.AssertionError: java.lang.NullPointerException: Cannot invoke "java.lang.invoke.MethodHandle.invokeExact(Object[])" because the return value of "a.a_h$constants.f_in_header$MH()" is null
>>> |        at a_h.f_in_header (a_h.java:30)
>>> |        at (#3:1)
>>> |  Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.invoke.MethodHandle.invokeExact(Object[])" because the return value of "a.a_h$constants.f_in_header$MH()" is null
>>> |        at a_h.f_in_header (a_h.java:28)
>>> |        …
>>>
>>>
>>> More details below.
>>>
>>> Paul.
>>>
>>>
>>> $ more a.h
>>> void f_in_source();
>>>
>>> static void f_in_header() {
>>> }
>>>
>>>
>>> $ more a.c
>>> #include "a.h"
>>>
>>> void f_in_source() {
>>> }
>>>
>>>
>>> $ more a/a_h.java
>>> // Generated by jextract
>>>
>>> package a;
>>>
>>> import java.lang.invoke.MethodHandle;
>>> import java.lang.invoke.VarHandle;
>>> import jdk.incubator.foreign.*;
>>> import jdk.incubator.foreign.MemoryLayout.PathElement;
>>> import static jdk.incubator.foreign.CSupport.SysV.*;
>>> public final class a_h {
>>>
>>>      private a_h() {}
>>>      public static MethodHandle f_in_source$MH() {
>>>          return a_h$constants.f_in_source$MH();
>>>      }
>>>      public static void f_in_source (Object... x0) {
>>>          try {
>>>              a_h$constants.f_in_source$MH().invokeExact(x0);
>>>          } catch (Throwable ex) {
>>>              throw new AssertionError(ex);
>>>          }
>>>      }
>>>      public static MethodHandle f_in_header$MH() {
>>>          return a_h$constants.f_in_header$MH();
>>>      }
>>>      public static void f_in_header (Object... x0) {
>>>          try {
>>>              a_h$constants.f_in_header$MH().invokeExact(x0);
>>>          } catch (Throwable ex) {
>>>              throw new AssertionError(ex);
>>>          }
>>>      }
>>> }
>>>


More information about the panama-dev mailing list