jextract woes
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Wed Jan 29 10:46:54 UTC 2020
On 29/01/2020 03:24, Michael Zucchi wrote:
> i.e. how do i solve the problem i've described in a way that isn't
> just the same as simply doing this (assuming such a variation of
> downcallHandle() call existed):
Describe 'context' - what is 'context' ? You could make that part of the
"recipe" too, and pass it as a _dynamic_ argument to an invokedynamic.
An invokedynamic instruction has some _static_ arguments, which are
fixed, for every call. And some _dynamic_ arguments. For instance, the
javac compiler uses invokedynamic instructions to compile lambda
expressions. The invokedynamic that javac generates has a static part:
* what is the type of the lambda
* what is the method that implements it (a method handle)
* what is the functional interface the lambda should be converted to
But there's also a _dynamic_ part:
* additional variables the lambda captures from the enclosing scope
Do, if you have something like:
for (int i = 0 ; i < 100 ; i++) {
Runnable r = () -> System.out.println(i);
r.run();
}
The lambda creation will be turned into an invokedynamic instruction
whose static arguments point to Runnable, and an implementation method
which does the 'println' of the incoming argument. But there will also
be a dynamic argument because the created lambda needs to capture 'i'
from the enclosing scope.
Ok, enough talking about lambdas... what I'm trying to say here is that
what you need is a _factory_ of native method handles; I guess this
factory will have some static parts (which will not change across calls)
and some dynamic parts (e.g. the parts you say depend on the context).
Invokedynamic is, albeit low level, perhaps the most efficient way to
map these kind of idioms in the Java bytecode in a way that the VM can
optimize.
Or maybe it's overkill, and if your context is 'simple' enough, you can
just use a map<Context, MethodHandle> :-)
If you need to manually instantiate the method handles, you can - you
can use RuntimeHelper, but at that point is probably better to just use
SystemABI.downcallHandle itself.
Maurizio
More information about the panama-dev
mailing list