Conversion between Java objects and native pointers

Gregory Klyushnikov grishka93 at gmail.com
Fri Dec 2 16:44:28 UTC 2022


Yes, I do want ObjC methods invoking Java code. And yes I'd use JNI handles if I were using JNI.

I looked into MethodHandles, of course, but the problem is that the ObjC runtime expects a pointer to a function that takes at least 2 arguments: the `self` on which the ObjC method was called, and the method selector (see https://developer.apple.com/documentation/objectivec/1418901-class_addmethod?language=objc <https://developer.apple.com/documentation/objectivec/1418901-class_addmethod?language=objc> ). So I can do it, but either I'd have to receive the calls in a static Java method with no way to link the ObjC `self` with the Java `this`, or I could use an instance method having to create a new ObjC class for each object because the MethodHandle would map to a different native function pointer for each different `this`.

I considered the approach you suggest with a map of ints/longs to all currently allocated Java objects that represent ObjC objects, but I kinda dismissed it because it felt like that map would be a bottleneck. But since you say the decision to not allow native Java object handles was intentional, I'll try it.

> 2 дек. 2022 г., в 16:11, Sundararajan Athijegannathan <sundararajan.athijegannathan at oracle.com> написал(а):
> 
> Suppose if you have to implement the same with JNI, would you use JNI handles? i.e., Pass a JNI handle as a void* to native callbacks and map it back to Java objects inside the callbacks?
> 
> Panama does not support something like JNI handles. It's something we considered/discussed and so far the risk outweighed the benefits.
> 
> One solution could be to pass "longs" to native API and have a Map of long to Java object on the Java side (to map 'native object' to Java mirror back).
> 
> Thanks,
> -Sundar
> From: panama-dev <panama-dev-retn at openjdk.org> on behalf of Maurizio Cimadamore <maurizio.cimadamore at oracle.com>
> Sent: 02 December 2022 18:03
> To: Gregory Klyushnikov <grishka93 at gmail.com>; panama-dev at openjdk.org <panama-dev at openjdk.org>
> Subject: Re: Conversion between Java objects and native pointers
>  
> To make sure I understand your use case (admittedly I'm not an expert in 
> ObjC). You want to define an ObjC class whose methods end up invoking 
> some piece of Java code? Would that be a fair description?
> 
> In general, "Java objects" cannot be converted into native pointers. 
> But, you can turn Java methods (method handles, to be precise) into 
> native pointers, and pass those to the ObjC class implementation. Would 
> that be enough?
> 
> Maurizio
> 
> On 02/12/2022 11:20, Gregory Klyushnikov wrote:
> > I'm trying to use the preview Panama API to build an FFI for Objective C, allowing Java to fully interoperate with native macOS libraries. Calling out to existing classes is simple: you just use objc_msgSend and sel_registerName a bunch with the right arguments. But many macOS APIs want you to create your own classes and override methods (NSApplicationDelegate is one example and you seemingly can't do GUI without creating one) — which isn't as easy to do in a non-ugly way with the current Panama API.
> >
> > You *can* define a new Objective C class at runtime using objc_registerClassPair, but you don't have a good way of linking an ObjC object to its corresponding Java object. You can, obviously, store the pointer to the ObjC object in the Java object, but you can't do it the other way around, which poses a problem for overridden methods. The obvious solution to receiving calls to overridden methods would be:
> > 1. Set their implementations as pointers to static methods
> > 2. Create an instance variable in the ObjC object that would hold the pointer to its corresponding Java object
> > 3. When a method is called, you'd get your Java `this` from that instance variable and forward the call to your Java object
> >
> > But I can't seem to find any way whatsoever, at least one that doesn't feel extremely hacky and thus unreliable, to convert a Java object to a native pointer and back. This would also be useful, in some circumstances, for those C APIs that take a function pointer and `void*` as the "context" and then pass that `void*` back to your function. Or are there better solutions that don't need this?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20221202/bd868130/attachment.htm>


More information about the panama-dev mailing list