-
Notifications
You must be signed in to change notification settings - Fork 16
Description
Many libraries in java call back into user code to perform its functions. One notable example is GUI libraries. We have another example in jvm-streaming. https://github.com/tweag/inline-java/blob/master/jvm-streaming/src/main/haskell/Language/Java/Streaming.hs#L92
iterator <-
[java| new Iterator() {
private boolean end = false;
private Object lookahead;
@Override
public boolean hasNext() { return !end; }
@Override
public Object next() {
if (hasNext()) {
final Object temp = lookahead;
lookahead = hsNext();
return temp;
} else
throw new java.util.NoSuchElementException();
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
private native void hsFinalize(long tblPtr);
private native Object hsNext();
@Override
public void finalize() { hsFinalize($tblPtr); }
} |]
The iterator is passed to a function that will retrieve values from Haskell via the native methods of our anonymous Iterator class.
Various housekeeping tasks are necessary to link Haskell functions with those native methods. This issue is about designing and implementing a mechanism that would allow to automate the housekeeping. Suppose we implemented somehow a new quasiquoter like:
iterator <-
[new| Iterator() {
private boolean end = false;
private Object lookahead;
@Override
public boolean hasNext() { return !end; }
@Override
public Object next() {
if (hasNext()) {
final Object temp = lookahead;
lookahead = hsNext();
return temp;
} else
throw new java.util.NoSuchElementException();
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
private native Object $$hsNext();
@Override
public void finalize() { hsFinalize(); }
} |]
Which is capable of identifying hsNext
, link it with the corresponding Haskell function and automatically synthesize hsFinalize
.
A major challenge with this approach is to produce the FFI wrappers of the haskell functions. Template Haskell can produce FFI wrappers with addTopDecls
, but the type information necessary to do so would be available only after type-checking.