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

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

View File

@@ -354,4 +354,18 @@ public class Lua {
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);
LValue value = stack[callInfo.base + i];
if (value != null) {
Type type = Type.valueOf(value.luaGetType().toJavaString());
Type type = Type.valueOf(value.luaGetTypeName().toJavaString());
DebugUtils.print("\tType: " + type);
if (type == Type.table) {
DebugUtils.println(" (selected)");

View File

@@ -4,6 +4,7 @@ import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import lua.Lua;
import lua.VM;
import lua.value.LBoolean;
import lua.value.LDouble;
@@ -56,18 +57,6 @@ public class LoadState {
/** The VM doing the loading */
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 {
return is.readUnsignedByte();
}
@@ -148,16 +137,16 @@ public class LoadState {
LValue[] values = new LValue[n];
for ( int i=0; i<n; i++ ) {
switch ( loadByte() ) {
case LUA_TNIL:
case Lua.LUA_TNIL:
values[i] = LNil.NIL;
break;
case LUA_TBOOLEAN:
case Lua.LUA_TBOOLEAN:
values[i] = (0 != loadByte()? LBoolean.TRUE: LBoolean.FALSE);
break;
case LUA_TNUMBER:
case Lua.LUA_TNUMBER:
values[i] = loadNumber();
break;
case LUA_TSTRING:
case Lua.LUA_TSTRING:
values[i] = loadString();
break;
default:

View File

@@ -1,13 +1,13 @@
package lua.value;
import lua.Lua;
public final class LBoolean extends LValue {
public static final LBoolean TRUE = new LBoolean("true",true);
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 boolean m_value;
@@ -32,7 +32,7 @@ public final class LBoolean extends LValue {
return value? TRUE: FALSE;
}
public LString luaGetType() {
return TYPE_NAME;
public int luaGetType() {
return Lua.LUA_TFUNCTION;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -20,8 +20,6 @@ import lua.VM;
*
*/
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
@@ -262,8 +260,8 @@ public class LTable extends LValue {
return new LString("table: "+id());
}
public LString luaGetType() {
return TYPE_NAME;
public int luaGetType() {
return Lua.LUA_TTABLE;
}
/** Valid for tables */

View File

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

View File

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

View File

@@ -176,7 +176,12 @@ public class LValue {
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 */
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;
private String name;
private LString lname;
private int ordinal;
Type(String name) {
this.name = name;
this.lname = new LString(name);
this.ordinal = ORDINAL++;
}
/* (non-Javadoc)
* @see java.lang.Enum#toString()
*/
@Override
public String toString() {
return name;
}
@@ -78,4 +79,8 @@ public class Type implements Serializable, Comparable {
public int compareTo(Object o) {
return this.ordinal - ((Type)o).ordinal;
}
public LString toLString() {
return lname;
}
}