CLinker migration from jdk17 to jdk18

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue Apr 5 14:32:07 UTC 2022


Hi Lev,
the ingredients are pretty much all there (in one form or another).

The big difference from 17 to 18 is that ValueLayout now contain a link 
to the Java carrier that should be used to dereference them.

Which means we don't need a MethodType in downcallMethodHandle.

Another difference is that there's no C_INT, C_POINTER anymore. You can 
use JAVA_INT, JAVA_LONG, ADDRESS (which is the same as using the JNI 
types jint, jlong, ...). Or, you can define alias for C types in your 
platform if you want (jextract will do that).

So, in short, your example will become something like:

```
h_native = CLinker.systemCLinker()
             .downcallHandle(
                 libLookup.lookup("somefunc").get(),
                 FunctionDescriptor.of(
                     ValueLayout.ADDRESS,
                     ValueLayout.JAVA_INT,
                     ValueLayout.ADDRESS,
                     ValueLayout.ADDRESS,
                     ValueLayout.JAVA_INT,
                     ValueLayout.JAVA_INT
                 )
             );
```

(the above assumes LP64 - e.g. where a C int is 32 bits).

As for DSL, we have abandoned that path a long time ago - and decided to 
focus on something lower level. There are projects which are using 
Panama as a backend, to define their own DSLs/annotations [1].

Cheers
Maurizio

[1] - https://github.com/boulder-on/JPassport/

On 05/04/2022 15:23, Lev Serebryakov wrote:
>
>   I have this code, which works on JDK-17:
>
>         h_native = CLinker.getInstance()
>             .downcallHandle(
>                 libLookup.lookup("somefunc").get(),
>                 MethodType.methodType(
>                     MemoryAddress.class,
>                     int.class,
>                     MemoryAddress.class,
>                     MemoryAddress.class,
>                     int.class,
>                     int.class
>                 ),
>                 FunctionDescriptor.of(
>                     CLinker.C_POINTER,
>                     CLinker.C_INT,
>                     CLinker.C_POINTER,
>                     CLinker.C_POINTER,
>                     CLinker.C_INT,
>                     CLinker.C_INT
>                 )
>             );
>
> But it doesn't work on JDK-18 and I don't see proper way to convert 
> it. Is "CLinker.systemCLinker()" replacement for ".getInstance()"? 
> Where are built-in types live now?
>
>
> BTW. my previous experiments with panama/foreign were long ago, and 
> then there was complex DSL to describe native data structures. Is it 
> gone forever?
>
>
> Thank you.
>


More information about the panama-dev mailing list