Improve compatibility with vm1 and C-based lua.

This commit is contained in:
James Roseborough
2010-04-25 22:40:04 +00:00
parent 79b0294207
commit 29d6f2ce58
9 changed files with 24 additions and 20 deletions

View File

@@ -107,7 +107,7 @@ public class LuaValue extends Varargs {
public Object touserdata() { return null; } public Object touserdata() { return null; }
public Object touserdata(Class c) { return null; } public Object touserdata(Class c) { return null; }
// object tojstring() maps to tojstring() // Object.toString() maps to tojstring()
public String toString() { return tojstring(); } public String toString() { return tojstring(); }
// type coercion to lua values // type coercion to lua values
@@ -519,9 +519,6 @@ public class LuaValue extends Varargs {
public LuaValue arg1() { public LuaValue arg1() {
return v1; return v1;
} }
public String tojstring() {
return "{"+v1+","+v2+"}";
}
} }

View File

@@ -144,6 +144,9 @@ public abstract class Varargs {
return sb.tojstring(); return sb.tojstring();
} }
// Object.toString() maps to tojstring()
public String toString() { return tojstring(); }
public Varargs subargs(final int start) { public Varargs subargs(final int start) {
int end = narg(); int end = narg();
switch ( end-start ) { switch ( end-start ) {

View File

@@ -22,9 +22,12 @@
package org.luaj.vm2.lib; package org.luaj.vm2.lib;
import org.luaj.vm2.Lua; import org.luaj.vm2.Lua;
import org.luaj.vm2.LuaBoolean;
import org.luaj.vm2.LuaClosure; import org.luaj.vm2.LuaClosure;
import org.luaj.vm2.LuaError; import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaFunction; import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaNil;
import org.luaj.vm2.LuaNumber;
import org.luaj.vm2.LuaString; import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaThread; import org.luaj.vm2.LuaThread;
@@ -534,10 +537,15 @@ public class DebugLib extends OneArgFunction {
private static Varargs _setmetatable(Varargs args) { private static Varargs _setmetatable(Varargs args) {
LuaValue object = args.arg(1); LuaValue object = args.arg(1);
try { try {
if ( ! args.isnoneornil(2) ) LuaValue mt = args.opttable(2, null);
object.setmetatable(args.checktable(2)); switch ( object.type() ) {
else case TNIL: LuaNil.s_metatable = mt; break;
object.setmetatable(null); case TNUMBER: LuaNumber.s_metatable = mt; break;
case TBOOLEAN: LuaBoolean.s_metatable = mt; break;
case TSTRING: LuaString.s_metatable = mt; break;
case TFUNCTION: LuaFunction.s_metatable = mt; break;
default: object.setmetatable( mt );
}
return LuaValue.TRUE; return LuaValue.TRUE;
} catch ( LuaError e ) { } catch ( LuaError e ) {
return varargsOf(FALSE, valueOf(e.toString())); return varargsOf(FALSE, valueOf(e.toString()));

View File

@@ -126,7 +126,7 @@ public class lua {
break; break;
} else if ( "-".equals( args[i] ) ) { } else if ( "-".equals( args[i] ) ) {
setGlobalArg( _G, args, i ); setGlobalArg( _G, args, i );
processScript( System.in, "stdin" ); processScript( System.in, "=stdin" );
break; break;
} else { } else {
switch ( args[i].charAt(1) ) { switch ( args[i].charAt(1) ) {
@@ -202,7 +202,7 @@ public class lua {
String line = reader.readLine(); String line = reader.readLine();
if ( line == null ) if ( line == null )
return; return;
processScript( new ByteArrayInputStream(line.getBytes()), "stdin" ); processScript( new ByteArrayInputStream(line.getBytes()), "=stdin" );
} }
} }
} }

View File

@@ -134,7 +134,7 @@ public class luac {
String chunkname = args[i].substring(0,args[i].length()-4); String chunkname = args[i].substring(0,args[i].length()-4);
processScript( new FileInputStream(args[i]), chunkname, fos ); processScript( new FileInputStream(args[i]), chunkname, fos );
} else if ( args[i].length() <= 1 ) { } else if ( args[i].length() <= 1 ) {
processScript( System.in, "stdin", fos ); processScript( System.in, "=stdin", fos );
} else { } else {
switch ( args[i].charAt(1) ) { switch ( args[i].charAt(1) ) {
case 'o': case 'o':

View File

@@ -60,7 +60,7 @@ public class ScriptDrivenTest extends TestCase {
default: default:
case JSE: case JSE:
case LUAJIT: case LUAJIT:
_G = org.luaj.vm2.lib.JsePlatform.standardGlobals(); _G = org.luaj.vm2.lib.JsePlatform.debugGlobals();
break; break;
case JME: case JME:
_G = org.luaj.vm2.lib.JmePlatform.debugGlobals(); _G = org.luaj.vm2.lib.JmePlatform.debugGlobals();
@@ -115,7 +115,7 @@ public class ScriptDrivenTest extends TestCase {
} }
default: default:
script = new FileInputStream(file); script = new FileInputStream(file);
return LoadState.load(script, "stdin", _G); return LoadState.load(script, "=stdin", _G);
} }
} catch ( Exception e ) { } catch ( Exception e ) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -92,7 +92,7 @@ public class Luajvm1CompatibilityTest extends TestCase {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream( outputStream ); PrintStream printStream = new PrintStream( outputStream );
try { try {
org.luaj.vm2.LuaTable _G = org.luaj.vm2.lib.JsePlatform.standardGlobals(); org.luaj.vm2.LuaTable _G = org.luaj.vm2.lib.JsePlatform.debugGlobals();
LuaThread.getRunning().setfenv(_G); LuaThread.getRunning().setfenv(_G);
_G.get("package").get("loaders").checktable().insert(1, new org.luaj.vm2.lib.OneArgFunction(_G) { _G.get("package").get("loaders").checktable().insert(1, new org.luaj.vm2.lib.OneArgFunction(_G) {
public LuaValue call(LuaValue arg) { public LuaValue call(LuaValue arg) {

View File

@@ -114,8 +114,8 @@ print( 'get='..tostring(s4)..','..tostring(x4==nil)..','..tostring(y4) )
print( 'set='..tostring(s5)..','..tostring(x5==a)..','..tostring(y5) ) print( 'set='..tostring(s5)..','..tostring(x5==a)..','..tostring(y5) )
print( 'get='..tostring(s6)..','..tostring(x6==nil)..','..tostring(y6) ) print( 'get='..tostring(s6)..','..tostring(x6==nil)..','..tostring(y6) )
print( pcall( debug.getmetatable, 1 ) ) print( pcall( debug.getmetatable, 1 ) )
-- print( pcall( debug.setmetatable, 1, {} ) ) print( pcall( debug.setmetatable, 1, {} ) )
-- print( pcall( debug.setmetatable, 1, nil ) ) print( pcall( debug.setmetatable, 1, nil ) )
print( '----- debug.getinfo' ) print( '----- debug.getinfo' )
local printfield = function(tbl, field) local printfield = function(tbl, field)
@@ -126,10 +126,6 @@ local printfield = function(tbl, field)
x = '{'..table.concat(x,',')..'}' x = '{'..table.concat(x,',')..'}'
elseif typ=='function' then elseif typ=='function' then
x = typ x = typ
elseif typ=='string' then
if field == 'source' then
x = x:sub(2)
end
end end
print( ' '..field..': '..tostring(x) ) print( ' '..field..': '..tostring(x) )
end end