new fs stuff
[nestedvm.git] / src / org / ibex / nestedvm / ClassLoader.java
1 package org.ibex.nestedvm;
2
3 import java.io.*;
4
5 // FEATURE: This is just a quick hack, it is really ugly and broken
6
7 // FEATURE: Cache based on org.ibex.util.Cache
8 // FEATURE: Base64 encode some id to form package name
9 // FEATURE: Timestamped cache entries, requests carry minimum timestamp
10 // NOTE: Need to handle binaries spanned accross many classfiles
11
12 public class ClassLoader extends java.lang.ClassLoader {
13     public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
14         Class c;
15         if(name.startsWith("mips.")) {
16             String path = name.substring(5).replace('.','/') + ".mips";
17             try {
18                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
19                 new ClassFileCompiler(path,name,bos).go();
20                 bos.close();
21                 byte[] buf = bos.toByteArray();
22                 c = defineClass(name,buf,0,buf.length);
23             } catch(IOException e) {
24                 throw new ClassNotFoundException(name);
25             } catch(Compiler.Exn e) {
26                 throw new ClassNotFoundException(e.getMessage());
27             }
28         } else {
29             c = findSystemClass(name);
30         }
31             
32         if(c == null) throw new ClassNotFoundException(name);
33         if(resolve) resolveClass(c);
34         return c;
35     }
36     
37     public Class classFromBinary(String path) throws ClassNotFoundException {
38         if(!path.endsWith(".mips")) throw new IllegalArgumentException("isn't a .mips");
39         return loadClass("mips." + path.substring(0,path.length()-5).replace('/','.'));
40     }
41     
42     public Runtime runtimeFromBinary(String path) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
43         return (Runtime) classFromBinary(path).newInstance();
44     }
45     
46     public static void main(String[] args) throws Exception {
47         System.exit(new ClassLoader().runtimeFromBinary(args[0]).run(args));
48     }
49 }