Beispiel #1
0
        public ToyVMObject(ClassFile type)
        {
            this.type = type;

            //if (type is ConstantPoolInfo_Class){
                if (log.IsDebugEnabled) log.DebugFormat("Pretending to create new instance of {0}",type.GetName());
            //}
            //else {
                //throw new Exception("Don't know how to handle " + type.getName());
            //}
        }
Beispiel #2
0
        public static ClassFile loadClass(string className,StackFrame frame)
        {
            if (classCache[className] == null){
            //	try {
                string classFileName = className + ".class";
                string path = classFileName;

                if (log.IsDebugEnabled) log.Debug(String.Format("Trying {0}",path));
                if (!File.Exists(path)){
                    path = "openjdk/" + path;
                    if (log.IsDebugEnabled) log.DebugFormat("Trying {0}",path);
                }
                BinaryReader reader = new BinaryReader(File.OpenRead(path),System.Text.Encoding.UTF8);

                ClassFile classFile = new ClassFile(reader);

                classCache[className] = classFile;

                string superClassName = classFile.GetSuperClassName();

                if (superClassName != null){

                    ClassFile superClass = loadClass(superClassName);
                    classFile.SetSuperClassFile(superClass);
                }

                bool initialized = doInitialize(classFile,frame);

                if (! initialized){
                    throw new ToyVMException("Unable to initialize " + classFile,frame);
                }
            //	} catch (ToyVMException e){
            //		throw e;

            //	} catch (Exception e){
            //		if (! (e is ToyVMException)){
            //			throw new ToyVMException("Error",e,frame);
            //		}
            //	}

                if (ClassLoadedEvent != null){
                    ClassLoadedEvent(className);
                }
            }

            return (ClassFile) classCache[className];
        }
Beispiel #3
0
        // Create or return HeapRef corresponding to this string
        public HeapReference createString(ClassFile stringType, ConstantPoolInfo_String stringConst)
        {
            // the reason this method exists is because I suck and can't seem
            // to properly output unicode characters without breaking everything
            string stringVal = stringConst.getValue();
            if (stringMap[stringVal] == null){

                HeapReference stringRef = newInstance(stringType);

                StackFrame stringFrame = new StackFrame(stringType.getConstantPool());

                // Attempt to initialize using the character array
                MethodInfo stringInit = stringType.getMethod("<init>","([C)V");

                if (stringInit == null){
                    throw new Exception("Unable to find init method for string");
                }
                stringFrame.setMethod(stringType,stringInit);
                stringFrame.getLocalVariables()[0] = stringRef;
                stringFrame.getLocalVariables()[1] = stringConst;

                string printableChars = "";

                //stringRef.isUnicode = true; // I suck
                char[] chars = stringVal.ToCharArray();
                for (int i = 0; i < chars.Length; i++){
                    if (chars[i] > 13 && chars[i] < 127){
                        printableChars += chars[i];
                    }
                }

                if (log.IsDebugEnabled) log.DebugFormat("stringval1:{0}",printableChars);
                //if (log.IsDebugEnabled) log.DebugFormat(stringVal);
            //	if (log.IsDebugEnabled) log.DebugFormat("U:" + stringRef.isUnicode);
                stringType.execute("<init>","([C)V",stringFrame);
                stringMap.Add(stringVal,stringRef);
                reverseStringMap.Add(stringRef,stringVal);
            }
            return (HeapReference) stringMap[stringVal];
        }
Beispiel #4
0
        public HeapReference newInstance(ClassFile type)
        {
            int addr = theHeap.Count;

            HeapReference href = new HeapReference();
            href.address = addr;
            href.obj = new ToyVMObject(type);
            href.type = type;
            theHeap.Add(href);
            ((ToyVMObject)href.obj).setHeapReference(href);
            return href;
        }
