Skip to content

Ability to call existing Java functions from extern "jvm" blocks #26

@CompeyDev

Description

@CompeyDev

Currently, it is possible to call Rust-defined types in Java, but the inverse is not true. I am not very well-versed with JVM internals but this is what I was able to gauge by looking into the source code. I had the following macro-based design in mind:

extern "jvm" {
    // Lowers to: invokestatic org/slf4j/LoggerFactory.getLogger
    #[jvm_class = "org/slf4j/LoggerFactory"]
    #[jvm_descriptor = "(Ljava/lang/String;)Lorg/slf4j/Logger;"]
    fn get_logger(name: JavaString) -> JavaObject;

    // Lowers to: invokeinterface org/slf4j/Logger.info
    #[jvm_class = "org/slf4j/Logger"]
    #[jvm_descriptor = "(Ljava/lang/String;)V"]
    fn logger_info(self: JavaObject, msg: JavaString);
}

Essentially, we would need to introduce extern "jvm" blocks and two attribute macros to annotate foreign declarations. What is challenging, though, is how we would infer what we lower to -- invokestatic, invokevirtual, or invokeinterface -- and my design does this based on whether a self argument of an object type is expected as a parameter.

The other part would be mapping Java types to Rust types (which can be seen in the above example), especially for objects. I am not entirely sure how this could be implemented in a type-safe manner so my example uses a pointer-sized opaque JavaObject type instead. Does this seem feasible and worth implementing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions