centralize type id defs

This commit is contained in:
James Roseborough
2007-10-04 20:10:50 +00:00
parent 9dee79cb80
commit 8ae4299764
16 changed files with 70 additions and 71 deletions

View File

@@ -4,6 +4,7 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import lua.Lua;
import lua.io.Proto; import lua.io.Proto;
import lua.value.LBoolean; import lua.value.LBoolean;
import lua.value.LNil; import lua.value.LNil;
@@ -36,17 +37,6 @@ public class DumpState {
private static final int SIZEOF_LUA_NUMBER = 8; private static final int SIZEOF_LUA_NUMBER = 8;
private static final int IS_NUMBER_INTEGRAL = 0; private static final int IS_NUMBER_INTEGRAL = 0;
// types of lua constants
private static final int LUA_TNIL = 0;
private static final int LUA_TBOOLEAN = 1;
private static final int LUA_TLIGHTUSERDATA = 2;
private static final int LUA_TNUMBER = 3;
private static final int LUA_TSTRING = 4;
private static final int LUA_TTABLE = 5;
private static final int LUA_TFUNCTION = 6;
private static final int LUA_TUSERDATA = 7;
private static final int LUA_TTHREAD = 8;
DataOutputStream writer; DataOutputStream writer;
boolean strip; boolean strip;
int status; int status;
@@ -94,16 +84,16 @@ public class DumpState {
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
final LValue o = f.k[i]; final LValue o = f.k[i];
if (o == LNil.NIL) { if (o == LNil.NIL) {
writer.write(LUA_TNIL); writer.write(Lua.LUA_TNIL);
// do nothing more // do nothing more
} else if (o instanceof LBoolean) { } else if (o instanceof LBoolean) {
writer.write(LUA_TBOOLEAN); writer.write(Lua.LUA_TBOOLEAN);
dumpChar(o.luaAsBoolean() ? 1 : 0); dumpChar(o.luaAsBoolean() ? 1 : 0);
} else if (o instanceof LNumber) { } else if (o instanceof LNumber) {
writer.write(LUA_TNUMBER); writer.write(Lua.LUA_TNUMBER);
dumpNumber(o.luaAsDouble()); dumpNumber(o.luaAsDouble());
} else if (o instanceof LString) { } else if (o instanceof LString) {
writer.write(LUA_TSTRING); writer.write(Lua.LUA_TSTRING);
dumpString((LString) o); dumpString((LString) o);
} else { } else {
throw new IllegalArgumentException("bad type for " + o); throw new IllegalArgumentException("bad type for " + o);

View File

@@ -539,7 +539,7 @@ public class StrLib {
if ( !repl.luaAsBoolean() ) { if ( !repl.luaAsBoolean() ) {
repl = s.substring( soffset, end ); repl = s.substring( soffset, end );
} else if ( ! ( repl instanceof LString || repl instanceof LNumber ) ) { } else if ( ! ( repl instanceof LString || repl instanceof LNumber ) ) {
vm.lua_error( "invalid replacement value (a "+repl.luaGetType()+")" ); vm.lua_error( "invalid replacement value (a "+repl.luaGetTypeName()+")" );
} }
vm.lua_pop( 1 ); vm.lua_pop( 1 );
lbuf.append( repl.luaAsString() ); lbuf.append( repl.luaAsString() );

View File

@@ -74,7 +74,7 @@ final class Builtin extends LFunction {
vm.setResult( t ); vm.setResult( t );
break; break;
case TYPE: case TYPE:
vm.setResult( vm.getArg(0).luaGetType() ); vm.setResult( vm.getArg(0).luaGetTypeName() );
break; break;
case PCALL: { case PCALL: {
int n = vm.getArgCount(); int n = vm.getArgCount();

View File

@@ -354,4 +354,18 @@ public class Lua {
null, null,
}; };
// type constants
public static final int LUA_TNONE = (-1);
public static final int LUA_TNIL = 0;
public static final int LUA_TBOOLEAN = 1;
public static final int LUA_TLIGHTUSERDATA = 2;
public static final int LUA_TNUMBER = 3;
public static final int LUA_TSTRING = 4;
public static final int LUA_TTABLE = 5;
public static final int LUA_TFUNCTION = 6;
public static final int LUA_TUSERDATA = 7;
public static final int LUA_TTHREAD = 8;
} }

View File

@@ -310,7 +310,7 @@ public class DebugStackState extends StackState implements DebugRequestListener
variablesSeen.add(varName); variablesSeen.add(varName);
LValue value = stack[callInfo.base + i]; LValue value = stack[callInfo.base + i];
if (value != null) { if (value != null) {
Type type = Type.valueOf(value.luaGetType().toJavaString()); Type type = Type.valueOf(value.luaGetTypeName().toJavaString());
DebugUtils.print("\tType: " + type); DebugUtils.print("\tType: " + type);
if (type == Type.table) { if (type == Type.table) {
DebugUtils.println(" (selected)"); DebugUtils.println(" (selected)");

View File

@@ -4,6 +4,7 @@ import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import lua.Lua;
import lua.VM; import lua.VM;
import lua.value.LBoolean; import lua.value.LBoolean;
import lua.value.LDouble; import lua.value.LDouble;
@@ -56,18 +57,6 @@ public class LoadState {
/** The VM doing the loading */ /** The VM doing the loading */
VM L; VM L;
private static final int LUA_TNONE = (-1);
private static final int LUA_TNIL = 0;
private static final int LUA_TBOOLEAN = 1;
private static final int LUA_TLIGHTUSERDATA = 2;
private static final int LUA_TNUMBER = 3;
private static final int LUA_TSTRING = 4;
private static final int LUA_TTABLE = 5;
private static final int LUA_TFUNCTION = 6;
private static final int LUA_TUSERDATA = 7;
private static final int LUA_TTHREAD = 8;
int loadByte() throws IOException { int loadByte() throws IOException {
return is.readUnsignedByte(); return is.readUnsignedByte();
} }
@@ -148,16 +137,16 @@ public class LoadState {
LValue[] values = new LValue[n]; LValue[] values = new LValue[n];
for ( int i=0; i<n; i++ ) { for ( int i=0; i<n; i++ ) {
switch ( loadByte() ) { switch ( loadByte() ) {
case LUA_TNIL: case Lua.LUA_TNIL:
values[i] = LNil.NIL; values[i] = LNil.NIL;
break; break;
case LUA_TBOOLEAN: case Lua.LUA_TBOOLEAN:
values[i] = (0 != loadByte()? LBoolean.TRUE: LBoolean.FALSE); values[i] = (0 != loadByte()? LBoolean.TRUE: LBoolean.FALSE);
break; break;
case LUA_TNUMBER: case Lua.LUA_TNUMBER:
values[i] = loadNumber(); values[i] = loadNumber();
break; break;
case LUA_TSTRING: case Lua.LUA_TSTRING:
values[i] = loadString(); values[i] = loadString();
break; break;
default: default:

View File

@@ -1,13 +1,13 @@
package lua.value; package lua.value;
import lua.Lua;
public final class LBoolean extends LValue { public final class LBoolean extends LValue {
public static final LBoolean TRUE = new LBoolean("true",true); public static final LBoolean TRUE = new LBoolean("true",true);
public static final LBoolean FALSE = new LBoolean("false",false); public static final LBoolean FALSE = new LBoolean("false",false);
public static final LString TYPE_NAME = new LString(Type.bool.toString());
private final LString m_name; private final LString m_name;
private final boolean m_value; private final boolean m_value;
@@ -32,7 +32,7 @@ public final class LBoolean extends LValue {
return value? TRUE: FALSE; return value? TRUE: FALSE;
} }
public LString luaGetType() { public int luaGetType() {
return TYPE_NAME; return Lua.LUA_TFUNCTION;
} }
} }

View File

@@ -1,12 +1,11 @@
package lua.value; package lua.value;
import lua.Lua;
import lua.VM; import lua.VM;
public class LFunction extends LValue { public class LFunction extends LValue {
public static final LString TYPE_NAME = new LString(Type.function.toString());
public LString luaAsString() { public LString luaAsString() {
return new LString( "function: "+hashCode() ); return new LString( "function: "+hashCode() );
} }
@@ -26,8 +25,8 @@ public class LFunction extends LValue {
vm.lua_call( 2, 1 ); vm.lua_call( 2, 1 );
} }
public LString luaGetType() { public int luaGetType() {
return TYPE_NAME; return Lua.LUA_TFUNCTION;
} }
} }

View File

@@ -1,21 +1,22 @@
package lua.value; package lua.value;
import lua.Lua;
public final class LNil extends LValue { public final class LNil extends LValue {
public static final LNil NIL = new LNil(); public static final LNil NIL = new LNil();
public static final LString TYPE_NAME = new LString(Type.nil.toString());
public final LString luaAsString() { public final LString luaAsString() {
return TYPE_NAME; return luaGetTypeName();
} }
public boolean luaAsBoolean() { public boolean luaAsBoolean() {
return false; return false;
} }
public LString luaGetType() { public int luaGetType() {
return TYPE_NAME; return Lua.LUA_TNIL;
} }
public int luaAsInt() { public int luaAsInt() {
return 0; return 0;
} }

View File

@@ -5,8 +5,6 @@ import lua.Lua;
abstract abstract
public class LNumber extends LValue { public class LNumber extends LValue {
public static final LString TYPE_NAME = new LString("number");
/** Compare for equivalence by using lua op comparator */ /** Compare for equivalence by using lua op comparator */
public boolean equals(Object o) { public boolean equals(Object o) {
if ( ! ( o instanceof LValue) ) if ( ! ( o instanceof LValue) )
@@ -15,10 +13,10 @@ public class LNumber extends LValue {
return this.luaBinCmpUnknown(Lua.OP_EQ, v ); return this.luaBinCmpUnknown(Lua.OP_EQ, v );
} }
public LString luaGetType() { public int luaGetType() {
return TYPE_NAME; return Lua.LUA_TNUMBER;
} }
/** /**
* Returns false by default for non-LNumbers, but subclasses of LNumber must * Returns false by default for non-LNumbers, but subclasses of LNumber must
* override. * override.

View File

@@ -24,8 +24,6 @@ import lua.Lua;
* efficiency, new LStrings and substrings never create copies. * efficiency, new LStrings and substrings never create copies.
*/ */
public class LString extends LValue { public class LString extends LValue {
public static final LString TYPE_NAME = new LString(Type.string.toString());
public final byte[] m_bytes; public final byte[] m_bytes;
public final int m_offset; public final int m_offset;
@@ -287,8 +285,8 @@ public class LString extends LValue {
return LInteger.valueOf( length() ); return LInteger.valueOf( length() );
} }
public LString luaGetType() { public int luaGetType() {
return TYPE_NAME; return Lua.LUA_TSTRING;
} }
public LTable luaGetMetatable() { public LTable luaGetMetatable() {

View File

@@ -20,8 +20,6 @@ import lua.VM;
* *
*/ */
public class LTable extends LValue { public class LTable extends LValue {
public static final LString TYPE_NAME = new LString(Type.table.toString());
/** /**
* Zero-length array to use instead of null, so that we don't need to * Zero-length array to use instead of null, so that we don't need to
@@ -262,8 +260,8 @@ public class LTable extends LValue {
return new LString("table: "+id()); return new LString("table: "+id());
} }
public LString luaGetType() { public int luaGetType() {
return TYPE_NAME; return Lua.LUA_TTABLE;
} }
/** Valid for tables */ /** Valid for tables */

View File

@@ -1,12 +1,13 @@
package lua.value; package lua.value;
public class LThread extends LValue { import lua.Lua;
public static final LString TYPE_NAME = new LString(Type.thread.toString());
public LString luaGetType() {
return TYPE_NAME;
}
public class LThread extends LValue {
public int luaGetType() {
return Lua.LUA_TTHREAD;
}
public LString luaAsString() { public LString luaAsString() {
return new LString("thread: "+hashCode()); return new LString("thread: "+hashCode());
} }

View File

@@ -1,7 +1,8 @@
package lua.value; package lua.value;
import lua.Lua;
public class LUserData extends LValue { public class LUserData extends LValue {
public static final LString TYPE_NAME = new LString(Type.userdata.toString());
public final Object m_instance; public final Object m_instance;
public LTable m_metatable; public LTable m_metatable;
@@ -23,8 +24,8 @@ public class LUserData extends LValue {
return System.identityHashCode( m_instance ); return System.identityHashCode( m_instance );
} }
public LString luaGetType() { public int luaGetType() {
return TYPE_NAME; return Lua.LUA_TUSERDATA;
} }
public LTable luaGetMetatable() { public LTable luaGetMetatable() {

View File

@@ -176,7 +176,12 @@ public class LValue {
luaUnsupportedOperation(); luaUnsupportedOperation();
} }
/** Valid for all types: return the int value identifying the type of this value */
public abstract int luaGetType();
/** Valid for all types: return the type of this value as an LString */ /** Valid for all types: return the type of this value as an LString */
public abstract LString luaGetType(); public LString luaGetTypeName() {
return Type.VALUES[luaGetType()].toLString();
}
} }

View File

@@ -47,16 +47,17 @@ public class Type implements Serializable, Comparable {
protected static int ORDINAL = 0; protected static int ORDINAL = 0;
private String name; private String name;
private LString lname;
private int ordinal; private int ordinal;
Type(String name) { Type(String name) {
this.name = name; this.name = name;
this.lname = new LString(name);
this.ordinal = ORDINAL++; this.ordinal = ORDINAL++;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see java.lang.Enum#toString() * @see java.lang.Enum#toString()
*/ */
@Override
public String toString() { public String toString() {
return name; return name;
} }
@@ -78,4 +79,8 @@ public class Type implements Serializable, Comparable {
public int compareTo(Object o) { public int compareTo(Object o) {
return this.ordinal - ((Type)o).ordinal; return this.ordinal - ((Type)o).ordinal;
} }
public LString toLString() {
return lname;
}
} }