I need a tip or two on how to do something in Rhino (and perhaps a Java
related tip).
I haven't programmed Java in years, though it was the first serious
programming language I learned, and it's not that hard getting back into
it to do some light stuff in Rhino.
The two issues I'm having right now are with Native* classes and bytes.
Right now I'm working on a binary data type. The frame of how the code
is laid out is based on NativeString since that's the closest existing
thing to what I am implementing (my NativeBlob is basically a String for
bytes).
My first issue is with classes like NativeInteger and NativeArray.
In some parts of my code I need to conditionally convert some arguments
to a suitable Java type. Mainly, I need to access items inside of a
NativeArray and I need to check if something is an instance of one of
the Native* types so I know what kind of stuff to do on it.
For example:
if(o instanceof NativeBlob)
return ((NativeBlob)o).bytes;
if(o instanceof NativeNumber) {
int ii = ScriptRuntime.toInt32( o );
byte[] b = new byte[1];
b[0] = intToByte(ii);
return b;
}
if(o instanceof NativeArray) {
NativeArray a = (NativeArray) o;
byte[] ba = new byte[(int)a.getLength()];
for (int i = 0; i < ba.length; i++) {
Object bo = NativeArray.getElm(cx, (Scriptable)a, (long)i);
if (!(bo instanceof NativeNumber))
throw ScriptRuntime.typeError("Contents of data array used as
argument to blob method was not entirely numbers");
int ii = ScriptRuntime.toInt32( bo );
byte b = intToByte(ii);
ba[i] = b;
}
return ba;
}
throw ScriptRuntime.typeError("Invalid data type used as argument to a
blob method");
Basically right now I'm trying to:
if NativeBlob, return a new NativeBlob with the same bytes.
if NativeNumber, convert to an appropriate byte and return a new
NativeBlob with that byte.
if NativeArray, iterate over and create a byte[] array out of
NativeNumbers in the NativeArray (throw an error if any aren't
NativeNumber).
The problem is it looks like I got the wrong way do do that. The Native*
classes can't be accessed from outside of org.mozilla.javascript thus
things like (o instanceof NativeNumber) throw errors. As well the
NativeArray.getElem helper I discovered doesn't work.
So the first two questions I'm asking are:
A) How do I check and see if an Object is an instance of a NativeNumber,
NativeArray, etc... so I can conditionally do code.
B) What is the preferred method of accessing items in a NativeArray from
Java.
The second issue is with bytes. One of the parts of blobs is conversion
between integer and byte formats. ie: Blob(0); to Blob(255); should be
converted to a byte. The issue there is Java's byte being signed. This
is the code I'm currently using to convert between int and byte format:
private static int byteToInt(byte b) {
Byte bb = new Byte(b);
int bi = bb.intValue();
Byte byteMin = new Byte(Byte.MIN_VALUE);
int bmin = byteMin.intValue();
return bi-bmin;
}
private static byte intToByte(int i) {
int bmax = Byte.MAX_VALUE-Byte.MIN_VALUE;//(new
Byte(Byte.MAX_VALUE-Byte.MIN_VALUE)).intValue();
int bmin = (new Byte(Byte.MIN_VALUE)).intValue();
if ( i > bmax )
throw ScriptRuntime.typeError("Integer representation of byte to
high.");
byte b = (byte)(i+bmin);
return b;
}
I probably got it a little messy with type conversions, but the real
thing I'm questioning is if I'm doing it right overall. I do remember
parts of my class explaining signed binary at the bit level and I don't
quite think I've got that right.
--
~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino