<font face="verdana,sans-serif">Thanks a lot for the detailed answer, Max!<br></font><div><font class="Apple-style-span" face="verdana, sans-serif">This gives me a good idea of how my problem can be solved.<br></font></div>
<div><font class="Apple-style-span" face="verdana, sans-serif"><br></font></div><div><font class="Apple-style-span" face="verdana, sans-serif">I'm actually not working on a specific project involving this</font></div>
<div><font class="Apple-style-span" face="verdana, sans-serif">problem right now. What I'm mostly looking for is a small example</font></div><div><font class="Apple-style-span" face="verdana, sans-serif">that shows how this can be done, so I can use it for later</font></div>
<div><font class="Apple-style-span" face="verdana, sans-serif">references.</font></div><div><font class="Apple-style-span" face="verdana, sans-serif"><br></font></div><div><font class="Apple-style-span" face="verdana, sans-serif">I will be busy with other things for the next two weeks but</font></div>
<div><font class="Apple-style-span" face="verdana, sans-serif">I'm gonna come back to this afterwards.</font></div><div><font class="Apple-style-span" face="verdana, sans-serif"><br></font></div><div><font class="Apple-style-span" face="verdana, sans-serif">I'll see how far I can get and I will write back as soon as I run</font></div>
<div><font class="Apple-style-span" face="verdana, sans-serif">into problems I can't solve by myself.</font></div><div><font class="Apple-style-span" face="verdana, sans-serif"><br></font></div><div><font class="Apple-style-span" face="verdana, sans-serif">It would defnitely be helpful to see some examples from your</font></div>
<div><font class="Apple-style-span" face="verdana, sans-serif">code, though.</font></div><div><font class="Apple-style-span" face="verdana, sans-serif"><br></font></div><div><font class="Apple-style-span" face="verdana, sans-serif">Steffen</font></div>
<div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote">On Mon, Aug 1, 2011 at 19:04, Max Lehn <span dir="ltr"><<a href="mailto:mlehn@dvs.tu-darmstadt.de">mlehn@dvs.tu-darmstadt.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi,<br>
<br>
I am working with Christof on the BubbleStorm project. I'll try to<br>
sketch the way we make our SML-to-Java bindings.<br>
<br>
The bindings basically consist of three 'layers' of (wrapper) code:<br>
1. SML wrapper code exporting the SML API code via FFI. The code is<br>
compiled to a dynamic library ("mlton -export-header <header>.h -format<br>
library ...").<br>
2. C code using the exported SML functions and implementing the native<br>
part for JNI. The C code is compiled to a second dynamic (JNI) library<br>
(depending on the first; I am not sure if the two libraries could be<br>
merged).<br>
3. Java code importing the native functions and wrapping them into<br>
nice-to-use Java classes. The Java application imports the two dynamic<br>
libraries.<br>
<br>
'Objects' between SML and Java: Java programmers expect a nice<br>
object-oriented API. Although SML is not OOP, an interface using<br>
structures in an 'class-like' fashion allows for a quite good mapping to<br>
Java classes. (Following our convention, a 'class' in SML is a structure<br>
with a type t and functions with an instance of type t as the first<br>
parameter (i.e, a 'method').) Since we cannot move objects between SML<br>
and Java, all SML 'object instances' are passed via handles. For this,<br>
we define one handle-to-object map per 'class' in the SML wrapper part.<br>
(The map is actually a custom implementation based on an array for<br>
efficient access.) When calling a method of an object or passing an<br>
object as a method parameter, we always use its handle.<br>
<br>
The second big thing to solve is the thread synchronization for Java.<br>
All typical Java applications use threading, so we have to translate<br>
between our single-threaded, event-based programming model in SML to<br>
Java. (We have active networking code in SML; if your SML parts are just<br>
passive, it may be somewhat easier. The very basic thing, is to prevent<br>
calls to SML from multiple threads at the same time.). Therefore, we<br>
start our SML event loop in a separate thread. Whenever an SML function<br>
is called from Java, the call information is stored in a queue, the SML<br>
part is notified to pull the data and call the function, and the results<br>
are in turn passed back through a queue. A similarly complex<br>
synchronization is necessary for callback functions implemented in Java<br>
and called from SML.<br>
This sounds rather expensive, and probably is. So I wouldn't recommend<br>
it for high performance applications. But we've used the concept already<br>
for some applications which are more than proof-of-concept implementations.<br>
<br>
One big drawback of our current solution is the need for a lot of<br>
hand-written wrapper code. So it's really annoying (and also<br>
error-prone) to port complex interfaces. We discussed automatic code<br>
generation, but from my experience there are too many special cases to<br>
make auto-generation really useful.<br>
Furthermore, error handling is sometimes nasty. We built an exception<br>
passing mechanism which throws Java exceptions instead of crashing the<br>
VM on unhandled SML exceptions. But a Java-C-SML stack trace is hardly<br>
readable.<br>
And there are several other tiny things to solve, but so far we have<br>
been able to do (almost) everything we needed.<br>
<br>
To conclude, it works, but it is messy in some places. This was the<br>
high-level overview and a disclaimer; if you're still interested, I can<br>
try to provide you the relevant extracts and example code from our project.<br>
<br>
Max<br>
<div><div></div><div class="h5"><br>
<br>
Matthew Fluet wrote:<br>
> On Fri, Jul 29, 2011 at 3:57 PM, Steffen J. Smolka<br>
> <<a href="mailto:steffen.smolka@in.tum.de">steffen.smolka@in.tum.de</a>> wrote:<br>
>> Hi,<br>
>> I have been wondering for a while now if there's a good way to make ML code<br>
>> and Java code work together.<br>
>> What I would love to do is write a program in ML and wrap it with a GUI<br>
>> written in Java/Scala.<br>
>> What I have found so far is that it's possible to make Java work with C<br>
>> (JNI/JNA) and C work with ML (with MLton's Foreign Function Interface).<br>
>> Combining this, I would hope to get Java working with ML (via an interface<br>
>> written in C).<br>
>> However, it seems that JNI/JNA only works with dynamic libraries, that is<br>
>> dlls in Windows.<br>
>> The problem is that i can't get MLton to compile my ML/C program to a<br>
>> dll. Compiling the program to an executable works just fine.<br>
>> Is it just not possible to compile ML programs to dlls with MLton or am I<br>
>> doing something wrong?<br>
>> Or is there possibly a better way to make ML code and Java code work<br>
>> together?<br>
>> I know it's possible to compile the ML programm to a standalone executable<br>
>> and simply call that executable from within Java.<br>
>> The problem is that this would limit data transfer beetween the programs to<br>
>> strings which might require lots of encoding and decoding and doesn't seem<br>
>> like a good solution.<br>
> You could certainly send binary data back and forth to the SML program<br>
> (rather than just text data). That still requires some amount of<br>
> serialization of complex data. But, unless you are simply passing<br>
> flat arrays of 32-bit integers (or something similar), you would still<br>
> need some amount of serialization, since Java doesn't know MLton's<br>
> native object format and MLton doesn't know Java's native object<br>
> format.<br>
><br>
> As for compiling to a DLL, there is some support for compiling a SML<br>
> program to a library. See "<a href="http://mlton.org/LibrarySupport" target="_blank">http://mlton.org/LibrarySupport</a>"; I'm not<br>
> sure that it supports compiling to a DLL, since there isn't a native<br>
> Windows port of MLton; on Windows, we use either Cygwin or MinGW.<br>
><br>
</div></div><div class="im">> _______________________________________________<br>
> MLton-user mailing list<br>
> <a href="mailto:MLton-user@mlton.org">MLton-user@mlton.org</a><br>
> <a href="http://mlton.org/mailman/listinfo/mlton-user" target="_blank">http://mlton.org/mailman/listinfo/mlton-user</a><br>
<br>
<br>
</div><font color="#888888">--<br>
Max Lehn<br>
Databases and Distributed Systems<br>
Technische Universität Darmstadt<br>
Hochschulstr. 10, 64289 Darmstadt, Germany<br>
Tel.: <a href="tel:%2B49%206151%20167425" value="+496151167425">+49 6151 167425</a><br>
<a href="http://www.dvs.tu-darmstadt.de/" target="_blank">http://www.dvs.tu-darmstadt.de/</a><br>
</font><div><div></div><div class="h5"><br>
<br>
_______________________________________________<br>
MLton-user mailing list<br>
<a href="mailto:MLton-user@mlton.org">MLton-user@mlton.org</a><br>
<a href="http://mlton.org/mailman/listinfo/mlton-user" target="_blank">http://mlton.org/mailman/listinfo/mlton-user</a><br>
</div></div></blockquote></div><br></div>