can load java bytecode

This commit is contained in:
Artur Ventura 2011-07-03 21:43:11 +01:00
parent 2afcb5bce3
commit 3be42dabb1
5 changed files with 169 additions and 11 deletions

View File

@ -1,3 +1,39 @@
var ExceptionTableEntry = function(dStream, constantPool){
this.start_pc = dStream.getU2();
this.end_pc = dStream.getU2();
this.handler_pc = dStream.getU2();
var catchType = dStream.getU2();
if (catchType){
this.catch_type = ConstantPoolRef(catchType, constantPool,CONSTANT_Class);
}else{
this.catch_type = null;
}
}
var InnerClass = function(dStream, constantPool){
this.inner_class_info = ConstantPoolRef(dStream.getU2(), constantPool,CONSTANT_Class);
var outerClassIndex = dStream.getU2();
if (outerClassIndex){
this.outer_class_info = ConstantPoolRef(outerClassIndex, constantPool,CONSTANT_Class);
}else{
this.outer_class_info = null;
}
this.inner_name = ConstantPoolRef(dStream.getU2(), constantPool,CONSTANT_Utf8);
this.inner_class_access_flags = dStream.getU2();
}
var LineNumberTableEntry = function(dStream){
this.start_pt = dStream.getU2();
this.line_number = dStream.getU2();
}
var LocalVariableTableEntry = function(dStream, constantPool){
this.start_pc = dStream.getU2();
this.length =dStream.getU2();
this.name = ConstantPoolRef(dStream.getU2(), constantPool, CONSTANT_Utf8);
this.descriptor = ConstantPoolRef(dStream.getU2(), constantPool, CONSTANT_Utf8);
this.index = dStream.getU2();
}
var Attributes_table = {
ConstantValue: function(){
@ -19,7 +55,100 @@ var Attributes_table = {
Code: function(){
this.read = function(dStream, constantPool){
this.max_stack = dStream.getU2();
this.max_locals = dStream.getU2();
this.code_length = dStream.getU4();
this.code = [];
for (var i=0; i< this.code_length; i++){
this.code[i] = dStream.getU1();
}
this.exception_table_length = dStream.getU2();
this.exception_table = [];
for (var i=0; i< this.exception_table_length; i++){
this.exception_table[i] = new ExceptionTableEntry(dStream, constantPool);
if (this.start_pc >= this.exception_table_length){
throw "Code attr Invalid Exception Table Entry, start_pc >= table length";
}
if (this.end_pc > this.exception_table_length){
throw "Code attr Invalid Exception Table Entry, end_pc > table length";
}
if (this.start_pc > this.end_pc){
throw "Code attr Invalid Exception Table Entry, start_pc > end_pc";
}
if (this.start_pc >= this.exception_table_length){
throw "Code attr Invalid Exception Table Entry, handler_pc >= table length";
}
}
this.attributes_count = dStream.getU2();
this.attributes = [];
for(var i=0; i<this.attributes_count; i++){
this.attributes[i] = Attribute(dStream,constantPool);
}
}
},
Exceptions: function(){
this.read = function(dStream, constantPool){
this.number_of_exceptions = dStream.getU2();
this.exception_table = [];
for(var i=0; i<this.number_of_exceptions; i++){
this.exception_table[i] = ConstantPoolRef(dStream.getU2(), constantPool,CONSTANT_Class);
}
}
},
InnerClasses: function(){
this.read = function(dStream, constantPool){
this.number_of_classes = dStream.getU2();
this.classes = [];
for (var i=0; i<this.number_of_classes; i++){
this.classes[i] = new InnerClass(dStream,constantPool);
}
}
},
Synthetic: function(){
this.read = function(dStream,constantPool){
if (this.attribute_length != 0){
throw "Synthetic Attr length not 0";
}
}
},
SourceFile: function(){
this.read = function(dStream,constantPool){
this.soucefile = ConstantPoolRef(dStream.getU2(), constantPool,CONSTANT_Utf8);
}
},
LineNumberTable: function(){
this.read = function(dStream,constantPool){
this.line_number_table_length = dStream.getU2();
this.line_number_table = [];
for (var i=0; i<this.line_number_table_length; i++){
this.line_number_table[i] = new LineNumberTableEntry(dStream);
}
}
},
LocalVariableTable: function(){
this.read=function(dStream,constantPool){
this.local_variable_table_length = dStream.getU2();
this.local_variable_table = [];
for(var i=0; i<local_variable_table_length; i++){
this.local_variable_table[i] = new LocalVariableTableEntry(dSteam,constantPool);
}
}
},
Deprecated: function(){
this.read = function(dStream,constantPool){
if (this.attribute_length != 0){
throw "Synthetic Attr length not 0";
}
}
}
};
@ -45,4 +174,5 @@ var Attribute = function(dStream, constantPool){
result.attribute_name = attribute_name;
result.attribute_length = dStream.getU4();
result.read(dStream, constantPool);
return result;
}

View File

@ -236,7 +236,6 @@ var ConstantPool = function(dStream){
this.constantPool = [];
for(var i = 1; i < this.constantPoolCount; i++){
var tag = dStream.getU1();
log(constTagName(tag));
var alloc = allocConstEntry(tag);
alloc.read(dStream);
this.constantPool[(i-1)] = alloc;
@ -296,6 +295,9 @@ var constTagName = function (info){
}
var ConstantPoolRef = function(index, constantPool, expected){
if (index-1 < 0 || index-1 >= constantPool.constantPool.length){
throw "ConstantPoolRef: ref out of bounds: " + (index-1).toString() + ", length: " + constantPool.constantPool.length;
}
var result = constantPool.constantPool[index - 1];
if (expected && result.id != expected){
throw "ConstantPoolRef: ref was expected to be " + constTagName(expected) + " but at " + index + " there's a " + constTagName(result.id);

View File

@ -1,8 +0,0 @@
var FieldInfo = function(dStream){
this.access_flags = dStream.getU2();
this.name_index = dStream.getU2();
this.descriptor_index = dStream.getU2();
this.attributes_count = dStream.getU2();
// VER: attributes must be restricted.
this.attributes = [];
}

21
infos.js Normal file
View File

@ -0,0 +1,21 @@
var FieldInfo = function(dStream,constantPool){
this.access_flags = dStream.getU2();
this.name_index = ConstantPoolRef(dStream.getU2(), constantPool, CONSTANT_Utf8);
this.descriptor_index = ConstantPoolRef(dStream.getU2(), constantPool, CONSTANT_Utf8);
this.attributes_count = dStream.getU2();
this.attributes = [];
for (var i=0; i<this.attributes_count; i++){
this.attributes[i] = Attribute(dStream,constantPool);
}
}
var MethodInfo = function(dStream, constantPool){
this.access_flags = dStream.getU2();
this.name_index = ConstantPoolRef(dStream.getU2(), constantPool, CONSTANT_Utf8);
this.descriptor_index = ConstantPoolRef(dStream.getU2(), constantPool, CONSTANT_Utf8);
this.attributes_count = dStream.getU2();
this.attributes = [];
for (var i=0; i<this.attributes_count; i++){
this.attributes[i] = Attribute(dStream,constantPool);
}
}

17
main.js
View File

@ -1,6 +1,8 @@
include("linearDataStream.js");
include("constantPool.js");
include("attributes.js");
include("infos.js");
// access flags DEFINE
var ACC_PUBLIC = 0x0001; // Declared public; may be accessed from outside its package.
@ -10,6 +12,7 @@ var ACC_STATIC = 0x0008; // Declared static.
var ACC_FINAL = 0x0010; // Declared final; no subclasses allowed.
var ACC_SUPER = 0x0020; // Treat superclass methods specially when invoked by the invokespecial instruction.
var ACC_VOLATILE = 0x0040; // Declared volatile; cannot be cached.
var ACC_NATIVE = 0x0100; // Declared native; implemented in a language other than Java.
var ACC_INTERFACE = 0x0200; // Is an interface, not a class.
var ACC_ABSTRACT = 0x0400; // Declared abstract; may not be instantiated.
var ACC_TRANSIENT = 0x0080; // Declared transient; not written or read by a persistent object manager.
@ -61,8 +64,7 @@ ClassDefinition = function (file){
if (this.magic != 0xCAFEBABE){
throw "Invalid Class Magic (" + this.magic + ")" ;
}
this.minorVersion = dataStream.getU2();
this.minorVersion = dataStream.getU2();
this.majorVersion = dataStream.getU2();
if (this.majorVersion > 50 || this.majorVersion < 45){
throw "Unsuported java class file format version";
@ -81,6 +83,17 @@ ClassDefinition = function (file){
}
this.fields_count = dataStream.getU2();
this.fields = []
for(var i=0; i<this.fields_count; i++){
this.fields[i] = new FieldInfo(dataStream,this.constantPool);
}
this.methods_count = dataStream.getU2();
this.methods=[];
for(var i=0; i<this.methods_count; i++){
this.methods[i] = new MethodInfo(dataStream, this.constantPool);
}
}
function main (args){