Improve compatibility with luaj 1.0

This commit is contained in:
James Roseborough
2010-04-17 14:29:33 +00:00
parent 00f8d291f9
commit ee098145c0
6 changed files with 34 additions and 24 deletions

View File

@@ -303,6 +303,8 @@ public class LuaTable extends LuaValue {
break; break;
} }
} }
if ( hashKeys.length == 0 )
error( "invalid key to 'next'" );
i = hashFindSlot(key); i = hashFindSlot(key);
if ( hashKeys[i] == null ) if ( hashKeys[i] == null )
error( "invalid key to 'next'" ); error( "invalid key to 'next'" );

View File

@@ -36,6 +36,10 @@ public class LuaUserdata extends LuaValue {
m_metatable = metatable; m_metatable = metatable;
} }
public String toString() {
return String.valueOf(m_instance);
}
public int type() { public int type() {
return LuaValue.TUSERDATA; return LuaValue.TUSERDATA;
} }

View File

@@ -150,8 +150,8 @@ public class LuaValue extends Varargs {
public static LuaValue error(int iarg, String message) { throw new LuaError("arg "+iarg+": "+message); } public static LuaValue error(int iarg, String message) { throw new LuaError("arg "+iarg+": "+message); }
public static void assert_(boolean b,String msg) { if(!b) throw new LuaError(msg); } public static void assert_(boolean b,String msg) { if(!b) throw new LuaError(msg); }
public static void argerror(int i,String msg) { throw new LuaError("arg "+i+": "+msg); } public static void argerror(int i,String msg) { throw new LuaError("arg "+i+": "+msg); }
protected LuaValue typerror(String expected) { throw new LuaError("expected "+expected+" got "+typename()); } protected LuaValue typerror(String expected) { throw new LuaError(expected+" expected, got "+typename()); }
protected LuaValue typerror(int iarg, String expected) { throw new LuaError("arg "+iarg+": expected "+expected+" got "+typename()); } protected LuaValue typerror(int iarg, String expected) { throw new LuaError("arg "+iarg+": "+expected+" expected, got "+typename()); }
protected LuaValue unimplemented(String fun) { throw new LuaError("'"+fun+"' not implemented for "+typename()); } protected LuaValue unimplemented(String fun) { throw new LuaError("'"+fun+"' not implemented for "+typename()); }
protected LuaValue aritherror() { throw new LuaError("attempt to perform arithmetic on "+typename()); } protected LuaValue aritherror() { throw new LuaError("attempt to perform arithmetic on "+typename()); }
protected LuaValue aritherror(String fun) { throw new LuaError("attempt to perform arithmetic '"+fun+"' on "+typename()); } protected LuaValue aritherror(String fun) { throw new LuaError("attempt to perform arithmetic '"+fun+"' on "+typename()); }
@@ -178,9 +178,9 @@ public class LuaValue extends Varargs {
public void rawset( String key, int value ) { rawset(valueOf(key),valueOf(value)); } public void rawset( String key, int value ) { rawset(valueOf(key),valueOf(value)); }
public void rawset( String key, String value ) { rawset(valueOf(key),valueOf(value)); } public void rawset( String key, String value ) { rawset(valueOf(key),valueOf(value)); }
public void rawsetlist( int key0, Varargs values ) { for ( int i=0, n=values.narg(); i<n; i++ ) rawset(key0+i,values.arg(i+1)); } public void rawsetlist( int key0, Varargs values ) { for ( int i=0, n=values.narg(); i<n; i++ ) rawset(key0+i,values.arg(i+1)); }
public void presize( int i) { unimplemented("presize"); } public void presize( int i) { typerror("table"); }
public Varargs next(LuaValue index) { unimplemented("next"); return null; } public Varargs next(LuaValue index) { return typerror("table"); }
public Varargs inext(LuaValue index) { unimplemented("inext"); return null; } public Varargs inext(LuaValue index) { return typerror("table"); }
public LuaValue load(LuaValue library) { library.setfenv(this); return library.call(); } public LuaValue load(LuaValue library) { library.setfenv(this); return library.call(); }
// varargs references // varargs references

View File

@@ -80,14 +80,12 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
bind1( env, new String[] { bind1( env, new String[] {
"getfenv", // ( [f] ) -> env "getfenv", // ( [f] ) -> env
"getmetatable", // ( object ) -> table "getmetatable", // ( object ) -> table
"tostring", // (e) -> value
} ); } );
bind2( env, new String[] { bind2( env, 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
"tonumber", // (e [,base]) -> value
} ); } );
bindv( env, new String[] { bindv( env, new String[] {
"assert", // ( v [,message] ) -> v, message | ERR "assert", // ( v [,message] ) -> v, message | ERR
@@ -104,6 +102,8 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
"rawget", // (table, index) -> value "rawget", // (table, index) -> value
"rawset", // (table, index, value) -> table "rawset", // (table, index, value) -> table
"setmetatable", // (table, metatable) -> table "setmetatable", // (table, metatable) -> table
"tostring", // (e) -> value
"tonumber", // (e [,base]) -> value
"pairs", // "pairs" (t) -> iter-func, t, nil "pairs", // "pairs" (t) -> iter-func, t, nil
"ipairs", // "ipairs", // (t) -> iter-func, t, 0 "ipairs", // "ipairs", // (t) -> iter-func, t, 0
"next", // "next" ( table, [index] ) -> next-index, next-value "next", // "next" ( table, [index] ) -> next-index, next-value
@@ -139,8 +139,6 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
case 1: // "getmetatable", // ( object ) -> table case 1: // "getmetatable", // ( object ) -> table
LuaValue mt = arg.getmetatable(); LuaValue mt = arg.getmetatable();
return mt!=null? mt: NIL; return mt!=null? mt: NIL;
case 2: // "tostring", // (e) -> value
return arg.type() == LuaValue.TSTRING? arg: valueOf(arg.toString());
} }
return NIL; return NIL;
} }
@@ -173,16 +171,6 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
f.setfenv(t); f.setfenv(t);
return f.isthread()? NONE: f; return f.isthread()? NONE: f;
} }
case 4: // "tonumber", // (e [,base]) -> value
final int base = arg2.optint(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;
}
} }
return NIL; return NIL;
} }
@@ -332,13 +320,29 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
t.setmetatable(mt.isnil()? null: mt.checktable()); t.setmetatable(mt.isnil()? null: mt.checktable());
return t; return t;
} }
case 14: // "pairs" (t) -> iter-func, t, nil case 14: { // "tostring", // (e) -> value
LuaValue arg = args.checkvalue(1);
return arg.type() == LuaValue.TSTRING? arg: valueOf(arg.toString());
}
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) ); return varargsOf( next, args.checktable(1) );
case 15: // "ipairs", // (t) -> iter-func, t, 0 case 17: // "ipairs", // (t) -> iter-func, t, 0
return varargsOf( inext, args.checktable(1), ZERO ); return varargsOf( inext, args.checktable(1), ZERO );
case 16: // "next" ( table, [index] ) -> next-index, next-value case 18: // "next" ( table, [index] ) -> next-index, next-value
return args.arg1().next(args.arg(2)); return args.arg1().next(args.arg(2));
case 17: // "inext" ( table, [int-index] ) -> next-index, next-value case 19: // "inext" ( table, [int-index] ) -> next-index, next-value
return args.arg1().inext(args.arg(2)); return args.arg1().inext(args.arg(2));
} }
return NONE; return NONE;

View File

@@ -177,7 +177,7 @@ public class PackageLib extends OneArgFunction {
} }
// set the environment of the current function // set the environment of the current function
LuaFunction f = LuaThread.getCallstackFunction(0); LuaFunction f = LuaThread.getCallstackFunction(1);
if ( f == null ) if ( f == null )
error(1, "no calling function"); error(1, "no calling function");
f.setfenv(module); f.setfenv(module);