From f20e17422d673a5e203811512af76b3759050158 Mon Sep 17 00:00:00 2001 From: Artur Ventura Date: Thu, 21 Jul 2011 19:03:57 +0100 Subject: [PATCH] adding more intrp code --- src/attributes.js | 3 +- src/class.js | 2 - src/cpu.js | 1 + src/infos.js | 8 ++ src/intrp.def | 340 ++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 342 insertions(+), 12 deletions(-) diff --git a/src/attributes.js b/src/attributes.js index 8a86675..a8b23d3 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -33,7 +33,8 @@ var InnerClass = function(dStream, constantPool){ }else{ this.outer_class_info = null; } - this.inner_name = ConstantPoolRef(dStream.getU2(), constantPool,CONSTANT_Utf8); + var innerNameIndex = dStream.getU2(); + this.inner_name = (innerNameIndex)?ConstantPoolRef(innerNameIndex, constantPool,CONSTANT_Utf8):null; this.inner_class_access_flags = dStream.getU2(); } diff --git a/src/class.js b/src/class.js index 88ca29e..9154393 100644 --- a/src/class.js +++ b/src/class.js @@ -286,8 +286,6 @@ function LoadClassFile (x,jvm){ }else{ def.makeForArray(x); } - - return def; } diff --git a/src/cpu.js b/src/cpu.js index 955aa65..164baa2 100644 --- a/src/cpu.js +++ b/src/cpu.js @@ -65,6 +65,7 @@ var JVM = function(params,args){ this.java_lang_object = this.classForName("java.lang.Object"); this.java_lang_cloneable = this.classForName("java.lang.Cloneable"); this.java_io_serializable = this.classForName("java.io.Serializable"); + this.java_lang_string = this.classForName("java.lang.String"); this.mainClass = this.args[0]; this.classForName(this.mainClass).makeInstance(); }; diff --git a/src/infos.js b/src/infos.js index 03fb8b5..3a274c6 100644 --- a/src/infos.js +++ b/src/infos.js @@ -11,6 +11,13 @@ #include "constantPool.jsh" +function parseArgs(description){ +// "([Ljava/lang/String;)V" + var length = description.length + var sides = description.substr(1,length).split(")") + return {args:sides[0].split(","),ret:sides[1]}; +} + /** @constructor */ var FieldInfo = function(dStream,constantPool){ this.access_flags = dStream.getU2(); @@ -30,6 +37,7 @@ var MethodInfo = function(dStream, constantPool){ this.access_flags = dStream.getU2(); this.name_ref = ConstantPoolRef(dStream.getU2(), constantPool, CONSTANT_Utf8); this.descriptor_ref = ConstantPoolRef(dStream.getU2(), constantPool, CONSTANT_Utf8); + this.descriptor = parseArgs(this.descriptor_ref.str); this.attributes_count = dStream.getU2(); this.attributes = []; for (var i=0; i> (value2 & 0x1f); + + if (IS_OVERFLOW(result,INT_MAX_VALUE) || IS_UNDERFLOW(result,INT_MIN_VALUE)){ + OPPUSH(INT_OVERFLOW(result)); + }else{ + OPPUSH(result); + } +ENDDEF + +DEFOP(ISTORE) + var index = READ_NEXT(); + LOCAL_VAR(index) = OPPOP(); +ENDDEF + +DEFALIAS(ISTORE_0) +DEFALIAS(ISTORE_1) +DEFALIAS(ISTORE_2) +DEFALIAS(ISTORE_3) +DEFNOP() + LOCAL_VAR(READ_NEXT()) = (OPCODE - ISTORE_0); +ENDDEF + +DEFOP(ISUB) + var value1 = OPPOP(); + var value2 = OPPOP(); + var result = value1 - value2; + if (IS_UNDERFLOW(result,INT_MAX_VALUE)){ + OPPUSH(INT_OVERFLOW(result)); + }else{ + OPPUSH(result); + } +ENDDEF + +DEFOP(IUSHR) + var value2 = OPPOP(); + var value1 = OPPOP(); + OPPUSH((value1<0)?(value1 >> (value2 & 0x1f)) + (2 << ~s):value1 >> (value2 & 0x1f)) +ENDDEF + +DEFOP(IXOR) + var value2 = OPPOP(); + var value1 = OPPOP(); + OPPUSH(INT_OVERFLOW(value1 ^ value2)); +ENDDEF + +DEFOP(JSR) + var branchbyte1 = READ_NEXT(); + var branchbyte2 = READ_NEXT(); + OPPUSH(PC + 1); + PC = (branchbyte1 << 8) | branchbyte2 +ENDDEF + +DEFOP(JSR_W) + var branchbyte1 = READ_NEXT(); + var branchbyte2 = READ_NEXT(); + var branchbyte3 = READ_NEXT(); + var branchbyte4 = READ_NEXT(); + + OPPUSH(PC + 1); + PC = (branchbyte1 << 24) | (branchbyte2 << 16) | (branchbyte3 << 8) | branchbyte4 +ENDDEF + +DEFOP(L2D) + OPPUSH(null); +ENDDEF + +DEFOP(L2F) + OPPUSH(null); +ENDDEF + +DEFOP(L2I) + var value = OPPOP() + + if (IS_OVERFLOW(value,LONG_MAX_VALUE) || IS_UNDERFLOW(result,LONG_MIN_VALUE)){ + OPPUSH(LONG_OVERFLOW(value)); + }else{ + OPPUSH(value); + } +ENDDEF + +DEFOP(LADD) + var value1 = OPPOP(); + var value2 = OPPOP(); + var result = value1 + value2; + if (IS_OVERFLOW(result,LONG_MAX_VALUE)){ + OPPUSH(LONG_OVERFLOW(result)); + }else{ + OPPUSH(result); + } +ENDDEF + +DEFOP(LALOAD) + var index = OPPOP(); + var arrayref = OPPOP(); + + CHECK_NULL(arrayref); + CHECK_ARRAY_INDEX(index,arrayref); + + OPPUSH(arrayref.value[index]); +ENDDEF + +DEFOP(LAND) + var value1 = OPPOP() + var value2 = OPPOP() + var result = value1 & value2 + OPPUSH(result); +ENDDEF + +DEFOP(LASTORE) + var index = OPPOP(); + var arrayref = OPPOP(); + var value = OPPOP(); + + CHECK_NULL(arrayref); + CHECK_ARRAY_INDEX(index,arrayref); + + arrayref.value[index] = value; +ENDDEF + +DEFOP(LCMP) + var value2 = OPPOP(); + var value1 = OPPOP(); + + if (value1 > value2){ + OPPUSH(1); + }else if (value1 == value2){ + OPPUSH(0); + }else{ + OPPUSH(-1) + } +ENDDEF + + +DEFALIAS(LCONST_0) +DEFALIAS(LCONST_1) +DEFNOP() + OPPUSH(OPCODE - LCONST_0) +ENDDEF + +DEFOP(LDC) + var index = READ_NEXT(); + var cstt = this_method_class.constantPool.get(index); + switch(cstt.id){ + case CONSTANT_Integer: + case CONSTANT_Float: + case CONSTANT_Double: + case CONSTANT_Long: + OPPUSH(cstt.value); + case CONSTANT_String: + PANIC("NEW STRING FROM CONSTANT POOL NOT IMPLEMENTED"); + default: + PANIC("What now?"); + } +ENDDEF + +DEFOP(LDC_W) + var index1 = READ_NEXT(); + var index2 = READ_NEXT(); + var index = (indexbyte1 << 8) | indexbyte2 + var cstt = this_method_class.constantPool.get(index); + switch(cstt.id){ + case CONSTANT_Integer: + OPPUSHD(cstt.value); + case CONSTANT_Float: + OPPUSH(cstt.value); + case CONSTANT_String: + PANIC("NEW STRING FROM CONSTANT POOL NOT IMPLEMENTED"); + default: + PANIC("What now?"); + } +ENDDEF + +DEFOP(LDC2_W) + var index1 = READ_NEXT(); + var index2 = READ_NEXT(); + var index = (indexbyte1 << 8) | indexbyte2 + var cstt = this_method_class.constantPool.get(index); + OPPUSHD(cstt.value); +ENDDEF + +DEFOP(LDIV) + var value1 = OPPOP(); + var value2 = OPPOP(); + if(value2 == 0){ + JVM_THROWS_NEW(java.lang.ArithmeticException); + } + + var result = Math.round(value1 / value2); + OPPUSH(result); +ENDDEF + +DEFOP(LLOAD) + var index = READ_NEXT(); + OPPUSH(LOCAL_VAR(index)); +ENDDEF + +DEFALIAS(LLOAD_0) +DEFALIAS(LLOAD_1) +DEFALIAS(LLOAD_2) +DEFALIAS(LLOAD_3) +DEFNOP() + OPPUSH(OPCODE - LLOAD_0); +ENDDEF + +DEFOP(LMUL) + var value1 = OPPOP(); + var value2 = OPPOP(); + var result = value1 * value2; + if (IS_OVERFLOW(result,INT_MAX_VALUE)){ + OPPUSH(INT_OVERFLOW(result)); + }else{ + OPPUSH(result); + } +ENDDEF + +DEFOP(LNEG) + var value = OPPOP(); + var result = 0 - value; + if (IS_OVERFLOW(result,INT_MAX_VALUE) || IS_UNDERFLOW(result,INT_MINVALUE)){ + OPPUSH(INT_OVERFLOW(result)); + }else{ + OPPUSH(result); + } +ENDDEF +