2007-06-08 05:11:37 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
package lua;
|
|
|
|
|
|
|
|
|
|
import lua.value.LFunction;
|
2007-06-11 04:30:03 +00:00
|
|
|
import lua.value.LNil;
|
2007-06-08 05:11:37 +00:00
|
|
|
import lua.value.LString;
|
|
|
|
|
import lua.value.LTable;
|
2007-06-10 22:53:09 +00:00
|
|
|
import lua.value.LValue;
|
2007-06-08 05:11:37 +00:00
|
|
|
|
|
|
|
|
final class Builtin extends LFunction {
|
|
|
|
|
|
|
|
|
|
static void addBuiltins(LTable table) {
|
2007-06-10 22:53:09 +00:00
|
|
|
for ( int i=0; i<NAMES.length; i++ )
|
|
|
|
|
table.luaSetTable( new LString( NAMES[i] ), new Builtin(i) );
|
2007-06-08 05:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final int PRINT = 0;
|
2007-06-10 22:53:09 +00:00
|
|
|
private static final int PAIRS = 1;
|
|
|
|
|
|
|
|
|
|
private static final String[] NAMES = { "print", "pairs" };
|
|
|
|
|
|
2007-06-08 05:11:37 +00:00
|
|
|
private int id;
|
2007-06-10 22:53:09 +00:00
|
|
|
private Builtin( int id ) {
|
2007-06-08 05:11:37 +00:00
|
|
|
this.id = id;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-10 22:53:09 +00:00
|
|
|
public String toString() {
|
2007-06-15 06:41:40 +00:00
|
|
|
return "builtin."+NAMES[id];
|
2007-06-10 22:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
2007-06-08 05:11:37 +00:00
|
|
|
// perform a lua call
|
2007-06-15 06:41:40 +00:00
|
|
|
public void luaStackCall(StackState state, int base, int top) {
|
2007-06-12 06:22:55 +00:00
|
|
|
int returnValues = 0;
|
2007-06-08 05:11:37 +00:00
|
|
|
switch ( id ) {
|
|
|
|
|
case PRINT:
|
2007-06-15 06:41:40 +00:00
|
|
|
for ( int i=base+1; i<top; i++ ) {
|
2007-06-11 05:51:19 +00:00
|
|
|
System.out.print( String.valueOf(state.stack[i]) );
|
|
|
|
|
System.out.print( "\t" );
|
2007-06-10 22:53:09 +00:00
|
|
|
}
|
|
|
|
|
System.out.println();
|
2007-06-12 06:22:55 +00:00
|
|
|
break;
|
2007-06-10 22:53:09 +00:00
|
|
|
case PAIRS:
|
2007-06-11 05:51:19 +00:00
|
|
|
state.adjustTop(base+2);
|
2007-06-10 22:53:09 +00:00
|
|
|
LValue value = state.stack[base+1].luaPairs();
|
|
|
|
|
state.stack[base] = value;
|
2007-06-12 06:22:55 +00:00
|
|
|
returnValues = 1;
|
|
|
|
|
break;
|
2007-06-08 05:11:37 +00:00
|
|
|
default:
|
|
|
|
|
luaUnsupportedOperation();
|
|
|
|
|
}
|
2007-06-15 15:20:21 +00:00
|
|
|
state.adjustTop(base+returnValues);
|
2007-06-08 05:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|