Support for iterator-style for loops, and "pairs" builting function
This commit is contained in:
@@ -10,6 +10,14 @@ public class LDouble extends LNumber {
|
||||
this.m_value = value;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
return o != null && o instanceof LDouble && m_value == ((LDouble)o).m_value;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return (int) m_value;
|
||||
}
|
||||
|
||||
public String luaAsString() {
|
||||
return String.valueOf(m_value);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,14 @@ public class LInteger extends LNumber {
|
||||
this.m_value = value;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
return o != null && o instanceof LInteger && m_value == ((LInteger)o).m_value;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return m_value;
|
||||
}
|
||||
|
||||
public int luaAsInt() {
|
||||
return m_value;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,14 @@ public class LString extends LValue {
|
||||
this.m_string = string;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
return o != null && o instanceof LString && m_string.equals(((LString)o).m_string);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return m_string.hashCode();
|
||||
}
|
||||
|
||||
// TODO: what to do with LuaState?
|
||||
public LString(StackState l, String string) {
|
||||
this(string);
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package lua.value;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Vector;
|
||||
|
||||
import lua.StackState;
|
||||
|
||||
public class LTable extends LValue {
|
||||
|
||||
private Hashtable m_hash = new Hashtable();
|
||||
private Vector m_array = new Vector();
|
||||
|
||||
public LTable() {
|
||||
}
|
||||
@@ -15,12 +17,11 @@ public class LTable extends LValue {
|
||||
}
|
||||
|
||||
public void luaSetTable(LValue key, LValue val) {
|
||||
m_hash.put( key.luaAsString(), val );
|
||||
m_array.add( val );
|
||||
m_hash.put( key, val );
|
||||
}
|
||||
|
||||
public LValue luaGetTable(LValue key) {
|
||||
return (LValue) m_hash.get( key.luaAsString() );
|
||||
return (LValue) m_hash.get( key );
|
||||
}
|
||||
|
||||
public String luaAsString() {
|
||||
@@ -29,6 +30,36 @@ public class LTable extends LValue {
|
||||
|
||||
/** Built-in opcode LEN, for Strings and Tables */
|
||||
public LValue luaLength() {
|
||||
return new LInteger( m_array.size() );
|
||||
return new LInteger( m_hash.size() );
|
||||
}
|
||||
|
||||
/** Valid for tables */
|
||||
public LValue luaPairs() {
|
||||
Enumeration e = m_hash.keys();
|
||||
return new LTableIterator(this,e);
|
||||
}
|
||||
|
||||
/** Iterator for tables */
|
||||
private static final class LTableIterator extends LValue {
|
||||
private final LTable t;
|
||||
private final Enumeration e;
|
||||
|
||||
private LTableIterator(LTable t, Enumeration e) {
|
||||
this.t = t;
|
||||
this.e = e;
|
||||
}
|
||||
|
||||
// perform a lua call
|
||||
public void luaStackCall(StackState state, int base, int nresults) {
|
||||
if ( e.hasMoreElements() ) {
|
||||
LValue key = (LValue) e.nextElement();
|
||||
state.stack[base] = key;
|
||||
state.stack[base+1] = t.luaGetTable(key);
|
||||
} else {
|
||||
state.stack[base] = LNil.NIL;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class LValue {
|
||||
}
|
||||
|
||||
// perform a lua call
|
||||
public void luaStackCall(StackState state, int base, int nresults) {
|
||||
public void luaStackCall(StackState state, int base, int nargs) {
|
||||
luaUnsupportedOperation();
|
||||
}
|
||||
|
||||
@@ -91,5 +91,10 @@ public class LValue {
|
||||
return luaUnsupportedOperation();
|
||||
}
|
||||
|
||||
/** Valid for tables */
|
||||
public LValue luaPairs() {
|
||||
return luaUnsupportedOperation();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user