[foreign-jextract] RFR: 8261906: Improve jextract support for virtual functions

Jorn Vernee jvernee at openjdk.java.net
Mon Feb 22 15:50:56 UTC 2021

On Thu, 18 Feb 2021 17:06:13 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> This patch add support for function pointer calls. Whenever jextract encounters a function pointer variable (be it a global variable, or a struct field), it will additionally emit code to _call_ the function pointer - by means of a static wrapper around a virtual method handle.
> The implementation is relatively straightforward, although there is some duplication in the code that is being emitted. I played with this a bit, to see if duplication could be removed, but I ended up with a single routine to generate wrappers which worked across the axis { virtual, non virtual } x { struct, global } - which in the end I found too complex for my taste (note that, e.g. when we're inside a struct, the logic to get the virtual address depends on what else is generated inside the struct, so there's a lot of ad-hocness).
> For these reason I ended up with separate emit function for virtual wrappers (e.g. I did not reuse the code for native functions) - the code which emit virtual function wrappers is also overridden in StructBuilder. My sense is that if we move the code to use text blocks, the duplication will be much less problematic.
> In OutputFactory, I cleaned up things a bit - since I realized that support for valist doesn't work - this is now removed. I will file a followup issue to add proper VaList support, both in downcalls and upcalls.

LGTM. Minor comments inline

src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/OutputFactory.java line 452:

> 450:             warn("varargs in callbacks is not supported: " + funcPtr);
> 451:             return;
> 452:         }

Seems the same as in `generateFunctionalInterface` ? Might be worth it to unify?

test/jdk/tools/jextract/funcPointerInvokers/TestFuncPointerInvokers.java line 2:

> 1: /*
> 2:  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.


 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.

test/jdk/tools/jextract/funcPointerInvokers/TestFuncPointerInvokers.java line 55:

> 53:             MemorySegment bar = Bar.allocate(scope);
> 54:             Bar.foo$set(bar, Foo.allocate((i) -> val.set(i), scope).address());
> 55:             Bar.foo(bar, 42);

Nice! :)


Marked as reviewed by jvernee (Committer).

PR: https://git.openjdk.java.net/panama-foreign/pull/456

More information about the panama-dev mailing list