JVM language interoperability

Michael :

Recently I've been writing a compiler for a JVM programming language and I've realised a problem.

I would like to access a Java method from my programming language and also allow a Java method to access a method in my language. The problem is that I need to know the Java methods signature to call it in the bytecode I generate and vice versa.

I've been trying to think of any methods for how Scala does this. Here are my thoughts.

  1. Scala accesses the .java files on the class path and parses them, extracting the method signatures from there.
  2. .java files are compiled to .class files. The Java ASM library is then used to access the .class files and get the method signatures. The problem with this method is that the .java files must be compiled first.
  3. .java files are loaded dynamically using reflection. The problem with this is I believe that the JVM doesn't allow for loading classes that are outside of the compilers class path.

Looking into Scala it works well with other JVM languages but I can't find information on exactly how it does it.

How does Scala get method signatures of other JVM language methods?

Alexey Romanov :

I think you are confusing class path and source path: there are no .java or .scala files on the class path, there are .class files (possibly inside .jars). So for dependencies (on the class path), you don't need to do anything special. They can have their own dependencies on your language, including previous versions of your project, but they are already compiled by definition.

Now, for mixed projects, where you have Java and your language on the source path, scalac does parse Java with its own parser (i.e. your option 1).

The problem with option 3 is not that "the JVM doesn't allow for loading classes that are outside of the compilers class path", but that reflection also only works on classes, not on source files.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=36951&siteId=1