Overview: iPhone/Objective-C
Apple's iPhone has generated huge interest amongst users and developers alike. Like MacOS X, the iPhone development environment is based on Objective-C as the development language and Cocoa for the GUI library. The iPhone SDK license agreement does not permit the development of a virtual machine. Using XMLVM, we circumvent this problem by cross-compiling Java to the iPhone. Just like a Java application can be cross-compiled to AJAX, XMLVM can be used to cross-compile a Java application to Objective-C. The cross-compilation is also accomplished by mimicking a stack-based machine in Objective-C. Consider the <jvm:irem> instruction (integer remainder) that pops two integers off the stack and pushes their remainder after division back onto the stack. Using the following XSL template, the <jvm:irem> instruction can be mapped to Objective-C:
<xsl:template match="jvm:irem">
<xsl:text>
_op2.i = _stack[--_sp].i; // Pop operand 1
_op1.i = _stack[--_sp].i; // Pop operand 2
_stack[_sp++].i = _op1.i % _op2.i; // Push remainder
</xsl:text>
</xsl:template>
Variable _stack represents the runtime stack, whereas _sp is the stack pointer that refers to the top of the stack. Pre-decrement and post-increment operations are used for popping and pushing of arguments respectively. Since the runtime stack can contain elements of arbitrary types, the declaration of helper variables _op1, _op2, and _stack[] are based on a union:
typedef union {
id o;
int i;
float f;
double d;
} XMLVMElem;
The following Objective-C program is the cross-compiled version of "Hello World" as explained in XMLVM's frontend:
@interface org_xmlvm_test_HelloWorld : java_lang_Object
+ (void) main___java_lang_String_ARRAYTYPE :(NSMutableArray*)n1;
@end
@implementation org_xmlvm_test_HelloWorld;
+ (void) main___java_lang_String_ARRAYTYPE :(NSMutableArray*)n1
{
XMLVMElem _stack[2];
XMLVMElem _locals[1];
int _sp = 0;
XMLVMElem _op1, _op2, _op3;
int _i;
for (_i = 0; _i <1; _i++) _locals[_i].o = nil;
NSAutoreleasePool* _pool = [[NSAutoreleasePool alloc] init];
_locals[0].o = n1;
_op1.o = [java_lang_System _GET_STATIC_java_lang_System_out];
_stack[_sp++].o = _op1.o;
_stack[_sp++].o = @"Hello World";
_sp -= 2;
[((java_io_PrintStream*) _stack[_sp].o) println___java_lang_String:_stack[_sp + 1].o];
[_pool release];
return;
}
@end
Since Objective-C does not support overloading of methods with identical selectors, XMLVM uses name-mangling to differentiate between different overloaded versions of a method. Apart of language-level cross-compilation, iPhone applications depend on iPhone-specific widgets. XMLVM features a prototype implementation of a Java version of the Cocoa API. The following iPhone application demonstrates a GUI version of "Hello World" in Java:
public class HelloWorld extends UIApplication
{
public void applicationDidFinishLaunching(NSNotification n)
{
UIScreen screen = UIScreen.mainScreen();
CGRect rect = screen.applicationFrame();
UIWindow window = new UIWindow(rect);
rect.origin.x = rect.origin.y = 0;
UIView mainView = new UIView(rect);
window.addSubview(mainView);
UILabel title = new UILabel(rect);
title.setText("Hello World!");
title.setTextAlignment(UITextAlignment.UITextAlignmentCenter);
mainView.addSubview(title);
window.makeKeyAndVisible();
}
}
The Java application above can be cross-compiled to Objective-C and executed on an iPhone. The Cocoa-specific Java classes such as UIWindow and UIView are mapped to their Objective-C counter-parts. XMLVM also features a Swing-based implementation of these Cocoa classes that make it possible to run a Java-based iPhone application on a desktop. The resulting iPhone simulator is shown below as a Java applet. The accelerometer controls influence the orientation of the fireworks. On a real iPhone the sparks will follow gravity:
Click here for a demo (requires Java)
Further details on the Java for the iPhone project can be found at a Google Tech Talk (Slides). Note that in the presentation it is mentioned that you need a jailbroken device to run the demo. With the official release of the iPhone SDK by Apple this is no longer necessary. Consult the XMLVM manual for instructions on how to run a Java-based iPhone application on an actual device.