On Mon, Sep 6, 2010 at 10:42 PM, Dibyendu Majumdar <[email protected]> wrote: > [snip] > The initial problem is how to load classes referred to in the source > code, i.e., how to handle imports. Do I need to use something like ASM > to locate and load references to classes, or is it possible to use a > custom class loader to do this? What is the general approach followed > by other implementations? > [/snip] > > I would be grateful for a reply to this question as the first thing I > want to tackle in my new language is the import statement.
I can describe how we do it in JRuby, even though it's a dynamic language. In JRuby, there's no "import" as part of the base language. "require" loads and executes a specific file and registers it as having been loaded (so you can safely "require" the same thing multiple times without it loading twice). The load process involves parsing and executing the toplevel of the file, which then defines (or modifies) classes, modules, and methods. The equivalent to Java's import in JRuby is called "java_import" and takes a class name (or a shortcut dotted Java-like name if the package starts with java, javax, org, or com) with an optional closure (for providing an alternative name). "java_import" simply defines a new Ruby constant on whatever scope it is called from, pointing at our proxy representation of the class. The trickiest bit with importing classes at runtime is that there's no standard way to get a list of all classes in a given package. Jython uses a trick where it pre-scans jars and saves an index to be used for package listings. JRuby just installs a "const_missing" hook into the appropriate scope which then does the equivalent of Class.forName. Since Go is statically typed, you'll want to do something closer to Java. What will "import" actually be loading for Ravi? Classes? Jar files? In JRuby you can also "require" a Jar file, and then we add it to the URLClassLoader which Ruby-land uses to locate classes. - Charlie -- You received this message because you are subscribed to the Google Groups "JVM Languages" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/jvm-languages?hl=en.
