From d45bd96536875f57d9fefd0f808d549779fabd84 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Thu, 20 Sep 2007 18:17:10 +0000 Subject: [PATCH] Add "require" support. --- .../java/lua/addon/luacompat/LuaCompat.java | 163 ++++++++++++++---- src/main/java/lua/debug/DebugStackState.java | 26 ++- 2 files changed, 139 insertions(+), 50 deletions(-) diff --git a/src/addon/java/lua/addon/luacompat/LuaCompat.java b/src/addon/java/lua/addon/luacompat/LuaCompat.java index ed808808..664b0e81 100644 --- a/src/addon/java/lua/addon/luacompat/LuaCompat.java +++ b/src/addon/java/lua/addon/luacompat/LuaCompat.java @@ -15,6 +15,7 @@ import lua.VM; import lua.io.Closure; import lua.io.LoadState; import lua.io.Proto; +import lua.value.LBoolean; import lua.value.LDouble; import lua.value.LFunction; import lua.value.LInteger; @@ -28,31 +29,36 @@ public class LuaCompat extends LFunction { public static InputStream STDIN = null; public static PrintStream STDOUT = System.out; + public static LTable LOADED = new LTable(); public static void install() { LTable globals = GlobalState.getGlobalsTable(); - for ( int i = 0; i < GLOBAL_NAMES.length; ++i ) { - globals.put( GLOBAL_NAMES[i], new LuaCompat( i ) ); - } - + installNames( globals, GLOBAL_NAMES, GLOBALS_BASE ); + + // math lib LTable math = new LTable(); - for ( int i = 0; i < MATH_NAMES.length; ++i ) { - math.put( MATH_NAMES[i], new LuaCompat( MATH_BASE + i ) ); - } - - // Some handy constants + installNames( math, MATH_NAMES, MATH_BASE ); math.put( "huge", new LDouble( Double.MAX_VALUE ) ); - math.put( "pi", new LDouble( Math.PI ) ); - + math.put( "pi", new LDouble( Math.PI ) ); globals.put( "math", math ); + // string lib LTable string = LString.getMetatable(); - for ( int i = 0; i < STRING_NAMES.length; ++i ) { - string.put( STRING_NAMES[i], new LuaCompat( STRING_BASE + i ) ); - } + installNames( string, STRING_NAMES, STRING_BASE ); globals.put( "string", string ); + + // packages lib + LTable pckg = new LTable(); + installNames( pckg, PACKAGE_NAMES, PACKAGES_BASE ); + globals.put( "package", pckg ); + pckg.put( "loaded", LOADED ); } - + + private static void installNames( LTable table, String[] names, int indexBase ) { + for ( int i=0; i breakpoints = new HashMap(); private boolean exiting = false; private boolean suspended = false; @@ -32,34 +30,30 @@ public class DebugStackState extends StackState implements DebugRequestListener Proto p = call.closure.p; if ( p != null && p.source != null ) source = p.source.toJavaString(); - if ( p.lineinfo != null && p.lineinfo.length > call.pc ) - line = String.valueOf( p.lineinfo[call.pc] ); + if ( p.lineinfo != null && p.lineinfo.length > call.pc-1 ) + line = String.valueOf( p.lineinfo[call.pc-1] ); // TODO: reverse lookup on function name ???? func = call.closure.luaAsString().toJavaString(); } return source+":"+line+"("+func+")"; } - - // override and fill in line number info - public void lua_error(String message) { - super.lua_error( getFileLine(cc)+": "+message ); - } - - private void printLuaTrace() { - System.out.println( "Lua location: "+getFileLine(cc) ); + private void printLuaTrace(String message) { + System.out.println( "Lua error: "+message ); for ( int cindex=cc-1; cindex>=0; cindex-- ) - System.out.println( "\tin "+getFileLine( cindex ) ); + System.out.println( "\tcalled by "+getFileLine( cindex ) ); } // intercept exceptions and fill in line numbers public void exec() { try { super.exec(); - } catch ( Exception t ) { - t.printStackTrace(); - printLuaTrace(); + } catch ( RuntimeException t ) { + String message = getFileLine(cc)+": "+t.getMessage(); + t.printStackTrace(); + printLuaTrace(message); System.out.flush(); + throw new RuntimeException( message, t ); } }