-
Notifications
You must be signed in to change notification settings - Fork 67
Helpers for constructing JNI signatures #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This probably cannot be fully shared with the logic in jnigen, because there we generate from the serialized Java type. But the logic in there should be similar to logic of doing it with Dart signatures. We could consider jnigen generating Dart function types representing the signatures, that would make the API a bit more typed. Unfortunately we cannot change |
There are few practical difficulties in using function types. First, signatures use qualified names, which don't appear in string representation of pointer type. Second, one primitive type in dart may map to multiple types in java. (Int -> byte, short, int, long). There maybe more I am forgetting. So unless you're trying to get signature for simplest methods using int, float, string etc.. it won't work. I also thought of inferring it from arguments passed. But that doesn't work because the actual type that function defines may be a superclass. But that may be a good default. |
In
class Foo {}
const Map<Type, String> _fullyQualifiedNames = {
Foo: "com.example.Foo",
}; We could generate that in jnigen, but that map needs to be set up by the user if using package:jni by hand. That would lead to
We might be able to get the type out of a pointer by: extension PointerArgType<T extends NativeType> on Pointer<T>{
Type get argType => T;
} And then use a String typeToJavaSignature(Type type) {
final splitOnArrow = type.toString().split(' => ');
final returnType = splitOnArrow[1];
final arguments = splitOnArrow[0];
final argumentsTrimmed = arguments.substring(1, arguments.length - 1);
final argumentTypes = argumentsTrimmed.split(', ');
final javaReturn = dartTypeToJavaSignatureType[returnType]!;
final javaArguments =
argumentTypes.map((e) => dartTypeToJavaSignatureType[e]!).join(',');
return '($javaArguments)$javaReturn';
} But lets shelf this idea for now. |
|
We can safely close this issue then? |
Well, we still would like these helpers (if anywhere in the jni or jnigen api users have to ever provide these signatures manually). We should just not construct them from types. |
Using the same comment, maybe for a function like |
To construct strings such as
"I(I)"
we could add some helper code that could construct them from Dart function types.The is probably not general enough, but maybe it would be a nice addition to the jni interface over having users having to construct the strings themselves.
The text was updated successfully, but these errors were encountered: