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

@@ -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;
}
}