Simplify bindings.
This commit is contained in:
@@ -76,17 +76,17 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
|
|||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
env.set( "_G", env );
|
env.set( "_G", env );
|
||||||
env.set( "_VERSION", VERSION );
|
env.set( "_VERSION", VERSION );
|
||||||
bind1( env, new String[] {
|
bind( env, BaseLib1.class, new String[] {
|
||||||
"getfenv", // ( [f] ) -> env
|
"getfenv", // ( [f] ) -> env
|
||||||
"getmetatable", // ( object ) -> table
|
"getmetatable", // ( object ) -> table
|
||||||
} );
|
} );
|
||||||
bind2( env, new String[] {
|
bind( env, BaseLib2.class, new String[] {
|
||||||
"collectgarbage", // ( opt [,arg] ) -> value
|
"collectgarbage", // ( opt [,arg] ) -> value
|
||||||
"error", // ( message [,level] ) -> ERR
|
"error", // ( message [,level] ) -> ERR
|
||||||
"rawequal", // (v1, v2) -> boolean
|
"rawequal", // (v1, v2) -> boolean
|
||||||
"setfenv", // (f, table) -> void
|
"setfenv", // (f, table) -> void
|
||||||
} );
|
} );
|
||||||
bindv( env, new String[] {
|
bind( env, BaseLibV.class, new String[] {
|
||||||
"assert", // ( v [,message] ) -> v, message | ERR
|
"assert", // ( v [,message] ) -> v, message | ERR
|
||||||
"dofile", // ( filename ) -> result1, ...
|
"dofile", // ( filename ) -> result1, ...
|
||||||
"load", // ( func [,chunkname] ) -> chunk | nil, msg
|
"load", // ( func [,chunkname] ) -> chunk | nil, msg
|
||||||
@@ -113,6 +113,11 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
|
|||||||
next = env.get("next");
|
next = env.get("next");
|
||||||
inext = env.get("__inext");
|
inext = env.get("__inext");
|
||||||
|
|
||||||
|
// inject base lib
|
||||||
|
((BaseLibV) env.get("print")).baselib = this;
|
||||||
|
((BaseLibV) env.get("pairs")).baselib = this;
|
||||||
|
((BaseLibV) env.get("ipairs")).baselib = this;
|
||||||
|
|
||||||
// set the default resource finder if not set already
|
// set the default resource finder if not set already
|
||||||
if ( FINDER == null )
|
if ( FINDER == null )
|
||||||
FINDER = this;
|
FINDER = this;
|
||||||
@@ -128,53 +133,57 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
|
|||||||
return c.getResourceAsStream(filename.startsWith("/")? filename: "/"+filename);
|
return c.getResourceAsStream(filename.startsWith("/")? filename: "/"+filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected LuaValue oncall1(int opcode, LuaValue arg) {
|
public static final class BaseLib1 extends OneArgFunction {
|
||||||
switch ( opcode ) {
|
public LuaValue call(LuaValue arg) {
|
||||||
case 0: { // "getfenv", // ( [f] ) -> env
|
switch ( opcode ) {
|
||||||
LuaValue f = getfenvobj(arg);
|
case 0: { // "getfenv", // ( [f] ) -> env
|
||||||
LuaValue e = f.getfenv();
|
LuaValue f = getfenvobj(arg);
|
||||||
return e!=null? e: NIL;
|
LuaValue e = f.getfenv();
|
||||||
}
|
return e!=null? e: NIL;
|
||||||
case 1: // "getmetatable", // ( object ) -> table
|
|
||||||
LuaValue mt = arg.getmetatable();
|
|
||||||
return mt!=null? mt: NIL;
|
|
||||||
}
|
|
||||||
return NIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected LuaValue oncall2(int opcode, LuaValue arg1, LuaValue arg2) {
|
|
||||||
switch ( opcode ) {
|
|
||||||
case 0: // "collectgarbage", // ( opt [,arg] ) -> value
|
|
||||||
String s = arg1.optjstring("collect");
|
|
||||||
int result = 0;
|
|
||||||
if ( "collect".equals(s) ) {
|
|
||||||
System.gc();
|
|
||||||
return ZERO;
|
|
||||||
}
|
}
|
||||||
else if ( "count".equals(s) ) {
|
case 1: // "getmetatable", // ( object ) -> table
|
||||||
Runtime rt = Runtime.getRuntime();
|
LuaValue mt = arg.getmetatable();
|
||||||
long used = rt.totalMemory() - rt.freeMemory();
|
return mt!=null? mt: NIL;
|
||||||
return valueOf(used/1024.);
|
|
||||||
} else if ( "step".equals(s) ) {
|
|
||||||
System.gc();
|
|
||||||
return LuaValue.TRUE;
|
|
||||||
}
|
}
|
||||||
return NIL;
|
return NIL;
|
||||||
case 1: // "error", // ( message [,level] ) -> ERR
|
|
||||||
throw new LuaError( arg1.isnil()? null: arg1.tojstring(), arg2.optint(1) );
|
|
||||||
case 2: // "rawequal", // (v1, v2) -> boolean
|
|
||||||
return valueOf(arg1 == arg2);
|
|
||||||
case 3: { // "setfenv", // (f, table) -> void
|
|
||||||
LuaTable t = arg2.checktable();
|
|
||||||
LuaValue f = getfenvobj(arg1);
|
|
||||||
f.setfenv(t);
|
|
||||||
return f.isthread()? NONE: f;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class BaseLib2 extends TwoArgFunction {
|
||||||
|
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||||
|
switch ( opcode ) {
|
||||||
|
case 0: // "collectgarbage", // ( opt [,arg] ) -> value
|
||||||
|
String s = arg1.optjstring("collect");
|
||||||
|
int result = 0;
|
||||||
|
if ( "collect".equals(s) ) {
|
||||||
|
System.gc();
|
||||||
|
return ZERO;
|
||||||
|
}
|
||||||
|
else if ( "count".equals(s) ) {
|
||||||
|
Runtime rt = Runtime.getRuntime();
|
||||||
|
long used = rt.totalMemory() - rt.freeMemory();
|
||||||
|
return valueOf(used/1024.);
|
||||||
|
} else if ( "step".equals(s) ) {
|
||||||
|
System.gc();
|
||||||
|
return LuaValue.TRUE;
|
||||||
|
}
|
||||||
|
return NIL;
|
||||||
|
case 1: // "error", // ( message [,level] ) -> ERR
|
||||||
|
throw new LuaError( arg1.isnil()? null: arg1.tojstring(), arg2.optint(1) );
|
||||||
|
case 2: // "rawequal", // (v1, v2) -> boolean
|
||||||
|
return valueOf(arg1 == arg2);
|
||||||
|
case 3: { // "setfenv", // (f, table) -> void
|
||||||
|
LuaTable t = arg2.checktable();
|
||||||
|
LuaValue f = getfenvobj(arg1);
|
||||||
|
f.setfenv(t);
|
||||||
|
return f.isthread()? NONE: f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NIL;
|
||||||
}
|
}
|
||||||
return NIL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private LuaValue getfenvobj(LuaValue arg) {
|
private static LuaValue getfenvobj(LuaValue arg) {
|
||||||
if ( arg.isclosure() )
|
if ( arg.isclosure() )
|
||||||
return arg;
|
return arg;
|
||||||
int level = arg.optint(1);
|
int level = arg.optint(1);
|
||||||
@@ -185,165 +194,168 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
|
|||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Varargs oncallv(int opcode, Varargs args) {
|
public static final class BaseLibV extends VarArgFunction {
|
||||||
switch ( opcode ) {
|
public BaseLib baselib;
|
||||||
case 0: // "assert", // ( v [,message] ) -> v, message | ERR
|
public Varargs invoke(Varargs args) {
|
||||||
if ( !args.arg1().toboolean() ) error("assertion failed!");
|
switch ( opcode ) {
|
||||||
return args;
|
case 0: // "assert", // ( v [,message] ) -> v, message | ERR
|
||||||
case 1: // "dofile", // ( filename ) -> result1, ...
|
if ( !args.arg1().toboolean() ) error("assertion failed!");
|
||||||
{
|
return args;
|
||||||
LuaValue chunk;
|
case 1: // "dofile", // ( filename ) -> result1, ...
|
||||||
try {
|
{
|
||||||
String filename = args.checkjstring(1);
|
LuaValue chunk;
|
||||||
chunk = loadFile(filename).arg1();
|
|
||||||
} catch ( IOException e ) {
|
|
||||||
return error(e.getMessage());
|
|
||||||
}
|
|
||||||
return chunk.invoke();
|
|
||||||
}
|
|
||||||
case 2: // "load", // ( func [,chunkname] ) -> chunk | nil, msg
|
|
||||||
try {
|
|
||||||
LuaValue func = args.checkfunction(1);
|
|
||||||
String chunkname = args.optString(2, "function");
|
|
||||||
return LoadState.load(new StringInputStream(func), chunkname, LuaThread.getGlobals());
|
|
||||||
} catch ( Exception e ) {
|
|
||||||
return varargsOf(NIL, valueOf(e.getMessage()));
|
|
||||||
}
|
|
||||||
case 3: // "loadfile", // ( [filename] ) -> chunk | nil, msg
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
String filename = args.checkjstring(1);
|
|
||||||
return loadFile(filename);
|
|
||||||
} catch ( Exception e ) {
|
|
||||||
return varargsOf(NIL, valueOf(e.getMessage()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case 4: // "loadstring", // ( string [,chunkname] ) -> chunk | nil, msg
|
|
||||||
try {
|
|
||||||
LuaString script = args.checkstring(1);
|
|
||||||
String chunkname = args.optString(2, "string");
|
|
||||||
return LoadState.load(script.toInputStream(),chunkname,LuaThread.getGlobals());
|
|
||||||
} catch ( Exception e ) {
|
|
||||||
return varargsOf(NIL, valueOf(e.getMessage()));
|
|
||||||
}
|
|
||||||
case 5: // "pcall", // (f, arg1, ...) -> status, result1, ...
|
|
||||||
try {
|
|
||||||
LuaThread.onCall(this);
|
|
||||||
try {
|
try {
|
||||||
return varargsOf(LuaValue.TRUE, args.arg1().invoke(args.subargs(2)));
|
String filename = args.checkjstring(1);
|
||||||
} finally {
|
chunk = loadFile(filename).arg1();
|
||||||
LuaThread.onReturn();
|
} catch ( IOException e ) {
|
||||||
|
return error(e.getMessage());
|
||||||
}
|
}
|
||||||
} catch ( LuaError le ) {
|
return chunk.invoke();
|
||||||
String m = le.getMessage();
|
|
||||||
return varargsOf(FALSE, m!=null? valueOf(m): NIL);
|
|
||||||
} catch ( Exception e ) {
|
|
||||||
String m = e.getMessage();
|
|
||||||
return varargsOf(FALSE, valueOf(m!=null? m: e.toString()));
|
|
||||||
}
|
}
|
||||||
case 6: // "xpcall", // (f, err) -> result1, ...
|
case 2: // "load", // ( func [,chunkname] ) -> chunk | nil, msg
|
||||||
{
|
|
||||||
LuaValue errfunc = args.checkvalue(2);
|
|
||||||
try {
|
|
||||||
LuaThread.onCall(this);
|
|
||||||
try {
|
try {
|
||||||
LuaThread thread = LuaThread.getRunning();
|
LuaValue func = args.checkfunction(1);
|
||||||
LuaValue olderr = thread.err;
|
String chunkname = args.optString(2, "function");
|
||||||
|
return LoadState.load(new StringInputStream(func), chunkname, LuaThread.getGlobals());
|
||||||
|
} catch ( Exception e ) {
|
||||||
|
return varargsOf(NIL, valueOf(e.getMessage()));
|
||||||
|
}
|
||||||
|
case 3: // "loadfile", // ( [filename] ) -> chunk | nil, msg
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
String filename = args.checkjstring(1);
|
||||||
|
return loadFile(filename);
|
||||||
|
} catch ( Exception e ) {
|
||||||
|
return varargsOf(NIL, valueOf(e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 4: // "loadstring", // ( string [,chunkname] ) -> chunk | nil, msg
|
||||||
|
try {
|
||||||
|
LuaString script = args.checkstring(1);
|
||||||
|
String chunkname = args.optString(2, "string");
|
||||||
|
return LoadState.load(script.toInputStream(),chunkname,LuaThread.getGlobals());
|
||||||
|
} catch ( Exception e ) {
|
||||||
|
return varargsOf(NIL, valueOf(e.getMessage()));
|
||||||
|
}
|
||||||
|
case 5: // "pcall", // (f, arg1, ...) -> status, result1, ...
|
||||||
|
try {
|
||||||
|
LuaThread.onCall(this);
|
||||||
try {
|
try {
|
||||||
thread.err = errfunc;
|
|
||||||
return varargsOf(LuaValue.TRUE, args.arg1().invoke(args.subargs(2)));
|
return varargsOf(LuaValue.TRUE, args.arg1().invoke(args.subargs(2)));
|
||||||
} finally {
|
} finally {
|
||||||
thread.err = olderr;
|
LuaThread.onReturn();
|
||||||
}
|
}
|
||||||
} finally {
|
} catch ( LuaError le ) {
|
||||||
LuaThread.onReturn();
|
String m = le.getMessage();
|
||||||
|
return varargsOf(FALSE, m!=null? valueOf(m): NIL);
|
||||||
|
} catch ( Exception e ) {
|
||||||
|
String m = e.getMessage();
|
||||||
|
return varargsOf(FALSE, valueOf(m!=null? m: e.toString()));
|
||||||
|
}
|
||||||
|
case 6: // "xpcall", // (f, err) -> result1, ...
|
||||||
|
{
|
||||||
|
LuaValue errfunc = args.checkvalue(2);
|
||||||
|
try {
|
||||||
|
LuaThread.onCall(this);
|
||||||
|
try {
|
||||||
|
LuaThread thread = LuaThread.getRunning();
|
||||||
|
LuaValue olderr = thread.err;
|
||||||
|
try {
|
||||||
|
thread.err = errfunc;
|
||||||
|
return varargsOf(LuaValue.TRUE, args.arg1().invoke(args.subargs(2)));
|
||||||
|
} finally {
|
||||||
|
thread.err = olderr;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
LuaThread.onReturn();
|
||||||
|
}
|
||||||
|
} catch ( LuaError le ) {
|
||||||
|
String m = le.getMessage();
|
||||||
|
return varargsOf(FALSE, m!=null? valueOf(m): NIL);
|
||||||
|
} catch ( Exception e ) {
|
||||||
|
String m = e.getMessage();
|
||||||
|
return varargsOf(FALSE, valueOf(m!=null? m: e.toString()));
|
||||||
}
|
}
|
||||||
} catch ( LuaError le ) {
|
|
||||||
String m = le.getMessage();
|
|
||||||
return varargsOf(FALSE, m!=null? valueOf(m): NIL);
|
|
||||||
} catch ( Exception e ) {
|
|
||||||
String m = e.getMessage();
|
|
||||||
return varargsOf(FALSE, valueOf(m!=null? m: e.toString()));
|
|
||||||
}
|
}
|
||||||
}
|
case 7: // "print", // (...) -> void
|
||||||
case 7: // "print", // (...) -> void
|
{
|
||||||
{
|
LuaValue tostring = LuaThread.getGlobals().get("tostring");
|
||||||
LuaValue tostring = LuaThread.getGlobals().get("tostring");
|
for ( int i=1, n=args.narg(); i<=n; i++ ) {
|
||||||
for ( int i=1, n=args.narg(); i<=n; i++ ) {
|
if ( i>1 ) baselib.STDOUT.write( '\t' );
|
||||||
if ( i>1 ) STDOUT.write( '\t' );
|
LuaString s = tostring.call( args.arg(i) ).strvalue();
|
||||||
LuaString s = tostring.call( args.arg(i) ).strvalue();
|
int z = s.indexOf((byte)0, 0);
|
||||||
int z = s.indexOf((byte)0, 0);
|
baselib.STDOUT.write( s.m_bytes, s.m_offset, z>=0? z: s.m_length );
|
||||||
STDOUT.write( s.m_bytes, s.m_offset, z>=0? z: s.m_length );
|
}
|
||||||
|
baselib.STDOUT.println();
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
case 8: // "select", // (f, ...) -> value1, ...
|
||||||
|
{
|
||||||
|
int n = args.narg()-1;
|
||||||
|
if ( args.arg1().equals(valueOf("#")) )
|
||||||
|
return valueOf(n);
|
||||||
|
int i = args.checkint(1);
|
||||||
|
if ( i == 0 || i < -n )
|
||||||
|
typerror(1,"index out of range");
|
||||||
|
return args.subargs(i<0? n+i+2: i+1);
|
||||||
|
}
|
||||||
|
case 9: // "unpack", // (list [,i [,j]]) -> result1, ...
|
||||||
|
{
|
||||||
|
int na = args.narg();
|
||||||
|
LuaTable t = args.checktable(1);
|
||||||
|
int n = t.length();
|
||||||
|
int i = na>=2? args.checkint(2): 1;
|
||||||
|
int j = na>=3? args.checkint(3): n;
|
||||||
|
n = j-i+1;
|
||||||
|
if ( n<0 ) return NONE;
|
||||||
|
if ( n==1 ) return t.get(i);
|
||||||
|
if ( n==2 ) return varargsOf(t.get(i),t.get(j));
|
||||||
|
LuaValue[] v = new LuaValue[n];
|
||||||
|
for ( int k=0; k<n; k++ )
|
||||||
|
v[k] = t.get(i+k);
|
||||||
|
return varargsOf(v);
|
||||||
|
}
|
||||||
|
case 10: // "type", // (v) -> value
|
||||||
|
return valueOf(args.checkvalue(1).typename());
|
||||||
|
case 11: // "rawget", // (table, index) -> value
|
||||||
|
return args.checktable(1).rawget(args.checkvalue(2));
|
||||||
|
case 12: { // "rawset", // (table, index, value) -> table
|
||||||
|
LuaTable t = args.checktable(1);
|
||||||
|
t.rawset(args.checknotnil(2), args.checkvalue(3));
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
case 13: { // "setmetatable", // (table, metatable) -> table
|
||||||
|
final LuaValue t = args.arg1();
|
||||||
|
final LuaValue mt = args.checkvalue(2);
|
||||||
|
return t.setmetatable(mt.isnil()? null: mt.checktable());
|
||||||
|
}
|
||||||
|
case 14: { // "tostring", // (e) -> value
|
||||||
|
LuaValue arg = args.checkvalue(1);
|
||||||
|
return arg.type() == LuaValue.TSTRING? arg: valueOf(arg.tojstring());
|
||||||
|
}
|
||||||
|
case 15: { // "tonumber", // (e [,base]) -> value
|
||||||
|
LuaValue arg1 = args.checkvalue(1);
|
||||||
|
final int base = args.optint(2,10);
|
||||||
|
if (base == 10) { /* standard conversion */
|
||||||
|
return arg1.tonumber();
|
||||||
|
} else {
|
||||||
|
if ( base < 2 || base > 36 )
|
||||||
|
argerror(2, "base out of range");
|
||||||
|
final LuaString str = arg1.optstring(null);
|
||||||
|
return str!=null? str.tonumber(base): NIL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 16: // "pairs" (t) -> iter-func, t, nil
|
||||||
|
return varargsOf( baselib.next, args.checktable(1) );
|
||||||
|
case 17: // "ipairs", // (t) -> iter-func, t, 0
|
||||||
|
return varargsOf( baselib.inext, args.checktable(1), ZERO );
|
||||||
|
case 18: // "next" ( table, [index] ) -> next-index, next-value
|
||||||
|
return args.arg1().next(args.arg(2));
|
||||||
|
case 19: // "inext" ( table, [int-index] ) -> next-index, next-value
|
||||||
|
return args.arg1().inext(args.arg(2));
|
||||||
}
|
}
|
||||||
STDOUT.println();
|
|
||||||
return NONE;
|
return NONE;
|
||||||
}
|
}
|
||||||
case 8: // "select", // (f, ...) -> value1, ...
|
|
||||||
{
|
|
||||||
int n = args.narg()-1;
|
|
||||||
if ( args.arg1().equals(valueOf("#")) )
|
|
||||||
return valueOf(n);
|
|
||||||
int i = args.checkint(1);
|
|
||||||
if ( i == 0 || i < -n )
|
|
||||||
typerror(1,"index out of range");
|
|
||||||
return args.subargs(i<0? n+i+2: i+1);
|
|
||||||
}
|
|
||||||
case 9: // "unpack", // (list [,i [,j]]) -> result1, ...
|
|
||||||
{
|
|
||||||
int na = args.narg();
|
|
||||||
LuaTable t = args.checktable(1);
|
|
||||||
int n = t.length();
|
|
||||||
int i = na>=2? args.checkint(2): 1;
|
|
||||||
int j = na>=3? args.checkint(3): n;
|
|
||||||
n = j-i+1;
|
|
||||||
if ( n<0 ) return NONE;
|
|
||||||
if ( n==1 ) return t.get(i);
|
|
||||||
if ( n==2 ) return varargsOf(t.get(i),t.get(j));
|
|
||||||
LuaValue[] v = new LuaValue[n];
|
|
||||||
for ( int k=0; k<n; k++ )
|
|
||||||
v[k] = t.get(i+k);
|
|
||||||
return varargsOf(v);
|
|
||||||
}
|
|
||||||
case 10: // "type", // (v) -> value
|
|
||||||
return valueOf(args.checkvalue(1).typename());
|
|
||||||
case 11: // "rawget", // (table, index) -> value
|
|
||||||
return args.checktable(1).rawget(args.checkvalue(2));
|
|
||||||
case 12: { // "rawset", // (table, index, value) -> table
|
|
||||||
LuaTable t = args.checktable(1);
|
|
||||||
t.rawset(args.checknotnil(2), args.checkvalue(3));
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
case 13: { // "setmetatable", // (table, metatable) -> table
|
|
||||||
final LuaValue t = args.arg1();
|
|
||||||
final LuaValue mt = args.checkvalue(2);
|
|
||||||
return t.setmetatable(mt.isnil()? null: mt.checktable());
|
|
||||||
}
|
|
||||||
case 14: { // "tostring", // (e) -> value
|
|
||||||
LuaValue arg = args.checkvalue(1);
|
|
||||||
return arg.type() == LuaValue.TSTRING? arg: valueOf(arg.tojstring());
|
|
||||||
}
|
|
||||||
case 15: { // "tonumber", // (e [,base]) -> value
|
|
||||||
LuaValue arg1 = args.checkvalue(1);
|
|
||||||
final int base = args.optint(2,10);
|
|
||||||
if (base == 10) { /* standard conversion */
|
|
||||||
return arg1.tonumber();
|
|
||||||
} else {
|
|
||||||
if ( base < 2 || base > 36 )
|
|
||||||
argerror(2, "base out of range");
|
|
||||||
final LuaString str = arg1.optstring(null);
|
|
||||||
return str!=null? str.tonumber(base): NIL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case 16: // "pairs" (t) -> iter-func, t, nil
|
|
||||||
return varargsOf( next, args.checktable(1) );
|
|
||||||
case 17: // "ipairs", // (t) -> iter-func, t, 0
|
|
||||||
return varargsOf( inext, args.checktable(1), ZERO );
|
|
||||||
case 18: // "next" ( table, [index] ) -> next-index, next-value
|
|
||||||
return args.arg1().next(args.arg(2));
|
|
||||||
case 19: // "inext" ( table, [int-index] ) -> next-index, next-value
|
|
||||||
return args.arg1().inext(args.arg(2));
|
|
||||||
}
|
|
||||||
return NONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Varargs loadFile(String filename) throws IOException {
|
public static Varargs loadFile(String filename) throws IOException {
|
||||||
|
|||||||
@@ -111,28 +111,31 @@ public class DebugLib extends OneArgFunction {
|
|||||||
|
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
LuaTable t = new LuaTable();
|
LuaTable t = new LuaTable();
|
||||||
bindv(t, NAMES);
|
bind(t, DebugLibV.class, NAMES);
|
||||||
env.set("debug", t);
|
env.set("debug", t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Varargs oncallv(int opcode, Varargs args) {
|
public static final class DebugLibV extends VarArgFunction {
|
||||||
switch ( opcode ) {
|
protected DebugLib debuglib;
|
||||||
case DEBUG: return _debug(args);
|
public Varargs invoke(Varargs args) {
|
||||||
case GETFENV: return _getfenv(args);
|
switch ( opcode ) {
|
||||||
case GETHOOK: return _gethook(args);
|
case DEBUG: return _debug(args);
|
||||||
case GETINFO: return _getinfo(args);
|
case GETFENV: return _getfenv(args);
|
||||||
case GETLOCAL: return _getlocal(args);
|
case GETHOOK: return _gethook(args);
|
||||||
case GETMETATABLE: return _getmetatable(args);
|
case GETINFO: return _getinfo(args,this);
|
||||||
case GETREGISTRY: return _getregistry(args);
|
case GETLOCAL: return _getlocal(args);
|
||||||
case GETUPVALUE: return _getupvalue(args);
|
case GETMETATABLE: return _getmetatable(args);
|
||||||
case SETFENV: return _setfenv(args);
|
case GETREGISTRY: return _getregistry(args);
|
||||||
case SETHOOK: return _sethook(args);
|
case GETUPVALUE: return _getupvalue(args);
|
||||||
case SETLOCAL: return _setlocal(args);
|
case SETFENV: return _setfenv(args);
|
||||||
case SETMETATABLE: return _setmetatable(args);
|
case SETHOOK: return _sethook(args);
|
||||||
case SETUPVALUE: return _setupvalue(args);
|
case SETLOCAL: return _setlocal(args);
|
||||||
case TRACEBACK: return _traceback(args);
|
case SETMETATABLE: return _setmetatable(args);
|
||||||
default: return NONE;
|
case SETUPVALUE: return _setupvalue(args);
|
||||||
|
case TRACEBACK: return _traceback(args);
|
||||||
|
default: return NONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,7 +409,7 @@ public class DebugLib extends OneArgFunction {
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Varargs _getinfo(Varargs args) {
|
protected static Varargs _getinfo(Varargs args, LuaValue level0func) {
|
||||||
int a=1;
|
int a=1;
|
||||||
LuaThread thread = args.isthread(a)? args.checkthread(a++): LuaThread.getRunning();
|
LuaThread thread = args.isthread(a)? args.checkthread(a++): LuaThread.getRunning();
|
||||||
LuaValue func = args.arg(a++);
|
LuaValue func = args.arg(a++);
|
||||||
@@ -419,7 +422,7 @@ public class DebugLib extends OneArgFunction {
|
|||||||
int level = func.checkint();
|
int level = func.checkint();
|
||||||
di = level>0?
|
di = level>0?
|
||||||
ds.getDebugInfo(level-1):
|
ds.getDebugInfo(level-1):
|
||||||
new DebugInfo( this );
|
new DebugInfo( level0func );
|
||||||
} else {
|
} else {
|
||||||
di = ds.findDebugInfo( func.checkfunction() );
|
di = ds.findDebugInfo( func.checkfunction() );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,14 +21,42 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.luaj.vm2.lib;
|
package org.luaj.vm2.lib;
|
||||||
|
|
||||||
|
import org.luaj.vm2.LuaError;
|
||||||
import org.luaj.vm2.LuaFunction;
|
import org.luaj.vm2.LuaFunction;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Varargs;
|
import org.luaj.vm2.Varargs;
|
||||||
|
|
||||||
abstract public class LibFunction extends LuaFunction {
|
abstract public class LibFunction extends LuaFunction {
|
||||||
|
|
||||||
|
protected int opcode;
|
||||||
|
protected String name;
|
||||||
|
|
||||||
protected LibFunction() {
|
protected LibFunction() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String tojstring() {
|
||||||
|
return name != null? name: super.tojstring();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void bind(LuaValue env, Class factory, String[] names ) {
|
||||||
|
bind( env, factory, names, 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void bind(LuaValue env, Class factory, String[] names, int firstopcode ) {
|
||||||
|
try {
|
||||||
|
for ( int i=0, n=names.length; i<n; i++ ) {
|
||||||
|
LibFunction f = (LibFunction) factory.newInstance();
|
||||||
|
f.opcode = firstopcode + i;
|
||||||
|
f.name = names[i];
|
||||||
|
f.env = env;
|
||||||
|
env.set(f.name, f);
|
||||||
|
}
|
||||||
|
} catch ( Exception e ) {
|
||||||
|
throw new LuaError( "bind failed: "+e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void bind0(LuaValue env, String[] names) {
|
protected void bind0(LuaValue env, String[] names) {
|
||||||
bind(env, names, 0, 0);
|
bind(env, names, 0, 0);
|
||||||
}
|
}
|
||||||
@@ -100,8 +128,6 @@ abstract public class LibFunction extends LuaFunction {
|
|||||||
|
|
||||||
/** Binding to a one-arg function */
|
/** Binding to a one-arg function */
|
||||||
private static class ZeroArgBinding extends LibFunction {
|
private static class ZeroArgBinding extends LibFunction {
|
||||||
private final String name;
|
|
||||||
private final int opcode;
|
|
||||||
private final LibFunction delegate;
|
private final LibFunction delegate;
|
||||||
|
|
||||||
private ZeroArgBinding(String name, int opcode, LibFunction delegate) {
|
private ZeroArgBinding(String name, int opcode, LibFunction delegate) {
|
||||||
@@ -137,8 +163,6 @@ abstract public class LibFunction extends LuaFunction {
|
|||||||
|
|
||||||
/** Binding to a one-arg function */
|
/** Binding to a one-arg function */
|
||||||
private static class OneArgBinding extends LibFunction {
|
private static class OneArgBinding extends LibFunction {
|
||||||
private final String name;
|
|
||||||
private final int opcode;
|
|
||||||
private final LibFunction delegate;
|
private final LibFunction delegate;
|
||||||
|
|
||||||
private OneArgBinding(String name, int opcode, LibFunction delegate) {
|
private OneArgBinding(String name, int opcode, LibFunction delegate) {
|
||||||
@@ -174,8 +198,6 @@ abstract public class LibFunction extends LuaFunction {
|
|||||||
|
|
||||||
/** Binding to a two-arg function */
|
/** Binding to a two-arg function */
|
||||||
private static class TwoArgBinding extends LibFunction {
|
private static class TwoArgBinding extends LibFunction {
|
||||||
private final String name;
|
|
||||||
private final int opcode;
|
|
||||||
private final LibFunction delegate;
|
private final LibFunction delegate;
|
||||||
|
|
||||||
private TwoArgBinding(String name, int opcode, LibFunction delegate) {
|
private TwoArgBinding(String name, int opcode, LibFunction delegate) {
|
||||||
@@ -211,8 +233,6 @@ abstract public class LibFunction extends LuaFunction {
|
|||||||
|
|
||||||
/** Binding to a three-arg function */
|
/** Binding to a three-arg function */
|
||||||
private static class ThreeArgBinding extends LibFunction {
|
private static class ThreeArgBinding extends LibFunction {
|
||||||
private final String name;
|
|
||||||
private final int opcode;
|
|
||||||
private final LibFunction delegate;
|
private final LibFunction delegate;
|
||||||
|
|
||||||
private ThreeArgBinding(String name, int opcode, LibFunction delegate) {
|
private ThreeArgBinding(String name, int opcode, LibFunction delegate) {
|
||||||
@@ -248,8 +268,6 @@ abstract public class LibFunction extends LuaFunction {
|
|||||||
|
|
||||||
/** Binding to a var-arg function */
|
/** Binding to a var-arg function */
|
||||||
private static class VarArgBinding extends LibFunction {
|
private static class VarArgBinding extends LibFunction {
|
||||||
private final String name;
|
|
||||||
private final int opcode;
|
|
||||||
private final LibFunction delegate;
|
private final LibFunction delegate;
|
||||||
|
|
||||||
private VarArgBinding(String name, int opcode, LibFunction delegate) {
|
private VarArgBinding(String name, int opcode, LibFunction delegate) {
|
||||||
|
|||||||
@@ -49,66 +49,73 @@ public class MathLib extends OneArgFunction {
|
|||||||
LuaTable t = new LuaTable(0,30);
|
LuaTable t = new LuaTable(0,30);
|
||||||
t.set( "pi", Math.PI );
|
t.set( "pi", Math.PI );
|
||||||
t.set( "huge", LuaDouble.POSINF );
|
t.set( "huge", LuaDouble.POSINF );
|
||||||
bind1( t, new String[] {
|
bind( t, MathLib1.class, new String[] {
|
||||||
"abs", "ceil", "cos", "deg",
|
"abs", "ceil", "cos", "deg",
|
||||||
"exp", "floor", "rad", "sin",
|
"exp", "floor", "rad", "sin",
|
||||||
"sqrt", "tan" } );
|
"sqrt", "tan" } );
|
||||||
bind2( t, new String[] {
|
bind( t, MathLib2.class, new String[] {
|
||||||
"fmod", "ldexp", "pow", "random", } );
|
"fmod", "ldexp", "pow", "random", } );
|
||||||
bindv( t, new String[] {
|
bind( t, MathLibV.class, new String[] {
|
||||||
"frexp", "max", "min", "modf",
|
"frexp", "max", "min", "modf",
|
||||||
"randomseed" } );
|
"randomseed" } );
|
||||||
|
((MathLib2) t.get("random" )).mathlib = this;
|
||||||
|
((MathLibV) t.get("randomseed")).mathlib = this;
|
||||||
env.set("math", t);
|
env.set("math", t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaValue oncall1(int opcode, LuaValue arg) {
|
public static final class MathLib1 extends OneArgFunction {
|
||||||
switch ( opcode ) {
|
public LuaValue call(LuaValue arg) {
|
||||||
case 0: return valueOf(Math.abs(arg.todouble()));
|
switch ( opcode ) {
|
||||||
case 1: return valueOf(Math.ceil(arg.todouble()));
|
case 0: return valueOf(Math.abs(arg.todouble()));
|
||||||
case 2: return valueOf(Math.cos(arg.todouble()));
|
case 1: return valueOf(Math.ceil(arg.todouble()));
|
||||||
case 3: return valueOf(Math.toDegrees(arg.todouble()));
|
case 2: return valueOf(Math.cos(arg.todouble()));
|
||||||
case 4: return dpow(Math.E,arg.todouble());
|
case 3: return valueOf(Math.toDegrees(arg.todouble()));
|
||||||
case 5: return valueOf(Math.floor(arg.todouble()));
|
case 4: return dpow(Math.E,arg.todouble());
|
||||||
case 6: return valueOf(Math.toRadians(arg.todouble()));
|
case 5: return valueOf(Math.floor(arg.todouble()));
|
||||||
case 7: return valueOf(Math.sin(arg.todouble()));
|
case 6: return valueOf(Math.toRadians(arg.todouble()));
|
||||||
case 8: return valueOf(Math.sqrt(arg.todouble()));
|
case 7: return valueOf(Math.sin(arg.todouble()));
|
||||||
case 9: return valueOf(Math.tan(arg.todouble()));
|
case 8: return valueOf(Math.sqrt(arg.todouble()));
|
||||||
|
case 9: return valueOf(Math.tan(arg.todouble()));
|
||||||
|
}
|
||||||
|
return NIL;
|
||||||
}
|
}
|
||||||
return NIL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaValue oncall2(int opcode,LuaValue arg1,LuaValue arg2) {
|
public static final class MathLib2 extends TwoArgFunction {
|
||||||
switch ( opcode ) {
|
protected MathLib mathlib;
|
||||||
case 0: { // fmod
|
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||||
double x = arg1.checkdouble();
|
switch ( opcode ) {
|
||||||
double y = arg2.checkdouble();
|
case 0: { // fmod
|
||||||
double q = x/y;
|
double x = arg1.checkdouble();
|
||||||
double f = x - y * (q>=0? Math.floor(q): Math.ceil(q));
|
double y = arg2.checkdouble();
|
||||||
return valueOf( f );
|
double q = x/y;
|
||||||
|
double f = x - y * (q>=0? Math.floor(q): Math.ceil(q));
|
||||||
|
return valueOf( f );
|
||||||
|
}
|
||||||
|
case 1: { // ldexp
|
||||||
|
double x = arg1.checkdouble();
|
||||||
|
double y = arg2.checkdouble()+1023.5;
|
||||||
|
long e = (long) ((0!=(1&((int)y)))? Math.floor(y): Math.ceil(y-1));
|
||||||
|
return valueOf(x * Double.longBitsToDouble(e << 52));
|
||||||
|
}
|
||||||
|
case 2: { // pow
|
||||||
|
return dpow(arg1.todouble(), arg2.todouble());
|
||||||
|
}
|
||||||
|
case 3: { // random
|
||||||
|
if ( mathlib.random == null )
|
||||||
|
mathlib.random = new Random();
|
||||||
|
if ( arg1.isnil() )
|
||||||
|
return valueOf( mathlib.random.nextDouble() );
|
||||||
|
int m = arg1.toint();
|
||||||
|
if ( arg2.isnil() )
|
||||||
|
return valueOf( 1 + mathlib.random.nextInt(m) );
|
||||||
|
else
|
||||||
|
return valueOf( m + mathlib.random.nextInt(arg2.toint()-m) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NIL;
|
||||||
}
|
}
|
||||||
case 1: { // ldexp
|
|
||||||
double x = arg1.checkdouble();
|
|
||||||
double y = arg2.checkdouble()+1023.5;
|
|
||||||
long e = (long) ((0!=(1&((int)y)))? Math.floor(y): Math.ceil(y-1));
|
|
||||||
return valueOf(x * Double.longBitsToDouble(e << 52));
|
|
||||||
}
|
|
||||||
case 2: { // pow
|
|
||||||
return dpow(arg1.todouble(), arg2.todouble());
|
|
||||||
}
|
|
||||||
case 3: { // random
|
|
||||||
if ( random == null )
|
|
||||||
random = new Random();
|
|
||||||
if ( arg1.isnil() )
|
|
||||||
return valueOf( random.nextDouble() );
|
|
||||||
int m = arg1.toint();
|
|
||||||
if ( arg2.isnil() )
|
|
||||||
return valueOf( 1 + random.nextInt(m) );
|
|
||||||
else
|
|
||||||
return valueOf( m + random.nextInt(arg2.toint()-m) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NIL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** compute power using installed math library, or default if there is no math library installed */
|
/** compute power using installed math library, or default if there is no math library installed */
|
||||||
@@ -148,40 +155,43 @@ public class MathLib extends OneArgFunction {
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Varargs oncallv(int opcode,Varargs args) {
|
public static final class MathLibV extends VarArgFunction {
|
||||||
switch ( opcode ) {
|
protected MathLib mathlib;
|
||||||
case 0: { // frexp
|
public Varargs invoke(Varargs args) {
|
||||||
double x = args.checkdouble(1);
|
switch ( opcode ) {
|
||||||
if ( x == 0 ) return varargsOf(ZERO,ZERO);
|
case 0: { // frexp
|
||||||
long bits = Double.doubleToLongBits( x );
|
double x = args.checkdouble(1);
|
||||||
double m = ((bits & (~(-1L<<52))) + (1L<<52)) * ((bits >= 0)? (.5 / (1L<<52)): (-.5 / (1L<<52)));
|
if ( x == 0 ) return varargsOf(ZERO,ZERO);
|
||||||
double e = (((int) (bits >> 52)) & 0x7ff) - 1022;
|
long bits = Double.doubleToLongBits( x );
|
||||||
return varargsOf( valueOf(m), valueOf(e) );
|
double m = ((bits & (~(-1L<<52))) + (1L<<52)) * ((bits >= 0)? (.5 / (1L<<52)): (-.5 / (1L<<52)));
|
||||||
}
|
double e = (((int) (bits >> 52)) & 0x7ff) - 1022;
|
||||||
case 1: { // max
|
return varargsOf( valueOf(m), valueOf(e) );
|
||||||
double m = args.checkdouble(1);
|
}
|
||||||
for ( int i=2,n=args.narg(); i<=n; ++i )
|
case 1: { // max
|
||||||
m = Math.max(m,args.checkdouble(i));
|
double m = args.checkdouble(1);
|
||||||
return valueOf(m);
|
for ( int i=2,n=args.narg(); i<=n; ++i )
|
||||||
}
|
m = Math.max(m,args.checkdouble(i));
|
||||||
case 2: { // min
|
return valueOf(m);
|
||||||
double m = args.checkdouble(1);
|
}
|
||||||
for ( int i=2,n=args.narg(); i<=n; ++i )
|
case 2: { // min
|
||||||
m = Math.min(m,args.checkdouble(i));
|
double m = args.checkdouble(1);
|
||||||
return valueOf(m);
|
for ( int i=2,n=args.narg(); i<=n; ++i )
|
||||||
}
|
m = Math.min(m,args.checkdouble(i));
|
||||||
case 3: { // modf
|
return valueOf(m);
|
||||||
double x = args.checkdouble(1);
|
}
|
||||||
double intPart = ( x > 0 ) ? Math.floor( x ) : Math.ceil( x );
|
case 3: { // modf
|
||||||
double fracPart = x - intPart;
|
double x = args.checkdouble(1);
|
||||||
return varargsOf( valueOf(intPart), valueOf(fracPart) );
|
double intPart = ( x > 0 ) ? Math.floor( x ) : Math.ceil( x );
|
||||||
}
|
double fracPart = x - intPart;
|
||||||
case 4: { // randomseed
|
return varargsOf( valueOf(intPart), valueOf(fracPart) );
|
||||||
long seed = args.checklong(1);
|
}
|
||||||
random = new Random(seed);
|
case 4: { // randomseed
|
||||||
|
long seed = args.checklong(1);
|
||||||
|
mathlib.random = new Random(seed);
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
return NONE;
|
return NONE;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return NONE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,9 +41,9 @@ public class StringLib extends OneArgFunction {
|
|||||||
|
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
LuaTable t = new LuaTable();
|
LuaTable t = new LuaTable();
|
||||||
bind1(t, new String[] {
|
bind(t, StringLib1.class, new String[] {
|
||||||
"dump", "len", "lower", "reverse", "upper", } );
|
"dump", "len", "lower", "reverse", "upper", } );
|
||||||
bindv(t, new String[] {
|
bind(t, StringLibV.class, new String[] {
|
||||||
"byte", "char", "find", "format",
|
"byte", "char", "find", "format",
|
||||||
"gmatch", "gsub", "match", "rep",
|
"gmatch", "gsub", "match", "rep",
|
||||||
"sub"} );
|
"sub"} );
|
||||||
@@ -52,31 +52,34 @@ public class StringLib extends OneArgFunction {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected LuaValue oncall1(int opcode, LuaValue arg) {
|
public static final class StringLib1 extends OneArgFunction {
|
||||||
switch ( opcode ) {
|
public LuaValue call(LuaValue arg) {
|
||||||
case 0: return dump(arg); // dump (function)
|
switch ( opcode ) {
|
||||||
case 1: return len(arg); // len (function)
|
case 0: return dump(arg); // dump (function)
|
||||||
case 2: return lower(arg); // lower (function)
|
case 1: return StringLib.len(arg); // len (function)
|
||||||
case 3: return reverse(arg); // reverse (function)
|
case 2: return lower(arg); // lower (function)
|
||||||
case 4: return upper(arg); // upper (function)
|
case 3: return reverse(arg); // reverse (function)
|
||||||
|
case 4: return upper(arg); // upper (function)
|
||||||
|
}
|
||||||
|
return NIL;
|
||||||
}
|
}
|
||||||
return NIL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final class StringLibV extends VarArgFunction {
|
||||||
protected Varargs oncallv(int opcode, Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
case 0: return StringLib.byte_( args );
|
case 0: return StringLib.byte_( args );
|
||||||
case 1: return StringLib.char_( args );
|
case 1: return StringLib.char_( args );
|
||||||
case 2: return StringLib.find( args );
|
case 2: return StringLib.find( args );
|
||||||
case 3: return StringLib.format( args );
|
case 3: return StringLib.format( args );
|
||||||
case 4: return StringLib.gmatch( args );
|
case 4: return StringLib.gmatch( args );
|
||||||
case 5: return StringLib.gsub( args );
|
case 5: return StringLib.gsub( args );
|
||||||
case 6: return StringLib.match( args );
|
case 6: return StringLib.match( args );
|
||||||
case 7: return StringLib.rep( args );
|
case 7: return StringLib.rep( args );
|
||||||
case 8: return StringLib.sub( args );
|
case 8: return StringLib.sub( args );
|
||||||
|
}
|
||||||
|
return NONE;
|
||||||
}
|
}
|
||||||
return NONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -32,11 +32,11 @@ public class TableLib extends OneArgFunction {
|
|||||||
|
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
LuaTable t = new LuaTable();
|
LuaTable t = new LuaTable();
|
||||||
bind1(t, new String[] {
|
bind(t, TableLib1.class, new String[] {
|
||||||
"getn", // (table) -> number
|
"getn", // (table) -> number
|
||||||
"maxn", // (table) -> number
|
"maxn", // (table) -> number
|
||||||
} );
|
} );
|
||||||
bindv(t, new String[] {
|
bind(t, TableLibV.class, new String[] {
|
||||||
"remove", // (table [, pos]) -> removed-ele
|
"remove", // (table [, pos]) -> removed-ele
|
||||||
"concat", // (table [, sep [, i [, j]]]) -> string
|
"concat", // (table [, sep [, i [, j]]]) -> string
|
||||||
"insert", // (table, [pos,] value) -> prev-ele
|
"insert", // (table, [pos,] value) -> prev-ele
|
||||||
@@ -48,46 +48,50 @@ public class TableLib extends OneArgFunction {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected LuaValue oncall1(int opcode, LuaValue arg) {
|
public static final class TableLib1 extends OneArgFunction {
|
||||||
switch ( opcode ) {
|
public LuaValue call(LuaValue arg) {
|
||||||
case 0: return arg.checktable().getn();
|
switch ( opcode ) {
|
||||||
case 1: return valueOf( arg.checktable().maxn());
|
case 0: return arg.checktable().getn();
|
||||||
|
case 1: return valueOf( arg.checktable().maxn());
|
||||||
|
}
|
||||||
|
return NIL;
|
||||||
}
|
}
|
||||||
return NIL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Varargs oncallv(int opcode, Varargs args) {
|
public static final class TableLibV extends VarArgFunction {
|
||||||
switch ( opcode ) {
|
public Varargs invoke(Varargs args) {
|
||||||
case 0: { // "remove" (table [, pos]) -> removed-ele
|
switch ( opcode ) {
|
||||||
LuaTable table = args.checktable(1);
|
case 0: { // "remove" (table [, pos]) -> removed-ele
|
||||||
int pos = args.narg()>1? args.checkint(2): 0;
|
LuaTable table = args.checktable(1);
|
||||||
return table.remove(pos);
|
int pos = args.narg()>1? args.checkint(2): 0;
|
||||||
}
|
return table.remove(pos);
|
||||||
case 1: { // "concat" (table [, sep [, i [, j]]]) -> string
|
}
|
||||||
LuaTable table = args.checktable(1);
|
case 1: { // "concat" (table [, sep [, i [, j]]]) -> string
|
||||||
return table.concat(
|
LuaTable table = args.checktable(1);
|
||||||
args.optstring(2,LuaValue.EMPTYSTRING),
|
return table.concat(
|
||||||
args.optint(3,1),
|
args.optstring(2,LuaValue.EMPTYSTRING),
|
||||||
args.isvalue(4)? args.checkint(4): table.length() );
|
args.optint(3,1),
|
||||||
}
|
args.isvalue(4)? args.checkint(4): table.length() );
|
||||||
case 2: { // "insert" (table, [pos,] value) -> prev-ele
|
}
|
||||||
final LuaTable table = args.checktable(1);
|
case 2: { // "insert" (table, [pos,] value) -> prev-ele
|
||||||
final int pos = args.narg()>2? args.checkint(2): 0;
|
final LuaTable table = args.checktable(1);
|
||||||
final LuaValue value = args.arg( args.narg()>2? 3: 2 );
|
final int pos = args.narg()>2? args.checkint(2): 0;
|
||||||
table.insert( pos, value );
|
final LuaValue value = args.arg( args.narg()>2? 3: 2 );
|
||||||
|
table.insert( pos, value );
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
case 3: { // "sort" (table [, comp]) -> void
|
||||||
|
args.checktable(1).sort( args.optvalue(2,NIL) );
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
case 4: { // (table, func) -> void
|
||||||
|
return args.checktable(1).foreach( args.checkfunction(2) );
|
||||||
|
}
|
||||||
|
case 5: { // "foreachi" (table, func) -> void
|
||||||
|
return args.checktable(1).foreachi( args.checkfunction(2) );
|
||||||
|
}
|
||||||
|
}
|
||||||
return NONE;
|
return NONE;
|
||||||
}
|
}
|
||||||
case 3: { // "sort" (table [, comp]) -> void
|
|
||||||
args.checktable(1).sort( args.optvalue(2,NIL) );
|
|
||||||
return NONE;
|
|
||||||
}
|
|
||||||
case 4: { // (table, func) -> void
|
|
||||||
return args.checktable(1).foreach( args.checkfunction(2) );
|
|
||||||
}
|
|
||||||
case 5: { // "foreachi" (table, func) -> void
|
|
||||||
return args.checktable(1).foreachi( args.checkfunction(2) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NONE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user