constant pool structure finished
This commit is contained in:
parent
1e5fd9944a
commit
50b18121b3
165
constantPool.js
165
constantPool.js
@ -10,49 +10,158 @@ var CONSTANT_Double = 6;
|
|||||||
var CONSTANT_NameAndType = 12;
|
var CONSTANT_NameAndType = 12;
|
||||||
var CONSTANT_Utf8 = 1;
|
var CONSTANT_Utf8 = 1;
|
||||||
|
|
||||||
constUtf8 = function(){
|
// constant pool members
|
||||||
|
|
||||||
|
var constUtf8 = function(){
|
||||||
this.str = null;
|
this.str = null;
|
||||||
this.read = ( dStream ) {
|
this.id = CONSTANT_Utf8;
|
||||||
StringBuffer strBuf;
|
this.read = function ( dStream ) {
|
||||||
int len, charCnt;
|
var strBuf;
|
||||||
byte one_byte;
|
var len, charCnt;
|
||||||
char one_char;
|
var one_byte;
|
||||||
|
var one_char;
|
||||||
|
|
||||||
one_char = '\u0000';
|
one_char = '\u0000';
|
||||||
len = readU2( dStream );
|
len = dStream.getU2();
|
||||||
strBuf = new StringBuffer();
|
strBuf = "";
|
||||||
charCnt = 0;
|
charCnt = 0;
|
||||||
while (charCnt < len) {
|
while (charCnt < len) {
|
||||||
one_byte = (byte)readU1( dStream );
|
one_byte = dStream .getU1();
|
||||||
charCnt++;
|
charCnt++;
|
||||||
if ((one_byte >> 7) == 1) {
|
if ((one_byte >> 7) == 1) {
|
||||||
short tmp;
|
var tmp;
|
||||||
|
|
||||||
// its a multi-byte character
|
// its a multi-byte character
|
||||||
tmp = (short)(one_byte & 0x3f); // Bits 5..0 (six bits)
|
tmp = (one_byte & 0x3f); // Bits 5..0 (six bits)
|
||||||
// read the next byte
|
// read the next byte
|
||||||
one_byte = (byte)readU1( dStream );
|
one_byte = dStream .getU1();
|
||||||
charCnt++;
|
charCnt++;
|
||||||
tmp = (short)(tmp | ((one_byte & 0x3f) << 6));
|
tmp = (tmp | ((one_byte & 0x3f) << 6));
|
||||||
if ((one_byte >> 6) == 0x2) {
|
if ((one_byte >> 6) == 0x2) {
|
||||||
// We have 12 bits so far, get bits 15..12
|
// We have 12 bits so far, get bits 15..12
|
||||||
one_byte = (byte)readU1( dStream );
|
one_byte = dStream .getU1();
|
||||||
charCnt++;
|
charCnt++;
|
||||||
one_byte = (byte)(one_byte & 0xf);
|
one_byte = (one_byte & 0xf);
|
||||||
tmp = (short)(tmp | (one_byte << 12));
|
tmp = (tmp | (one_byte << 12));
|
||||||
}
|
}
|
||||||
one_char = (char)tmp;
|
one_char = tmp;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
one_char = (char)one_byte;
|
one_char = one_byte;
|
||||||
}
|
}
|
||||||
strBuf.append(one_char);
|
strBuf += String.fromCharCode(one_char);
|
||||||
} // while
|
} // while
|
||||||
this.str = strBuf.toString();
|
|
||||||
|
this.str = strBuf.toString();
|
||||||
} // read
|
} // read
|
||||||
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
allocConstEntry = function(tag){
|
|
||||||
|
var constDummy = function(){
|
||||||
|
this.read = function (stream){};
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
var constInt = function(){
|
||||||
|
this.value = null;
|
||||||
|
this.id = CONSTANT_Integer;
|
||||||
|
this.read = function ( dSStream ){
|
||||||
|
this.value = dStream.getU4();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var constFloat = function(){
|
||||||
|
this.value = null;
|
||||||
|
this.id = CONSTANT_Float;
|
||||||
|
this.read = function ( dSStream ){
|
||||||
|
this.value = dStream.getU4();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var constLong = function(){
|
||||||
|
this.high = null;
|
||||||
|
this.low = null;
|
||||||
|
this.id = CONSTANT_Long;
|
||||||
|
this.read = function (dStream){
|
||||||
|
this.high = dStream.getU4();
|
||||||
|
this.low = dStream.getU4();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var constDouble = function(){
|
||||||
|
this.high = null;
|
||||||
|
this.low = null;
|
||||||
|
this.id = CONSTANT_Double;
|
||||||
|
this.read = function (dStream){
|
||||||
|
this.high = dStream.getU4();
|
||||||
|
this.low = dStream.getU4();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var constClass = function(){
|
||||||
|
this.name_index = null;
|
||||||
|
this.id = CONSTANT_Class;
|
||||||
|
this.read = function(dStream){
|
||||||
|
this.name_index = dstream.getU2();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var constString = function(){
|
||||||
|
this.string_index = null;
|
||||||
|
this.id = CONSTANT_String;
|
||||||
|
this.read = function(dStream){
|
||||||
|
this.string_index = dstream.getU2();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var constString = function(){
|
||||||
|
this.string_index = null;
|
||||||
|
this.id = CONSTANT_String;
|
||||||
|
this.read = function(dStream){
|
||||||
|
this.string_index = dstream.getU2();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var constRef = function(){
|
||||||
|
this.class_index = null;
|
||||||
|
this.name_and_type_index = null;
|
||||||
|
this.read = function(dStream){
|
||||||
|
this.class_index = dStream.getU2();
|
||||||
|
this.name_and_type_index = dStream.getU2();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var constFieldRef = function(){
|
||||||
|
var temp = new constRef();
|
||||||
|
temp.id = CONSTANT_Fieldref;
|
||||||
|
return temp;
|
||||||
|
};
|
||||||
|
|
||||||
|
var constMethodRef = function(){
|
||||||
|
var temp = new constRef();
|
||||||
|
temp.id = CONSTANT_Methodref;
|
||||||
|
return temp;
|
||||||
|
};
|
||||||
|
|
||||||
|
var constInterfaceMethodRef = function(){
|
||||||
|
var temp = new constRef();
|
||||||
|
temp.id = CONSTANT_InterfaceMethodref;
|
||||||
|
return temp;
|
||||||
|
};
|
||||||
|
|
||||||
|
var constName_and_Type_info = function(){
|
||||||
|
this.name_index = null;
|
||||||
|
this.descriptor_index = null;
|
||||||
|
this.id = CONSTANT_NameAndType;
|
||||||
|
this.read = function(dStream){
|
||||||
|
this.name_index = dStream.getU2();
|
||||||
|
this.descriptor_index = dStream.getU2();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var allocConstEntry = function(tag){
|
||||||
var obj = null;
|
var obj = null;
|
||||||
|
|
||||||
switch ( tag ) {
|
switch ( tag ) {
|
||||||
@ -72,19 +181,27 @@ allocConstEntry = function(tag){
|
|||||||
obj = new constDouble();
|
obj = new constDouble();
|
||||||
break;
|
break;
|
||||||
case CONSTANT_Class:
|
case CONSTANT_Class:
|
||||||
|
obj = new constClass();
|
||||||
|
break;
|
||||||
case CONSTANT_String:
|
case CONSTANT_String:
|
||||||
obj = new constClass_or_String();
|
obj = new constString();
|
||||||
break;
|
break;
|
||||||
case CONSTANT_Fieldref:
|
case CONSTANT_Fieldref:
|
||||||
|
obj = new constFieldRef();
|
||||||
|
break;
|
||||||
case CONSTANT_Methodref:
|
case CONSTANT_Methodref:
|
||||||
|
obj = new constMethodRef();
|
||||||
|
break;
|
||||||
case CONSTANT_InterfaceMethodref:
|
case CONSTANT_InterfaceMethodref:
|
||||||
obj = new constRef();
|
obj = new constInterfaceMethodRef();
|
||||||
break;
|
break;
|
||||||
case CONSTANT_NameAndType:
|
case CONSTANT_NameAndType:
|
||||||
obj = new constName_and_Type_info();
|
obj = new constName_and_Type_info();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println("allocConstEntry: bad tag value = " + tag);
|
obj = new constDummy();
|
||||||
|
//throw "allocConstEntry: bad tag value = " + tag;
|
||||||
break;
|
break;
|
||||||
} // switch
|
} // switch
|
||||||
|
return obj;
|
||||||
}
|
}
|
@ -2,6 +2,12 @@
|
|||||||
<head>
|
<head>
|
||||||
<title>JS JVM</title>
|
<title>JS JVM</title>
|
||||||
<script src="preload.js" type="text/javascript"></script>
|
<script src="preload.js" type="text/javascript"></script>
|
||||||
|
<script>
|
||||||
|
write = function(msg){
|
||||||
|
var l = document.getElementById("log");
|
||||||
|
l.innerHTML = l.innerHTML + msg + "\n";
|
||||||
|
}
|
||||||
|
</script>
|
||||||
<script src="main.js" type="text/javascript"></script>
|
<script src="main.js" type="text/javascript"></script>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||||
<style>
|
<style>
|
||||||
@ -29,5 +35,6 @@
|
|||||||
<body onload="main()">
|
<body onload="main()">
|
||||||
<h1>JS JVM</h1>
|
<h1>JS JVM</h1>
|
||||||
Open JavaScript Debug Window to read the debug;
|
Open JavaScript Debug Window to read the debug;
|
||||||
|
<pre id="log"></pre>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,47 +1,73 @@
|
|||||||
DataStream = function(data){
|
var DataStream = function(data){
|
||||||
this.i = 0;
|
this.i = 0;
|
||||||
this.getU = function(size){
|
|
||||||
switch(size){
|
|
||||||
case 1:
|
|
||||||
this.constantPoolCount = new DataView(data,this.i,1).getUint8(0);
|
|
||||||
this.i += 1;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
this.constantPoolCount = new DataView(data,this.i,2).getUint16(0);
|
|
||||||
this.i += 2;
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
this.constantPoolCount = new DataView(data,this.i,4).getUint32(0);
|
|
||||||
this.i += 4;
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
this.constantPoolCount = new DataView(data,this.i,8).getUint64(0);
|
|
||||||
this.i += 8;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.get = function(size){
|
this.getF = function(size){
|
||||||
switch(size){
|
var result;
|
||||||
case 1:
|
switch(size){
|
||||||
this.constantPoolCount = new DataView(data,this.i,1).getInt8(0);
|
case 4:
|
||||||
this.i += 1;
|
result = new DataView(data,this.i,4).getFloat32(0);
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
this.constantPoolCount = new DataView(data,this.i,2).getInt16(0);
|
|
||||||
this.i += 2;
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
this.constantPoolCount = new DataView(data,this.i,4).getInt32(0);
|
|
||||||
this.i += 4;
|
this.i += 4;
|
||||||
break;
|
break;
|
||||||
case 8:
|
case 8:
|
||||||
this.constantPoolCount = new DataView(data,this.i,8).getInt64(0);
|
result = new DataView(data,this.i,8).getFloat64(0);
|
||||||
this.i += 8;
|
this.i += 8;
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
throw "Weird size float " + size;
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.getU = function(size){
|
||||||
|
var result;
|
||||||
|
switch(size){
|
||||||
|
case 1:
|
||||||
|
result = new DataView(data,this.i,1).getUint8(0);
|
||||||
|
this.i += 1;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
result = new DataView(data,this.i,2).getUint16(0);
|
||||||
|
this.i += 2;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
result = new DataView(data,this.i,4).getUint32(0);
|
||||||
|
this.i += 4;
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
result = new DataView(data,this.i,8).getUint64(0);
|
||||||
|
this.i += 8;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw "Weird size " + size;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.get = function(size){
|
||||||
|
var result;
|
||||||
|
switch(size){
|
||||||
|
case 1:
|
||||||
|
result = new DataView(data,this.i,1).getInt8(0);
|
||||||
|
this.i += 1;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
result = new DataView(data,this.i,2).getInt16(0);
|
||||||
|
this.i += 2;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
result = new DataView(data,this.i,4).getInt32(0);
|
||||||
|
this.i += 4;
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
result = new DataView(data,this.i,8).getInt64(0);
|
||||||
|
this.i += 8;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw "Weird size " + size;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
this.getUint8 = function () { return this.getU(1) }
|
this.getUint8 = function () { return this.getU(1) }
|
||||||
this.getU1 = this.getUint8;
|
this.getU1 = this.getUint8;
|
||||||
this.getUint16 = function () { return this.getU(2) }
|
this.getUint16 = function () { return this.getU(2) }
|
||||||
@ -55,4 +81,7 @@ DataStream = function(data){
|
|||||||
this.getInt16 = function () { return this.get(2) }
|
this.getInt16 = function () { return this.get(2) }
|
||||||
this.getInt32 = function () { return this.get(4) }
|
this.getInt32 = function () { return this.get(4) }
|
||||||
this.getInt64 = function () { return this.get(8) }
|
this.getInt64 = function () { return this.get(8) }
|
||||||
|
this.getFloat32 = function () { return this.getF(4) }
|
||||||
|
this.getFloat64 = function () { return this.getF(8) }
|
||||||
|
return this;
|
||||||
}
|
}
|
15
main.js
15
main.js
@ -1,6 +1,8 @@
|
|||||||
|
|
||||||
include("linearDataStream.js");
|
include("linearDataStream.js");
|
||||||
include("constantPool.js");
|
include("constantPool.js");
|
||||||
|
|
||||||
|
|
||||||
function slurpFile (filename, fa) {
|
function slurpFile (filename, fa) {
|
||||||
var xmlHttpRequest, response, result ;
|
var xmlHttpRequest, response, result ;
|
||||||
// ie support if (typeof ActiveXObject == "function") return this.load_binary_ie9(filename, fa);
|
// ie support if (typeof ActiveXObject == "function") return this.load_binary_ie9(filename, fa);
|
||||||
@ -36,6 +38,7 @@ function slurpFile (filename, fa) {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
log = function (msg){
|
log = function (msg){
|
||||||
|
write(msg);
|
||||||
if (console){
|
if (console){
|
||||||
console.log(msg);
|
console.log(msg);
|
||||||
}
|
}
|
||||||
@ -87,17 +90,19 @@ ConstantPoolStruct = function (tag,info){
|
|||||||
ClassDefinition = function (file){
|
ClassDefinition = function (file){
|
||||||
var dataStream = new DataStream(slurpFile(file)[1]);
|
var dataStream = new DataStream(slurpFile(file)[1]);
|
||||||
this.magic = dataStream.getU4();
|
this.magic = dataStream.getU4();
|
||||||
this.magic == 0xCAFEBABE || alert("Invalid Class Magic");
|
if (this.magic != 0xCAFEBABE){
|
||||||
|
throw "Invalid Class Magic (" + this.magic + ")" ;
|
||||||
|
}
|
||||||
this.minorVersion = dataStream.getU2();
|
this.minorVersion = dataStream.getU2();
|
||||||
this.majorVersion = dataStream.getU2();
|
this.majorVersion = dataStream.getU2();
|
||||||
this.constantPoolCount = dataStream.getU2();
|
this.constantPoolCount = dataStream.getU2();
|
||||||
this.constantPool = [];
|
this.constantPool = [];
|
||||||
for(var i = 1; i <= this.constantPoolCount; i++){
|
for(var i = 1; i <= this.constantPoolCount; i++){
|
||||||
var tag = dataStream.getU1();
|
var tag = dataStream.getU1();
|
||||||
var alloc = allocConstEntry(tag);
|
// new ConstantPoolStruct(tag,0);
|
||||||
|
var alloc = allocConstEntry(tag);
|
||||||
alloc.read(dataStream);
|
alloc.read(dataStream);
|
||||||
var info = new DataView(x, 10 + (i-1) * (1 + 1) + 1,1).getUint8(0);
|
this.constantPool[(i-1)] = alloc;
|
||||||
this.constantPool[(i-1)] = new ConstantPoolStruct(tag,info);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user