2007-06-08 05:11:37 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
package lua;
|
|
|
|
|
|
|
|
|
|
import lua.value.LFunction;
|
|
|
|
|
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() {
|
|
|
|
|
return "Builtin('"+NAMES[id]+"')";
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-08 05:11:37 +00:00
|
|
|
// perform a lua call
|
2007-06-10 22:53:09 +00:00
|
|
|
public void luaStackCall(StackState state, int base, int nargs) {
|
2007-06-08 05:11:37 +00:00
|
|
|
switch ( id ) {
|
|
|
|
|
case PRINT:
|
2007-06-10 22:53:09 +00:00
|
|
|
for ( int i=0; i<nargs; i++ ) {
|
|
|
|
|
if ( i > 0 )
|
|
|
|
|
System.out.print( "\t" );
|
|
|
|
|
System.out.print( String.valueOf(state.stack[base+1+i]) );
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
return;
|
|
|
|
|
case PAIRS:
|
|
|
|
|
LValue value = state.stack[base+1].luaPairs();
|
|
|
|
|
state.stack[base] = value;
|
2007-06-08 05:11:37 +00:00
|
|
|
return;
|
|
|
|
|
default:
|
|
|
|
|
luaUnsupportedOperation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|