Java FFI docs say that arrays of objects map to Fantom List of appropriate type, but I can't get it to work. Tried variations on nullable, but no luck...
Did you try returning fan.sys.List instead of String[] in FfiPeer.java?
KevinKelleyThu 10 Mar 2011
Yes, changing the peer method to
public fan.sys.List fails(fan.klk.Ffi self) { return null; }
works, but leaves me needing to manually create and fill the fan.sys.List from the Java array. Maybe I'm being dense -- the fandoc I quoted sounds as though there's an automatic mapping for that, and I'm not seeing how to trigger it.
peterThu 10 Mar 2011
One approach that works for me is (assuming you import fan.sys.*):
public List fails(fan.klk.Ffi self) {
String[] ss = {"f","a","i","l","s"};
return new List(Type.of(ss[0]), ss);
}
and then in the Fantom code:
native Str[] fails
The non-list/array types seem to convert automatically. Also the primitive-array types. But I also fail to have an object-list/array type convert automatically, without something like the above.
brianThu 10 Mar 2011
Kevin,
A native method is a different beast than Java FFI. So if a native method returns Str[], then your implementation must return the exact Fantom representation of that type which is List(Sys.StrType, ...).
Think about it like this:
a class with native methods must look exactly like any other Fantom class, there is never any translation
a Java FFI class on the other hand might do all sorts of translations, boxing, or unboxing
KevinKelleyThu 10 Mar 2011
@Peter, thanks, that's the invocation I needed; manually constructing the wrapper list under the covers. That works.
@Brian, in the Natives overview, there's a sentence saying "see Java FFI to map between Fantom and Java types", and I was trying to language-lawyer my way into having my cake and eating it too. :-)
KevinKelley Thu 10 Mar 2011
Java FFI docs say that arrays of objects map to Fantom List of appropriate type, but I can't get it to work. Tried variations on nullable, but no luck...
An example:
Ffi.fan
FfiPeer.java
build.fan
}
building that and running, yields:
>fan klk::Ffi works sys::Err: java.lang.NoSuchMethodError: fan.klk.FfiPeer.fails(Lfan/klk/Ffi;)Lfan/ sys/List;
kaushik Thu 10 Mar 2011
Did you try returning fan.sys.List instead of String[] in FfiPeer.java?
KevinKelley Thu 10 Mar 2011
Yes, changing the peer method to
works, but leaves me needing to manually create and fill the fan.sys.List from the Java array. Maybe I'm being dense -- the fandoc I quoted sounds as though there's an automatic mapping for that, and I'm not seeing how to trigger it.
peter Thu 10 Mar 2011
One approach that works for me is (assuming you
import fan.sys.*
):and then in the Fantom code:
The non-list/array types seem to convert automatically. Also the primitive-array types. But I also fail to have an object-list/array type convert automatically, without something like the above.
brian Thu 10 Mar 2011
Kevin,
A native method is a different beast than Java FFI. So if a native method returns
Str[]
, then your implementation must return the exact Fantom representation of that type which isList(Sys.StrType, ...)
.Think about it like this:
KevinKelley Thu 10 Mar 2011
@Peter, thanks, that's the invocation I needed; manually constructing the wrapper list under the covers. That works.
@Brian, in the Natives overview, there's a sentence saying "see Java FFI to map between Fantom and Java types", and I was trying to language-lawyer my way into having my cake and eating it too. :-)
Thanks for the good comments.