Skip to content

Commit

Permalink
Use new bindings for commonly accessed fields and adds check for debu…
Browse files Browse the repository at this point in the history
…g builds.
  • Loading branch information
brendandahl committed Jan 27, 2015
1 parent db3d98d commit 18405ac
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 29 deletions.
39 changes: 39 additions & 0 deletions bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,45 @@ module J2ME {
"priority.I": "priority"
}
}
},
"java/io/ByteArrayOutputStream": {
fields: {
instanceSymbols: {
"count.I": "count",
"buf.[B": "buf"
}
}
},
"com/sun/cldc/i18n/j2me/UTF_8_Writer": {
fields: {
instanceSymbols: {
"pendingSurrogate.I": "pendingSurrogate"
}
}
},
"com/nokia/mid/ui/DirectGraphicsImp": {
fields: {
instanceSymbols: {
"graphics.Ljavax/microedition/lcdui/Graphics;": "graphics"
}
}
},
"javax/microedition/lcdui/Image": {
fields: {
instanceSymbols: {
"imageData.Ljavax/microedition/lcdui/ImageData;": "imageData",
"width.I": "width",
"height.I": "height"
}
}
},
"javax/microedition/lcdui/ImageData": {
fields: {
instanceSymbols: {
"width.I": "width",
"height.I": "height"
}
}
}
}
}
29 changes: 14 additions & 15 deletions midp/gfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ var currentlyFocusedTextEditor;
}

function setImageData(imageData, width, height, data) {
imageData.klass.classInfo.getField("I.width.I").set(imageData, width);
imageData.klass.classInfo.getField("I.height.I").set(imageData, height);
imageData.width = width;
imageData.height = height;
imageData.context = data;
}

Expand Down Expand Up @@ -288,14 +288,14 @@ var currentlyFocusedTextEditor;
};

Native["com/nokia/mid/ui/DirectUtils.makeMutable.(Ljavax/microedition/lcdui/Image;)V"] = function(image) {
var imageData = image.klass.classInfo.getField("I.imageData.Ljavax/microedition/lcdui/ImageData;").get(image);
var imageData = image.imageData;
imageData.klass.classInfo.getField("I.isMutable.Z").set(imageData, 1);
};

Native["com/nokia/mid/ui/DirectUtils.setPixels.(Ljavax/microedition/lcdui/Image;I)V"] = function(image, argb) {
var width = image.klass.classInfo.getField("I.width.I").get(image);
var height = image.klass.classInfo.getField("I.height.I").get(image);
var imageData = image.klass.classInfo.getField("I.imageData.Ljavax/microedition/lcdui/ImageData;").get(image);
var width = image.width;
var height = image.height;
var imageData = image.imageData;

var ctx = createContext2d(width, height);
setImageData(imageData, width, height, ctx);
Expand Down Expand Up @@ -424,7 +424,7 @@ var currentlyFocusedTextEditor;
if (img === null) {
c = MIDP.Context2D;
} else {
var imgData = img.klass.classInfo.getField("I.imageData.Ljavax/microedition/lcdui/ImageData;").get(img),
var imgData = img.imageData,
c = imgData.context;
}
Expand Down Expand Up @@ -761,7 +761,7 @@ var currentlyFocusedTextEditor;
var TYPE_USHORT_565_RGB = 565;

Native["com/nokia/mid/ui/DirectGraphicsImp.setARGBColor.(I)V"] = function(rgba) {
var g = this.klass.classInfo.getField("I.graphics.Ljavax/microedition/lcdui/Graphics;").get(this);
var g = this.graphics;
var red = (rgba >> 16) & 0xff;
var green = (rgba >> 8) & 0xff;
var blue = rgba & 0xff;
Expand All @@ -772,7 +772,7 @@ var currentlyFocusedTextEditor;
};

Native["com/nokia/mid/ui/DirectGraphicsImp.getAlphaComponent.()I"] = function() {
var g = this.klass.classInfo.getField("I.graphics.Ljavax/microedition/lcdui/Graphics;").get(this);
var g = this.graphics;
return (g.pixel >> 24) & 0xff;
};

Expand All @@ -791,12 +791,12 @@ var currentlyFocusedTextEditor;
throw $.newIllegalArgumentException("Format unsupported");
}

