cleanup callJava
authorbrian <brian@brianweb.net>
Sat, 17 Apr 2004 01:08:48 +0000 (18:08 -0700)
committerbrian <brian@brianweb.net>
Sat, 17 Apr 2004 01:08:48 +0000 (18:08 -0700)
darcs-hash:20040417010848-24bed-e1688c975aa88960143e07b97c2dc1ba25d34044.gz

src/org/ibex/nestedvm/Runtime.java
src/tests/CallTest.java

index 6675d59..902e377 100644 (file)
@@ -86,6 +86,11 @@ public abstract class Runtime implements UsermodeConstants,Registers {
     final static int OPEN_MAX = 256;
     /** Table containing all open file descriptors. (Entries are null if the fd is not in use */
     FD[] fds  = new FD[OPEN_MAX];
+    
+    /** Pointer to a callback for the call_java syscall */
+    protected CallJavaCB callJavaCB;
+    public void setCallJavaCB(CallJavaCB callJavaCB) { this.callJavaCB = callJavaCB; }
+    public CallJavaCB getCallJavaCB() { return callJavaCB; }
         
     /** Temporary buffer for read/write operations */
     private byte[] _byteBuf = null;
@@ -542,6 +547,7 @@ public abstract class Runtime implements UsermodeConstants,Registers {
     /** Hook for subclasses to do their own startup */
     protected void _start() { /* noop */ }
     
+    // FEATURE: call() that accepts an Object[] array and automatically allocates strings/arrays/etc on the stack
     public final int call(String sym) throws CallException { return call(sym,0,0,0,0,0,0,0); }
     public final int call(String sym, int a0) throws CallException  { return call(sym,a0,0,0,0,0,0,0); }
     public final int call(String sym, int a0, int a1) throws CallException  { return call(sym,a0,a1,0,0,0,0,0); }
@@ -594,12 +600,6 @@ public abstract class Runtime implements UsermodeConstants,Registers {
         return cpustate.r[V1];
     }
     
-    // FEATURE: This is ugly - we should have some kind of way  to specify a callback rather than requiring subclassing
-    protected int callJava(int a, int b, int c, int d) {
-        System.err.println("WARNING: Default implementation of callJava() called with args " + toHex(a) + "," + toHex(b) + "," + toHex(c) + "," + toHex(d));
-        return 0;
-    }
-    
     /** Determines if the process can access <i>fileName</i>. The default implementation simply logs 
         the request and allows it */
     protected boolean allowFileAccess(String fileName, boolean write) {
@@ -885,12 +885,19 @@ public abstract class Runtime implements UsermodeConstants,Registers {
     private int sys_getpid() { return getPid(); }
     protected int getPid() { return 1; }
     
+    public static interface CallJavaCB { public int call(int a, int b, int c, int d); }
+    
     private int sys_calljava(int a, int b, int c, int d) {
         if(state != RUNNING) throw new IllegalStateException("wound up calling sys_calljava while not in RUNNING");
-        state = CALLJAVA;
-        int ret = callJava(a,b,c,d);
-        state = RUNNING;
-        return ret;
+        if(callJavaCB != null) {
+            state = CALLJAVA;
+            int ret = callJavaCB.call(a,b,c,d);
+            state = RUNNING;
+            return ret;
+        } else {
+                       System.err.println("WARNING: calljava syscall invoked without a calljava callback set");
+                       return 0;
+        }
     }
         
     private int sys_pause() {
index e0c9732..77fd217 100644 (file)
@@ -2,6 +2,7 @@ package tests;
 
 import org.ibex.nestedvm.Runtime;
 import java.io.*;
+import java.util.Date;
 
 public class CallTest {
     public static void main(String[] args) throws Exception {
@@ -13,27 +14,28 @@ public class CallTest {
         int a6 = args.length > 5 ? Integer.parseInt(args[5]) : 0;
         
         System.out.println("Version is: " + System.getProperty("os.version"));
-        Runtime rt;
+        final Runtime rt;
         if(a1 == 99) // yeah.. this is ugly
             rt = new org.ibex.nestedvm.Interpreter("build/tests/Test.mips");
         else
-               //FIXME: Callback not subclass
-            rt = new Test() {
-                protected int callJava(int a, int b, int c, int d) {
+            rt = (Runtime) Class.forName("tests.Test").newInstance();
+               
+        rt.setCallJavaCB(new Runtime.CallJavaCB() {
+                public int call(int a, int b, int c, int d) {
                     switch(a) {
-                        case 1: return strdup("OS: " + System.getProperty("os.name"));
-                        case 2: return strdup(System.getProperty("os.version"));
-                        case 3: return strdup(new Date().toString());
-                        case 4: return allocFDEnt(new OutputStreamFD(new CustomOS()));
+                        case 1: return rt.strdup("OS: " + System.getProperty("os.name"));
+                        case 2: return rt.strdup(System.getProperty("os.version"));
+                        case 3: return rt.strdup(new Date().toString());
+                        case 4: return rt.addFD(new Runtime.OutputStreamFD(new CustomOS()));
                         case 5:
                             System.out.println("In callJava() in Java"); 
-                            try { call("backinmips"); } catch(CallException e) { }
+                            try { rt.call("backinmips"); } catch(Runtime.CallException e) { }
                             System.out.println("Back in callJava() in Java");
                             return 0;
-                        default: return super.callJava(a,b,c,d);
+                        default: return 0;
                     }
                 }
-            };
+            });
         System.out.println("Runtime: " + rt);
         
         rt.start(new String[]{"Test","calltest"});