development branch

This commit is contained in:
Artur Ventura 2011-11-15 11:26:58 +00:00
parent db2b49e22a
commit fe48706b77
7 changed files with 75 additions and 32 deletions

8
src/auxiliarJNI.js Normal file
View File

@ -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
}

View File

@ -231,10 +231,10 @@ ClassDefinition.prototype.initializeClass = function(){
} }
this.inited = true; this.inited = true;
// call <cinit> // call <cinit>
// Y U NO WORK 4 java.io.Number ????????? // Y U NO WORK 4 java.lang.Number ?????????
// if (!(this.access_flags & ACC_INTERFACE)){ if (this["method <clinit>()V"]){
// this["method <clinit>()V"].invoke([],this) this["method <clinit>()V"].invoke([],this)
// } }
} }
ClassDefinition.prototype.calculateEffectiveMembers = function(){ ClassDefinition.prototype.calculateEffectiveMembers = function(){
@ -255,13 +255,17 @@ ClassDefinition.prototype.calculateEffectiveMembers = function(){
// methods // methods
this.effectiveMethods = {} this.effectiveMethods = {}
for(var k in superEffective[1]){ for(var k in superEffective[1]){
this.effectiveMethods[k] = superEffective[1][k]; this.effectiveMethods[k] = superEffective[1][k];
this[k] = superEffective[1][k]; this[k] = superEffective[1][k];
} }
for(var i=0; i<this.methods_count; i++){ for(var i=0; i<this.methods_count; i++){
var method = this.methods[i] var method = this.methods[i]
this.effectiveMethods["method " + method.name_ref.str + method.descriptor_ref.str] = method; if (!(method.name_ref.str + method.descriptor_ref.str === "<clinit>()V")){
this.effectiveMethods["method " + method.name_ref.str + method.descriptor_ref.str] = method;
}
this["method " + method.name_ref.str + method.descriptor_ref.str] = method; this["method " + method.name_ref.str + method.descriptor_ref.str] = method;
} }

View File

@ -63,6 +63,7 @@ var JVM = function(params,args){
} }
if(!loaded_class.inited){ if(!loaded_class.inited){
loaded_class.initializeClass(); loaded_class.initializeClass();
LOG("[Inited " + name + "]");
} }
return loaded_class; return loaded_class;
@ -89,9 +90,21 @@ var JVM = function(params,args){
that.verifyAndLoadClass(canonicalName(constant.name_ref)); that.verifyAndLoadClass(canonicalName(constant.name_ref));
}, CONSTANT_Class);*/ }, 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.run = function (){
this.java_lang_object = this.classForName("java.lang.Object"); 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_lang_cloneable = this.classForName("java.lang.Cloneable");
this.java_io_serializable = this.classForName("java.io.Serializable"); this.java_io_serializable = this.classForName("java.io.Serializable");
this.java_lang_string = this.classForName("java.lang.String"); this.java_lang_string = this.classForName("java.lang.String");

View File

@ -11,9 +11,18 @@
{ {
'java.lang.System' : { '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' : { 'java.lang.Object' : {
'method registerNatives()V' : java_lang_Object_registerNatives '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
}
} }

View File

@ -936,26 +936,22 @@ DEFOP(INVOKESTATIC)
var args = OPSTACK_MULTIPOP(OPSTACK_LENGTH() - method.descriptor.args.length); var args = OPSTACK_MULTIPOP(OPSTACK_LENGTH() - method.descriptor.args.length);
var result; var result;
if(method.access_flags & ACC_NATIVE){ 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); var className = canonicalName(cl.this_class.name_ref);
if (className in xl.jvm.internalJNITable){ LOG("Calling " + className + " " + method.name_ref.str + method.descriptor_ref.str)
if (methodId in xl.jvm.internalJNITable[className]){ LOG("!! NATIVE !!")
if (xl.jvm.internalJNITable[className] && methodId in xl.jvm.internalJNITable[className]){
result = xl.jvm.internalJNITable[className][methodId].apply(cl,args); 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) result = xl.jvm.JNITable[className][methodId].apply(cl,args)
}else{ }else{
PANIC(methodId + " declared as native but not mapped"); PANIC(methodId + " declared as native but not mapped");
} }
LOG("Returing from " + xl.className+ " " + method.name_ref.str + method.descriptor_ref.str) LOG("Returing from " + className + " " + method.name_ref.str + method.descriptor_ref.str)
}else{
PANIC(className + " has no native mappings");
}
}else{ }else{
result = method.invoke(args,cl); result = method.invoke(args,cl);
} }
if (result != undefined){ if (result != undefined){
OPPUSH(result.return_object); OPPUSH(result);
} }
ENDDEF ENDDEF
@ -1455,12 +1451,11 @@ DEFOP(PUTSTATIC)
var indexbyte2 = READ_NEXT(); var indexbyte2 = READ_NEXT();
var value = OPPOP(); var value = OPPOP();
CHECK_NULL(objectref)
var field = xl.constantPool.get((indexbyte1 << 8) | indexbyte2); 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 //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 ENDDEF
DEFOP(RET) DEFOP(RET)
@ -1494,8 +1489,8 @@ DEFOP(SASTORE)
ENDDEF ENDDEF
DEFOP(SIPUSH) DEFOP(SIPUSH)
var byte1 = OPPOP(); var byte1 = READ_NEXT();
var byte2 = OPPOP(); var byte2 = READ_NEXT();
OPPUSH((byte1 << 8) | byte2); OPPUSH((byte1 << 8) | byte2);
ENDDEF ENDDEF

View File

@ -20,4 +20,18 @@ function java_lang_System_arraycopy(src,srcPos,dest,destPos,length){
//java.lang.Object registerNatives()V //java.lang.Object registerNatives()V
function java_lang_Object_registerNatives(){ 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)];
} }

View File

@ -13,16 +13,16 @@
#define _TYPES_JSH_ #define _TYPES_JSH_
// Local Variables Types // Local Variables Types
#define LOC_VAR_boolean 0x001; #define T_boolean 0x001
#define LOC_VAR_byte 0x002; #define T_byte 0x002
#define LOC_VAR_char 0x004; #define T_char 0x004
#define LOC_VAR_short 0x008; #define T_short 0x008
#define LOC_VAR_int 0x010; #define T_int 0x010
#define LOC_VAR_float 0x020; #define T_float 0x020
#define LOC_VAR_reference 0x040; #define T_reference 0x040
#define LOC_VAR_returnAddress 0x080; #define T_returnAddress 0x080
#define LOC_VAR_long 0x100; #define T_long 0x100
#define LOC_VAR_double 0x200; #define T_double 0x200
// Reference Types // Reference Types
#define REF_TYPE_class 0x1 #define REF_TYPE_class 0x1