Partial implementation of metatables.
This commit is contained in:
@@ -18,8 +18,10 @@ final class Builtin extends LFunction {
|
||||
|
||||
private static final int PRINT = 0;
|
||||
private static final int PAIRS = 1;
|
||||
private static final int GETMETATABLE = 2;
|
||||
private static final int SETMETATABLE = 3;
|
||||
|
||||
private static final String[] NAMES = { "print", "pairs" };
|
||||
private static final String[] NAMES = { "print", "pairs", "getmetatable", "setmetatable" };
|
||||
|
||||
private int id;
|
||||
private Builtin( int id ) {
|
||||
@@ -31,7 +33,7 @@ final class Builtin extends LFunction {
|
||||
}
|
||||
|
||||
// perform a lua call
|
||||
public void luaStackCall(StackState state, int base, int top) {
|
||||
public void luaStackCall(StackState state, int base, int top, int nresults) {
|
||||
switch ( id ) {
|
||||
case PRINT:
|
||||
for ( int i=base+1; i<top; i++ ) {
|
||||
@@ -39,16 +41,26 @@ final class Builtin extends LFunction {
|
||||
System.out.print( "\t" );
|
||||
}
|
||||
System.out.println();
|
||||
state.adjustTop(base);
|
||||
return;
|
||||
state.top = base;
|
||||
break;
|
||||
case PAIRS:
|
||||
LValue value = state.stack[base+1].luaPairs();
|
||||
state.top = base+1;
|
||||
state.stack[base] = value;
|
||||
return;
|
||||
state.top = base+1;
|
||||
break;
|
||||
case GETMETATABLE:
|
||||
state.stack[base] = state.stack[base+1].luaGetMetatable();
|
||||
state.top = base+1;
|
||||
break;
|
||||
case SETMETATABLE:
|
||||
state.stack[base+1].luaSetMetatable(state.stack[base+2]);
|
||||
state.top = base;
|
||||
break;
|
||||
default:
|
||||
luaUnsupportedOperation();
|
||||
}
|
||||
if (nresults >= 0)
|
||||
state.adjustTop(base + nresults);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public class Closure extends LValue {
|
||||
}
|
||||
|
||||
// perform a lua call
|
||||
public void luaStackCall(StackState state, int base, int top) {
|
||||
public void luaStackCall(StackState state, int base, int top, int nresults) {
|
||||
state.setupCall( this, base, top );
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,13 @@ 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 LValue m_metatable;
|
||||
|
||||
public LTable() {
|
||||
}
|
||||
@@ -34,6 +34,16 @@ public class LTable extends LValue {
|
||||
return new LInteger( m_hash.size() );
|
||||
}
|
||||
|
||||
/** Valid for tables */
|
||||
public LValue luaGetMetatable() {
|
||||
return this.m_metatable;
|
||||
}
|
||||
|
||||
/** Valid for tables */
|
||||
public void luaSetMetatable(LValue metatable) {
|
||||
this.m_metatable = metatable;
|
||||
}
|
||||
|
||||
/** Valid for tables */
|
||||
public LValue luaPairs() {
|
||||
Enumeration e = m_hash.keys();
|
||||
@@ -51,15 +61,18 @@ public class LTable extends LValue {
|
||||
}
|
||||
|
||||
// perform a lua call
|
||||
public void luaStackCall(StackState state, int base, int top) {
|
||||
public void luaStackCall(StackState state, int base, int top, int nresults) {
|
||||
if ( e.hasMoreElements() ) {
|
||||
LValue key = (LValue) e.nextElement();
|
||||
state.adjustTop(base+2);
|
||||
state.stack[base] = key;
|
||||
state.stack[base+1] = t.luaGetTable(key);
|
||||
state.top = base+2;
|
||||
} else {
|
||||
state.adjustTop(base);
|
||||
state.stack[base] = LNil.NIL;
|
||||
state.top = base+1;
|
||||
}
|
||||
if ( nresults >= 0 )
|
||||
state.adjustTop(base + nresults);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ public class LValue {
|
||||
}
|
||||
|
||||
// perform a lua call, return number of results actually produced
|
||||
public void luaStackCall(StackState state, int base, int top) {
|
||||
public void luaStackCall(StackState state, int base, int top, int nresults) {
|
||||
luaUnsupportedOperation();
|
||||
}
|
||||
|
||||
@@ -111,5 +111,15 @@ public class LValue {
|
||||
return luaUnsupportedOperation();
|
||||
}
|
||||
|
||||
/** Valid for tables */
|
||||
public LValue luaGetMetatable() {
|
||||
return luaUnsupportedOperation();
|
||||
}
|
||||
|
||||
/** Valid for tables */
|
||||
public void luaSetMetatable(LValue metatable) {
|
||||
luaUnsupportedOperation();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user