/** * Put a class representation of the primitive type */ public static void getPrimitiveClass(StackFrame frame) { char typeChar = (char)((int)(frame.getLocalVariables())[0]); if (log.IsDebugEnabled) log.DebugFormat("Returning class for {0}",typeChar); switch (typeChar){ case 'I':{ frame.getPrev().pushOperand(Type.GetType("System.Int32")); break; } case 'Z':{ frame.getPrev().pushOperand(Type.GetType("System.Boolean")); break; } case 'C':{ frame.getPrev().pushOperand(Type.GetType("System.Char")); break; } default: { throw new ToyVMException("Not handling " + typeChar,frame); } } }
/** * Returns full path to file based on short name of lib */ public static void mapLibraryName(StackFrame frame) { Heap.HeapReference nameRef = (Heap.HeapReference) frame.getLocalVariables()[0]; string shortName = Heap.GetInstance().GetStringVal(nameRef); string fullName = "lib"+shortName+".so"; frame.getPrev().pushOperand(Heap.GetInstance().createString(ToyVMClassLoader.loadClass("java/lang/String"),fullName)); }
/** * "native" implementation of Class[] VMStackWalker::getClassContext() */ public static void getClassContext(StackFrame frame) { // should be an array ref Heap.HeapReference heapRef = Heap.GetInstance().newArray(ToyVMClassLoader.loadClass("java/lang/Class"),4); ((ArrayList)heapRef.obj).Add(frame.getThis()); ((ArrayList)heapRef.obj).Add(frame.getThis()); ((ArrayList)heapRef.obj).Add(frame.getThis()); // since this is a method call we are just // storing the return value frame.getPrev().pushOperand(heapRef); }
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]; }
public static void isFile(StackFrame frame) { Heap.HeapReference fileRef = (Heap.HeapReference) frame.getLocalVariables()[0]; ToyVMObject fileObj = (ToyVMObject)fileRef.obj; Heap.HeapReference filenameCharRef = (Heap.HeapReference)fileObj.getFieldValue("value",ConstantPoolInfo_FieldRef.TYPE_REF); System.Char[] filenameChars = (System.Char[]) filenameCharRef.obj; string filename = new string(filenameChars); if (log.IsDebugEnabled) log.DebugFormat("Determining if {0} is a file or not",filename); FileInfo file = new FileInfo(filename); frame.getPrev().pushOperand(((file.Exists && (file.Attributes & FileAttributes.Directory) == 0) ? 1 : 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]; }
/** * int write(int fd,ByteBuffer src) */ public static void write(StackFrame frame) { int fd = (int)frame.getLocalVariables()[1]; Heap.HeapReference byteBufRef = (Heap.HeapReference) frame.getLocalVariables()[2]; if (fd != 2){ throw new ToyVMException("Can only handle stdout(2), have " + fd, frame); } // need to get the actual byte array StackFrame frame2 = new StackFrame(frame); ClassFile byteBufferClass = byteBufRef.type; string getterType = "()[B"; string getterName = "array"; MethodInfo method = byteBufferClass.getMethod(getterName,getterType); if (method == null){ foreach (MethodInfo m in byteBufferClass.getMethods()){ if (log.IsDebugEnabled) log.DebugFormat(m.ToString()); } throw new ToyVMException("Unable to find " + getterName + getterType,frame); } frame2.setMethod(byteBufferClass,method); frame2.getLocalVariables()[0] = byteBufRef; byteBufferClass.execute(getterName,getterType,frame2); Heap.HeapReference byteArrRef = (Heap.HeapReference) frame.popOperand(); byte[] bytes = (byte[]) byteArrRef.obj; for (int i = 0; i < bytes.Length; i++){ Console.Write((char)bytes[i]); } frame.getPrev().pushOperand(bytes.Length); }
public void start(string className) { try { EventLogger logger = new EventLogger("/home/jdewald/ToyVMEvents.log"); CountingHandler counter = new CountingHandler("/home/jdewald/ToyVMCounter"); ToyVMClassLoader.ClassLoadedEvent += logger.handleClassLoaded; MethodInfo.EnterEvent += counter.HandleMethodInfoEnterEvent; MethodInfo.ByteCodeEnterEvent += counter.HandleByteCodeEnterEvent; ClassFile mainClass = ToyVMClassLoader.loadClass(className); if (log.IsDebugEnabled) log.DebugFormat("Loaded"); if (log.IsDebugEnabled) log.DebugFormat(mainClass.GetName() + " was loaded"); //classCache[mainClass.GetName()] = mainClass; if (log.IsDebugEnabled) log.DebugFormat(mainClass.GetName() + " stored in cache"); // initialize from the root of the class tree string superClassName = mainClass.GetSuperClassName(); if (log.IsDebugEnabled) log.DebugFormat("Super class is {0}",superClassName); ClassFile superClass = ToyVMClassLoader.loadClass(superClassName); if (superClass == null) { throw new Exception("ClassLoader did not load super class!"); //superClass = loadClass(superClassName); } // Execute main(String[]) //MethodInfo info = mainClass.getMethod("main","[Ljava/lang/String;V"); StackFrame frame = new StackFrame(mainClass.getConstantPool()); //frame.pushOperand(mainClass); // info.execute(frame); frame.setMethod(mainClass,mainClass.getMethod("main","([Ljava/lang/String;)V")); mainClass.execute("main","([Ljava/lang/String;)V",frame); /*if (info == null) { throw new Exception("Unable to find 'main' method of class " + className); } else { StackFrame frame = new StackFrame(mainClass.getConstantPool()); frame.pushOperand(mainClass); info.execute(frame); } */ counter.Complete(); } catch (ToyVMException te){ if (te.InnerException == null){ Console.WriteLine("{0}\n{1}",te.Message,te.getStackFrame()); } else { Console.WriteLine("{0}:{1}\n{2}",te.InnerException,te.Message,te.getStackFrame()); } } catch (Exception e) { Console.WriteLine(e.Message + ":" + e.StackTrace); //throw e; } }
/* * Pretend to load a library, but not really * since we will use DllImport for now */ public static void nativeLoad(StackFrame frame) { //loadNio(); frame.getPrev().pushOperand(1); // "success" }
// TODO: Right now we always assume bootstrap loader public static void getClassLoader(StackFrame frame) { frame.getPrev().pushOperand(NullValue.INSTANCE); }
public void staticInitialize(StackFrame frame) { MethodInfo staticInit = getMethod("<clinit>","()V"); if (staticInit != null){ StackFrame newFrame = null; if (frame != null){ newFrame = new StackFrame(frame); } else { newFrame = new StackFrame(getConstantPool()); } newFrame.setMethod(this,staticInit); execute("<clinit>","()V", newFrame); //StackFrame frame = new StackFrame(getConstantPool()); //frame.setMethod(this,staticInit); //staticInit.execute(frame); } }
public StackFrame(StackFrame prev) { this.prev = prev; this.constantPool = prev.getConstantPool(); }
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]; }
/** * array copy: * this: void arraycopy(Object source,int start,Object dest,int start,int length) */ public static void arraycopy(StackFrame frame) { if (log.IsDebugEnabled) log.DebugFormat("Performing array copy"); Object source = frame.getLocalVariables()[0]; if (log.IsDebugEnabled) log.DebugFormat("Source is {0}",source); int start = (int)frame.getLocalVariables()[1]; Object target = frame.getLocalVariables()[2]; if (log.IsDebugEnabled) log.DebugFormat("target is {0}",target); int end = (int)frame.getLocalVariables()[3]; int length = (int)frame.getLocalVariables()[4]; Heap.HeapReference arrayRef = (Heap.HeapReference) target; if (log.IsDebugEnabled) log.DebugFormat("arrayref obj is {0}",arrayRef.obj); if (arrayRef.obj is System.Char[]){ char[] targetChars = (System.Char[])(arrayRef.obj); char[] sourceChars; if (source is string){ sourceChars = ((string)source).ToCharArray(); } else if (source is ConstantPoolInfo_String){ sourceChars = ((ConstantPoolInfo_String)source).getValue().ToCharArray(); } else if (source is Heap.HeapReference){ Heap.HeapReference heapRef = (Heap.HeapReference)source; if (heapRef.isArray && heapRef.isPrimitive && heapRef.primitiveType.FullName.Equals("System.Char")){ sourceChars = (System.Char[])heapRef.obj; } else { throw new ToyVMException("Can only handle char arrays, have " + heapRef,frame); } } else { throw new ToyVMException("Can only copy strings, have " + source.GetType(),frame); } System.Array.Copy(sourceChars,start,targetChars,end,length); string printableChars = ""; //stringRef.isUnicode = true; // I suck for (int i = 0; i < targetChars.Length; i++){ if (targetChars[i] > 13 && targetChars[i] < 127){ printableChars += targetChars[i]; } } //if (log.IsDebugEnabled) log.DebugFormat("Target is now: {0}",printableChars); } else if (arrayRef.obj is System.Byte[]){ byte[] targetBytes = (System.Byte[]) arrayRef.obj; Heap.HeapReference sourceRef = (Heap.HeapReference) source; byte[] sourceBytes = (System.Byte[]) sourceRef.obj; System.Array.Copy(sourceBytes,start,targetBytes,end,length); } else { throw new ToyVMException("Target is neither a byte or a char: " + arrayRef,frame); } }
public void execute(StackFrame frame) { if ( EnterEvent != null) { EnterEvent(classFileName, name.getUTF8String(),descriptor.getUTF8String()); } if (! isNative()) { Hashtable byteCodes = methodCode.getCode(); ByteCode bc = null; frame.setProgramCounter(0); do { //foreach (ByteCode bc in byteCode){ bc = (ByteCode) byteCodes[frame.getProgramCounter()]; if (bc != null){ frame.setByteCode(bc); if (log.IsDebugEnabled) log.DebugFormat("{0}: {1}",frame.getProgramCounter(),bc); if (methodCode.hasLineNumbers() && methodCode.getLineNumber((UInt16)frame.getProgramCounter()) != 0){ if (log.IsDebugEnabled) log.DebugFormat(classFile.GetName() + ":{0}",methodCode.getLineNumber((UInt16)frame.getProgramCounter())); } if (ByteCodeEnterEvent != null) { ByteCodeEnterEvent(bc.getName()); } bc.execute(frame); if (ByteCodeExitEvent != null) { ByteCodeExitEvent(bc.getName()); } // the instruction might have moved the counter // but it is assumed to not have accounted // for its own length frame.setProgramCounter(frame.getProgramCounter() + bc.getSize()); } } while (bc != null && ! (bc is ByteCode_areturn || bc is ByteCode_ireturn || bc is ByteCode_freturn || bc is ByteCode_lreturn || bc is ByteCode_dreturn || bc is ByteCode_return)); } else { throw new ToyVMException("Method is native!",frame); } if (ExitEvent != null) { ExitEvent(classFileName, name.getUTF8String(),descriptor.getUTF8String()); } }
public static void getName(StackFrame frame) { ClassFile clazz = (ClassFile) frame.getLocalVariables()[0]; frame.getPrev().pushOperand(Heap.GetInstance().createString(ToyVMClassLoader.loadClass("java/lang/String"),clazz.GetName())); }
/** * Sets the core default system properties */ public static void preInit(StackFrame sf) { Heap.HeapReference propsObj = (Heap.HeapReference) sf.getLocalVariables()[0]; ClassFile stringType = ToyVMClassLoader.loadClass("java/lang/String"); ClassFile propsClass = (ClassFile)(propsObj.type); string setterType = "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;"; string setterName = "setProperty"; MethodInfo method = propsClass.getMethod(setterName,setterType); if (method == null){ foreach (MethodInfo m in propsClass.getMethods()){ if (log.IsDebugEnabled) log.DebugFormat(m.ToString()); } throw new ToyVMException("Unable to find " + setterName + setterType,sf); } StackFrame frame = new StackFrame(sf); frame.setMethod(propsClass,method); Heap.HeapReference prop = Heap.GetInstance().createString(stringType,"java.version"); Heap.HeapReference val = Heap.GetInstance().createString(stringType,"5"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.boot.class.path"); val = Heap.GetInstance().createString(stringType,"/home/jdewald/ToyVM/ToyVM/openjdk"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.vendor"); val = Heap.GetInstance().createString(stringType,"jdewald"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.vendor.url"); val = Heap.GetInstance().createString(stringType,"http://quay.wordpress.com"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.vm.vendor"); val = Heap.GetInstance().createString(stringType,"jdewald"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.vm.name"); val = Heap.GetInstance().createString(stringType,"ToyVM"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.vm.vendor.url"); val = Heap.GetInstance().createString(stringType,"http://quay.wordpress.com"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.vm.specification.version"); val = Heap.GetInstance().createString(stringType,"2"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.vm.specification.vendor"); val = Heap.GetInstance().createString(stringType,"Sun"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.vm.specification.url"); val = Heap.GetInstance().createString(stringType,"http://quay.wordpress.com"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.vm.specification.version"); val = Heap.GetInstance().createString(stringType,"http://quay.wordpress.com"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.class.version"); val = Heap.GetInstance().createString(stringType,"49"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.home"); val = Heap.GetInstance().createString(stringType,"/home/jdewald/ToyVM/ToyVM"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.class.path"); val = Heap.GetInstance().createString(stringType,"/home/jdewald/ToyVM/ToyVM/openjdk"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.library.path"); val = Heap.GetInstance().createString(stringType,"/home/jdewald/nativelibs"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"java.io.tmpdir"); val = Heap.GetInstance().createString(stringType,"/tmp"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"os.name"); val = Heap.GetInstance().createString(stringType,"Linux"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"os.arch"); val = Heap.GetInstance().createString(stringType,"i386"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"file.separator"); val = Heap.GetInstance().createString(stringType,"/"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"path.separator"); val = Heap.GetInstance().createString(stringType,":"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"line.separator"); val = Heap.GetInstance().createString(stringType,"\n"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"user.name"); val = Heap.GetInstance().createString(stringType,"jdewald"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"user.home"); val = Heap.GetInstance().createString(stringType,"/home/jdewald"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"user.dir"); val = Heap.GetInstance().createString(stringType,Environment.CurrentDirectory); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); prop = Heap.GetInstance().createString(stringType,"gnu.cpu.endian"); val = Heap.GetInstance().createString(stringType,"little"); frame.getLocalVariables()[0] = propsObj; frame.getLocalVariables()[1] = prop; frame.getLocalVariables()[2] = val; propsClass.execute(setterName,setterType,frame); }
/** * Returns the file descriptor for stdout, note this is "supposed" to be using actual c code */ public static void stdout_fd(StackFrame frame) { frame.getPrev().pushOperand(2); }
public static void stderr_fd(StackFrame frame) { frame.getPrev().pushOperand(3); }
public ToyVMException(string message, Exception source, StackFrame sf) : base(message, source) { this.sf = sf; }
public void execute(string methodName, string descriptor, StackFrame frame) { MethodInfo method = getMethod(methodName,descriptor); if (method != null){ //frame.setMethod(this,method); if (! method.isNative()){ if (log.IsDebugEnabled) log.DebugFormat("**** START {0}.{1}{2} ****",this.GetName(),methodName,descriptor); try { method.execute(frame); } catch (ToyVMException te) { throw te; } catch (Exception e) { throw new ToyVMException("Unable to execute",e,frame); } if (log.IsDebugEnabled) log.DebugFormat("**** END {0}.{1}{2} ****",this.GetName(),methodName,descriptor); } else { if (! handleNative(method,frame)) throw new ToyVMException("Method is native!",frame); } } else { throw new Exception("Unable to retrieve " + methodName + descriptor); } }
/** * Calculate hash code... currently uses C# version * this: int identityHashCode(Object obj) */ public static void identityHashCode(StackFrame frame) { if (log.IsDebugEnabled) log.DebugFormat("Calculating hash for {0}",frame.getLocalVariables()[0]); Object obj = frame.getLocalVariables()[0]; // valid? if (obj is NullValue){ frame.getPrev().pushOperand(0); } else { frame.getPrev().pushOperand(((Heap.HeapReference)obj).address); } }
public void execute(ConstantPoolInfo_NameAndType nameAndType, StackFrame frame) { execute(nameAndType.getRefName(),nameAndType.getDescriptor(),frame); }
// Thread currentThread() public static void currentThread(StackFrame frame) { Thread thread = Thread.CurrentThread; // will return or create new java.lang.Thread instance holding the current Thread frame.getPrev().pushOperand(Heap.GetInstance().createThread(thread)); }
// TODO: reflection, bitches public bool handleNative(MethodInfo method,StackFrame frame) { if (this.GetName().StartsWith("gnu/classpath")){ if (this.GetName().Equals("gnu/classpath/VMStackWalker")){ if (method.getMethodName().Equals("getClassContext")){ VMStackWalker.getClassContext(frame); return true; } if (method.getMethodName().Equals("getClassLoader")){ VMStackWalker.getClassLoader(frame); return true; } } else if (this.GetName().Equals("gnu/classpath/VMSystemProperties")){ if (method.getMethodName().Equals("preInit")){ VMSystemProperties.preInit(frame); return true; } } } else if (this.GetName().StartsWith("gnu/java")){ if (this.GetName().Equals("gnu/java/nio/VMChannel")){ if (method.getMethodName().Equals("initIDs")){ if (log.IsDebugEnabled) log.DebugFormat("DOING NOTHING FOR initIDs"); /*unsafe { void * jniEnv = null; void * jclass = null; javanio.Java_gnu_java_nio_VMChannel_initIDs(jniEnv,jclass); } */ return true; } if (method.getMethodName().Equals("stdin_fd")){ if (log.IsDebugEnabled) log.DebugFormat("RETURNING 1 FOR stdin_fd"); /*unsafe { void * jniEnv = null; void * jclass = null; javanio.Java_gnu_java_nio_VMChannel_initIDs(jniEnv,jclass); } */ frame.getPrev().pushOperand(1); return true; } else if (method.getMethodName().Equals("stdout_fd")){ VMChannel.stdout_fd(frame); return true; } else if (method.getMethodName().Equals("stderr_fd")){ VMChannel.stderr_fd(frame); return true; } else if (method.getMethodName().Equals("write")){ VMChannel.write(frame); return true; } } } else if (this.GetName().StartsWith("java/lang")){ if (this.GetName().Equals("java/lang/VMSystem")){ if (method.getMethodName().Equals("identityHashCode")){ VMSystem.identityHashCode(frame); return true; } else if (method.getMethodName().Equals("arraycopy")){ VMSystem.arraycopy(frame); return true; } } else if (this.GetName().Equals("java/lang/VMObject")){ if (method.getMethodName().Equals("clone")){ VMObject.clone(frame); return true; } if (method.getMethodName().Equals("getClass")){ VMObject.getClass(frame); return true; } } else if (this.GetName().Equals("java/lang/VMClass")){ if (method.getMethodName().Equals("getName")){ VMClass.getName(frame); return true; } } else if (this.GetName().Equals("java/lang/VMClassLoader")){ if (method.getMethodName().Equals("getPrimitiveClass")){ VMClassLoader.getPrimitiveClass(frame); return true; } } else if (this.GetName().Equals("java/lang/VMRuntime")){ if (method.getMethodName().Equals("mapLibraryName")){ VMRuntime.mapLibraryName(frame); return true; } if (method.getMethodName().Equals("nativeLoad")){ VMRuntime.nativeLoad(frame); return true; } } else if (this.GetName().Equals("java/lang/VMThread")){ if (method.getMethodName().Equals("currentThread")){ VMThread.currentThread(frame); return true; } } } else if (this.GetName().StartsWith("java/io")){ if (this.GetName().Equals("java/io/VMFile")){ if (method.getMethodName().Equals("isDirectory")){ VMFile.isDirectory(frame); return true; } if (method.getMethodName().Equals("isFile")){ VMFile.isDirectory(frame); return true; } } } return false; }
public virtual void execute(StackFrame frame) { throw new ToyVMException("Not yet implemented " + getName(),frame ); }
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; }