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.
.java
files on the class path and parses them, extracting the method signatures from there. .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. .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?
The JVM was initially designed to support only the programming language Java. However, as time passed, even more languages were adapted or designed to run on the Java platform.
Any language can be compiled into bytecode which the JVM understands - all you need for this is a compiler emitting bytecode. From then on, there is no difference from the JVM's point of view. So much so that you can take a compiled Scala, Clojure, Jython etc.
The languages we'll look at in this first category are Scala, Groovy, Xtend, Ceylon, Kotlin, and Fantom. The second category is existing languages that were ported to the JVM. Many languages, such as Python and Ruby, can interact with Java APIs and are popular for scripting and quick prototyping.
Java vs Go: A quick glance Java is the older and more widely used programming language. It is object-oriented, has a larger community—thus library, and relies on the Java virtual machine (JVM). Go, or Golang, is newer, supports concurrency, is more readable, and is not object-oriented.
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 .jar
s). 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With