<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Jul 17, 2016 at 1:39 PM, Geordie J via swift-dev <span dir="ltr">&lt;<a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi, I’m hoping to add CF_SWIFT_NAME annotations to an imported Clang module (JNI) without editing the original header file itself. Is this possible, or is there a working alternative other than creating a Swift wrapper? Maybe this is trivial but C is not my strong point.</div></blockquote><div><br></div><div>AIUI, apinotes were designed specifically to address this need.  You should be able to augment the header with an apinotes set for that header.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Specifically, there are a bunch of type names in the Android JNI that I’d rather just keep „Swifty“ (the CF_SWIFT_NAME additions are my own of course, working with a copy of the original file for now):<br><div><br></div><div><div>typedef unsigned char   <span style="white-space:pre-wrap">                </span>jboolean; // can be 0 or 1 - is this the same as CFBoolean?</div><div>typedef signed char     <span style="white-space:pre-wrap">                </span>jbyte CF_SWIFT_NAME(Int8);</div><div>typedef unsigned short  <span style="white-space:pre-wrap">                </span>jchar CF_SWIFT_NAME(UInt8);</div><div>typedef short           <span style="white-space:pre-wrap">                </span>jshort CF_SWIFT_NAME(Int16);</div><div>typedef int             <span style="white-space:pre-wrap">                        </span>jint CF_SWIFT_NAME(Int32);</div><div>typedef long long       <span style="white-space:pre-wrap">                </span>jlong CF_SWIFT_NAME(Int);</div><div>typedef float           <span style="white-space:pre-wrap">                        </span>jfloat CF_SWIFT_NAME(Float);</div><div>typedef double          <span style="white-space:pre-wrap">                </span>jdouble CF_SWIFT_NAME(Double);</div></div><div><br></div><div><br></div></div><div>Note that there’s no good way of wrapping these in pure Swift, as far as I can see (they show up throughout the rest of the APIs). Also, it’d be great if the following was possible from outside the header. Instead of:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">typedef</span><span> </span><span style="color:rgb(187,44,162)">enum </span><span>jobjectRefType {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    JNIInvalidRefType = </span><span style="color:rgb(39,42,216)">0</span><span>,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    JNILocalRefType = </span><span style="color:rgb(39,42,216)">1</span><span>,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    JNIGlobalRefType = </span><span style="color:rgb(39,42,216)">2</span><span>,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    JNIWeakGlobalRefType = </span><span style="color:rgb(39,42,216)">3</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>} jobjectRefType;</span></div></div><div><br></div><div>… to use:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">typedef</span><span> </span><span style="color:#bb2ca2">NS_ENUM</span><span>(NSInteger, jobjectRefType) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    JNIInvalidRefType CF_SWIFT_NAME(invalid) = </span><span style="color:#272ad8">0</span><span>,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    JNILocalRefType CF_SWIFT_NAME(local) = </span><span style="color:#272ad8">1</span><span>,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    JNIGlobalRefType CF_SWIFT_NAME(global) = </span><span style="color:#272ad8">2</span><span>,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    JNIWeakGlobalRefType CF_SWIFT_NAME(weakGlobal) = </span><span style="color:#272ad8">3</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>};</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span><br></span></div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">The final question is whether it’s at all possible to annotate this kind of API:</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">struct</span><span> JNINativeInterface {</span></div><div style="margin:0px;line-height:normal"><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span>    jclass      (*DefineClass)(JNIEnv*, </span><span style="color:#bb2ca2">const</span><span> </span><span style="color:#bb2ca2">char</span><span>*, jobject, </span><span style="color:#bb2ca2">const</span><span> jbyte*,</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span>                        jsize);</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span>    // and about 100 more of these ...</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span>}</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span><br></span></div><div style="margin:0px;line-height:normal"><span style="font-family:Helvetica;font-size:12px">This imports into Swift as </span><b>jni.DefineClass(env, string, obj, bytes, size)</b>. Attempting to put a type annotation into the header results like this:</div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;font-size:11px"><br></span></div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;font-size:11px">jclass (*DefineClass)(JNIEnv*, </span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">const</span><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">char</span><span style="font-family:Menlo;font-size:11px">*, jobject, </span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">const</span><span style="font-family:Menlo;font-size:11px"> jbyte*, </span><font face="Menlo"><span style="font-size:11px">size)</span></font></div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;font-size:11px">        CF_SWIFT_NAME(defineClass(env:name:object:byteContents:size:))</span><span style="font-family:Menlo;font-size:11px">;</span></div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">results in a strange warning: &quot;‚swift_name‘ attribute has invalid identifier for base name“. It <i>is</i> possible to just rename <b>DefineClass</b> to e.g. <b>defineClass</b>, via <span style="font-family:Menlo;font-size:11px">CF_SWIFT_NAME(defineClass); </span>I assume this is because this is a function pointer on a struct rather than a global function.</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">Any help regarding the above would be much appreciated. Playing around with those changes locally has made huge improvements to the readability of Android/JNI-specific Swift code. If I can find a clean solution to this I’d like to finally clean up and upstream the SwiftJNI module I started here: <a href="https://github.com/SwiftAndroid/swift-jni" target="_blank">https://github.com/SwiftAndroid/swift-jni</a></div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">Thanks,</div><div style="margin:0px;line-height:normal">Geordie</div></div></div></div><br>_______________________________________________<br>
swift-dev mailing list<br>
<a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-dev" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-dev</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Saleem Abdulrasool<br>compnerd (at) compnerd (dot) org</div>
</div></div>