Updated: 3/13/2003; 2:19:45 PM.
IKVM.NET Weblog
The development of a Java VM for .NET
        

Thursday, June 20, 2002
This weblog has moved to http://weblog.ikvm.net/

The .NET Guy: I believe that if you check out his implementation, he's JITting Java byte-code into MSIL, and then implementing the Java libraries in terms of the .NET BCL. At least, I couldn't see any other way to make it reasonably fast... why re-implement garbage collection if there's already a garbage collector? :)

Right, except that I'm not reimplementing the Java libraries. I plan to use GNU Classpath for that. The only thing I need to reimplement is the native part of it, which I will do in terms of the .NET BCL (as much as possible). It'll be a long while before AWT is running though ;-)


4:02:03 PM    Comments
This weblog has moved to http://weblog.ikvm.net/

Exception woes

I just found another problem with exception handling. There is no way in .NET to throw an exception without overwriting the stack trace information.

When native code is invoked from Java and the native code in turn invokes a Java method which throws an exception, the native code can handle the exception, but if it doesn't the Java code that called the native code gets the exception.

I need the ability to rethrow an existing exception object. In Java the stack trace for the exception is done at construction time, not when the exception is actually thrown.


3:45:40 PM    Comments
This weblog has moved to http://weblog.ikvm.net/

I forgot Exception in my reply to Sam. I'm still thinking about the exception mappings. java.lang.Throwable should probably map to System.Exception, and then use reflection to emulate the virtual methods that don't correspond, but what to do about java.lang.NullPointerException vs System.NullReferenceException? Obviously when the CLR throws a System.NullReferenceException, Java code should be able to catch it as a java.lang.NullPointerException, but also as a java.lang.Exception or java.lang.RuntimeException.

But what should happen when Java code throws a NullPointerException (or worse, it's own subclass of it)? Should this be translated to System.NullReferenceException? One thing I haven't talked about much is my wish for interoperability between Java and .NET code, but I do intent for it to be possible (and convenient) to use Java class libraries from your C# (or whatever .NET language) code. In order for this to work, the exception mappings need to make sense to both the Java and the .NET side.


11:31:21 AM    Comments
This weblog has moved to http://weblog.ikvm.net/

Sam Ruby: I<<K.VM.NET is a Java byte code to CIL converter (some prefer to call it MSIL, but not me).  My guess is that Jeroen is using the same hooks that CLAW does to modify the byte codes immediately prior to JIT, but in this case the translation is a wee bit more involved.

I'm not using the same hooks as CLAW, I'm using the AppDomain.TypeResolve event. This works very well together with Reflection.Emit, because it allows me to lazily emit a type whenever the CLR needs it. The one downside of it is that it doesn't provide the assembly where it thinks the type lives, so when I'm going to implement multiple classloader support I will need to mangle the classnames.

If all goes well, one should be able to simply put JAR files in a CLASSPATH and transparently call Java code from C#.

That's exactly the idea. My starter executable (the equivalent of jre.exe) looks like this:

public class Starter
{
 static void Main(string[] args)
 {
  JVM.Init();
  string[] vmargs = new string[args.Length - 1];
  for(int i = 1; i < args.Length; i++)
  {
   vmargs[i - 1] = args[i];
  }
  Type type = Type.GetType(args[0], true);
  MethodInfo main = type.GetMethod("main");
  main.Invoke(null, new object[] { vmargs });
 }
}

I suspect the hardest part will be handling the class libraries - in particular three classes: Object, String, and Exception.

Exactly. Mapping java.lang.Object to System.Object turns out to be easy, they have the exact same virtual methods, but in order for this to work java.lang.String also has to be mapped to System.String, since string is final, not much to worry about (methods can be redirected to static helpers), except for one thing: interfaces. If java.lang.String implements an interface, System.String should implement the equivalent interfaces. In JDK 1.4 java.lang.String implements: Serializable, Comparable and CharSequence. Serializable has no methods, so it's not a big deal. Comparable maps nicely to System.IComparable, so that's easy, but CharSequence is a problem, System.String doesn't have that, so that interface will probably have to be emulated via some reflection hack. Of course, at the moment I'm aiming for JDK 1.1 compatibility, so it's not really an issue yet :-)


9:36:27 AM    Comments

© Copyright 2003 Jeroen Frijters.
 
June 2002
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
May   Jul