Permalink
Browse files

development branch

  • Loading branch information...
1 parent b602e61 commit 4e93f5c58806f00206a3c33e4e1c9e1aa51075b3 Artur Ventura committed Nov 15, 2011
Showing with 75 additions and 32 deletions.
  1. +8 −0 src/auxiliarJNI.js
  2. +9 −5 src/class.js
  3. +13 −0 src/cpu.js
  4. +11 −2 src/internalJNI.jsh
  5. +10 −15 src/intrp.def
  6. +14 −0 src/javaNativeInterface.js
  7. +10 −10 src/types.jsh
View
@@ -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
@@ -231,10 +231,10 @@ ClassDefinition.prototype.initializeClass = function(){
}
this.inited = true;
// call <cinit>
- // Y U NO WORK 4 java.io.Number ?????????
-// if (!(this.access_flags & ACC_INTERFACE)){
-// this["method <clinit>()V"].invoke([],this)
-// }
+ // Y U NO WORK 4 java.lang.Number ?????????
+ if (this["method <clinit>()V"]){
+ this["method <clinit>()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<this.methods_count; 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;
}
View
@@ -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");
View
@@ -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
+ }
}
View
@@ -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
@@ -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)];
}
View
@@ -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

0 comments on commit 4e93f5c

Please sign in to comment.