Beispiel #5
0
 public HeapReference newArray(ClassFile type, int length)
 {
     int addr = theHeap.Count;
     HeapReference href = new HeapReference();
     ArrayList arr = new ArrayList(length);
     for (int i = 0; i < length; i++) { arr.Add(NullValue.INSTANCE); }
     href.obj = arr;
     href.address = addr;
     href.type = type;
     href.isArray = true;
     href.length = length;
     theHeap.Add(href);
     return href;
 }
Beispiel #6
0
        public HeapReference createString(ClassFile stringType, string stringVal)
        {
            if (stringMap[stringVal] == null){

                HeapReference stringRef = newInstance(stringType);

                StackFrame stringFrame = new StackFrame(stringType.getConstantPool());

                // Attempt to initialize using the character array
                MethodInfo stringInit = stringType.getMethod("<init>","([C)V");

                if (stringInit == null){
                    throw new Exception("Unable to find init method for string");
                }
                stringFrame.setMethod(stringType,stringInit);
                stringFrame.getLocalVariables()[0] = stringRef;
                stringFrame.getLocalVariables()[1] = stringVal;
            //	stringRef.isUnicode = containsUnicode(stringVal);
                char[] chars = stringVal.ToCharArray();
                    string printableChars = "";

                //stringRef.isUnicode = true; // I suck

                for (int i = 0; i < chars.Length; i++){
                    if (chars[i] > 13 && chars[i] < 127){
                        printableChars += chars[i];
                    }
                }

                if (log.IsDebugEnabled) log.DebugFormat("stringval2:{0}",printableChars);

             	stringType.execute("<init>","([C)V",stringFrame);
                stringMap.Add(stringVal,stringRef);
                reverseStringMap.Add(stringRef,stringVal);
            }
            return (HeapReference) stringMap[stringVal];
        }
Beispiel #7
0
 public void SetSuperClassFile(ClassFile superClassFile)
 {
     this.superClassFile = superClassFile;
     if (log.IsDebugEnabled) log.DebugFormat(GetName() + ": Set super class instance " + superClassFile);
 }
Beispiel #8
0
        public void setMethod(ClassFile clazz,MethodInfo method, int paramCount)
        {
            this.clazz = clazz;
            this.method = method;

            if (localVariables == null){

                if (! method.isNative()){
                    localVariables = new ArrayList(method.getMaxLocals());
                }
                else {
                    localVariables = new ArrayList(paramCount+1); // allow for single

                }

                for (int i = 0; i < localVariables.Capacity; i++){
                    localVariables.Add(0);
                }
            }
        }
Beispiel #9
0
 public void setMethod(ClassFile file, MethodInfo method)
 {
     setMethod(file,method,0);
 }
Beispiel #10
0
        protected static bool doInitialize(ClassFile theClass,StackFrame frame)
        {
            if (! theClass.isInitialized()){
                if (log.IsDebugEnabled) log.Debug(String.Format("Initializing {0}",theClass));
                string superClassName = theClass.GetSuperClassName();

                if (superClassName != null) { // got to the top
                    if (log.IsDebugEnabled) log.Debug(String.Format("Need to initialize {0}",superClassName));
                    ClassFile superClass = (ClassFile) classCache[superClassName];

                    if (! doInitialize(superClass,frame)){
                        return false;
                    }
                }

                // TODO: According to the spec, this should be in "textual order"
                if (log.IsDebugEnabled) log.DebugFormat("Executing static initializer");

                theClass.staticInitialize(frame);

                // now we can initialize the original class
                if (log.IsDebugEnabled) log.DebugFormat("Loading fields...");
                FieldInfo[] fields = theClass.getFields();

                if (fields != null){
                foreach (FieldInfo field in fields){
                    if (field == null) {
                        throw new Exception("FIeld is null?");
                    }
                    if (field.isStatic()){
                        if (log.IsDebugEnabled) log.DebugFormat("Static initializing {0}",field);

                    }
                }
                }

                theClass.setInitialized(true);
                return true;
            }
            return true;
        }
Beispiel #11
0
 public void SetClassFile(ClassFile clazz)
 {
     this.classFile = clazz;
     classFileName = classFile.GetName();
 }