Interfacing via Java Native Interface

Andrew diehertz at gmail.com
Wed Oct 16 03:57:46 PDT 2013


On Wednesday, 16 October 2013 at 10:15:17 UTC, Iain Buclaw wrote:
> On Wednesday, 16 October 2013 at 10:11:32 UTC, Andrew wrote:
>> Hello there!
>> I've decided to learn a bit of D, as I am currently Android 
>> Developer (mostly C++ <- JNI -> Java), I'm trying to create a 
>> D shared library which exports function (with extern (C)) for 
>> invocation from Java.
>> My .d file contains only a single function returning an int, 
>> .java calls this function several times.
>> This approach only works if d-function is trivial, e.g. it's 
>> not using any D functionality (I believe that it is related to 
>> GC and memory allocation).
>> For example, if my d-function just returns some constant 
>> integer, java side receives it. core.stdc.stdio also works and 
>> allows console output, but it's not the case with 
>> std.stdio.writeln and similar functions.
>> If I use any d-specific functions my program crashes while 
>> calling native code with some weird message like following:
>> "Invalid memory access of location 0x0 rip=addr".
>>
>> It seems like D is not initialized at all, maybe there is some 
>> internal function like dinit() which I can call in order to 
>> activate everything?
>>
>> Thanks.
>
>
> JNI requires (to some degree) compiler support.  This could be 
> done in gdc as backend support is there because of g++ JNI 
> interface to gcj. But it would be some magic module or change 
> to the language to add the Java types to the language.
>
> Regards
> Iain.

I was talking about calling D from Java in following fashion:
https://gist.github.com/DieHertz/7005898

Java calls dinit in static initializer after loading shader 
library and then proceeds with calling dget.


More information about the Digitalmars-d-learn mailing list