Allow dump state to take arguments for endian-ness, force to integer

This commit is contained in:
James Roseborough
2007-10-24 23:43:21 +00:00
parent e97b842b45
commit 72c01f76c0

View File

@@ -30,12 +30,12 @@ public class DumpState {
private static final byte[] LUAC_HEADER_SIGNATURE = { '\033', 'L', 'u', 'a' }; private static final byte[] LUAC_HEADER_SIGNATURE = { '\033', 'L', 'u', 'a' };
// header fields // header fields
private static final int IS_LITTLE_ENDIAN = 0; private boolean IS_LITTLE_ENDIAN = false;
private boolean IS_NUMBER_INTEGRAL = false;
private int SIZEOF_LUA_NUMBER = 8;
private static final int SIZEOF_INT = 4; private static final int SIZEOF_INT = 4;
private static final int SIZEOF_SIZET = 4; private static final int SIZEOF_SIZET = 4;
private static final int SIZEOF_INSTRUCTION = 4; private static final int SIZEOF_INSTRUCTION = 4;
private static final int SIZEOF_LUA_NUMBER = 8;
private static final int IS_NUMBER_INTEGRAL = 0;
DataOutputStream writer; DataOutputStream writer;
boolean strip; boolean strip;
@@ -56,7 +56,14 @@ public class DumpState {
} }
void dumpInt(int x) throws IOException { void dumpInt(int x) throws IOException {
writer.writeInt(x); if ( IS_LITTLE_ENDIAN ) {
writer.writeByte(x&0xff);
writer.writeByte((x>>8)&0xff);
writer.writeByte((x>>16)&0xff);
writer.writeByte((x>>24)&0xff);
} else {
writer.writeInt(x);
}
} }
void dumpString(LString s) throws IOException { void dumpString(LString s) throws IOException {
@@ -67,8 +74,15 @@ public class DumpState {
} }
void dumpNumber(double d) throws IOException { void dumpNumber(double d) throws IOException {
long l = Double.doubleToLongBits(d); if ( IS_NUMBER_INTEGRAL ) {
writer.writeLong(l); int i = (int) d;
if ( i != d )
throw new java.lang.IllegalArgumentException("not an integer: "+d);
dumpInt( i );
} else {
long l = Double.doubleToLongBits(d);
writer.writeLong(l);
}
} }
void dumpCode( final Proto f ) throws IOException { void dumpCode( final Proto f ) throws IOException {
@@ -144,12 +158,12 @@ public class DumpState {
writer.write( LUAC_HEADER_SIGNATURE ); writer.write( LUAC_HEADER_SIGNATURE );
writer.write( LUAC_VERSION ); writer.write( LUAC_VERSION );
writer.write( LUAC_FORMAT ); writer.write( LUAC_FORMAT );
writer.write( IS_LITTLE_ENDIAN ); writer.write( IS_LITTLE_ENDIAN? 1: 0 );
writer.write( SIZEOF_INT ); writer.write( SIZEOF_INT );
writer.write( SIZEOF_SIZET ); writer.write( SIZEOF_SIZET );
writer.write( SIZEOF_INSTRUCTION ); writer.write( SIZEOF_INSTRUCTION );
writer.write( SIZEOF_LUA_NUMBER ); writer.write( SIZEOF_LUA_NUMBER );
writer.write( IS_NUMBER_INTEGRAL ); writer.write( IS_NUMBER_INTEGRAL? 1: 0 );
} }
/* /*
@@ -161,4 +175,14 @@ public class DumpState {
D.dumpFunction(f,null); D.dumpFunction(f,null);
return D.status; return D.status;
} }
public static int dump(Proto f, OutputStream w, boolean strip, boolean intonly, boolean littleendian) throws IOException {
DumpState D = new DumpState(w,strip);
D.IS_LITTLE_ENDIAN = littleendian;
D.IS_NUMBER_INTEGRAL = intonly;
D.SIZEOF_LUA_NUMBER = (intonly? 4: 8);
D.dumpHeader();
D.dumpFunction(f,null);
return D.status;
}
} }