-
Notifications
You must be signed in to change notification settings - Fork 154
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
Auto-generate native Java-interfacing files #81
Comments
For a similar reason I ended up writing https://github.com/viperproject/jni-gen (undocumented...) Our approach inspects the JVM by reflection at Rust's compile time and generates wrappers for methods listed in a whitelist in An example of the generated bindings: https://viperproject.github.io/prusti-dev/viper_sys/wrappers/index.html Some problems that we found:
The end result works fine, but there is a lot that can be improved. For example, the whitelist in |
Federico, thank you for sharing your experience! Just to avoid any confusion: are you talking about the opposite use-case -- creating native proxies of Java objects? I think that deserves a separate issue, it would be great if you submit one! There might be something the library can provide to ease that task. We actually have both use-cases, yours on smaller scale, just a couple of hand-written proxies. They look like this: // Pseudo-code
struct JavaFooProxy {
// A global reference to the Java object.
foo: GlobalRef;
// An abstraction that allows you to execute some code with JNIEnv in a thread-safe manner.
//
// How it provides a context (JNIEnv) -- is an implementation detail.
// For example, there might be implementations that:
// - Always attach/detach threads, which is VERY costly.
// - Keep a couple of attached threads and pass the lambda to them,
// - Rely on the fact that your app never creates more than N threads
// and never `detach` them (a benign leak).
// - For local references: use JNIEnv directly.
executor: Executor;
// Calls a Java `Foo#bar` method.
def bar() {
executor.with_context((env: JNIEnv) -> {
env.call_method(foo, "bar", BAR_SIGNATURE, …);
}
}
} Regarding method name translation, you might find useful the JNI rules (that's the opposite use-case, but there might be some solutions that you may re-use): https://docs.oracle.com/javase/9/docs/specs/jni/design.html#resolving-native-method-names |
Oh, I got it the other way around. Yes, I meant creating native proxies of Java objects. I just opened an issue to track it (#82). |
Overview
This is a feature request and an another take on #80 .
Consider adding a tool that takes a Java source file with native methods or a native C header of such file, produced by
javac
, and turns it into a Rust-template. Basically:Such a tool may produce a single template file, similar to what one currently writes by hand, or a couple of files:
nativeCreate
instead ofJava_com_example_company_NativePeerProxy_nativeCreate
). Such methods might also enjoy some conversions that are performed by the code in intermediate file (e.g.,jint -> i32
, again, see Higher-level api for defining java functions in a more "rusty" manner #80).If there is a user-facing file, intermediate one calls its functions.
Requirements
javac -h
produces. Restrictions may apply on method names (e.g., method names that contain only ASCII symbols).javac -h
output.Criteria
Possible approaches
javac -h
output.javac -h
output.javac
..rs
files by hand.Non-goals
See also
The text was updated successfully, but these errors were encountered: