diff --git a/src/auxiliarJNI.js b/src/auxiliarJNI.js new file mode 100644 index 0000000..4ccba62 --- /dev/null +++ b/src/auxiliarJNI.js @@ -0,0 +1,8 @@ +function javaString2JS (string) { + var arr = string['java/lang/String value']['value']; + var result = ""; + for (var i=0; i < arr.length; i++) { + result += String.fromCharCode(arr[i]); + }; + return result +} \ No newline at end of file diff --git a/src/class.js b/src/class.js index 636533b..d5f9e93 100644 --- a/src/class.js +++ b/src/class.js @@ -231,10 +231,10 @@ ClassDefinition.prototype.initializeClass = function(){ } this.inited = true; // call - // Y U NO WORK 4 java.io.Number ????????? -// if (!(this.access_flags & ACC_INTERFACE)){ -// this["method ()V"].invoke([],this) -// } + // Y U NO WORK 4 java.lang.Number ????????? + if (this["method ()V"]){ + this["method ()V"].invoke([],this) + } } ClassDefinition.prototype.calculateEffectiveMembers = function(){ @@ -255,13 +255,17 @@ ClassDefinition.prototype.calculateEffectiveMembers = function(){ // methods this.effectiveMethods = {} for(var k in superEffective[1]){ + this.effectiveMethods[k] = superEffective[1][k]; this[k] = superEffective[1][k]; } for(var i=0; i()V")){ + this.effectiveMethods["method " + method.name_ref.str + method.descriptor_ref.str] = method; + } + this["method " + method.name_ref.str + method.descriptor_ref.str] = method; } diff --git a/src/cpu.js b/src/cpu.js index 3bc1d53..cce4961 100644 --- a/src/cpu.js +++ b/src/cpu.js @@ -63,6 +63,7 @@ var JVM = function(params,args){ } if(!loaded_class.inited){ loaded_class.initializeClass(); + LOG("[Inited " + name + "]"); } return loaded_class; @@ -89,9 +90,21 @@ var JVM = function(params,args){ that.verifyAndLoadClass(canonicalName(constant.name_ref)); }, CONSTANT_Class);*/ }; + this.createPrimitiveTypes = function() { + this.primitive_boolean = {type: T_boolean, 'toString' : instanceToString, 'class' : this.java_lang_class}; + this.primitive_byte = {type: T_byte, 'toString' : instanceToString, 'class' : this.java_lang_class}; + this.primitive_char = {type: T_char, 'toString' : instanceToString, 'class' : this.java_lang_class}; + this.primitive_short = {type: T_short, 'toString' : instanceToString, 'class' : this.java_lang_class}; + this.primitive_int = {type: T_int, 'toString' : instanceToString, 'class' : this.java_lang_class}; + this.primitive_float = {type: T_float, 'toString' : instanceToString, 'class' : this.java_lang_class}; + this.primitive_long = {type: T_long, 'toString' : instanceToString, 'class' : this.java_lang_class}; + this.primitive_double = {type: T_double, 'toString' : instanceToString, 'class' : this.java_lang_class}; + }; this.run = function (){ this.java_lang_object = this.classForName("java.lang.Object"); + this.java_lang_class = this.classForName("java.lang.Class"); + this.createPrimitiveTypes(); 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"); diff --git a/src/internalJNI.jsh b/src/internalJNI.jsh index 7b916f9..279a33e 100644 --- a/src/internalJNI.jsh +++ b/src/internalJNI.jsh @@ -11,9 +11,18 @@ { 'java.lang.System' : { - 'method arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V' : java_lang_System_arraycopy + 'method arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V' : java_lang_System_arraycopy, + 'method registerNatives()V' : java_lang_System_registerNatives, + 'method currentTimeMillis()J' : java_lang_System_currentTimeMillis }, 'java.lang.Object' : { 'method registerNatives()V' : java_lang_Object_registerNatives - } + }, + 'java.lang.String' : { + 'method arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V' : java_lang_System_arraycopy + }, + 'java.lang.Class' : { + 'method registerNatives()V' : java_lang_Object_registerNatives, + 'method getPrimitiveClass(Ljava/lang/String;)Ljava/lang/Class;' : java_lang_Class_getPrimitiveClass + } } \ No newline at end of file diff --git a/src/intrp.def b/src/intrp.def index 1075495..b43ac60 100644 --- a/src/intrp.def +++ b/src/intrp.def @@ -936,26 +936,22 @@ DEFOP(INVOKESTATIC) var args = OPSTACK_MULTIPOP(OPSTACK_LENGTH() - method.descriptor.args.length); var result; if(method.access_flags & ACC_NATIVE){ - LOG("Calling " + xl.className+ " " + method.name_ref.str + method.descriptor_ref.str) - LOG("!! NATIVE !!") var className = canonicalName(cl.this_class.name_ref); - if (className in xl.jvm.internalJNITable){ - if (methodId in xl.jvm.internalJNITable[className]){ + LOG("Calling " + className + " " + method.name_ref.str + method.descriptor_ref.str) + LOG("!! NATIVE !!") + if (xl.jvm.internalJNITable[className] && methodId in xl.jvm.internalJNITable[className]){ result = xl.jvm.internalJNITable[className][methodId].apply(cl,args); - }else if (methodId in xl.jvm.JNITable[className][methodId]){ + }else if (xl.jvm.JNITable[className] && methodId in xl.jvm.JNITable[className]){ result = xl.jvm.JNITable[className][methodId].apply(cl,args) }else{ PANIC(methodId + " declared as native but not mapped"); } - LOG("Returing from " + xl.className+ " " + method.name_ref.str + method.descriptor_ref.str) - }else{ - PANIC(className + " has no native mappings"); - } + LOG("Returing from " + className + " " + method.name_ref.str + method.descriptor_ref.str) }else{ result = method.invoke(args,cl); } if (result != undefined){ - OPPUSH(result.return_object); + OPPUSH(result); } ENDDEF @@ -1455,12 +1451,11 @@ DEFOP(PUTSTATIC) var indexbyte2 = READ_NEXT(); var value = OPPOP(); - CHECK_NULL(objectref) var field = xl.constantPool.get((indexbyte1 << 8) | indexbyte2); - var aClass = field.class_ref.name_ref.str; + var aClass = field.class_ref.jvmClassName; //check if static - jvm.classForName(aClass)[canonicalName(field.class_ref.name_ref) + " " + field.name_and_type_ref.name_ref.str] = value; + xl.jvm.classForName(aClass)[canonicalName(field.class_ref.name_ref) + " " + field.name_and_type_ref.name_ref.str] = value; ENDDEF DEFOP(RET) @@ -1494,8 +1489,8 @@ DEFOP(SASTORE) ENDDEF DEFOP(SIPUSH) - var byte1 = OPPOP(); - var byte2 = OPPOP(); + var byte1 = READ_NEXT(); + var byte2 = READ_NEXT(); OPPUSH((byte1 << 8) | byte2); ENDDEF diff --git a/src/javaNativeInterface.js b/src/javaNativeInterface.js index f3bfd62..0a12422 100644 --- a/src/javaNativeInterface.js +++ b/src/javaNativeInterface.js @@ -20,4 +20,18 @@ function java_lang_System_arraycopy(src,srcPos,dest,destPos,length){ //java.lang.Object registerNatives()V function java_lang_Object_registerNatives(){ +} + +//java.lang.System registerNatives()V +function java_lang_System_registerNatives(){ + +} + +//java.lang.System currentTimeMillis()J +function java_lang_System_currentTimeMillis(){ + return math.Long.fromInt(new Date().getTime()); +} +//java.lang.Class getPrimitiveClass(Ljava/lang/String;)Ljava/lang/Class; +function java_lang_Class_getPrimitiveClass (type) { + return this.jvm['primitive_' + javaString2JS(type)]; } \ No newline at end of file diff --git a/src/types.jsh b/src/types.jsh index e07faef..609d55d 100644 --- a/src/types.jsh +++ b/src/types.jsh @@ -13,16 +13,16 @@ #define _TYPES_JSH_ // Local Variables Types -#define LOC_VAR_boolean 0x001; -#define LOC_VAR_byte 0x002; -#define LOC_VAR_char 0x004; -#define LOC_VAR_short 0x008; -#define LOC_VAR_int 0x010; -#define LOC_VAR_float 0x020; -#define LOC_VAR_reference 0x040; -#define LOC_VAR_returnAddress 0x080; -#define LOC_VAR_long 0x100; -#define LOC_VAR_double 0x200; +#define T_boolean 0x001 +#define T_byte 0x002 +#define T_char 0x004 +#define T_short 0x008 +#define T_int 0x010 +#define T_float 0x020 +#define T_reference 0x040 +#define T_returnAddress 0x080 +#define T_long 0x100 +#define T_double 0x200 // Reference Types #define REF_TYPE_class 0x1