A very basic HelloWorld with jextract and panama
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Tue Apr 25 20:59:21 UTC 2023
I guess it depends on what you mean by boilerplate.
In the link you include the Java code just has a single code to the
"sayHello", so boilerplate-wise seems good :-)
That said, the goal of Panama is that of (mechanically) binding to
native libraries (esp. those written in C) w/o the need of intermediate
C code (unlike JNI). That is, what Panama provides is similar to what's
been done in other languages, see Python's ctypes library [1]. This
approach has been used successfully in a lot of real world libraries and
frameworks (see the links in [2]).
Given all this, I think that a better "hello world" for Panama would be
to call a function in some _existing_ library. One obvious, simple
enough, example would be the standard library function "strlen" (the
document I shared in my previous emails contains code that does exactly
that), which can be done 100% in Java (albeit using a low-level API).
Now, it might be that Panama does not 100% satisfy your needs, and
that's ok too. Maybe you are after something that is higher-level than
what Panama provides. If that's the case, the fact that Panama is
low-level is a good thing, because it means that a new breed of
frameworks can be built on top, providing higher-level ways to interact
with native code, w/o the need to generate JNI glue code. JPassport [3]
is one such framework, but I believe (and hope!) that JNR and JNA might
also, one day, be rebuilt on top of Panama and take advantage of the
performance model provided by it.
I hope this clarifies what Panama is really about and what you can
expect from it. Finally, note that, while the jextract tool might
change, the Foreign Function and Memory API [4] is relatively stable
now, and we don't expect it to change much at this point.
Cheers
Maurizio
[1] - https://docs.python.org/3/library/ctypes.html
[2] -
https://mail.openjdk.org/pipermail/panama-dev/2022-December/018182.html
[3] - https://github.com/boulder-on/JPassport
[4] - https://openjdk.org/jeps/442
On 25/04/2023 20:17, Rebecca Ahlvarsson wrote:
> There is a lot of boilerplate still needed. And I somehow miss the
> native method declaration. But I understand this is a work in progress
> and It will improve in terms of simplicity.
>
> See here:
> https://stackoverflow.com/questions/76103402/using-jextract-and-panama-to-call-a-custom-made-c-function-from-java
> <https://urldefense.com/v3/__https://stackoverflow.com/questions/76103402/using-jextract-and-panama-to-call-a-custom-made-c-function-from-java__;!!ACWV5N9M2RV99hQ!PQrjj3CR9v7gdn_FICf8CJtI8UV6w40YznqoQu2VXGjTFWWzIQ430MzpVOtiyeqNbrqXyx6Josesc3S4ANztAEtDYfsH$>
>
> -Rebecca
>
>
> On Tue, Apr 25, 2023 at 3:16 PM Maurizio Cimadamore
> <maurizio.cimadamore at oracle.com> wrote:
>
> Hi Rebecca,
> I believe neither JNA nor JNR support C++, so I'm not 100% what
> you mean here.
>
> You would at the very least need to add "extern C" to your
> function declaration, to make sure that no mangling occurs.
>
> Before I answer further, please clarify what you would like to
> achieve - if you really want to call a C++ library using Panama
> and jextract, I'm afraid that's not possible at the moment (as is
> the case for the other frameworks you mentioned).
>
> If you are ok adding "extern C" or, if C++ is not really central
> to the question you are asking, I'm happy to help further.
>
> Please also refer to the tutorial here (assuming Java 20):
>
> https://github.com/openjdk/panama-foreign/blob/78d460f04d93f3df8e8f703e78780a152793f44a/doc/panama_ffi.md
> <https://urldefense.com/v3/__https://github.com/openjdk/panama-foreign/blob/78d460f04d93f3df8e8f703e78780a152793f44a/doc/panama_ffi.md__;!!ACWV5N9M2RV99hQ!PQrjj3CR9v7gdn_FICf8CJtI8UV6w40YznqoQu2VXGjTFWWzIQ430MzpVOtiyeqNbrqXyx6Josesc3S4ANztAPl_t4Ue$>
>
> Maurizio
>
> On 25/04/2023 19:08, Michel Trudeau wrote:
>>
>> [Moving to the right list]
>>
>> *From: *code-tools-dev <code-tools-dev-retn at openjdk.org>
>> <mailto:code-tools-dev-retn at openjdk.org> on behalf of Rebecca
>> Ahlvarsson <rahlvarsson at gmail.com> <mailto:rahlvarsson at gmail.com>
>> *Date: *Tuesday, April 25, 2023 at 9:18 AM
>> *To: *code-tools-dev at openjdk.org <code-tools-dev at openjdk.org>
>> <mailto:code-tools-dev at openjdk.org>
>> *Subject: *A very basic HelloWorld with jextract and panama
>>
>> This question is so basic that I must be missing something. Sorry
>> if that's a dumb rookie question.
>>
>> I have the following C++ code:
>>
>> #include <iostream>
>>
>> using namespace std;
>>
>> void sayHello() {
>> cout << "Hello, Panama!" << endl;
>> }
>>
>> Great!!!
>>
>> Now how do I go about calling the "sayHello()" C++ method from
>> Java using jextract and panama?
>>
>> *I can use Linux or Mac, you choose. And I have jextract built on
>> both machines.*
>>
>> That's easy and straightforward with JNI, JNA and JNR. But not
>> with Panama? :(
>>
>> Your help clarifying this issue will be greatly appreciated!
>>
>> -Rebecca
>>
>
>
> --
> -Becky
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jextract-dev/attachments/20230425/bdc4a7ed/attachment-0001.htm>
More information about the jextract-dev
mailing list