<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
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?</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
Panama does not support something like JNI handles. <span style="color:rgb(29, 28, 29);font-family:Slack-Lato, Slack-Fractions, appleLogo, sans-serif;font-size:15px;font-variant-ligatures:common-ligatures;orphans:2;text-align:left;widows:2;background-color:rgb(248, 248, 248);display:inline !important" class="ContentPasted0">It's
something we considered/discussed and so far the risk outweighed the benefits.</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
<span style="color:rgb(29, 28, 29);font-family:Slack-Lato, Slack-Fractions, appleLogo, sans-serif;font-size:15px;font-variant-ligatures:common-ligatures;orphans:2;text-align:left;widows:2;background-color:rgb(248, 248, 248);display:inline !important" class="ContentPasted0"><br>
</span></div>
<div style="orphans: 2; widows: 2;" class="elementToProof"><font color="#1d1c1d" face="Slack-Lato, Slack-Fractions, appleLogo, sans-serif"><span style="caret-color: rgb(29, 28, 29); font-size: 15px; background-color: rgb(248, 248, 248);">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).</span></font></div>
<div style="orphans: 2; widows: 2;" class="elementToProof"><font color="#1d1c1d" face="Slack-Lato, Slack-Fractions, appleLogo, sans-serif"><span style="caret-color: rgb(29, 28, 29); font-size: 15px; background-color: rgb(248, 248, 248);"><br>
</span></font></div>
<div style="orphans: 2; widows: 2;" class="elementToProof"><font color="#1d1c1d" face="Slack-Lato, Slack-Fractions, appleLogo, sans-serif"><span style="caret-color: rgb(29, 28, 29); font-size: 15px; background-color: rgb(248, 248, 248);">Thanks,</span></font></div>
<div style="orphans: 2; widows: 2;" class="elementToProof"><font color="#1d1c1d" face="Slack-Lato, Slack-Fractions, appleLogo, sans-serif"><span style="caret-color: rgb(29, 28, 29); font-size: 15px; background-color: rgb(248, 248, 248);">-Sundar</span></font></div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> panama-dev <panama-dev-retn@openjdk.org> on behalf of Maurizio Cimadamore <maurizio.cimadamore@oracle.com><br>
<b>Sent:</b> 02 December 2022 18:03<br>
<b>To:</b> Gregory Klyushnikov <grishka93@gmail.com>; panama-dev@openjdk.org <panama-dev@openjdk.org><br>
<b>Subject:</b> Re: Conversion between Java objects and native pointers</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">To make sure I understand your use case (admittedly I'm not an expert in
<br>
ObjC). You want to define an ObjC class whose methods end up invoking <br>
some piece of Java code? Would that be a fair description?<br>
<br>
In general, "Java objects" cannot be converted into native pointers. <br>
But, you can turn Java methods (method handles, to be precise) into <br>
native pointers, and pass those to the ObjC class implementation. Would <br>
that be enough?<br>
<br>
Maurizio<br>
<br>
On 02/12/2022 11:20, Gregory Klyushnikov wrote:<br>
> 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.<br>
><br>
> 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:<br>
> 1. Set their implementations as pointers to static methods<br>
> 2. Create an instance variable in the ObjC object that would hold the pointer to its corresponding Java object<br>
> 3. When a method is called, you'd get your Java `this` from that instance variable and forward the call to your Java object<br>
><br>
> 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?<br>
</div>
</span></font></div>
</body>
</html>