var graphics = this.klass.classInfo.getField("I.graphics.Ljavax/microedition/lcdui/Graphics;").get(this);
var graphics = this.graphics;
var image = graphics.img;
if (!image) {
throw $.newIllegalArgumentException("getPixels with no image not yet supported");
}
var imageData = image.klass.classInfo.getField("I.imageData.Ljavax/microedition/lcdui/ImageData;").get(image);
var imageData = image.imageData;

var abgrData = new Int32Array(imageData.context.getImageData(x, y, width, height).data.buffer);
converterFunc(abgrData, pixels, width, height, offset, scanlength);
Expand All @@ -815,7 +815,7 @@ var currentlyFocusedTextEditor;
throw $.newIllegalArgumentException("Format unsupported");
}

var graphics = this.klass.classInfo.getField("I.graphics.Ljavax/microedition/lcdui/Graphics;").get(this);
var graphics = this.graphics;

var context = createContext2d(width, height);
var imageData = context.createImageData(width, height);
Expand All @@ -842,8 +842,7 @@ var currentlyFocusedTextEditor;
};

function renderImage(g, image, x, y, anchor) {
var texture = image.klass.classInfo.getField("I.imageData.Ljavax/microedition/lcdui/ImageData;").get(image)
.context.canvas;
var texture = image.imageData.context.canvas;

var c = withGraphics(g);
c.save();
Expand Down Expand Up @@ -1383,7 +1382,7 @@ var currentlyFocusedTextEditor;
var TRANS_MIRROR_ROT90 = 7;

function renderRegion(g, image, sx, sy, sw, sh, transform, x, y, anchor) {
var imgData = image.klass.classInfo.getField("I.imageData.Ljavax/microedition/lcdui/ImageData;").get(image),
var imgData = image.imageData,
texture = imgData.context.canvas;

var c = withGraphics(g);
Expand Down
6 changes: 3 additions & 3 deletions native.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ Native["java/io/DataOutputStream.UTFToBytes.(Ljava/lang/String;)[B"] = function(
Native["com/sun/cldc/i18n/j2me/UTF_8_Writer.encodeUTF8.([CII)[B"] = function(cbuf, off, len) {
var outputArray = [];

var pendingSurrogate = this.klass.classInfo.getField("I.pendingSurrogate.I").get(this);
var pendingSurrogate = this.pendingSurrogate;

var inputChar = 0;
var outputSize = 0;
Expand Down Expand Up @@ -918,7 +918,7 @@ Native["com/sun/cldc/i18n/j2me/UTF_8_Writer.encodeUTF8.([CII)[B"] = function(cbu
count++;
}

this.klass.classInfo.getField("I.pendingSurrogate.I").set(this, pendingSurrogate);
this.pendingSurrogate = pendingSurrogate;

var totalSize = outputArray.reduce(function(total, cur) {
return total + cur.length;
Expand All @@ -938,7 +938,7 @@ Native["com/sun/cldc/i18n/j2me/UTF_8_Writer.sizeOf.([CII)I"] = function(cbuf, of
var outputSize = 0;
var outputCount = 0;
var count = 0;
var localPendingSurrogate = this.klass.classInfo.getField("I.pendingSurrogate.I").get(this);
var localPendingSurrogate = this.pendingSurrogate;
while (count < length) {
inputChar = 0xffff & cbuf[offset + count];
if (0 != localPendingSurrogate) {
Expand Down
16 changes: 8 additions & 8 deletions override.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,35 @@ Override["java/io/ByteArrayOutputStream.write.([BII)V"] = function(b, off, len)
return;
}

var count = this.klass.classInfo.getField("I.count.I").get(this);
var buf = this.klass.classInfo.getField("I.buf.[B").get(this);
var count = this.count;
var buf = this.buf;

var newcount = count + len;
if (newcount > buf.length) {
var newbuf = J2ME.newByteArray(Math.max(buf.length << 1, newcount));
newbuf.set(buf);
buf = newbuf;
this.klass.classInfo.getField("I.buf.[B").set(this, buf);
this.buf = buf;
}

buf.set(b.subarray(off, off + len), count);
this.klass.classInfo.getField("I.count.I").set(this, newcount);
this.count = newcount;
};

Override["java/io/ByteArrayOutputStream.write.(I)V"] = function(value) {
var count = this.klass.classInfo.getField("I.count.I").get(this);
var buf = this.klass.classInfo.getField("I.buf.[B").get(this);
var count = this.count;
var buf = this.buf;

var newcount = count + 1;
if (newcount > buf.length) {
var newbuf = J2ME.newByteArray(Math.max(buf.length << 1, newcount));
newbuf.set(buf);
buf = newbuf;
this.klass.classInfo.getField("I.buf.[B").set(this, buf);
this.buf = buf;
}

buf[count] = value;
this.klass.classInfo.getField("I.count.I").set(this, newcount);
this.count = newcount;
};

Override["java/io/ByteArrayInputStream.<init>.([B)V"] = function(buf) {
Expand Down
24 changes: 21 additions & 3 deletions runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,19 @@ module J2ME {
var symbolName = symbols[key];
var object = field.isStatic ? klass : klass.prototype;
release || assert (!object.hasOwnProperty(symbolName), "Should not overwrite existing properties.");
ObjectUtilities.defineNonEnumerableForwardingProperty(object, symbolName, field.mangledName);
var getter = FunctionUtilities.makeForwardingGetter(field.mangledName);
var setter;
if (release) {
setter = FunctionUtilities.makeForwardingSetter(field.mangledName);
} else {
setter = FunctionUtilities.makeDebugForwardingSetter(field.mangledName, getKindCheck(field.kind));
}
Object.defineProperty(object, symbolName, {
get: getter,
set: setter,
configurable: true,
enumerable: false
});
}
}
}
Expand Down Expand Up @@ -1777,8 +1789,14 @@ module J2ME {
}

export enum Constants {
INT_MAX = 2147483647,
INT_MIN = -2147483648
BYTE_MIN = -128,
BYTE_MAX = 127,
SHORT_MIN = -32768,
SHORT_MAX = 32767,
CHAR_MIN = 0,
CHAR_MAX = 65535,
INT_MIN = -2147483648,
INT_MAX = 2147483647
}

export function monitorEnter(object: J2ME.java.lang.Object) {
Expand Down
27 changes: 27 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,33 @@ module J2ME {
}
}

export function getKindCheck(kind: Kind): (x: any) => boolean {
switch (kind) {
case Kind.Boolean:
return (x) => x === 0 || x === 1;
case Kind.Byte:
return (x) => (x | 0) === x && x >= Constants.BYTE_MIN && x <= Constants.BYTE_MAX;
case Kind.Short:
return (x) => (x | 0) === x && x >= Constants.SHORT_MIN && x <= Constants.SHORT_MAX;
case Kind.Char:
return (x) => (x | 0) === x && x >= Constants.CHAR_MIN && x <= Constants.CHAR_MAX;
case Kind.Int:
return (x) => (x | 0) === x;
case Kind.Float:
return (x) => Math.fround(x) === x;
case Kind.Long:
return (x) => x instanceof Long;
case Kind.Double:
return (x) => (+x) === x;
case Kind.Reference:
return (x) => x === null || x instanceof Object;
case Kind.Void:
return (x) => typeof x === "undefined";
default:
throw Debug.unexpected("Unknown kind: " + kind);
}
}

export function getSignatureKind(signature: string): Kind {
switch (signature[0]) {
case 'Z':
Expand Down
7 changes: 7 additions & 0 deletions utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,13 @@ module J2ME {
export function makeForwardingSetter(target: string): (any) => void {
return <(any) => void> new Function("value", "this[\"" + target + "\"] = value;");
}

export function makeDebugForwardingSetter(target: string, checker: (x: any) => boolean): (any) => void {
return function (value) {
Debug.assert(checker(value), "Unexpected value for target " + target);
this[target] = value;
}
}
}

export module StringUtilities {
Expand Down

0 comments on commit 18405ac

Please sign in to comment.