Add new built-in function type() that returns the name of the type of

the given value as a string. Includes test case.
This commit is contained in:
Ian Farmer
2007-07-16 02:37:08 +00:00
parent c65dec54fb
commit 635f127cd0
14 changed files with 69 additions and 11 deletions

View File

@@ -133,6 +133,9 @@ public final class LuaJava extends LFunction {
throw new RuntimeException(e);
}
}
public LString luaGetType() {
return new LString("userdata");
}
}

View File

@@ -21,8 +21,9 @@ final class Builtin extends LFunction {
private static final int PAIRS = 1;
private static final int GETMETATABLE = 2;
private static final int SETMETATABLE = 3;
private static final int TYPE = 4;
private static final String[] NAMES = { "print", "pairs", "getmetatable", "setmetatable" };
private static final String[] NAMES = { "print", "pairs", "getmetatable", "setmetatable", "type" };
private static PrintStream stdout = System.out;
@@ -62,6 +63,10 @@ final class Builtin extends LFunction {
call.stack[base] = call.stack[base+1];
call.top = base+1;
break;
case TYPE:
call.stack[base] = call.stack[base+1].luaGetType();
call.top = base+1;
break;
default:
luaUnsupportedOperation();
}

View File

@@ -7,7 +7,7 @@ import lua.value.LString;
/*
** Function Prototypes
*/
public class Proto extends LValue {
public class Proto {
public Proto(StackState l) {
}
public Proto() {
@@ -38,8 +38,4 @@ public class Proto extends LValue {
public boolean is_vararg;
public int maxstacksize;
public String luaAsString() {
return "proto: "+id();
}
}

View File

@@ -6,6 +6,8 @@ public final class LBoolean extends LValue {
public static final LBoolean FALSE = new LBoolean("false",false);
public static final LString TYPE_NAME = new LString("boolean");
private final String m_name;
private final boolean m_value;
@@ -25,4 +27,8 @@ public final class LBoolean extends LValue {
public final static LBoolean valueOf(boolean value) {
return value? TRUE: FALSE;
}
public LString luaGetType() {
return TYPE_NAME;
}
}

View File

@@ -5,6 +5,8 @@ import lua.CallFrame;
public class LFunction extends LValue {
public static final LString TYPE_NAME = new LString("function");
public String luaAsString() {
return "function: "+hashCode();
}
@@ -26,4 +28,8 @@ public class LFunction extends LValue {
this.luaStackCall(call, base, call.top, 1);
}
public LString luaGetType() {
return TYPE_NAME;
}
}

View File

@@ -2,6 +2,7 @@ package lua.value;
public final class LNil extends LValue {
public static final LNil NIL = new LNil();
public static final LString TYPE_NAME = new LString("nil");
public final String luaAsString() {
return "nil";
@@ -10,4 +11,8 @@ public final class LNil extends LValue {
public boolean luaAsBoolean() {
return false;
}
public LString luaGetType() {
return TYPE_NAME;
}
}

View File

@@ -5,6 +5,8 @@ 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) )
@@ -13,4 +15,8 @@ public class LNumber extends LValue {
return this.luaBinCmpUnknown(Lua.OP_EQ, v );
}
public LString luaGetType() {
return TYPE_NAME;
}
}

View File

@@ -5,6 +5,8 @@ import lua.StackState;
public class LString extends LValue {
public static final LString TYPE_NAME = new LString("string");
final String m_string;
final int m_hash;
@@ -53,4 +55,8 @@ public class LString extends LValue {
return new LInteger( m_string.length() );
}
public LString luaGetType() {
return TYPE_NAME;
}
}

View File

@@ -7,6 +7,8 @@ import lua.CallFrame;
public class LTable extends LValue {
public static final LString TYPE_NAME = new LString("table");
/** Metatable tag for intercepting table gets */
private static final LString TM_INDEX = new LString("__index");
@@ -95,7 +97,7 @@ public class LTable extends LValue {
}
/** Iterator for tables */
private static final class LTableIterator extends LValue {
private static final class LTableIterator extends LFunction {
private final LTable t;
private final Enumeration e;
@@ -121,4 +123,8 @@ public class LTable extends LValue {
}
}
public LString luaGetType() {
return TYPE_NAME;
}
}

View File

@@ -1,5 +1,10 @@
package lua.value;
public class LThread extends LValue {
public static final LString TYPE_NAME = new LString("thread");
public LString luaGetType() {
return TYPE_NAME;
}
}

View File

@@ -137,4 +137,6 @@ public class LValue {
luaUnsupportedOperation();
}
/** Valid for all types: return the type of this value as an LString */
public abstract LString luaGetType();
}

View File

@@ -47,6 +47,14 @@ public class LuaJTest extends TestCase {
runTest( "compare" );
}
public void testSetlist() throws IOException, InterruptedException {
runTest( "setlist" );
}
public void testType() throws IOException, InterruptedException {
runTest( "type" );
}
public void testUpvalues() throws IOException, InterruptedException {
runTest( "upvalues" );
}
@@ -55,10 +63,6 @@ public class LuaJTest extends TestCase {
runTest( "upvalues2" );
}
public void testSetlist() throws IOException, InterruptedException {
runTest( "setlist" );
}
private void runTest( String testName ) throws IOException, InterruptedException {
// add LuaJava bindings
LuaJava.install();

8
src/test/res/type.lua Normal file
View File

@@ -0,0 +1,8 @@
print(type(5))
print(type(3.14))
print(type("hello"))
print(type(function() return 1 end))
print(type({}))
print(type(nil))
print(type(2 < 5))
print(type(pairs({})))

BIN
src/test/res/type.luac Normal file

Binary file not shown.