Skip to content

Commit

Permalink
use proper String objects for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasgal committed Jul 9, 2014
1 parent ee080b9 commit 7972db1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
27 changes: 25 additions & 2 deletions classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,30 @@ Classes.prototype.getStaticMethod = function(className, methodName, signature) {

Classes.prototype.newObject = function(className) {
// Force initialization of the class (if not already done).
this.getClass(className, true);
return { class: this.getClass(className, true) };
}

return {};
Classes.prototype.newArray = function(type, size) {
switch (type) {
case ARRAY_TYPE.T_BOOLEAN: return new Uint8Array(size);
case ARRAY_TYPE.T_CHAR: return new Uint16Array(size);
case ARRAY_TYPE.T_FLOAT: return new Float32Array(size);
case ARRAY_TYPE.T_DOUBLE: return new Float64Array(size);
case ARRAY_TYPE.T_BYTE: return new Int8Array(size);
case ARRAY_TYPE.T_SHORT: return new Int16Array(size);
case ARRAY_TYPE.T_INT: return new Int32Array(size);
case ARRAY_TYPE.T_LONG: return new Int64Array(size);
}
}

Classes.prototype.newString = function(s) {
var obj = this.newObject("java/lang/String");
var length = s.length;
var chars = this.newArray(ARRAY_TYPE.T_CHAR, length);
for (var n = 0; n < length; ++n)
chars[n] = s.charCodeAt(n);
obj.value = chars;
obj.offset = 0;
obj.count = length;
return obj;
}
6 changes: 0 additions & 6 deletions classfile/classarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,12 @@ var getClassImage = function(classBytes) {
var classImage = {};

var getAttribues = function(attribute_name_index, bytes) {

var reader = new Reader(bytes);
var attribute = { attribute_name_index: attribute_name_index };


var item = classImage.constant_pool[attribute_name_index];

switch(item.tag) {

case TAGS.CONSTANT_Long:
case TAGS.CONSTANT_Float:
case TAGS.CONSTANT_Double:
Expand All @@ -74,11 +71,8 @@ var getClassImage = function(classBytes) {
attribute.constantvalue_index = reader.read16();
return attribute;


case TAGS.CONSTANT_Utf8:

switch(item.bytes) {

case ATTRIBUTE_TYPES.Code:
attribute.type = ATTRIBUTE_TYPES.Code;
attribute.max_stack = reader.read16();
Expand Down
6 changes: 3 additions & 3 deletions frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Frame.prototype.ldc = function(done) {
var constant = this._cp[this._read8()];
switch(constant.tag) {
case TAGS.CONSTANT_String:
this._stack.push(this._cp[constant.string_index].bytes);
this._stack.push(CLASSES.newString(this._cp[constant.string_index].bytes));
break;
default:
throw new Error("not support constant type");
Expand Down Expand Up @@ -1163,7 +1163,7 @@ Frame.prototype.newarray = function(done) {
if (size < 0) {
return this._newException("java/lang/NegativeSizeException", done);
}
this._stack.push(new Array(size));
this._stack.push(CLASSES.newArray(type, size));
return done();
}

Expand Down Expand Up @@ -1684,7 +1684,7 @@ Frame.prototype.instanceof = function(done) {
var idx = this._read16();
var className = this._cp[this._cp[idx].name_index].bytes;
var obj = this._stack.pop();
if (obj.getClassName() === className) {
if (obj.class.getClassName() === className) {
this._stack.push(true);
} else {
this._stack.push(false);
Expand Down

0 comments on commit 7972db1

Please sign in to comment.