diff --git a/.classpath b/.classpath index ade268a2..1009c29a 100644 --- a/.classpath +++ b/.classpath @@ -1,7 +1,9 @@ - - + + + + diff --git a/build.xml b/build.xml index 8d71aeec..d7d1a9ff 100644 --- a/build.xml +++ b/build.xml @@ -15,9 +15,8 @@ - - - + + @@ -53,11 +52,10 @@ - - - - - + + + + @@ -65,10 +63,10 @@ - - - - + + + + @@ -76,9 +74,8 @@ - - - + + diff --git a/src/addon/java/lua/addon/compile/CheckCode.java b/src/addon/java/lua/addon/compile/CheckCode.java deleted file mode 100644 index 9d59a4cf..00000000 --- a/src/addon/java/lua/addon/compile/CheckCode.java +++ /dev/null @@ -1,12 +0,0 @@ -package lua.addon.compile; - -import lua.io.Proto; - -public class CheckCode { - - public static boolean checkcode(Proto f) { - // TODO: port logic from ldebug.c - return true; - } - -} diff --git a/src/addon/java/lua/addon/compile/Compiler.java b/src/core/org/luaj/compiler/Compiler.java similarity index 88% rename from src/addon/java/lua/addon/compile/Compiler.java rename to src/core/org/luaj/compiler/Compiler.java index 920c44e5..1bd21705 100644 --- a/src/addon/java/lua/addon/compile/Compiler.java +++ b/src/core/org/luaj/compiler/Compiler.java @@ -1,13 +1,14 @@ -package lua.addon.compile; +package org.luaj.compiler; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.Hashtable; -import lua.addon.luacompat.Platform; -import lua.io.Proto; -import lua.value.LString; +import org.luaj.vm.LString; +import org.luaj.vm.LPrototype; +import org.luaj.vm.Platform; + public class Compiler { @@ -23,12 +24,12 @@ public class Compiler { * @param stream InputStream to read from. * @param name Name of the chunk * @return null if the first byte indicates it is a binary chunk, - * a Proto instance if it can be compiled, + * a LPrototype instance if it can be compiled, * or an exception is thrown if there is an error. * @throws IOException if an I/O exception occurs * @throws RuntimeException if there is a syntax error. */ - public static Proto compile(InputStream stream, String name) throws IOException { + public static LPrototype compile(InputStream stream, String name) throws IOException { int c = stream.read(); if ( c == LUAC_BINARY_SIG ) @@ -40,7 +41,7 @@ public class Compiler { public int nCcalls; - Proto luaY_parser(int firstByte, Reader z, String name) { + LPrototype luaY_parser(int firstByte, Reader z, String name) { LexState lexstate = new LexState(this, z); FuncState funcstate = new FuncState(); // lexstate.buff = buff; diff --git a/src/addon/java/lua/addon/compile/DumpState.java b/src/core/org/luaj/compiler/DumpState.java similarity index 85% rename from src/addon/java/lua/addon/compile/DumpState.java rename to src/core/org/luaj/compiler/DumpState.java index 5356fe95..3c7cc75d 100644 --- a/src/addon/java/lua/addon/compile/DumpState.java +++ b/src/core/org/luaj/compiler/DumpState.java @@ -1,16 +1,17 @@ -package lua.addon.compile; +package org.luaj.compiler; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; -import lua.Lua; -import lua.io.Proto; -import lua.value.LBoolean; -import lua.value.LNil; -import lua.value.LNumber; -import lua.value.LString; -import lua.value.LValue; +import org.luaj.vm.LBoolean; +import org.luaj.vm.LNil; +import org.luaj.vm.LNumber; +import org.luaj.vm.LString; +import org.luaj.vm.LValue; +import org.luaj.vm.Lua; +import org.luaj.vm.LPrototype; + public class DumpState { @@ -85,14 +86,14 @@ public class DumpState { } } - void dumpCode( final Proto f ) throws IOException { + void dumpCode( final LPrototype f ) throws IOException { int n = f.code.length; dumpInt( n ); for ( int i=0; i= f.k.length) f.k = realloc( f.k, nk*2 + 1 ); f.k[this.nk++] = v; @@ -1010,7 +1011,7 @@ public class FuncState extends LuaC { int code(int instruction, int line) { - Proto f = this.f; + LPrototype f = this.f; this.dischargejpc(); /* `pc' will change */ /* put new instruction in code array */ if (f.code == null || this.pc + 1 > f.code.length) diff --git a/src/addon/java/lua/addon/compile/InstructionPtr.java b/src/core/org/luaj/compiler/InstructionPtr.java similarity index 89% rename from src/addon/java/lua/addon/compile/InstructionPtr.java rename to src/core/org/luaj/compiler/InstructionPtr.java index 23ef70dc..cb2b9900 100644 --- a/src/addon/java/lua/addon/compile/InstructionPtr.java +++ b/src/core/org/luaj/compiler/InstructionPtr.java @@ -1,5 +1,5 @@ -package lua.addon.compile; +package org.luaj.compiler; class InstructionPtr { final int[] code; diff --git a/src/addon/java/lua/addon/compile/IntPtr.java b/src/core/org/luaj/compiler/IntPtr.java similarity index 76% rename from src/addon/java/lua/addon/compile/IntPtr.java rename to src/core/org/luaj/compiler/IntPtr.java index 5157cf9c..4150cfc2 100644 --- a/src/addon/java/lua/addon/compile/IntPtr.java +++ b/src/core/org/luaj/compiler/IntPtr.java @@ -1,4 +1,4 @@ -package lua.addon.compile; +package org.luaj.compiler; public class IntPtr { int i; diff --git a/src/addon/java/lua/addon/compile/LexState.java b/src/core/org/luaj/compiler/LexState.java similarity index 99% rename from src/addon/java/lua/addon/compile/LexState.java rename to src/core/org/luaj/compiler/LexState.java index 47af179b..18d267af 100644 --- a/src/addon/java/lua/addon/compile/LexState.java +++ b/src/core/org/luaj/compiler/LexState.java @@ -1,17 +1,18 @@ -package lua.addon.compile; +package org.luaj.compiler; import java.io.IOException; import java.io.Reader; import java.util.Hashtable; -import lua.Lua; -import lua.addon.compile.FuncState.BlockCnt; -import lua.io.LocVars; -import lua.io.Proto; -import lua.value.LDouble; -import lua.value.LInteger; -import lua.value.LNumber; -import lua.value.LString; +import org.luaj.compiler.FuncState.BlockCnt; +import org.luaj.vm.LDouble; +import org.luaj.vm.LInteger; +import org.luaj.vm.LNumber; +import org.luaj.vm.LString; +import org.luaj.vm.LocVars; +import org.luaj.vm.Lua; +import org.luaj.vm.LPrototype; + public class LexState extends LuaC { @@ -781,7 +782,7 @@ public class LexState extends LuaC { int registerlocalvar(LString varname) { FuncState fs = this.fs; - Proto f = fs.f; + LPrototype f = fs.f; if (f.locvars == null || fs.nlocvars + 1 > f.locvars.length) f.locvars = realloc( f.locvars, fs.nlocvars*2+1 ); f.locvars[fs.nlocvars] = new LocVars(varname,0,0); @@ -860,7 +861,7 @@ public class LexState extends LuaC { void pushclosure(FuncState func, expdesc v) { FuncState fs = this.fs; - Proto f = fs.f; + LPrototype f = fs.f; if (f.p == null || fs.np + 1 > f.p.length) f.p = realloc( f.p, fs.np*2 + 1 ); f.p[fs.np++] = func.f; @@ -874,7 +875,7 @@ public class LexState extends LuaC { void open_func (FuncState fs) { Compiler L = this.L; - Proto f = new Proto(); + LPrototype f = new LPrototype(); if ( this.fs!=null ) f.source = this.fs.f.source; fs.f = f; @@ -898,9 +899,9 @@ public class LexState extends LuaC { } void close_func() { - Compiler L = this.L; + // Compiler L = this.L; FuncState fs = this.fs; - Proto f = fs.f; + LPrototype f = fs.f; this.removevars(0); fs.ret(0, 0); /* final return */ f.code = realloc(f.code, fs.pc); @@ -911,7 +912,7 @@ public class LexState extends LuaC { f.locvars = realloc(f.locvars, fs.nlocvars); // f.sizelocvars = fs.nlocvars; f.upvalues = realloc(f.upvalues, f.nups); - _assert (CheckCode.checkcode(f)); + // _assert (CheckCode.checkcode(f)); _assert (fs.bl == null); this.fs = fs.prev; // L.top -= 2; /* remove table and prototype from the stack */ @@ -1052,7 +1053,7 @@ public class LexState extends LuaC { void parlist () { /* parlist -> [ param { `,' param } ] */ FuncState fs = this.fs; - Proto f = fs.f; + LPrototype f = fs.f; int nparams = 0; f.is_vararg = false; if (this.t.token != ')') { /* is `parlist' not empty? */ diff --git a/src/addon/java/lua/addon/compile/LuaC.java b/src/core/org/luaj/compiler/LuaC.java similarity index 88% rename from src/addon/java/lua/addon/compile/LuaC.java rename to src/core/org/luaj/compiler/LuaC.java index a91fbdf4..60d4fef9 100644 --- a/src/addon/java/lua/addon/compile/LuaC.java +++ b/src/core/org/luaj/compiler/LuaC.java @@ -1,21 +1,24 @@ -package lua.addon.compile; +package org.luaj.compiler; + +import org.luaj.vm.LString; +import org.luaj.vm.LValue; +import org.luaj.vm.LocVars; +import org.luaj.vm.Lua; +import org.luaj.vm.LPrototype; -import lua.Lua; -import lua.io.LocVars; -import lua.io.Proto; -import lua.value.LString; -import lua.value.LValue; /** * Additional constants and utilities required for the compiler, * but not required for the interpreter. * + * * @deprecated - this class will go away. Constants will probably move to LexState */ public class LuaC extends Lua { protected static void _assert(boolean b) { if (!b) throw new RuntimeException("assert failed"); } + public static final int MAXSTACK = 250; static final int LUAI_MAXUPVALUES = 60; static final int LUAI_MAXVARS = 200; static final int LFIELDS_PER_FLUSH = 50; @@ -87,8 +90,8 @@ public class LuaC extends Lua { return a; } - static Proto[] realloc(Proto[] v, int n) { - Proto[] a = new Proto[n]; + static LPrototype[] realloc(LPrototype[] v, int n) { + LPrototype[] a = new LPrototype[n]; if ( v != null ) System.arraycopy(v, 0, a, 0, Math.min(v.length,n)); return a; diff --git a/src/addon/java/lua/addon/luacompat/CoroutinesLib.java b/src/core/org/luaj/lib/CoroutineLib.java similarity index 69% rename from src/addon/java/lua/addon/luacompat/CoroutinesLib.java rename to src/core/org/luaj/lib/CoroutineLib.java index 669bd028..3695623f 100644 --- a/src/addon/java/lua/addon/luacompat/CoroutinesLib.java +++ b/src/core/org/luaj/lib/CoroutineLib.java @@ -1,14 +1,12 @@ -package lua.addon.luacompat; +package org.luaj.lib; -import lua.GlobalState; -import lua.VM; -import lua.io.Closure; -import lua.value.LFunction; -import lua.value.LTable; -import lua.value.LThread; +import org.luaj.vm.LClosure; +import org.luaj.vm.LFunction; +import org.luaj.vm.LTable; +import org.luaj.vm.LThread; +import org.luaj.vm.LuaState; - -public class CoroutinesLib extends LFunction { +public class CoroutineLib extends LFunction { private static final String[] NAMES = { "loadlib", @@ -21,22 +19,22 @@ public class CoroutinesLib extends LFunction { "wrapped" }; - public static void install() { + public static void install( LTable globals ) { LTable lib = new LTable(0,6); for ( int i=1; i<=6; i++ ) - lib.put(NAMES[i], new CoroutinesLib(i)); - GlobalState.getGlobalsTable().put("coroutine",lib); + lib.put(NAMES[i], new CoroutineLib(i)); + globals.put("coroutine",lib); } private final int id; private final LThread thread; - public CoroutinesLib() { + public CoroutineLib() { this.id = 0; this.thread = null; } - private CoroutinesLib( int id ) { + private CoroutineLib( int id ) { this.id = id; this.thread = null; } @@ -45,20 +43,20 @@ public class CoroutinesLib extends LFunction { return "coroutine."+NAMES[id]; } - private CoroutinesLib( int id, LThread thread ) { + private CoroutineLib( int id, LThread thread ) { this.id = id; this.thread = thread; } - public boolean luaStackCall( VM vm ) { + public boolean luaStackCall( LuaState vm ) { switch ( id ) { case 0: { // load lib - install(); + install(vm._G); vm.pushnil(); break; } case 1: { // create - Closure c = (Closure) vm.topointer(2); + LClosure c = (LClosure) vm.topointer(2); vm.pushlvalue( new LThread(c) ); break; } @@ -81,8 +79,8 @@ public class CoroutinesLib extends LFunction { break; } case 5: { // wrap - Closure c = (Closure) vm.topointer(2); - vm.pushlvalue( new CoroutinesLib(7,new LThread(c)) ); + LClosure c = (LClosure) vm.topointer(2); + vm.pushlvalue( new CoroutineLib(7,new LThread(c)) ); break; } case 6: { // yield diff --git a/src/addon/java/lua/addon/luacompat/LBuffer.java b/src/core/org/luaj/lib/LBuffer.java similarity index 94% rename from src/addon/java/lua/addon/luacompat/LBuffer.java rename to src/core/org/luaj/lib/LBuffer.java index 22b3d001..17e8ee85 100644 --- a/src/addon/java/lua/addon/luacompat/LBuffer.java +++ b/src/core/org/luaj/lib/LBuffer.java @@ -1,6 +1,6 @@ -package lua.addon.luacompat; +package org.luaj.lib; -import lua.value.LString; +import org.luaj.vm.LString; public class LBuffer { diff --git a/src/addon/java/lua/addon/luacompat/LuaCompat.java b/src/core/org/luaj/lib/MathLib.java similarity index 88% rename from src/addon/java/lua/addon/luacompat/LuaCompat.java rename to src/core/org/luaj/lib/MathLib.java index a9be8a7c..f35463d1 100644 --- a/src/addon/java/lua/addon/luacompat/LuaCompat.java +++ b/src/core/org/luaj/lib/MathLib.java @@ -1,4 +1,4 @@ -package lua.addon.luacompat; +package org.luaj.lib; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -7,30 +7,29 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; -import lua.CallInfo; -import lua.GlobalState; -import lua.Lua; -import lua.StackState; -import lua.VM; -import lua.io.Closure; -import lua.value.LBoolean; -import lua.value.LDouble; -import lua.value.LFunction; -import lua.value.LInteger; -import lua.value.LNil; -import lua.value.LNumber; -import lua.value.LString; -import lua.value.LTable; -import lua.value.LValue; +import org.luaj.vm.CallInfo; +import org.luaj.vm.LBoolean; +import org.luaj.vm.LClosure; +import org.luaj.vm.LDouble; +import org.luaj.vm.LFunction; +import org.luaj.vm.LInteger; +import org.luaj.vm.LNil; +import org.luaj.vm.LNumber; +import org.luaj.vm.LString; +import org.luaj.vm.LTable; +import org.luaj.vm.LValue; +import org.luaj.vm.Lua; +import org.luaj.vm.LuaState; +import org.luaj.vm.Platform; -public class LuaCompat extends LFunction { + +public class MathLib 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(); + public static void install( LTable globals ) { installNames( globals, GLOBAL_NAMES, GLOBALS_BASE ); // math lib @@ -53,16 +52,16 @@ public class LuaCompat extends LFunction { // table lib LTable table = new LTable(); - installNames( pckg, TABLE_NAMES, TABLES_BASE ); - globals.put( "table", pckg ); + installNames( table, TABLE_NAMES, TABLES_BASE ); + globals.put( "table", table ); // coroutines - CoroutinesLib.install(); + CoroutineLib.install(globals); } private static void installNames( LTable table, String[] names, int indexBase ) { for ( int i=0; i 0 ) ? Math.floor( v ) : Math.ceil( v ); @@ -399,7 +398,7 @@ public class LuaCompat extends LFunction { vm.pushnumber( fracPart ); } - private LValue toNumber( VM vm ) { + private LValue toNumber( LuaState vm ) { LValue input = vm.topointer(2); if ( input instanceof LNumber ) { return input; @@ -413,17 +412,17 @@ public class LuaCompat extends LFunction { return LNil.NIL; } - private void setfenv( StackState state ) { + private void setfenv( LuaState state ) { LValue f = state.topointer(2); LValue newenv = state.topointer(3); - Closure c = null; + LClosure c = null; // Lua reference manual says that first argument, f, can be a "Lua // function" or an integer. Lots of things extend LFunction, but only // instances of Closure are "Lua functions". - if ( f instanceof Closure ) { - c = (Closure) f; + if ( f instanceof LClosure ) { + c = (LClosure) f; } else { int callStackDepth = f.toJavaInt(); if ( callStackDepth > 0 ) { @@ -472,7 +471,7 @@ public class LuaCompat extends LFunction { } // return true if laoded, false if error put onto the stack - private static boolean loadis(VM vm, InputStream is, String chunkname ) { + private static boolean loadis(LuaState vm, InputStream is, String chunkname ) { try { vm.settop(0); if ( 0 != vm.load(is, chunkname) ) { @@ -488,7 +487,7 @@ public class LuaCompat extends LFunction { // return true if loaded, false if error put onto stack - public static boolean loadfile( VM vm, String fileName ) { + public static boolean loadfile( LuaState vm, String fileName ) { InputStream is; String script; @@ -509,7 +508,7 @@ public class LuaCompat extends LFunction { } // if load succeeds, return 0 for success, 1 for error (as per lua spec) - private void dofile( VM vm ) { + private void dofile( LuaState vm ) { String filename = vm.tostring(2); if ( loadfile( vm, filename ) ) { int s = vm.pcall(1, 0, 0); @@ -520,20 +519,20 @@ public class LuaCompat extends LFunction { } // return true if loaded, false if error put onto stack - private boolean loadstring(VM vm, LValue string, String chunkname) { + private boolean loadstring(LuaState vm, LValue string, String chunkname) { return loadis( vm, string.luaAsString().toInputStream(), ("".equals(chunkname)? "(string)": chunkname) ); } // return true if loaded, false if error put onto stack - private boolean load(VM vm, LValue chunkPartLoader, String chunkname) { - if ( ! (chunkPartLoader instanceof Closure) ) { + private boolean load(LuaState vm, LValue chunkPartLoader, String chunkname) { + if ( ! (chunkPartLoader instanceof LClosure) ) { vm.error("not a closure: "+chunkPartLoader); } // load all the parts - Closure c = (Closure) chunkPartLoader; + LClosure c = (LClosure) chunkPartLoader; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { while ( true ) { @@ -562,7 +561,7 @@ public class LuaCompat extends LFunction { } } - private LValue tostring(VM vm, LValue arg) { + private LValue tostring(LuaState vm, LValue arg) { return arg.luaAsString(); } @@ -574,7 +573,7 @@ public class LuaCompat extends LFunction { * except that the above code can be written only for a fixed number of elements. * By default, i is 1 and j is the length of the list, as defined by the length operator (see §2.5.5). */ - private void unpack(VM vm) { + private void unpack(LuaState vm) { LValue v = vm.topointer(2); int i = vm.tointeger(3); int j = vm.tointeger(4); @@ -588,14 +587,14 @@ public class LuaCompat extends LFunction { vm.pushlvalue( list.get(k) ); } - private LValue next(VM vm, LValue table, int index) { + private LValue next(LuaState vm, LValue table, int index) { throw new java.lang.RuntimeException("next() not supported yet"); } // ======================== Module, Package loading ============================= - public static void module( VM vm ) { + public static void module( LuaState vm ) { vm.error( "module not implemented" ); } @@ -625,7 +624,7 @@ public class LuaCompat extends LFunction { * If there is any error loading or running the module, or if it cannot find any loader for * the module, then require signals an error. */ - public static void require( VM vm ) { + public static void require( LuaState vm ) { LString modname = vm.tolstring(2); if ( LOADED.containsKey(modname) ) setResult( vm, LOADED.get(modname) ); @@ -644,11 +643,11 @@ public class LuaCompat extends LFunction { } } - public static void loadlib( VM vm ) { + public static void loadlib( LuaState vm ) { vm.error( "loadlib not implemented" ); } - public static void seeall( VM vm ) { + public static void seeall( LuaState vm ) { vm.error( "seeall not implemented" ); } @@ -660,7 +659,7 @@ public class LuaCompat extends LFunction { * The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table. * If i is greater than j, returns the empty string. */ - private void concat(VM vm) { + private void concat(LuaState vm) { int n = vm.gettop(); LTable table = vm.totable(2); LString sep = (n>=3? vm.tolstring(3): null); @@ -692,7 +691,7 @@ public class LuaCompat extends LFunction { * The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call * table.insert(t,x) inserts x at the end of table t. */ - private void insert(VM vm) { + private void insert(LuaState vm) { int n = vm.gettop(); LTable table = vm.totable(2); int pos = (n>=4? vm.tointeger(3): 0); @@ -706,7 +705,7 @@ public class LuaCompat extends LFunction { * Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical * indices. (To do its job this function does a linear traversal of the whole table.) */ - private void maxn(VM vm) { + private void maxn(LuaState vm) { LTable table = vm.totable(2); vm.settop(0); vm.pushinteger( table.luaMaxN() ); @@ -719,7 +718,7 @@ public class LuaCompat extends LFunction { * Returns the value of the removed element. The default value for pos is n, where n is the length of the table, * so that a call table.remove(t) removes the last element of table t. */ - private void remove(VM vm) { + private void remove(LuaState vm) { int n = vm.gettop(); LTable table = vm.totable(2); int pos = (n>=3? vm.tointeger(3): 0); @@ -736,7 +735,7 @@ public class LuaCompat extends LFunction { * * The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort. */ - private void sort(VM vm) { + private void sort(LuaState vm) { LTable table = vm.totable(2); LValue compare = vm.topointer(3); table.luaSort( vm, compare ); diff --git a/src/addon/java/lua/addon/luacompat/StrLib.java b/src/core/org/luaj/lib/StringLib.java similarity index 96% rename from src/addon/java/lua/addon/luacompat/StrLib.java rename to src/core/org/luaj/lib/StringLib.java index 66c76875..5d451ea6 100644 --- a/src/addon/java/lua/addon/luacompat/StrLib.java +++ b/src/core/org/luaj/lib/StringLib.java @@ -1,13 +1,14 @@ -package lua.addon.luacompat; +package org.luaj.lib; -import lua.VM; -import lua.value.LFunction; -import lua.value.LNumber; -import lua.value.LString; -import lua.value.LTable; -import lua.value.LValue; +import org.luaj.vm.LFunction; +import org.luaj.vm.LNumber; +import org.luaj.vm.LString; +import org.luaj.vm.LTable; +import org.luaj.vm.LValue; +import org.luaj.vm.LuaState; -public class StrLib { + +public class StringLib { /** * string.byte (s [, i [, j]]) * @@ -19,7 +20,7 @@ public class StrLib { * * @param vm the calling vm */ - static void byte_( VM vm ) { + static void byte_( LuaState vm ) { LString ls = vm.tolstring(2); int l = ls.length(); final int top = vm.gettop(); @@ -48,7 +49,7 @@ public class StrLib { * * @param vm the calling VM */ - public static void char_( VM vm) { + public static void char_( LuaState vm) { int nargs = vm.gettop()-1; byte[] bytes = new byte[nargs]; for ( int i=0; i x="lua-5.1.tar.gz" */ - static void gsub( VM vm ) { + static void gsub( LuaState vm ) { LString src = vm.tolstring(2); final int srclen = src.length(); LString p = vm.tolstring(3); @@ -262,7 +263,7 @@ public class StrLib { * Receives a string and returns its length. The empty string "" has length 0. * Embedded zeros are counted, so "a\000bc\000" has length 5. */ - static void len( VM vm ) { + static void len( LuaState vm ) { int l = vm.tostring(2).length(); vm.settop(0); vm.pushinteger( l ); @@ -275,7 +276,7 @@ public class StrLib { * changed to lowercase. All other characters are left unchanged. * The definition of what an uppercase letter is depends on the current locale. */ - static void lower( VM vm ) { + static void lower( LuaState vm ) { String s = vm.tostring(2).toLowerCase(); vm.settop(0); vm.pushstring( s ); @@ -290,7 +291,7 @@ public class StrLib { * A third, optional numerical argument init specifies where to start the * search; its default value is 1 and may be negative. */ - static void match( VM vm ) { + static void match( LuaState vm ) { str_find_aux( vm, false ); } @@ -299,7 +300,7 @@ public class StrLib { * * Returns a string that is the concatenation of n copies of the string s. */ - static void rep( VM vm ) { + static void rep( LuaState vm ) { LString s = vm.tolstring(2); int n = vm.tointeger( 3 ); vm.settop(0); @@ -318,7 +319,7 @@ public class StrLib { * * Returns a string that is the string s reversed. */ - static void reverse( VM vm ) { + static void reverse( LuaState vm ) { LString s = vm.tolstring(2); int n = s.length(); byte[] b = new byte[n]; @@ -339,7 +340,7 @@ public class StrLib { * string.sub(s, -i) * returns a suffix of s with length i. */ - static void sub( VM vm ) { + static void sub( LuaState vm ) { final int top = vm.gettop(); final LString s = vm.tolstring(2); final int len = s.length(); @@ -368,7 +369,7 @@ public class StrLib { * changed to uppercase. All other characters are left unchanged. * The definition of what a lowercase letter is depends on the current locale. */ - static void upper( VM vm ) { + static void upper( LuaState vm ) { String s = vm.tostring(2).toUpperCase(); vm.settop(0); vm.pushstring(s); @@ -377,7 +378,7 @@ public class StrLib { /** * This utility method implements both string.find and string.match. */ - static void str_find_aux( VM vm, boolean find ) { + static void str_find_aux( LuaState vm, boolean find ) { LString s = vm.tolstring(2); LString pat = vm.tolstring(3); int init = vm.gettop() >= 4 ? vm.tointeger( 4 ) : 1; @@ -482,12 +483,12 @@ public class StrLib { private static class MatchState { final LString s; final LString p; - final VM vm; + final LuaState vm; int level; int[] cinit; int[] clen; - MatchState( VM vm, LString s, LString pattern ) { + MatchState( LuaState vm, LString s, LString pattern ) { this.s = s; this.p = pattern; this.vm = vm; diff --git a/src/main/java/lua/Builtin.java b/src/core/org/luaj/vm/BaseLib.java similarity index 85% rename from src/main/java/lua/Builtin.java rename to src/core/org/luaj/vm/BaseLib.java index cb0ecf2d..6e0fc526 100644 --- a/src/main/java/lua/Builtin.java +++ b/src/core/org/luaj/vm/BaseLib.java @@ -1,19 +1,18 @@ /** * */ -package lua; +package org.luaj.vm; import java.io.OutputStream; import java.io.PrintStream; -import lua.value.LTable; -import lua.value.LValue; -final class Builtin extends JavaFunction { - static void addBuiltins(LTable table) { +public class BaseLib extends LFunction { + + static void install(LTable globals) { for ( int i=0; i 0x07FF ) { - count = 3; - b3 = (byte)( 0xE0 | ( c >> 12 ) ); - b2 = (byte)( 0x80 | ( ( c >> 6 ) & 0x03F ) ); - b1 = (byte)( 0x80 | ( ( c ) & 0x03F ) ); - } else if ( c > 0x07F ) { - count = 2; - b2 = (byte)( 0xC0 | ( c >> 6 ) ); - b1 = (byte)( 0x80 | ( c & 0x03F ) ); - } else { - count = 1; - b1 = (byte) c; - } - if ( j + count > bytes.length ) { - bytes = realloc( bytes, ( j + count ) * 2 ); - } - switch ( count ) { - case 3: - bytes[j++] = b3; - case 2: - bytes[j++] = b2; - case 1: - bytes[j++] = b1; - } - } - - if ( j != bytes.length ) { - bytes = realloc( bytes, j ); - } - return bytes; - } - - private static byte[] realloc( byte[] a, int newSize ) { - final byte[] newbytes = new byte[ newSize ]; - System.arraycopy( a, 0, newbytes, 0, Math.min( newSize, a.length ) ); - return newbytes; - } - public int luaByte(int index) { return m_bytes[m_offset + index] & 0x0FF; } diff --git a/src/main/java/lua/value/LTable.java b/src/core/org/luaj/vm/LTable.java similarity index 96% rename from src/main/java/lua/value/LTable.java rename to src/core/org/luaj/vm/LTable.java index f294c12e..4cbc8f70 100644 --- a/src/main/java/lua/value/LTable.java +++ b/src/core/org/luaj/vm/LTable.java @@ -1,7 +1,5 @@ -package lua.value; +package org.luaj.vm; -import lua.Lua; -import lua.VM; /** * Simple implementation of table structure for Lua VM. Maintains both an array @@ -213,7 +211,7 @@ public class LTable extends LValue { return m_hashKeys[ slot ] != null; } - public void luaGetTable(VM vm, LValue table, LValue key) { + public void luaGetTable(LuaState vm, LValue table, LValue key) { LValue v = get(key); if ( v == LNil.NIL && m_metatable != null ) { super.luaGetTable( vm, table, key ); @@ -222,7 +220,7 @@ public class LTable extends LValue { } } - public void luaSetTable(VM vm, LValue table, LValue key, LValue val) { + public void luaSetTable(LuaState vm, LValue table, LValue key, LValue val) { if ( (!containsKey( key )) && m_metatable != null && m_metatable.containsKey(TM_NEWINDEX) ) m_metatable.get(TM_NEWINDEX).luaSetTable( vm, table, key, val ); else @@ -281,7 +279,7 @@ public class LTable extends LValue { } // perform a lua call - public boolean luaStackCall(VM vm) { + public boolean luaStackCall(LuaState vm) { vm.settop(0); int i; while ( ( i = arrayIndex++ ) < m_vector.length ) { @@ -563,11 +561,11 @@ public class LTable extends LValue { // // implemented heap sort from wikipedia // - public void luaSort(VM vm, LValue compare) { + public void luaSort(LuaState vm, LValue compare) { heapSort(m_arrayEntries, vm, compare); } - private void heapSort(int count, VM vm, LValue cmpfunc) { + private void heapSort(int count, LuaState vm, LValue cmpfunc) { heapify(count, vm, cmpfunc); for ( int end=count-1; end>0; ) { swap(end, 0); @@ -575,12 +573,12 @@ public class LTable extends LValue { } } - private void heapify(int count, VM vm, LValue cmpfunc) { + private void heapify(int count, LuaState vm, LValue cmpfunc) { for ( int start=count/2-1; start>=0; --start ) siftDown(start, count - 1, vm, cmpfunc); } - private void siftDown(int start, int end, VM vm, LValue cmpfunc) { + private void siftDown(int start, int end, LuaState vm, LValue cmpfunc) { for ( int root=start; root*2+1 <= end; ) { int child = root*2+1; if (child < end && compare(child, child + 1, vm, cmpfunc)) @@ -593,7 +591,7 @@ public class LTable extends LValue { } } - private boolean compare(int i, int j, VM vm, LValue cmpfunc) { + private boolean compare(int i, int j, LuaState vm, LValue cmpfunc) { if ( cmpfunc != LNil.NIL ) { vm.pushlvalue(cmpfunc); vm.pushlvalue(m_vector[i]); diff --git a/src/main/java/lua/value/LThread.java b/src/core/org/luaj/vm/LThread.java similarity index 89% rename from src/main/java/lua/value/LThread.java rename to src/core/org/luaj/vm/LThread.java index cea853d4..864dfb8a 100644 --- a/src/main/java/lua/value/LThread.java +++ b/src/core/org/luaj/vm/LThread.java @@ -1,9 +1,6 @@ -package lua.value; +package org.luaj.vm; + -import lua.Lua; -import lua.StackState; -import lua.VM; -import lua.io.Closure; /** * Implementation of lua coroutines using Java Threads @@ -22,16 +19,15 @@ public class LThread extends LValue implements Runnable { private int status = STATUS_SUSPENDED; - private StackState threadVm; + private LuaState threadVm; private Thread thread; private static LThread running; - public LThread(Closure c) { - // TODO: inherit globals! - threadVm = new StackState(); - threadVm.pushlvalue(new Closure(c.p, threadVm._G)); + public LThread(LClosure c) { + threadVm = new LuaState(c.env); + threadVm.pushlvalue(new LClosure(c.p, threadVm._G)); } public int luaGetType() { @@ -83,7 +79,7 @@ public class LThread extends LValue implements Runnable { * @param vm * @param nargs */ - public void resumeFrom(VM vm, int nargs) { + public void resumeFrom(LuaState vm, int nargs) { synchronized ( this ) { if ( status == STATUS_DEAD ) { diff --git a/src/main/java/lua/value/LUserData.java b/src/core/org/luaj/vm/LUserData.java similarity index 94% rename from src/main/java/lua/value/LUserData.java rename to src/core/org/luaj/vm/LUserData.java index d8d1651c..ef437b12 100644 --- a/src/main/java/lua/value/LUserData.java +++ b/src/core/org/luaj/vm/LUserData.java @@ -1,6 +1,5 @@ -package lua.value; +package org.luaj.vm; -import lua.Lua; public class LUserData extends LValue { diff --git a/src/main/java/lua/value/LValue.java b/src/core/org/luaj/vm/LValue.java similarity index 96% rename from src/main/java/lua/value/LValue.java rename to src/core/org/luaj/vm/LValue.java index 5a76a829..1b266199 100644 --- a/src/main/java/lua/value/LValue.java +++ b/src/core/org/luaj/vm/LValue.java @@ -1,7 +1,5 @@ -package lua.value; +package org.luaj.vm; -import lua.Lua; -import lua.VM; abstract public class LValue { @@ -32,7 +30,7 @@ public class LValue { // perform a lua call, return true if the call is to a lua function, false // if it ran to completion. - public boolean luaStackCall(VM vm) { + public boolean luaStackCall(LuaState vm) { vm.error("attempt to call "+this); return false; } @@ -91,7 +89,7 @@ public class LValue { * @param the key to set * @param the value to set */ - public void luaSetTable(VM vm, LValue table, LValue key, LValue val) { + public void luaSetTable(LuaState vm, LValue table, LValue key, LValue val) { LTable mt = luaGetMetatable(); if ( mt != null ) { LValue event = mt.get( TM_NEWINDEX ); @@ -108,7 +106,7 @@ public class LValue { * @param table the table from which to get the value * @param key the key to look up */ - public void luaGetTable(VM vm, LValue table, LValue key) { + public void luaGetTable(LuaState vm, LValue table, LValue key) { LTable mt = luaGetMetatable(); if ( mt != null ) { LValue event = mt.get( TM_INDEX ); diff --git a/src/main/java/lua/io/LoadState.java b/src/core/org/luaj/vm/LoadState.java similarity index 88% rename from src/main/java/lua/io/LoadState.java rename to src/core/org/luaj/vm/LoadState.java index 46d42953..ff535042 100644 --- a/src/main/java/lua/io/LoadState.java +++ b/src/core/org/luaj/vm/LoadState.java @@ -1,18 +1,11 @@ -package lua.io; +package org.luaj.vm; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; -import lua.Lua; -import lua.VM; -import lua.value.LBoolean; -import lua.value.LDouble; -import lua.value.LInteger; -import lua.value.LNil; -import lua.value.LNumber; -import lua.value.LString; -import lua.value.LValue; + + /* ** Function Prototypes @@ -55,7 +48,7 @@ public class LoadState { String name; /** The VM doing the loading */ - VM L; + LuaState L; int loadByte() throws IOException { return is.readUnsignedByte(); @@ -94,7 +87,7 @@ public class LoadState { return new LString( bytes, 0, bytes.length - 1 ); } - static LNumber longBitsToLuaNumber( long bits ) { + public static LNumber longBitsToLuaNumber( long bits ) { if ( ( bits & ( ( 1L << 63 ) - 1 ) ) == 0L ) { return LInteger.valueOf( 0 ); } @@ -124,7 +117,7 @@ public class LoadState { } } - public void loadCode( Proto f ) throws IOException { + public void loadCode( LPrototype f ) throws IOException { int n = loadInt(); int[] code = new int[n]; for ( int i=0; iud, is an opaque pointer that Lua simply * passes to the allocator in every call. */ - public StackState() { + public LuaState() { + _G = new LTable(); + _G.put("_G", _G); + BaseLib.install( _G ); + } + + public LuaState(LTable globals) { + _G = globals; } // ================ interfaces for performing calls public void prepStackCall() { - Closure c = (Closure) stack[base]; + LClosure c = (LClosure) stack[base]; int resultbase = base; // Expand the stack if necessary checkstack( c.p.maxstacksize ); @@ -106,7 +101,7 @@ public class StackState extends Lua implements VM { exec(); } - public void doCall( Closure c, LValue[] args ) { + public void doCall( LClosure c, LValue[] args ) { settop(0); pushlvalue( c ); for ( int i=0, n=(args!=null? args.length: 0); i=0? calls[cc].base: 0); } - public void invokeJavaFunction(JavaFunction javaFunction) { + public void invokeJavaFunction(LFunction javaFunction) { int resultbase = base; int resultsneeded = nresults; ++base; int nactual = javaFunction.invoke(this); debugAssert(nactual>=0); debugAssert(top-nactual>=base); - base = resultbase; - System.arraycopy(stack, top-nactual, stack, base, nactual); + System.arraycopy(stack, top-nactual, stack, base=resultbase, nactual); settop( nactual ); if ( resultsneeded >= 0 ) settop( resultsneeded ); @@ -201,8 +195,8 @@ public class StackState extends Lua implements VM { // loader public int load( InputStream is, String chunkname ) { try { - Proto p = LoadState.undump(this, is, chunkname ); - pushlvalue( new Closure( p, _G ) ); + LPrototype p = LoadState.undump(this, is, chunkname ); + pushlvalue( new LClosure( p, _G ) ); return 0; } catch ( Throwable t ) { pushstring( t.getMessage() ); @@ -212,8 +206,8 @@ public class StackState extends Lua implements VM { // ================ execute instructions private LValue RKBC(LValue[] k, int bc) { - return StackState.ISK(bc) ? - k[StackState.INDEXK(bc)]: + return LuaState.ISK(bc) ? + k[LuaState.INDEXK(bc)]: stack[base + bc]; } @@ -240,14 +234,14 @@ public class StackState extends Lua implements VM { LValue rkb, rkc, nvarargs, key, val; LValue i0, step, idx, limit, init, table; boolean back, body; - Proto proto; - Closure newClosure; + LPrototype proto; + LClosure newClosure; // reload values from the current call frame // into local variables CallInfo ci = calls[cc]; - Closure cl = ci.closure; - Proto p = cl.p; + LClosure cl = ci.closure; + LPrototype p = cl.p; int[] code = p.code; LValue[] k = p.k; @@ -261,9 +255,6 @@ public class StackState extends Lua implements VM { // sync up top ci.top = top; - if (TRACE) - Print.printState(this, base, top, base+p.maxstacksize, cl, ci.pc); - // allow debug hooks a chance to operate debugHooks( ci.pc ); @@ -271,81 +262,81 @@ public class StackState extends Lua implements VM { i = code[ci.pc++]; // get first opcode arg - a = StackState.GETARG_A(i); - switch (StackState.GET_OPCODE(i)) { - case StackState.OP_MOVE: { - b = StackState.GETARG_B(i); + a = LuaState.GETARG_A(i); + switch (LuaState.GET_OPCODE(i)) { + case LuaState.OP_MOVE: { + b = LuaState.GETARG_B(i); this.stack[base + a] = this.stack[base + b]; continue; } - case StackState.OP_LOADK: { - b = StackState.GETARG_Bx(i); + case LuaState.OP_LOADK: { + b = LuaState.GETARG_Bx(i); this.stack[base + a] = k[b]; continue; } - case StackState.OP_LOADBOOL: { - b = StackState.GETARG_B(i); - c = StackState.GETARG_C(i); + case LuaState.OP_LOADBOOL: { + b = LuaState.GETARG_B(i); + c = LuaState.GETARG_C(i); this.stack[base + a] = (b != 0 ? LBoolean.TRUE : LBoolean.FALSE); if (c != 0) ci.pc++; /* skip next instruction (if C) */ continue; } - case StackState.OP_LOADNIL: { - b = StackState.GETARG_B(i); + case LuaState.OP_LOADNIL: { + b = LuaState.GETARG_B(i); do { this.stack[base + b] = LNil.NIL; } while ((--b) >= a); continue; } - case StackState.OP_GETUPVAL: { - b = StackState.GETARG_B(i); + case LuaState.OP_GETUPVAL: { + b = LuaState.GETARG_B(i); this.stack[base + a] = cl.upVals[b].getValue(); continue; } - case StackState.OP_GETGLOBAL: { - b = StackState.GETARG_Bx(i); + case LuaState.OP_GETGLOBAL: { + b = LuaState.GETARG_Bx(i); key = k[b]; table = cl.env; top = base + a; table.luaGetTable(this, table, key); continue; } - case StackState.OP_GETTABLE: { - b = StackState.GETARG_B(i); + case LuaState.OP_GETTABLE: { + b = LuaState.GETARG_B(i); key = GETARG_RKC(k, i); table = this.stack[base + b]; top = base + a; table.luaGetTable(this, table, key); continue; } - case StackState.OP_SETGLOBAL: { - b = StackState.GETARG_Bx(i); + case LuaState.OP_SETGLOBAL: { + b = LuaState.GETARG_Bx(i); key = k[b]; val = this.stack[base + a]; table = cl.env; table.luaSetTable(this, table, key, val); continue; } - case StackState.OP_SETUPVAL: { - b = StackState.GETARG_B(i); + case LuaState.OP_SETUPVAL: { + b = LuaState.GETARG_B(i); cl.upVals[b].setValue( this.stack[base + a] ); continue; } - case StackState.OP_SETTABLE: { + case LuaState.OP_SETTABLE: { key = GETARG_RKB(k, i); val = GETARG_RKC(k, i); table = this.stack[base + a]; table.luaSetTable(this, table, key, val); continue; } - case StackState.OP_NEWTABLE: { - b = StackState.GETARG_B(i); - c = StackState.GETARG_C(i); + case LuaState.OP_NEWTABLE: { + b = LuaState.GETARG_B(i); + c = LuaState.GETARG_C(i); this.stack[base + a] = new LTable(b, c); continue; } - case StackState.OP_SELF: { + case LuaState.OP_SELF: { rkb = GETARG_RKB(k, i); rkc = GETARG_RKC(k, i); top = base + a; @@ -356,37 +347,37 @@ public class StackState extends Lua implements VM { // Protect(luaV_gettable(L, rb, RKC(i), ra)); continue; } - case StackState.OP_ADD: - case StackState.OP_SUB: - case StackState.OP_MUL: - case StackState.OP_DIV: - case StackState.OP_MOD: - case StackState.OP_POW: { - o = StackState.GET_OPCODE(i); + case LuaState.OP_ADD: + case LuaState.OP_SUB: + case LuaState.OP_MUL: + case LuaState.OP_DIV: + case LuaState.OP_MOD: + case LuaState.OP_POW: { + o = LuaState.GET_OPCODE(i); rkb = GETARG_RKB(k, i); rkc = GETARG_RKC(k, i); this.stack[base + a] = rkc.luaBinOpUnknown(o, rkb); continue; } - case StackState.OP_UNM: { + case LuaState.OP_UNM: { rkb = GETARG_RKB(k, i); this.stack[base + a] = rkb.luaUnaryMinus(); continue; } - case StackState.OP_NOT: { + case LuaState.OP_NOT: { rkb = GETARG_RKB(k, i); this.stack[base + a] = (!rkb.toJavaBoolean() ? LBoolean.TRUE : LBoolean.FALSE); continue; } - case StackState.OP_LEN: { + case LuaState.OP_LEN: { rkb = GETARG_RKB(k, i); this.stack[base + a] = LInteger.valueOf( rkb.luaLength() ); continue; } - case StackState.OP_CONCAT: { - b = StackState.GETARG_B(i); - c = StackState.GETARG_C(i); + case LuaState.OP_CONCAT: { + b = LuaState.GETARG_B(i); + c = LuaState.GETARG_C(i); int numValues = c - b + 1; LString[] strings = new LString[numValues]; @@ -397,14 +388,14 @@ public class StackState extends Lua implements VM { this.stack[base + a] = LString.concat( strings ); continue; } - case StackState.OP_JMP: { - ci.pc += StackState.GETARG_sBx(i); + case LuaState.OP_JMP: { + ci.pc += LuaState.GETARG_sBx(i); continue; } - case StackState.OP_EQ: - case StackState.OP_LT: - case StackState.OP_LE: { - o = StackState.GET_OPCODE(i); + case LuaState.OP_EQ: + case LuaState.OP_LT: + case LuaState.OP_LE: { + o = LuaState.GET_OPCODE(i); rkb = GETARG_RKB(k, i); rkc = GETARG_RKC(k, i); boolean test = rkc.luaBinCmpUnknown(o, rkb); @@ -412,33 +403,33 @@ public class StackState extends Lua implements VM { ci.pc++; continue; } - case StackState.OP_TEST: { - c = StackState.GETARG_C(i); + case LuaState.OP_TEST: { + c = LuaState.GETARG_C(i); if (this.stack[base + a].toJavaBoolean() != (c != 0)) ci.pc++; continue; } - case StackState.OP_TESTSET: { + case LuaState.OP_TESTSET: { rkb = GETARG_RKB(k, i); - c = StackState.GETARG_C(i); + c = LuaState.GETARG_C(i); if (rkb.toJavaBoolean() != (c != 0)) ci.pc++; else this.stack[base + a] = rkb; continue; } - case StackState.OP_CALL: { + case LuaState.OP_CALL: { // ra is base of new call frame this.base += a; // number of args - b = StackState.GETARG_B(i); + b = LuaState.GETARG_B(i); if (b != 0) // else use previous instruction set top top = base + b; // number of return values we need - c = StackState.GETARG_C(i); + c = LuaState.GETARG_C(i); // make or set up the call this.nresults = c - 1; @@ -457,13 +448,13 @@ public class StackState extends Lua implements VM { continue; } - case StackState.OP_TAILCALL: { + case LuaState.OP_TAILCALL: { close(base); // copy down the frame before calling! // number of args (including the function) - b = StackState.GETARG_B(i); + b = LuaState.GETARG_B(i); if (b == 0) b = top - (base + a); @@ -497,9 +488,9 @@ public class StackState extends Lua implements VM { return; } - case StackState.OP_RETURN: { + case LuaState.OP_RETURN: { // number of return vals to return - b = StackState.GETARG_B(i) - 1; + b = LuaState.GETARG_B(i) - 1; if (b == -1) b = top - (base + a); @@ -520,7 +511,7 @@ public class StackState extends Lua implements VM { // force a reload of the calling context return; } - case StackState.OP_FORLOOP: { + case LuaState.OP_FORLOOP: { i0 = this.stack[base + a]; step = this.stack[base + a + 2]; idx = step.luaBinOpUnknown(Lua.OP_ADD, i0); @@ -532,20 +523,20 @@ public class StackState extends Lua implements VM { this.stack[base + a] = idx; this.stack[base + a + 3] = idx; top = base + a + 3 + 1; - ci.pc += StackState.GETARG_sBx(i); + ci.pc += LuaState.GETARG_sBx(i); } continue; } - case StackState.OP_FORPREP: { + case LuaState.OP_FORPREP: { init = this.stack[base + a]; step = this.stack[base + a + 2]; this.stack[base + a] = step.luaBinOpUnknown(Lua.OP_SUB, init); - b = StackState.GETARG_sBx(i); + b = LuaState.GETARG_sBx(i); ci.pc += b; continue; } - case StackState.OP_TFORLOOP: { + case LuaState.OP_TFORLOOP: { cb = a + 3; /* call base */ System.arraycopy(this.stack, base + a, this.stack, base + cb, 3); @@ -554,7 +545,7 @@ public class StackState extends Lua implements VM { top = base + 3; /* func. + 2 args (state and index) */ // call the iterator - c = StackState.GETARG_C(i); + c = LuaState.GETARG_C(i); if (this.stack[base].luaStackCall(this)) execute(); adjustTop( base + c - 1 ); @@ -570,9 +561,9 @@ public class StackState extends Lua implements VM { } continue; } - case StackState.OP_SETLIST: { - b = StackState.GETARG_B(i); - c = StackState.GETARG_C(i); + case LuaState.OP_SETLIST: { + b = LuaState.GETARG_B(i); + c = LuaState.GETARG_C(i); int listBase = base + a; if (b == 0) { b = top - listBase - 1; @@ -589,21 +580,21 @@ public class StackState extends Lua implements VM { top = base + a - 1; continue; } - case StackState.OP_CLOSE: { + case LuaState.OP_CLOSE: { close( a ); // close upvals higher in the stack than position a continue; } - case StackState.OP_CLOSURE: { - b = StackState.GETARG_Bx(i); + case LuaState.OP_CLOSURE: { + b = LuaState.GETARG_Bx(i); proto = cl.p.p[b]; - newClosure = new Closure(proto, _G); + newClosure = new LClosure(proto, _G); for (int j = 0; j < newClosure.upVals.length; j++, ci.pc++) { i = code[ci.pc]; - o = StackState.GET_OPCODE(i); - b = StackState.GETARG_B(i); - if (o == StackState.OP_GETUPVAL) { + o = LuaState.GET_OPCODE(i); + b = LuaState.GETARG_B(i); + if (o == LuaState.OP_GETUPVAL) { newClosure.upVals[j] = cl.upVals[b]; - } else if (o == StackState.OP_MOVE) { + } else if (o == LuaState.OP_MOVE) { newClosure.upVals[j] = findUpVal( proto.upvalues[j], base + b ); } else { throw new java.lang.IllegalArgumentException( @@ -613,12 +604,12 @@ public class StackState extends Lua implements VM { this.stack[base + a] = newClosure; continue; } - case StackState.OP_VARARG: { + case LuaState.OP_VARARG: { // figure out how many args to copy - b = StackState.GETARG_B(i) - 1; + b = LuaState.GETARG_B(i) - 1; nvarargs = this.stack[base - 1]; n = nvarargs.toJavaInt(); - if (b == StackState.LUA_MULTRET) { + if (b == LuaState.LUA_MULTRET) { b = n; // use entire varargs supplied } @@ -671,8 +662,8 @@ public class StackState extends Lua implements VM { throw new java.lang.RuntimeException("AbstractStack: not yet implemented"); } - public JavaFunction atpanic(JavaFunction panicf) { - JavaFunction f = panic; + public LFunction atpanic(LFunction panicf) { + LFunction f = panic; panic = panicf; return f; } @@ -695,11 +686,11 @@ public class StackState extends Lua implements VM { notImplemented(); } - public void pushclosure(JavaFunction fn, int n) { + public void pushclosure(LFunction fn, int n) { notImplemented(); } - public int javapcall(JavaFunction func, Object ud) { + public int javapcall(LFunction func, Object ud) { this.pushjavafunction(func); this.pushlightuserdata(ud); return this.pcall(1, 0, 0); @@ -811,7 +802,7 @@ public class StackState extends Lua implements VM { } public boolean isjavafunction(int index) { - return topointer(index) instanceof JavaFunction; + return topointer(index) instanceof LFunction; } public boolean islightuserdata(int index) { @@ -903,7 +894,7 @@ public class StackState extends Lua implements VM { pushlvalue( LInteger.valueOf(n) ); } - public void pushjavafunction(JavaFunction f) { + public void pushjavafunction(LFunction f) { pushlvalue( f ); } @@ -968,7 +959,7 @@ public class StackState extends Lua implements VM { t.put(n,v); } - public void register(String name, JavaFunction f) { + public void register(String name, LFunction f) { pushjavafunction(f); setglobal(name); } @@ -1017,7 +1008,7 @@ public class StackState extends Lua implements VM { notImplemented(); } - public StackState tothread(int index) { + public LuaState tothread(int index) { notImplemented(); return null; } @@ -1030,8 +1021,8 @@ public class StackState extends Lua implements VM { return topointer(index).toJavaInt(); } - public JavaFunction tojavafunction(int index) { - return (JavaFunction) topointer(index); + public LFunction tojavafunction(int index) { + return (LFunction) topointer(index); } public LString tolstring(int index) { @@ -1094,10 +1085,10 @@ public class StackState extends Lua implements VM { return topointer(index).luaGetTypeName().toJavaString(); } - public void xmove(VM to, int n) { + public void xmove(LuaState to, int n) { if ( n > 0 ) { to.checkstack(n); - StackState ss = (StackState)to; + LuaState ss = (LuaState)to; System.arraycopy(stack, top-n, ss.stack, ss.top, n); ss.top += n; } diff --git a/src/addon/java/lua/addon/luacompat/Platform.java b/src/core/org/luaj/vm/Platform.java similarity index 93% rename from src/addon/java/lua/addon/luacompat/Platform.java rename to src/core/org/luaj/vm/Platform.java index 4aa89ceb..bc99945d 100644 --- a/src/addon/java/lua/addon/luacompat/Platform.java +++ b/src/core/org/luaj/vm/Platform.java @@ -1,4 +1,4 @@ -package lua.addon.luacompat; +package org.luaj.vm; import java.io.InputStream; import java.io.InputStreamReader; @@ -7,6 +7,7 @@ import java.io.Reader; /** * Singleton to manage platform-specific behaviors. * + * @deprecated - will probably be replaced with Config, LuaConfig or something similar. */ abstract public class Platform { private static Platform instance; diff --git a/src/main/java/lua/io/UpVal.java b/src/core/org/luaj/vm/UpVal.java similarity index 90% rename from src/main/java/lua/io/UpVal.java rename to src/core/org/luaj/vm/UpVal.java index 87f0c201..56b4ddbc 100644 --- a/src/main/java/lua/io/UpVal.java +++ b/src/core/org/luaj/vm/UpVal.java @@ -1,7 +1,5 @@ -package lua.io; +package org.luaj.vm; -import lua.value.LString; -import lua.value.LValue; public class UpVal { diff --git a/src/main/java/lua/debug/AbortException.java b/src/debug/org/luaj/debug/AbortException.java similarity index 98% rename from src/main/java/lua/debug/AbortException.java rename to src/debug/org/luaj/debug/AbortException.java index 74c0ba46..2e26ded2 100644 --- a/src/main/java/lua/debug/AbortException.java +++ b/src/debug/org/luaj/debug/AbortException.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; public class AbortException extends RuntimeException { private static final long serialVersionUID = 8043724992294286647L; diff --git a/src/main/java/lua/debug/DebugStackState.java b/src/debug/org/luaj/debug/DebugStackState.java similarity index 88% rename from src/main/java/lua/debug/DebugStackState.java rename to src/debug/org/luaj/debug/DebugStackState.java index 1ce96a3a..f9c9f310 100644 --- a/src/main/java/lua/debug/DebugStackState.java +++ b/src/debug/org/luaj/debug/DebugStackState.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; import java.io.IOException; import java.util.Hashtable; @@ -27,29 +27,32 @@ import java.util.Timer; import java.util.TimerTask; import java.util.Vector; -import lua.CallInfo; -import lua.Lua; -import lua.StackState; -import lua.addon.compile.LexState; -import lua.debug.event.DebugEvent; -import lua.debug.event.DebugEventBreakpoint; -import lua.debug.event.DebugEventError; -import lua.debug.event.DebugEventType; -import lua.debug.request.DebugRequest; -import lua.debug.request.DebugRequestLineBreakpointToggle; -import lua.debug.request.DebugRequestListener; -import lua.debug.request.DebugRequestStack; -import lua.debug.request.DebugRequestType; -import lua.debug.response.DebugResponse; -import lua.debug.response.DebugResponseCallgraph; -import lua.debug.response.DebugResponseSimple; -import lua.debug.response.DebugResponseVariables; -import lua.io.LocVars; -import lua.io.Proto; -import lua.value.LTable; -import lua.value.LValue; +import org.luaj.compiler.LexState; +import org.luaj.debug.event.DebugEvent; +import org.luaj.debug.event.DebugEventBreakpoint; +import org.luaj.debug.event.DebugEventError; +import org.luaj.debug.event.DebugEventType; +import org.luaj.debug.request.DebugRequest; +import org.luaj.debug.request.DebugRequestLineBreakpointToggle; +import org.luaj.debug.request.DebugRequestListener; +import org.luaj.debug.request.DebugRequestStack; +import org.luaj.debug.request.DebugRequestType; +import org.luaj.debug.response.DebugResponse; +import org.luaj.debug.response.DebugResponseCallgraph; +import org.luaj.debug.response.DebugResponseSimple; +import org.luaj.debug.response.DebugResponseStack; +import org.luaj.vm.CallInfo; +import org.luaj.vm.LClosure; +import org.luaj.vm.LTable; +import org.luaj.vm.LValue; +import org.luaj.vm.LocVars; +import org.luaj.vm.Lua; +import org.luaj.vm.LPrototype; +import org.luaj.vm.LuaState; -public class DebugStackState extends StackState implements DebugRequestListener { + +public class DebugStackState extends LuaState implements DebugRequestListener { + private static final boolean TRACE = (null != System.getProperty("TRACE")); // stepping constants and stepping state protected static final int STEP_NONE = 0; @@ -97,7 +100,7 @@ public class DebugStackState extends StackState implements DebugRequestListener String source = "?"; if (cindex >= 0) { CallInfo call = this.calls[cindex]; - Proto p = call.closure.p; + LPrototype p = call.closure.p; if (p != null && p.source != null) source = p.source.toJavaString(); int pc = getCurrentPc(call); @@ -149,6 +152,13 @@ public class DebugStackState extends StackState implements DebugRequestListener // debug hooks public void debugHooks(int pc) { + if (TRACE) { + CallInfo ci = calls[cc]; + LClosure cl = ci.closure; + LPrototype p = cl.p; + Print.printState(this, base, top, base+p.maxstacksize, cl, pc); + } + if (exiting) { throw new AbortException("aborted by debug client"); } @@ -171,7 +181,7 @@ public class DebugStackState extends StackState implements DebugRequestListener } CallInfo currentCallInfo = calls[cc]; - Proto currentProto = currentCallInfo.closure.p; + LPrototype currentProto = currentCallInfo.closure.p; // if we are not stepping, we would keep going if the line doesn't // change @@ -185,8 +195,8 @@ public class DebugStackState extends StackState implements DebugRequestListener DebugUtils.println("debugHook - executing line: " + line); int i = currentProto.code[pc]; - int opCode = StackState.GET_OPCODE(i); - if (isStepping() && opCode == StackState.OP_RETURN && cc == 0) { + int opCode = LuaState.GET_OPCODE(i); + if (isStepping() && opCode == LuaState.OP_RETURN && cc == 0) { cancelStepping(); } else if (shouldPauseForStepping) { shouldPauseForStepping = false; @@ -194,7 +204,7 @@ public class DebugStackState extends StackState implements DebugRequestListener } else if (stepping == STEP_INTO) { if (lastline != line) { suspendOnStepping(); - } else if (opCode == StackState.OP_CALL) { + } else if (opCode == LuaState.OP_CALL) { shouldPauseForStepping = true; } } else if (stepping == STEP_OVER) { @@ -203,8 +213,8 @@ public class DebugStackState extends StackState implements DebugRequestListener suspendOnStepping(); } } else if (stepping == STEP_RETURN) { - if ((opCode == StackState.OP_RETURN && cc == this.steppingFrame) || - (opCode == StackState.OP_TAILCALL && cc == this.steppingFrame)) { + if ((opCode == LuaState.OP_RETURN && cc == this.steppingFrame) || + (opCode == LuaState.OP_TAILCALL && cc == this.steppingFrame)) { shouldPauseForStepping = true; } } @@ -328,9 +338,7 @@ public class DebugStackState extends StackState implements DebugRequestListener } else if (DebugRequestType.stack == requestType) { DebugRequestStack stackRequest = (DebugRequestStack) request; int index = stackRequest.getIndex(); - return new DebugResponseVariables(getStack(index)); - } else if (DebugRequestType.global == requestType) { - return new DebugResponseVariables(getGlobals()); + return new DebugResponseStack(getStack(index)); } else if (DebugRequestType.stepInto == requestType) { DebugEvent event = new DebugEvent( DebugEventType.resumedOnSteppingInto); @@ -477,45 +485,8 @@ public class DebugStackState extends StackState implements DebugRequestListener Vector variables = new Vector(); Hashtable variablesSeen = new Hashtable(); - Proto p = calls[index].closure.p; - for (int i = index; i >= 0; i--) { - if (i == index || isInScope(p, calls[i])) { - addVariables(variables, variablesSeen, i); - } - } - Variable[] result = new Variable[variables.size()]; - for (int i = 0; i < variables.size(); i++) { - result[i] = (Variable) variables.elementAt(i); - } + addVariables(variables, variablesSeen, index); - return result; - } - - protected boolean isInScope(Proto p, CallInfo ci) { - Proto[] enclosingProtos = ci.closure.p.p; - boolean bFound = false; - for (int i = 0; enclosingProtos!= null && i < enclosingProtos.length; i++) { - if (enclosingProtos[i] == p) { - bFound = true; - break; - } - } - - return bFound; - } - - /** - * Returns the visible globals to the current VM. - * @return the visible globals. - */ - public Variable[] getGlobals() { - Vector variables = new Vector(); - variables.addElement( - new TableVariable(0, - "*Globals*", - Lua.LUA_TTABLE, - (LTable) _G)); - Variable[] result = new Variable[variables.size()]; for (int i = 0; i < variables.size(); i++) { result[i] = (Variable) variables.elementAt(i); @@ -561,7 +532,7 @@ public class DebugStackState extends StackState implements DebugRequestListener private void addVariables(Vector variables, Hashtable variablesSeen, int index) { CallInfo callInfo = calls[index]; - Proto prototype = callInfo.closure.p; + LPrototype prototype = callInfo.closure.p; int base = callInfo.base; int top = callInfo.top < callInfo.base ? callInfo.base+1 : callInfo.top; diff --git a/src/main/java/lua/debug/DebugSupport.java b/src/debug/org/luaj/debug/DebugSupport.java similarity index 96% rename from src/main/java/lua/debug/DebugSupport.java rename to src/debug/org/luaj/debug/DebugSupport.java index 3af0c601..a7b30570 100644 --- a/src/main/java/lua/debug/DebugSupport.java +++ b/src/debug/org/luaj/debug/DebugSupport.java @@ -1,15 +1,16 @@ -package lua.debug; +package org.luaj.debug; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; -import lua.debug.event.DebugEvent; -import lua.debug.event.DebugEventListener; -import lua.debug.request.DebugRequest; -import lua.debug.request.DebugRequestListener; -import lua.debug.response.DebugResponse; +import org.luaj.debug.event.DebugEvent; +import org.luaj.debug.event.DebugEventListener; +import org.luaj.debug.request.DebugRequest; +import org.luaj.debug.request.DebugRequestListener; +import org.luaj.debug.response.DebugResponse; + public class DebugSupport implements DebugRequestListener, DebugEventListener { protected static final int UNKNOWN = 0; diff --git a/src/main/java/lua/debug/DebugUtils.java b/src/debug/org/luaj/debug/DebugUtils.java similarity index 96% rename from src/main/java/lua/debug/DebugUtils.java rename to src/debug/org/luaj/debug/DebugUtils.java index 65cf2fa4..bacce020 100644 --- a/src/main/java/lua/debug/DebugUtils.java +++ b/src/debug/org/luaj/debug/DebugUtils.java @@ -19,10 +19,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; + +import org.luaj.vm.LString; +import org.luaj.vm.LoadState; -import lua.io.LoadState; -import lua.value.LString; public class DebugUtils { public static final boolean IS_DEBUG = false; diff --git a/src/main/java/lua/debug/EnumType.java b/src/debug/org/luaj/debug/EnumType.java similarity index 98% rename from src/main/java/lua/debug/EnumType.java rename to src/debug/org/luaj/debug/EnumType.java index 08a50afe..ec6f2f5c 100644 --- a/src/main/java/lua/debug/EnumType.java +++ b/src/debug/org/luaj/debug/EnumType.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; import java.io.DataOutputStream; import java.io.IOException; diff --git a/src/main/java/lua/debug/NullableString.java b/src/debug/org/luaj/debug/NullableString.java similarity index 98% rename from src/main/java/lua/debug/NullableString.java rename to src/debug/org/luaj/debug/NullableString.java index ca3e274b..81c4778d 100644 --- a/src/main/java/lua/debug/NullableString.java +++ b/src/debug/org/luaj/debug/NullableString.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/lua/Print.java b/src/debug/org/luaj/debug/Print.java similarity index 81% rename from src/main/java/lua/Print.java rename to src/debug/org/luaj/debug/Print.java index 5f95b13e..a56135a0 100644 --- a/src/main/java/lua/Print.java +++ b/src/debug/org/luaj/debug/Print.java @@ -1,21 +1,70 @@ -package lua; +package org.luaj.debug; import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import lua.io.Closure; -import lua.io.Proto; -import lua.value.LDouble; -import lua.value.LFunction; -import lua.value.LInteger; -import lua.value.LString; -import lua.value.LValue; +import org.luaj.vm.LClosure; +import org.luaj.vm.LDouble; +import org.luaj.vm.LFunction; +import org.luaj.vm.LInteger; +import org.luaj.vm.LPrototype; +import org.luaj.vm.LString; +import org.luaj.vm.LValue; +import org.luaj.vm.Lua; +import org.luaj.vm.LuaState; + + + public class Print extends Lua { + /** opcode names */ private static final String STRING_FOR_NULL = "null"; public static PrintStream ps = System.out; + private static final String[] luaP_opnames = { + "MOVE", + "LOADK", + "LOADBOOL", + "LOADNIL", + "GETUPVAL", + "GETGLOBAL", + "GETTABLE", + "SETGLOBAL", + "SETUPVAL", + "SETTABLE", + "NEWTABLE", + "SELF", + "ADD", + "SUB", + "MUL", + "DIV", + "MOD", + "POW", + "UNM", + "NOT", + "LEN", + "CONCAT", + "JMP", + "EQ", + "LT", + "LE", + "TEST", + "TESTSET", + "CALL", + "TAILCALL", + "RETURN", + "FORLOOP", + "FORPREP", + "TFORLOOP", + "SETLIST", + "CLOSE", + "CLOSURE", + "VARARG", + null, + }; + + static void printString(String s) { char[] chars = s.toCharArray(); ps.print('"'); @@ -78,11 +127,11 @@ public class Print extends Lua { } } - static void printConstant(Proto f, int i) { + static void printConstant(LPrototype f, int i) { printValue( f.k[i] ); } - public static void printCode(Proto f) { + public static void printCode(LPrototype f) { int[] code = f.code; int pc, n = code.length; for (pc = 0; pc < n; pc++) { @@ -91,7 +140,7 @@ public class Print extends Lua { } } - static void printOpCode(Proto f, int pc) { + static void printOpCode(LPrototype f, int pc) { int[] code = f.code; int i = code[pc]; int o = GET_OPCODE(i); @@ -195,11 +244,11 @@ public class Print extends Lua { } } - private static int getline(Proto f, int pc) { + private static int getline(LPrototype f, int pc) { return f.lineinfo[pc]; } - static void printHeader(Proto f) { + static void printHeader(LPrototype f) { String s = String.valueOf(f.source); if (s.startsWith("@") || s.startsWith("=")) s = s.substring(1); @@ -217,7 +266,7 @@ public class Print extends Lua { + " constant, " + f.p.length + " function\n"); } - static void printConstants(Proto f) { + static void printConstants(LPrototype f) { int i, n = f.k.length; ps.print("constants (" + n + ") for " + id(f) + ":\n"); for (i = 0; i < n; i++) { @@ -227,7 +276,7 @@ public class Print extends Lua { } } - static void printLocals(Proto f) { + static void printLocals(LPrototype f) { int i, n = f.locvars.length; ps.print("locals (" + n + ") for " + id(f) + ":\n"); for (i = 0; i < n; i++) { @@ -235,7 +284,7 @@ public class Print extends Lua { } } - static void printUpValues(Proto f) { + static void printUpValues(LPrototype f) { int i, n = f.upvalues.length; ps.print("upvalues (" + n + ") for " + id(f) + ":\n"); for (i = 0; i < n; i++) { @@ -243,7 +292,7 @@ public class Print extends Lua { } } - public void printFunction(Proto f, boolean full) { + public void printFunction(LPrototype f, boolean full) { int i, n = f.p.length; printHeader(f); printCode(f); @@ -267,8 +316,8 @@ public class Print extends Lua { } } - public static void printState(StackState state, int base, int top, int max, - Closure cl, int pc) { + public static void printState(LuaState state, int base, int top, int max, + LClosure cl, int pc) { // print opcode into buffer PrintStream previous = ps; @@ -301,7 +350,7 @@ public class Print extends Lua { ps.println(); } - private static String id(Proto f) { + private static String id(LPrototype f) { return "Proto"; } diff --git a/src/main/java/lua/debug/Serializable.java b/src/debug/org/luaj/debug/Serializable.java similarity index 98% rename from src/main/java/lua/debug/Serializable.java rename to src/debug/org/luaj/debug/Serializable.java index 400a796c..629b485d 100644 --- a/src/main/java/lua/debug/Serializable.java +++ b/src/debug/org/luaj/debug/Serializable.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; diff --git a/src/debug/org/luaj/debug/SerializationHelper.java b/src/debug/org/luaj/debug/SerializationHelper.java new file mode 100644 index 00000000..1608a773 --- /dev/null +++ b/src/debug/org/luaj/debug/SerializationHelper.java @@ -0,0 +1,176 @@ +package org.luaj.debug; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.luaj.debug.event.DebugEvent; +import org.luaj.debug.event.DebugEventBreakpoint; +import org.luaj.debug.event.DebugEventError; +import org.luaj.debug.event.DebugEventType; +import org.luaj.debug.request.DebugRequest; +import org.luaj.debug.request.DebugRequestLineBreakpointToggle; +import org.luaj.debug.request.DebugRequestStack; +import org.luaj.debug.request.DebugRequestType; +import org.luaj.debug.response.DebugResponseCallgraph; +import org.luaj.debug.response.DebugResponseSimple; +import org.luaj.debug.response.DebugResponseStack; + + +public class SerializationHelper { + + public static byte[] serialize(Serializable object) throws IOException { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + DataOutputStream dout = new DataOutputStream(bout); + + serialize(object, dout); + + byte[] data = bout.toByteArray(); + + bout.close(); + dout.close(); + + return data; + } + + public static Serializable deserialize(byte[] data) throws IOException { + ByteArrayInputStream bin = new ByteArrayInputStream(data); + DataInputStream din = new DataInputStream(bin); + + Serializable object = deserialize(din); + + bin.close(); + din.close(); + + return object; + } + + + static final int SERIAL_TYPE_NullableString = 0; + static final int SERIAL_TYPE_TableVariable = 1; + static final int SERIAL_TYPE_Variable = 2; + static final int SERIAL_TYPE_DebugResponseCallgraph = 3; + static final int SERIAL_TYPE_DebugResponseStack = 4; + static final int SERIAL_TYPE_DebugResponseSimple = 5; + static final int SERIAL_TYPE_StackFrame = 6; + static final int SERIAL_TYPE_DebugRequestType = 7; + static final int SERIAL_TYPE_DebugRequest = 8; + static final int SERIAL_TYPE_DebugRequestStack = 9; + static final int SERIAL_TYPE_DebugRequestLineBreakpointToggle = 10; + static final int SERIAL_TYPE_DebugEventType = 11; + static final int SERIAL_TYPE_DebugEvent = 12; + static final int SERIAL_TYPE_DebugEventBreakpoint = 13; + static final int SERIAL_TYPE_DebugEventError = 14; + + public static void serialize(Serializable object, DataOutputStream dout) + throws IOException { + if (object instanceof NullableString) { + dout.writeInt(SERIAL_TYPE_NullableString); + NullableString.serialize(dout, (NullableString)object); + } else if (object instanceof TableVariable) { + dout.writeInt(SERIAL_TYPE_TableVariable); + TableVariable.serialize(dout, (TableVariable)object); + } else if (object instanceof Variable) { + dout.writeInt(SERIAL_TYPE_Variable); + Variable.serialize(dout, (Variable)object); + } else if (object instanceof StackFrame) { + dout.writeInt(SERIAL_TYPE_StackFrame); + StackFrame.serialize(dout, (StackFrame)object); + } else if (object instanceof DebugResponseSimple) { + dout.writeInt(SERIAL_TYPE_DebugResponseSimple); + DebugResponseSimple.serialize(dout, (DebugResponseSimple)object); + } else if (object instanceof DebugResponseStack) { + dout.writeInt(SERIAL_TYPE_DebugResponseStack); + DebugResponseStack.serialize(dout, (DebugResponseStack)object); + } else if (object instanceof DebugResponseCallgraph) { + dout.writeInt(SERIAL_TYPE_DebugResponseCallgraph); + DebugResponseCallgraph.serialize(dout, (DebugResponseCallgraph)object); + } else if (object instanceof DebugRequestType) { + dout.writeInt(SERIAL_TYPE_DebugRequestType); + DebugRequestType.serialize(dout, (DebugRequestType)object); + } else if (object instanceof DebugRequestStack) { + dout.writeInt(SERIAL_TYPE_DebugRequestStack); + DebugRequestStack.serialize(dout, (DebugRequestStack)object); + } else if (object instanceof DebugRequestLineBreakpointToggle) { + dout.writeInt(SERIAL_TYPE_DebugRequestLineBreakpointToggle); + DebugRequestLineBreakpointToggle.serialize(dout, (DebugRequestLineBreakpointToggle)object); + } else if (object instanceof DebugRequest) { + dout.writeInt(SERIAL_TYPE_DebugRequest); + DebugRequest.serialize(dout, (DebugRequest)object); + } else if (object instanceof DebugEventType) { + dout.writeInt(SERIAL_TYPE_DebugEventType); + DebugEventType.serialize(dout, (DebugEventType)object); + } else if (object instanceof DebugEventBreakpoint) { + dout.writeInt(SERIAL_TYPE_DebugEventBreakpoint); + DebugEventBreakpoint.serialize(dout, (DebugEventBreakpoint)object); + } else if (object instanceof DebugEventError) { + dout.writeInt(SERIAL_TYPE_DebugEventError); + DebugEventError.serialize(dout, (DebugEventError)object); + } else if (object instanceof DebugEvent) { + dout.writeInt(SERIAL_TYPE_DebugEvent); + DebugEvent.serialize(dout, (DebugEvent)object); + } else { + // catch the errors: forgot to implement serialization/deserialization + throw new RuntimeException("serialization operation is not supported"); + } + } + + public static Serializable deserialize(DataInputStream din) + throws IOException { + Serializable object = null; + int type = din.readInt(); + switch (type) { + case SERIAL_TYPE_NullableString: + object = NullableString.deserialize(din); + break; + case SERIAL_TYPE_TableVariable: + object = TableVariable.deserialize(din); + break; + case SERIAL_TYPE_Variable: + object = Variable.deserialize(din); + break; + case SERIAL_TYPE_StackFrame: + object = StackFrame.deserialize(din); + break; + case SERIAL_TYPE_DebugResponseSimple: + object = DebugResponseSimple.deserialize(din); + break; + case SERIAL_TYPE_DebugResponseCallgraph: + object = DebugResponseCallgraph.deserialize(din); + break; + case SERIAL_TYPE_DebugResponseStack: + object = DebugResponseStack.deserialize(din); + break; + case SERIAL_TYPE_DebugRequestType: + object = DebugRequestType.deserialize(din); + break; + case SERIAL_TYPE_DebugRequestStack: + object = DebugRequestStack.deserialize(din); + break; + case SERIAL_TYPE_DebugRequestLineBreakpointToggle: + object = DebugRequestLineBreakpointToggle.deserialize(din); + break; + case SERIAL_TYPE_DebugRequest: + object = DebugRequest.deserialize(din); + break; + case SERIAL_TYPE_DebugEventType: + object = DebugEventType.deserialize(din); + break; + case SERIAL_TYPE_DebugEventBreakpoint: + object = DebugEventBreakpoint.deserialize(din); + break; + case SERIAL_TYPE_DebugEventError: + object = DebugEventError.deserialize(din); + break; + case SERIAL_TYPE_DebugEvent: + object = DebugEvent.deserialize(din); + break; + default: + throw new RuntimeException("deserialization operation is not supported"); + } + + return object; + } +} diff --git a/src/main/java/lua/debug/StackFrame.java b/src/debug/org/luaj/debug/StackFrame.java similarity index 98% rename from src/main/java/lua/debug/StackFrame.java rename to src/debug/org/luaj/debug/StackFrame.java index 02b52c73..c5f490d9 100644 --- a/src/main/java/lua/debug/StackFrame.java +++ b/src/debug/org/luaj/debug/StackFrame.java @@ -40,13 +40,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import lua.CallInfo; +import org.luaj.vm.CallInfo; + public class StackFrame implements Serializable { protected int lineNumber; diff --git a/src/main/java/lua/debug/TableVariable.java b/src/debug/org/luaj/debug/TableVariable.java similarity index 98% rename from src/main/java/lua/debug/TableVariable.java rename to src/debug/org/luaj/debug/TableVariable.java index dc1de246..3fcb0857 100644 --- a/src/main/java/lua/debug/TableVariable.java +++ b/src/debug/org/luaj/debug/TableVariable.java @@ -19,16 +19,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.Vector; -import lua.Lua; -import lua.value.LTable; -import lua.value.LValue; +import org.luaj.vm.LTable; +import org.luaj.vm.LValue; +import org.luaj.vm.Lua; + public class TableVariable extends Variable { protected String[] keys; diff --git a/src/main/java/lua/debug/VMException.java b/src/debug/org/luaj/debug/VMException.java similarity index 88% rename from src/main/java/lua/debug/VMException.java rename to src/debug/org/luaj/debug/VMException.java index 6d07503e..224490e6 100644 --- a/src/main/java/lua/debug/VMException.java +++ b/src/debug/org/luaj/debug/VMException.java @@ -1,4 +1,4 @@ -package lua.debug; +package org.luaj.debug; public class VMException extends RuntimeException { private static final long serialVersionUID = 7876955153693775429L; diff --git a/src/main/java/lua/debug/Variable.java b/src/debug/org/luaj/debug/Variable.java similarity index 98% rename from src/main/java/lua/debug/Variable.java rename to src/debug/org/luaj/debug/Variable.java index db926136..151e658a 100644 --- a/src/main/java/lua/debug/Variable.java +++ b/src/debug/org/luaj/debug/Variable.java @@ -19,13 +19,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import lua.Lua; +import org.luaj.vm.Lua; + public class Variable implements Serializable { protected int index; diff --git a/src/main/java/lua/debug/event/DebugEvent.java b/src/debug/org/luaj/debug/event/DebugEvent.java similarity index 95% rename from src/main/java/lua/debug/event/DebugEvent.java rename to src/debug/org/luaj/debug/event/DebugEvent.java index 90c41d58..d345d15b 100644 --- a/src/main/java/lua/debug/event/DebugEvent.java +++ b/src/debug/org/luaj/debug/event/DebugEvent.java @@ -19,14 +19,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.event; +package org.luaj.debug.event; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import lua.debug.Serializable; -import lua.debug.SerializationHelper; +import org.luaj.debug.Serializable; +import org.luaj.debug.SerializationHelper; + public class DebugEvent implements Serializable { diff --git a/src/main/java/lua/debug/event/DebugEventBreakpoint.java b/src/debug/org/luaj/debug/event/DebugEventBreakpoint.java similarity index 98% rename from src/main/java/lua/debug/event/DebugEventBreakpoint.java rename to src/debug/org/luaj/debug/event/DebugEventBreakpoint.java index 89e1e979..daa5fd36 100644 --- a/src/main/java/lua/debug/event/DebugEventBreakpoint.java +++ b/src/debug/org/luaj/debug/event/DebugEventBreakpoint.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.event; +package org.luaj.debug.event; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/lua/debug/event/DebugEventError.java b/src/debug/org/luaj/debug/event/DebugEventError.java similarity index 98% rename from src/main/java/lua/debug/event/DebugEventError.java rename to src/debug/org/luaj/debug/event/DebugEventError.java index 67db2f5a..689d8842 100644 --- a/src/main/java/lua/debug/event/DebugEventError.java +++ b/src/debug/org/luaj/debug/event/DebugEventError.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.event; +package org.luaj.debug.event; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/lua/debug/event/DebugEventListener.java b/src/debug/org/luaj/debug/event/DebugEventListener.java similarity index 97% rename from src/main/java/lua/debug/event/DebugEventListener.java rename to src/debug/org/luaj/debug/event/DebugEventListener.java index 7344cadb..d0e65bb1 100644 --- a/src/main/java/lua/debug/event/DebugEventListener.java +++ b/src/debug/org/luaj/debug/event/DebugEventListener.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.event; +package org.luaj.debug.event; public interface DebugEventListener { public void notifyDebugEvent(DebugEvent event); diff --git a/src/main/java/lua/debug/event/DebugEventType.java b/src/debug/org/luaj/debug/event/DebugEventType.java similarity index 98% rename from src/main/java/lua/debug/event/DebugEventType.java rename to src/debug/org/luaj/debug/event/DebugEventType.java index d663501f..5367dc9f 100644 --- a/src/main/java/lua/debug/event/DebugEventType.java +++ b/src/debug/org/luaj/debug/event/DebugEventType.java @@ -19,12 +19,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.event; +package org.luaj.debug.event; import java.io.DataInputStream; import java.io.IOException; -import lua.debug.EnumType; +import org.luaj.debug.EnumType; + public class DebugEventType extends EnumType { diff --git a/src/main/java/lua/debug/j2me/DebugSupportImpl.java b/src/debug/org/luaj/debug/j2me/DebugSupportImpl.java similarity index 96% rename from src/main/java/lua/debug/j2me/DebugSupportImpl.java rename to src/debug/org/luaj/debug/j2me/DebugSupportImpl.java index e340b4be..4e8f1eba 100644 --- a/src/main/java/lua/debug/j2me/DebugSupportImpl.java +++ b/src/debug/org/luaj/debug/j2me/DebugSupportImpl.java @@ -1,4 +1,4 @@ -package lua.debug.j2me; +package org.luaj.debug.j2me; import java.io.IOException; @@ -6,8 +6,9 @@ import javax.microedition.io.Connector; import javax.microedition.io.ServerSocketConnection; import javax.microedition.io.SocketConnection; -import lua.debug.DebugSupport; -import lua.debug.event.DebugEvent; +import org.luaj.debug.DebugSupport; +import org.luaj.debug.event.DebugEvent; + public class DebugSupportImpl extends DebugSupport { protected ServerSocketConnection requestServerConnection; diff --git a/src/main/java/lua/debug/j2se/DebugSupportImpl.java b/src/debug/org/luaj/debug/j2se/DebugSupportImpl.java similarity index 97% rename from src/main/java/lua/debug/j2se/DebugSupportImpl.java rename to src/debug/org/luaj/debug/j2se/DebugSupportImpl.java index 1e61bf8f..6c3e728f 100644 --- a/src/main/java/lua/debug/j2se/DebugSupportImpl.java +++ b/src/debug/org/luaj/debug/j2se/DebugSupportImpl.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.j2se; +package org.luaj.debug.j2se; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -27,8 +27,9 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; -import lua.debug.DebugSupport; -import lua.debug.event.DebugEvent; +import org.luaj.debug.DebugSupport; +import org.luaj.debug.event.DebugEvent; + public class DebugSupportImpl extends DebugSupport { protected ServerSocket requestSocket; diff --git a/src/main/java/lua/debug/j2se/StandardLuaJVM.java b/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java similarity index 88% rename from src/main/java/lua/debug/j2se/StandardLuaJVM.java rename to src/debug/org/luaj/debug/j2se/StandardLuaJVM.java index 31034c4d..c2d1790a 100644 --- a/src/main/java/lua/debug/j2se/StandardLuaJVM.java +++ b/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java @@ -19,26 +19,27 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.j2se; +package org.luaj.debug.j2se; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import lua.GlobalState; -import lua.StackState; -import lua.addon.luacompat.LuaCompat; -import lua.addon.luajava.LuaJava; -import lua.debug.DebugStackState; -import lua.debug.DebugSupport; -import lua.debug.DebugUtils; -import lua.debug.VMException; -import lua.io.Closure; -import lua.io.LoadState; -import lua.io.Proto; -import lua.value.LString; -import lua.value.LValue; +import org.luaj.debug.DebugStackState; +import org.luaj.debug.DebugSupport; +import org.luaj.debug.DebugUtils; +import org.luaj.debug.VMException; +import org.luaj.lib.MathLib; +import org.luaj.lib.j2se.LuajavaLib; +import org.luaj.vm.LClosure; +import org.luaj.vm.LPrototype; +import org.luaj.vm.LString; +import org.luaj.vm.LTable; +import org.luaj.vm.LValue; +import org.luaj.vm.LoadState; +import org.luaj.vm.LuaState; + /** * StandardLuaJVM executes a lua program in normal run mode or debug mode. @@ -52,7 +53,7 @@ public class StandardLuaJVM { protected int eventPort; protected String script; protected String[] scriptArgs; - protected StackState state; + protected LuaState state; protected boolean isReady = false; protected boolean isTerminated = false; @@ -110,7 +111,7 @@ public class StandardLuaJVM { } private void parseScriptArgs(String[] args) - throws lua.debug.j2se.StandardLuaJVM.ParseException { + throws org.luaj.debug.j2se.StandardLuaJVM.ParseException { if (args == null || args.length < 1) { throw new ParseException("script is missing."); } @@ -159,22 +160,20 @@ public class StandardLuaJVM { } } - protected void init() { - //Reset the _G table - GlobalState.resetGlobals(); + protected void init(LTable globals) { // add LuaJava bindings - LuaJava.install(); + LuajavaLib.install(globals); // add LuaCompat bindings - LuaCompat.install(); + MathLib.install(globals); } protected void doRun() throws IOException { - init(); // new lua state - state = new StackState(); + state = new LuaState(); + init(state._G); // convert args to lua String[] scriptArgs = getScriptArgs(); @@ -187,24 +186,24 @@ public class StandardLuaJVM { // load the Lua file DebugUtils.println("loading Lua script '" + getScript() + "'"); InputStream is = new FileInputStream(new File(getScript())); - Proto p = LoadState.undump(state, is, getScript()); + LPrototype p = LoadState.undump(state, is, getScript()); // create closure and execute - Closure c = new Closure(state, p); + LClosure c = new LClosure(state, p); state.doCall(c, vargs); } protected void doDebug() throws IOException { DebugUtils.println("setting up LuaJava and debug stack state..."); - init(); // new lua debug state state = new DebugStackState(); + init(state._G); // load the Lua file DebugUtils.println("loading Lua script '" + getScript() + "'"); InputStream is = new FileInputStream(new File(getScript())); - Proto p = LoadState.undump(state, is, getScript()); + LPrototype p = LoadState.undump(state, is, getScript()); // set up debug support if the file is successfully loaded DebugUtils.println("start debugging..."); @@ -214,7 +213,7 @@ public class StandardLuaJVM { getDebugState().setSuspendAtStart(true); // create closure and execute - final Closure c = new Closure(state, p); + final LClosure c = new LClosure(p, state._G); String[] args = getScriptArgs(); int numOfScriptArgs = (args != null ? args.length : 0); LValue[] vargs = new LValue[numOfScriptArgs]; diff --git a/src/main/java/lua/debug/request/DebugRequest.java b/src/debug/org/luaj/debug/request/DebugRequest.java similarity index 75% rename from src/main/java/lua/debug/request/DebugRequest.java rename to src/debug/org/luaj/debug/request/DebugRequest.java index 6a31ad2d..feca7c61 100644 --- a/src/main/java/lua/debug/request/DebugRequest.java +++ b/src/debug/org/luaj/debug/request/DebugRequest.java @@ -19,14 +19,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.request; +package org.luaj.debug.request; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import lua.debug.Serializable; -import lua.debug.SerializationHelper; +import org.luaj.debug.Serializable; +import org.luaj.debug.SerializationHelper; + public class DebugRequest implements Serializable { protected DebugRequestType type; @@ -46,16 +47,14 @@ public class DebugRequest implements Serializable { return type.toString(); } - public static void serialize(DataOutputStream out, DebugRequest request) - throws IOException { - DebugRequestType type = request.getType(); - SerializationHelper.serialize(type, out); - } - - public static DebugRequest deserialize(DataInputStream in) - throws IOException { - DebugRequestType type = (DebugRequestType) SerializationHelper - .deserialize(in); - return new DebugRequest(type); - } + public static void serialize(DataOutputStream out, DebugRequest request) + throws IOException { + DebugRequestType type = request.getType(); + SerializationHelper.serialize(type, out); + } + + public static DebugRequest deserialize(DataInputStream in) throws IOException { + DebugRequestType type = (DebugRequestType) SerializationHelper.deserialize(in); + return new DebugRequest(type); + } } diff --git a/src/main/java/lua/debug/request/DebugRequestLineBreakpointToggle.java b/src/debug/org/luaj/debug/request/DebugRequestLineBreakpointToggle.java similarity index 97% rename from src/main/java/lua/debug/request/DebugRequestLineBreakpointToggle.java rename to src/debug/org/luaj/debug/request/DebugRequestLineBreakpointToggle.java index 09da6599..4b81932b 100644 --- a/src/main/java/lua/debug/request/DebugRequestLineBreakpointToggle.java +++ b/src/debug/org/luaj/debug/request/DebugRequestLineBreakpointToggle.java @@ -19,13 +19,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.request; +package org.luaj.debug.request; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import lua.debug.SerializationHelper; +import org.luaj.debug.SerializationHelper; + public class DebugRequestLineBreakpointToggle extends DebugRequest { protected String source; diff --git a/src/main/java/lua/debug/request/DebugRequestListener.java b/src/debug/org/luaj/debug/request/DebugRequestListener.java similarity index 96% rename from src/main/java/lua/debug/request/DebugRequestListener.java rename to src/debug/org/luaj/debug/request/DebugRequestListener.java index 655f2af1..7118a686 100644 --- a/src/main/java/lua/debug/request/DebugRequestListener.java +++ b/src/debug/org/luaj/debug/request/DebugRequestListener.java @@ -19,9 +19,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.request; +package org.luaj.debug.request; -import lua.debug.response.DebugResponse; +import org.luaj.debug.response.DebugResponse; public interface DebugRequestListener { diff --git a/src/main/java/lua/debug/request/DebugRequestStack.java b/src/debug/org/luaj/debug/request/DebugRequestStack.java similarity index 96% rename from src/main/java/lua/debug/request/DebugRequestStack.java rename to src/debug/org/luaj/debug/request/DebugRequestStack.java index d7019bda..ae78af81 100644 --- a/src/main/java/lua/debug/request/DebugRequestStack.java +++ b/src/debug/org/luaj/debug/request/DebugRequestStack.java @@ -19,13 +19,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.request; +package org.luaj.debug.request; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import lua.debug.Serializable; +import org.luaj.debug.Serializable; + public class DebugRequestStack extends DebugRequest implements Serializable { protected int index; diff --git a/src/main/java/lua/debug/request/DebugRequestType.java b/src/debug/org/luaj/debug/request/DebugRequestType.java similarity index 92% rename from src/main/java/lua/debug/request/DebugRequestType.java rename to src/debug/org/luaj/debug/request/DebugRequestType.java index 03f0d457..bf30cd9a 100644 --- a/src/main/java/lua/debug/request/DebugRequestType.java +++ b/src/debug/org/luaj/debug/request/DebugRequestType.java @@ -19,18 +19,19 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.request; +package org.luaj.debug.request; import java.io.DataInputStream; import java.io.IOException; -import lua.debug.EnumType; +import org.luaj.debug.EnumType; + public class DebugRequestType extends EnumType { public static final DebugRequestType start = new DebugRequestType("start", 0); public static final DebugRequestType resume = new DebugRequestType("resume", 1); - public static final DebugRequestType suspend = new DebugRequestType("suspend", 2); + public static final DebugRequestType suspend = new DebugRequestType("suspend", 2); public static final DebugRequestType exit = new DebugRequestType("exit", 3); public static final DebugRequestType lineBreakpointSet = new DebugRequestType("lineBreakpointSet", 4); public static final DebugRequestType lineBreakpointClear = new DebugRequestType("lineBreakpointClear", 5); @@ -41,7 +42,6 @@ public class DebugRequestType extends EnumType { public static final DebugRequestType stepInto = new DebugRequestType("stepInto", 10); public static final DebugRequestType stepOver = new DebugRequestType("stepOver", 11); public static final DebugRequestType stepReturn = new DebugRequestType("stepReturn", 12); - public static final DebugRequestType global = new DebugRequestType("global", 13); protected static final DebugRequestType[] ENUMS = new DebugRequestType[] { start, @@ -56,8 +56,7 @@ public class DebugRequestType extends EnumType { stack, stepInto, stepOver, - stepReturn, - global + stepReturn }; public DebugRequestType(String name, int ordinal) { diff --git a/src/main/java/lua/debug/request/DebugRequestWatchpointToggle.java b/src/debug/org/luaj/debug/request/DebugRequestWatchpointToggle.java similarity index 97% rename from src/main/java/lua/debug/request/DebugRequestWatchpointToggle.java rename to src/debug/org/luaj/debug/request/DebugRequestWatchpointToggle.java index 4a1972be..9cbc3fff 100644 --- a/src/main/java/lua/debug/request/DebugRequestWatchpointToggle.java +++ b/src/debug/org/luaj/debug/request/DebugRequestWatchpointToggle.java @@ -19,9 +19,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.request; +package org.luaj.debug.request; -import lua.debug.EnumType; +import org.luaj.debug.EnumType; public class DebugRequestWatchpointToggle extends DebugRequest { public static class AccessType extends EnumType { diff --git a/src/main/java/lua/debug/response/DebugResponse.java b/src/debug/org/luaj/debug/response/DebugResponse.java similarity index 95% rename from src/main/java/lua/debug/response/DebugResponse.java rename to src/debug/org/luaj/debug/response/DebugResponse.java index 8bca33ec..ed29ec62 100644 --- a/src/main/java/lua/debug/response/DebugResponse.java +++ b/src/debug/org/luaj/debug/response/DebugResponse.java @@ -19,8 +19,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.response; +package org.luaj.debug.response; -import lua.debug.Serializable; +import org.luaj.debug.Serializable; public interface DebugResponse extends Serializable {} diff --git a/src/main/java/lua/debug/response/DebugResponseCallgraph.java b/src/debug/org/luaj/debug/response/DebugResponseCallgraph.java similarity index 97% rename from src/main/java/lua/debug/response/DebugResponseCallgraph.java rename to src/debug/org/luaj/debug/response/DebugResponseCallgraph.java index 1f9b047c..e255c14d 100644 --- a/src/main/java/lua/debug/response/DebugResponseCallgraph.java +++ b/src/debug/org/luaj/debug/response/DebugResponseCallgraph.java @@ -19,13 +19,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.response; +package org.luaj.debug.response; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import lua.debug.StackFrame; +import org.luaj.debug.StackFrame; + public class DebugResponseCallgraph implements DebugResponse { protected StackFrame[] stackFrames; diff --git a/src/main/java/lua/debug/response/DebugResponseSimple.java b/src/debug/org/luaj/debug/response/DebugResponseSimple.java similarity index 98% rename from src/main/java/lua/debug/response/DebugResponseSimple.java rename to src/debug/org/luaj/debug/response/DebugResponseSimple.java index bbc2422f..4290b452 100644 --- a/src/main/java/lua/debug/response/DebugResponseSimple.java +++ b/src/debug/org/luaj/debug/response/DebugResponseSimple.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.response; +package org.luaj.debug.response; import java.io.DataInputStream; import java.io.DataOutputStream; diff --git a/src/main/java/lua/debug/response/DebugResponseVariables.java b/src/debug/org/luaj/debug/response/DebugResponseStack.java similarity index 66% rename from src/main/java/lua/debug/response/DebugResponseVariables.java rename to src/debug/org/luaj/debug/response/DebugResponseStack.java index 18670772..fb1caae7 100644 --- a/src/main/java/lua/debug/response/DebugResponseVariables.java +++ b/src/debug/org/luaj/debug/response/DebugResponseStack.java @@ -19,21 +19,22 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.response; +package org.luaj.debug.response; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import lua.debug.SerializationHelper; -import lua.debug.Variable; +import org.luaj.debug.SerializationHelper; +import org.luaj.debug.Variable; -public class DebugResponseVariables implements DebugResponse { + +public class DebugResponseStack implements DebugResponse { protected Variable[] variables; - public DebugResponseVariables(Variable[] variables) { + public DebugResponseStack(Variable[] variables) { if (variables == null) { - this.variables = new Variable[0]; + this.variables = new Variable[0]; } else { this.variables = variables; } @@ -54,23 +55,21 @@ public class DebugResponseVariables implements DebugResponse { return buffer.toString(); } - public static void serialize(DataOutputStream out, - DebugResponseVariables response) + public static void serialize(DataOutputStream out, DebugResponseStack response) throws IOException { - Variable[] variables = response.getVariables(); - out.writeInt(variables == null ? 0 : variables.length); - for (int i = 0; i < variables.length; i++) { - SerializationHelper.serialize(variables[i], out); - } - } + Variable[] variables = response.getVariables(); + out.writeInt(variables == null ? 0 : variables.length); + for (int i = 0; i < variables.length; i++) { + SerializationHelper.serialize(variables[i], out); + } + } - public static DebugResponseVariables deserialize(DataInputStream in) - throws IOException { - int count = in.readInt(); - Variable[] variables = new Variable[count]; - for (int i = 0; i < count; i++) { - variables[i] = (Variable) SerializationHelper.deserialize(in); - } - return new DebugResponseVariables(variables); - } + public static DebugResponseStack deserialize(DataInputStream in) throws IOException { + int count = in.readInt(); + Variable[] variables = new Variable[count]; + for (int i = 0; i < count; i++) { + variables[i] = (Variable) SerializationHelper.deserialize(in); + } + return new DebugResponseStack(variables); + } } diff --git a/src/addon/java/lua/addon/luajava/CoerceJavaToLua.java b/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java similarity index 86% rename from src/addon/java/lua/addon/luajava/CoerceJavaToLua.java rename to src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java index 61b32a92..c54dd12c 100644 --- a/src/addon/java/lua/addon/luajava/CoerceJavaToLua.java +++ b/src/j2se/org/luaj/lib/j2se/CoerceJavaToLua.java @@ -1,15 +1,16 @@ -package lua.addon.luajava; +package org.luaj.lib.j2se; import java.util.HashMap; import java.util.Map; -import lua.addon.luajava.LuaJava.LInstance; -import lua.value.LBoolean; -import lua.value.LDouble; -import lua.value.LInteger; -import lua.value.LNil; -import lua.value.LString; -import lua.value.LValue; +import org.luaj.lib.j2se.LuajavaLib.LInstance; +import org.luaj.vm.LBoolean; +import org.luaj.vm.LDouble; +import org.luaj.vm.LInteger; +import org.luaj.vm.LNil; +import org.luaj.vm.LString; +import org.luaj.vm.LValue; + public class CoerceJavaToLua { diff --git a/src/addon/java/lua/addon/luajava/CoerceLuaToJava.java b/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java similarity index 94% rename from src/addon/java/lua/addon/luajava/CoerceLuaToJava.java rename to src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java index 0d067541..56aa6f3d 100644 --- a/src/addon/java/lua/addon/luajava/CoerceLuaToJava.java +++ b/src/j2se/org/luaj/lib/j2se/CoerceLuaToJava.java @@ -1,16 +1,17 @@ -package lua.addon.luajava; +package org.luaj.lib.j2se; import java.util.HashMap; import java.util.Map; -import lua.value.LBoolean; -import lua.value.LDouble; -import lua.value.LInteger; -import lua.value.LNil; -import lua.value.LNumber; -import lua.value.LString; -import lua.value.LUserData; -import lua.value.LValue; +import org.luaj.vm.LBoolean; +import org.luaj.vm.LDouble; +import org.luaj.vm.LInteger; +import org.luaj.vm.LNil; +import org.luaj.vm.LNumber; +import org.luaj.vm.LString; +import org.luaj.vm.LUserData; +import org.luaj.vm.LValue; + public class CoerceLuaToJava { diff --git a/src/addon/java/lua/addon/luajava/LuaJava.java b/src/j2se/org/luaj/lib/j2se/LuajavaLib.java similarity index 91% rename from src/addon/java/lua/addon/luajava/LuaJava.java rename to src/j2se/org/luaj/lib/j2se/LuajavaLib.java index df03706b..86d905a3 100644 --- a/src/addon/java/lua/addon/luajava/LuaJava.java +++ b/src/j2se/org/luaj/lib/j2se/LuajavaLib.java @@ -1,4 +1,4 @@ -package lua.addon.luajava; +package org.luaj.lib.j2se; /** LuaJava-like bindings to Java scripting. @@ -14,21 +14,20 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import lua.GlobalState; -import lua.VM; -import lua.value.LFunction; -import lua.value.LString; -import lua.value.LTable; -import lua.value.LUserData; -import lua.value.LValue; +import org.luaj.vm.LFunction; +import org.luaj.vm.LTable; +import org.luaj.vm.LUserData; +import org.luaj.vm.LValue; +import org.luaj.vm.LuaState; -public final class LuaJava extends LFunction { - public static void install() { +public final class LuajavaLib extends LFunction { + + public static void install(LTable globals) { LTable luajava = new LTable(); for ( int i=0; i -In order to communicate properly with Lua, -a Java function must use the following protocol, -which defines the way parameters and results are passed: -a Java function receives its arguments from Lua in its stack -in direct order (the first argument is pushed first). -So, when the function starts, -lua_gettop(L) returns the number of arguments received by the function. -The first argument (if any) is at index 1 -and its last argument is at index lua_gettop(L). -To return values to Lua, a Java function just pushes them onto the stack, -in direct order (the first result is pushed first), -and returns the number of results. -Any other value in the stack below the results will be properly -discarded by Lua. -Like a Lua function, a Java function called by Lua can also return -many results. - - -

-As an example, the following function receives a variable number -of numerical arguments and returns their average and sum: - -


-     int foo (VM lua) {
-		int n = lua.gettop();    // number of arguments 
-		double sum = 0;
-		int i;
-		for (i = 1; i <= n; i++) {
-			if (!lua.isnumber(L, i)) {
-				lua.pushstring(L, "incorrect argument");
-				lua.error(L);
-			}
-			sum += lua.tonumber(L, i);
-		}
-		lua.pushnumber(L, sum/n);   // first result 
-		lua.pushnumber(L, sum);     // second result 
-		return 2;                   // number of results 
-	}
-
- */ - -abstract public class JavaFunction extends LFunction { - - /** - * Called to invoke a JavaFunction. - * - * The implementation should manipulate the stack - * via the VM Java API in the same way that lua_CFunctions - * do so in standard lua. - * - * @param lua the LuaState calling this function. - * @return number of results pushed onto the stack. - */ - abstract public int invoke( VM lua ); - - /** - * Set up a Java invocation, and fix up the results - * when it returns. - */ - public boolean luaStackCall(VM vm) { - vm.invokeJavaFunction( this ); - return false; - } -} diff --git a/src/main/java/lua/VM.java b/src/main/java/lua/VM.java deleted file mode 100644 index 8239a859..00000000 --- a/src/main/java/lua/VM.java +++ /dev/null @@ -1,1442 +0,0 @@ -package lua; - -import java.io.InputStream; - -import lua.io.Closure; -import lua.value.LString; -import lua.value.LTable; -import lua.value.LValue; -/** - *
- *

VM

- * - *
- * typedef struct VM;
- * 
- * - *

- * Opaque structure that keeps the whole state of a Lua interpreter. The Lua - * library is fully reentrant: it has no global variables. All information about - * a state is kept in this structure. - * - * - *

- * Here we list all functions and types from the C API in alphabetical - * order. Each function has an indicator like this: [-o, +p, - * x] - * - *

- * The first field, o, is how many elements the function pops - * from the stack. The second field, p, is how many elements the - * function pushes onto the stack. (Any function always pushes its results after - * popping its arguments.) A field in the form x|y means the - * function may push (or pop) x or y elements, - * depending on the situation; an interrogation mark '?' means - * that we cannot know how many elements the function pops/pushes by looking - * only at its arguments (e.g., they may depend on what is on the stack). The - * third field, x, tells whether the function may throw errors: '-' - * means the function never throws any error; 'm' means the - * function may throw an error only due to not enough memory; 'e' - * means the function may throw other kinds of errors; 'v' - * means the function may throw an error on purpose. - * - * - */ -public interface VM { - - // ================ interfaces for performing calls - - /** - * Create a call frame for a call that has been set up on - * the stack. The first value on the stack must be a Closure, - * and subsequent values are arguments to the closure. - */ - public void prepStackCall(); - - /** - * Invoke a JavaFunction being called via prepStackCall() - * @param javaFunction - */ - public void invokeJavaFunction(JavaFunction javaFunction); - - /** - * Execute bytecodes until the current call completes - * or the vm yields. - */ - public void execute(); - - /** - * Put the closure on the stack with arguments, - * then perform the call. Leave return values - * on the stack for later querying. - * - * @param c - * @param values - */ - public void doCall(Closure c, LValue[] values); - - - // =============================================================== - // Lua Java API - // =============================================================== - - /** - * Sets a new panic function and returns the old one. [-0, +0, -] - * - * - *

- * If an error happens outside any protected environment, Lua calls a - * panic function and then calls exit(EXIT_FAILURE), - * thus exiting the host application. Your panic function may avoid this - * exit by never returning (e.g., doing a long jump). - * - * - *

- * The panic function can access the error message at the top of the stack. - */ - public JavaFunction atpanic(JavaFunction panicf); - - /** - * Calls a function. [-(nargs + 1), +nresults, e] - * - * - *

- * To call a function you must use the following protocol: first, the - * function to be called is pushed onto the stack; then, the arguments to - * the function are pushed in direct order; that is, the first argument is - * pushed first. Finally you call lua_call; - * nargs is the number of arguments that you pushed onto the - * stack. All arguments and the function value are popped from the stack - * when the function is called. The function results are pushed onto the - * stack when the function returns. The number of results is adjusted to - * nresults, unless nresults is LUA_MULTRET. In this case, - * all results from the function are pushed. Lua takes care that - * the returned values fit into the stack space. The function results are - * pushed onto the stack in direct order (the first result is pushed first), - * so that after the call the last result is on the top of the stack. - * - * - *

- * Any error inside the called function is propagated upwards (with a - * longjmp). - * - * - *

- * The following example shows how the host program may do the equivalent to - * this Lua code: - * - *

-	 * a = f("how", t.x, 14)
-	 * 
- * - *

- * Here it is in C: - * - *

-	 * lua_getfield(L, LUA_GLOBALSINDEX, "f"); // function to be called 
-	 * lua_pushstring(L, "how"); // 1st argument 
-	 * lua_getfield(L, LUA_GLOBALSINDEX, "t"); // table to be indexed 
-	 * lua_getfield(L, -1, "x"); // push result of t.x (2nd arg) 
-	 * lua_remove(L, -2); // remove 't' from the stack 
-	 * lua_pushinteger(L, 14); // 3rd argument 
-	 * lua_call(L, 3, 1); // call 'f' with 3 arguments and 1 result 
-	 * lua_setfield(L, LUA_GLOBALSINDEX, "a"); // set global 'a' 
-	 * 
- * - *

- * Note that the code above is "balanced": at its end, the stack is back to - * its original configuration. This is considered good programming practice. - */ - public void call(int nargs, int nresults); - - /** - * - * Ensures that there are at least extra free stack slots in - * the stack. [-0, +0, -] - * - *

- * It returns false if it cannot grow the stack to that size. This function - * never shrinks the stack; if the stack is already larger than the new - * size, it is left unchanged. - * - */ - public void checkstack(int extra); - - /** - * Closes the given Lua state. [-0, +0, -] - * - *

- * Destroys all objects in the given Lua state (calling the corresponding - * garbage-collection metamethods, if any) and frees all dynamic memory used - * by this state. On several platforms, you may not need to call this - * function, because all resources are naturally released when the host - * program ends. On the other hand, long-running programs, such as a daemon - * or a web server, might need to release states as soon as they are not - * needed, to avoid growing too large. - */ - public void close(); - - /** - * Concatenates the n values at the top of the stack. [-n, +1, e] - * - *

- * Concatenates the n values at the top of the stack, pops - * them, and leaves the result at the top. If n is 1, - * the result is the single value on the stack (that is, the function does - * nothing); if n is 0, the result is the empty string. - * Concatenation is performed following the usual semantics of Lua (see §2.5.4). - */ - public void concat(int n); - - /** - * Calls the C function func in protected mode. [-0, +(0|1), -] - * - *

- * func starts with only one element in its stack, a light - * userdata containing ud. In case of errors, lua_cpcall returns the same error - * codes as lua_pcall, plus the - * error object on the top of the stack; otherwise, it returns zero, and - * does not change the stack. All values returned by func are - * discarded. - */ - public int javapcall(JavaFunction func, Object ud); - - /** - * Creates a new empty table and pushes it onto the stack. [-0, +1, m] - * - *

- * The new table has space pre-allocated for narr array - * elements and nrec non-array elements. This pre-allocation - * is useful when you know exactly how many elements the table will have. - * Otherwise you can use the function lua_newtable. - */ - public void createtable(int narr, int nrec); - - /** - * Dumps a function as a binary chunk. [-0, +0, - * m] - * - *

- * Receives a Lua function on the top of the stack and produces a binary - * chunk that, if loaded again, results in a function equivalent to the one - * dumped. As it produces parts of the chunk, lua_dump - * calls function writer (see lua_Writer) - * with the given data to write them. - *

- * The value returned is the error code returned by the last call to the - * writer; 0 means no errors. - * - * - *

- * This function does not pop the Lua function from the stack. - * - */ - public void dump(); - - /** - * Tests if two items on the stack are equal. [-0, +0, - * e] - * - *

- * Returns 1 if the two values in acceptable indices index1 - * and index2 are equal, following the semantics of the Lua - * == operator (that is, may call metamethods). Otherwise - * returns 0. Also returns 0 if any of the indices is non valid. - * - * - */ - public boolean equal(int index1, int index2); - - /** - * Generates a Lua error. [-1, +0, v] - * - *

- * The error message (which can actually be a Lua value of any type) must be - * on the stack top. This function does a long jump, and therefore never - * returns. (see luaL_error). - * - */ - public void error(); - - /** - * Raises an error with the default level. - * - * In the java implementation this throws a RuntimeException, possibly filling - * line number information first. - */ - public void error(String message); - - /** - * Raises an error. The message is pushed onto the stack and used as the error message. - * It also adds at the beginning of the message the file name and the line number where - * the error occurred, if this information is available. - * - * In the java implementation this throws a RuntimeException, possibly filling - * line number information first. - */ - public void error(String message, int level); - - /** - * Controls the garbage collector. [-0, +0, e] - * - *

- * This function performs several tasks, according to the value of the - * parameter what: - * - *

    - * - *
  • LUA_GCSTOP: stops the garbage collector. - *
  • - * - *
  • LUA_GCRESTART: restarts the garbage - * collector.
  • - * - *
  • LUA_GCCOLLECT: performs a full - * garbage-collection cycle.
  • - * - *
  • LUA_GCCOUNT: returns the current amount of - * memory (in Kbytes) in use by Lua.
  • - * - *
  • LUA_GCCOUNTB: returns the remainder of - * dividing the current amount of bytes of memory in use by Lua by 1024. - *
  • - * - *
  • LUA_GCSTEP: performs an incremental step of - * garbage collection. The step "size" is controlled by data - * (larger values mean more steps) in a non-specified way. If you want to - * control the step size you must experimentally tune the value of - * data. The function returns 1 if the step finished a - * garbage-collection cycle.
  • - * - *
  • LUA_GCSETPAUSE: sets data/100 - * as the new value for the pause of the collector (see §2.10). The function returns the previous value of - * the pause.
  • - * - *
  • LUA_GCSETSTEPMUL: sets data/100 - * as the new value for the step multiplier of the collector (see - * §2.10). The function returns the previous value - * of the step multiplier.
  • - * - *
- */ - public void gc(int what, int data); - - /** - * Pushes a value's environment table. [-0, +1, - * -] - * - *

- * Pushes onto the stack the environment table of the value at the given - * index. - */ - public void getfenv(int index); - - /** - * Dereference a tables field. [-0, +1, e] - * - *

- * Pushes onto the stack the value t[k], where - * t is the value at the given valid index. As in Lua, this - * function may trigger a metamethod for the "index" event (see §2.8). - * - */ - public void getfield(int index, String k); - - /** - * Look up a global value. [-0, +1, e] - * - *

- * Pushes onto the stack the value of the global name. It is - * defined as a macro: - * - *

-	 * 	 #define lua_getglobal(L,s)  lua_getfield(L, LUA_GLOBALSINDEX, s)
-	 * 	
-	 * 
- */ - public void getglobal(String s); - - /** - * Get a value's metatable. [-0, +(0|1), -] - * - *

- * Pushes onto the stack the metatable of the value at the given acceptable - * index. If the index is not valid, or if the value does not have a - * metatable, the function returns 0 and pushes nothing on the stack. - */ - public int getmetatable(int index); - - /** - * Dereference a table's list element. [-1, +1, - * e] - * - *

- * Pushes onto the stack the value t[k], where - * t is the value at the given valid index and k - * is the value at the top of the stack. - * - *

- * This function pops the key from the stack (putting the resulting value in - * its place). As in Lua, this function may trigger a metamethod for the - * "index" event (see §2.8). - */ - public void gettable(int index); - - /** - * Returns the index of the top element in the stack. [-0, +0, -] - * - *

- * Because indices start at 1, this result is equal to the number of - * elements in the stack (and so 0 means an empty stack). - */ - public int gettop(); - - /** - * Insert the top item somewhere in the stack. [-1, +1, - * -] - * - *

- * Moves the top element into the given valid index, shifting up the - * elements above this index to open space. Cannot be called with a - * pseudo-index, because a pseudo-index is not an actual stack position. - * - */ - public void insert(int index); - - /** - * Test if a value is boolean. [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index has type boolean, - * and 0 otherwise. - * - */ - public boolean isboolean(int index); - - /** - * Test if a value is a JavaFunction. [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index is a - * C function, and 0 otherwise. - * - */ - public boolean isjavafunction(int index); - - /** - * Test if a value is a function. [-0, +0, -] - * - *

- * Returns true if the value at the given acceptable index is a function - * (either C or Lua), and false otherwise. - * - */ - public boolean isfunction(int index); - - /** - * Test if a value is light user data [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index is a light userdata, - * and 0 otherwise. - */ - public boolean islightuserdata(int index); - - /** - * Test if a value is nil [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index is nil, and - * 0 otherwise. - */ - public boolean isnil(int index); - - /** - * Test if a value is not valid [-0, +0, -] - * - *

- * Returns 1 if the the given acceptable index is not valid (that is, it - * refers to an element outside the current stack), and 0 otherwise. - */ - public boolean isnone(int index); - - /** - * Test if a value is nil or not valid [-0, +0, - * -] - * - *

- * Returns 1 if the the given acceptable index is not valid (that is, it - * refers to an element outside the current stack) or if the value at this - * index is nil, and 0 otherwise. - */ - public boolean isnoneornil(int index); - - /** - * Test if a value is a number [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index is a number or a - * string convertible to a number, and 0 otherwise. - */ - public boolean isnumber(int index); - - /** - * Test if a value is a string [-0, +0, m] - * - *

- * Returns 1 if the value at the given acceptable index is a string or a - * number (which is always convertible to a string), and 0 otherwise. - */ - public boolean isstring(int index); - - /** - * Test if a value is a table [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index is a table, and - * 0 otherwise. - */ - public boolean istable(int index); - - /** - * Test if a value is a thread [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index is a thread, and - * 0 otherwise. - */ - public boolean isthread(int index); - - /** - * Test if a value is a userdata [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index is a userdata - * (either full or light), and 0 otherwise. - */ - public boolean isuserdata(int index); - - /** - * Compare two values [-0, +0, e] - * - *

- * Returns 1 if the value at acceptable index index1 is - * smaller than the value at acceptable index index2, - * following the semantics of the Lua < operator (that is, - * may call metamethods). Otherwise returns 0. Also returns 0 if - * any of the indices is non valid. - */ - public boolean lessthan(int index1, int index2); - - /** - * Loads a Lua chunk. [-0, +1, -] - * - *

- * If there are no errors, lua_load - * pushes the compiled chunk as a Lua function on top of the stack. - * Otherwise, it pushes an error message. The return values of lua_load are: - * - *

    - * - *
  • 0: no errors;
  • - * - *
  • LUA_ERRSYNTAX: - * syntax error during pre-compilation;
  • - * - *
  • LUA_ERRMEM: - * memory allocation error.
  • - * - *
- * - *

- * This function only loads a chunk; it does not run it. - * - * - *

- * lua_load automatically detects - * whether the chunk is text or binary, and loads it accordingly (see - * program luac). - * - * - *

- * The lua_load function uses a - * user-supplied reader function to read the chunk (see lua_Reader). The - * data argument is an opaque value passed to the reader - * function. - * - * - *

- * The chunkname argument gives a name to the chunk, which is - * used for error messages and in debug information (see §3.8). - */ - public int load(InputStream is, String chunkname); - - /** - * Create a table [-0, +1, m] - * - *

- * Creates a new empty table and pushes it onto the stack. It is equivalent - * to lua_createtable(L, 0, 0). - */ - public void newtable(); - - /** - * Create a thread [-0, +1, m] - * - *

- * Creates a new thread, pushes it on the stack, and returns a pointer to a - * lua_State that represents this - * new thread. The new state returned by this function shares with the - * original state all global objects (such as tables), but has an - * independent execution stack. - * - * - *

- * There is no explicit function to close or to destroy a thread. Threads - * are subject to garbage collection, like any Lua object. - */ - public void newthread(); - - /** - * Create a userdata [-0, +1, m] - * - *

- * This function allocates a new block of memory with the given size, pushes - * onto the stack a new full userdata with the block address, and returns - * this address. - * - * - *

- * Userdata represent C values in Lua. A full userdata - * represents a block of memory. It is an object (like a table): you must - * create it, it can have its own metatable, and you can detect when it is - * being collected. A full userdata is only equal to itself (under raw - * equality). - * - * - *

- * When Lua collects a full userdata with a gc metamethod, - * Lua calls the metamethod and marks the userdata as finalized. When this - * userdata is collected again then Lua frees its corresponding memory. - */ - public void newuserdata(Object o); - - /** - * Traverse to the next table item. [-1, +(2|0), - * e] - * - *

- * Pops a key from the stack, and pushes a key-value pair from the table at - * the given index (the "next" pair after the given key). If there are no - * more elements in the table, then lua_next - * returns 0 (and pushes nothing). - * - * - *

- * A typical traversal looks like this: - * - *

-	 * // table is in the stack at index 't' 
-	 * lua_pushnil(L); // first key 
-	 * while (lua_next(L, t) != 0) {
-	 * 	// uses 'key' (at index -2) and 'value' (at index -1) 
-	 * 	printf("%s - %s\n", lua_typename(L, lua_type(L, -2)), lua_typename(L,
-	 * 			lua_type(L, -1)));
-	 * 	// removes 'value'; keeps 'key' for next iteration 
-	 * 	lua_pop(L, 1);
-	 * }
-	 * 
- * - *

- * While traversing a table, do not call lua_tolstring - * directly on a key, unless you know that the key is actually a string. - * Recall that lua_tolstring - * changes the value at the given index; this confuses the next - * call to lua_next. - */ - public int next(int index); - - /** - * Get the length of an object [-0, +0, -] - * - *

- * Returns the "length" of the value at the given acceptable index: for - * strings, this is the string length; for tables, this is the result of the - * length operator ('#'); for userdata, this is the size of - * the block of memory allocated for the userdata; for other values, it - * is 0. - */ - public int objlen(int index); - - /** - * Calls a function in protected mode. [-(nargs + 1), - * +(nresults|1), -] - * - * - *

- * Both nargs and nresults have the same - * meaning as in lua_call. If there - * are no errors during the call, lua_pcall - * behaves exactly like lua_call. - * However, if there is any error, lua_pcall - * catches it, pushes a single value on the stack (the error message), and - * returns an error code. Like lua_call, - * lua_pcall always removes the - * function and its arguments from the stack. - * - * - *

- * If errfunc is 0, then the error message returned on the - * stack is exactly the original error message. Otherwise, - * errfunc is the stack index of an - * error handler function. (In the current implementation, this - * index cannot be a pseudo-index.) In case of runtime errors, this function - * will be called with the error message and its return value will be the - * message returned on the stack by lua_pcall. - * - * - *

- * Typically, the error handler function is used to add more debug - * information to the error message, such as a stack traceback. Such - * information cannot be gathered after the return of lua_pcall, - * since by then the stack has unwound. - * - * - *

- * The lua_pcall function returns - * 0 in case of success or one of the following error codes (defined in - * lua.h): - * - *

    - * - *
  • LUA_ERRRUN: a - * runtime error.
  • - * - *
  • LUA_ERRMEM: - * memory allocation error. For such errors, Lua does not call the error - * handler function.
  • - * - *
  • LUA_ERRERR: - * error while running the error handler function.
  • - * - *
- */ - public int pcall(int nargs, int nresults, int errfunc); - - /** - * Pops n elements from the stack. [-n, - * +0, -] - */ - public void pop(int n); - - /** - * Pushes a boolean value with value b onto the stack. [-0, +1, -] - * - */ - public void pushboolean(boolean b); - - /** - * Pushes a new C closure onto the stack. [-n, +1, - * m] - * - * - *

- * When a Java function is created, it is possible to associate some - * values with it, thus creating a C closure (see §3.4); these values are then accessible to the - * function whenever it is called. To associate values with a - * C function, first these values should be pushed onto the stack (when - * there are multiple values, the first value is pushed first). Then lua_pushcclosure is called - * to create and push the C function onto the stack, with the argument - * n telling how many values should be associated with the - * function. lua_pushcclosure - * also pops these values from the stack. - */ - public void pushclosure(JavaFunction fn, int n); - - /** - * Pushes a Java function onto the stack. [-0, +1, - * m] - * - *

- * This function receives a pointer to a C function and pushes onto the - * stack a Lua value of type function that, when called, - * invokes the corresponding C function. - * - * - *

- * Any function to be registered in Lua must follow the correct protocol to - * receive its parameters and return its results (see lua_CFunction). - * - */ - public void pushjavafunction(JavaFunction f); - - /** - * Format and push a string. [-0, +1, m] - * - *

- * Pushes onto the stack a formatted string and returns a pointer to this - * string. It is similar to the C function sprintf, but - * has some important differences: - * - *

    - * - *
  • You do not have to allocate space for the result: the result is a - * Lua string and Lua takes care of memory allocation (and deallocation, - * through garbage collection).
  • - * - *
  • The conversion specifiers are quite restricted. There are no flags, - * widths, or precisions. The conversion specifiers can only be '%%' - * (inserts a '%' in the string), '%s' - * (inserts a zero-terminated string, with no size restrictions), '%f' - * (inserts a lua_Number), '%p' - * (inserts a pointer as a hexadecimal numeral), '%d' - * (inserts an int), and '%c' (inserts an - * int as a character).
  • - * - *
- */ - public String pushfstring(String fmt, Object[] args); - - /** - * Pushes a number with value n onto the stack. [-0, +1, -] - */ - public void pushinteger(int n); - - /** - * Pushes a light userdata onto the stack. [-0, +1, - * -] - * - * - *

- * Userdata represent C values in Lua. A light userdata - * represents a pointer. It is a value (like a number): you do not create - * it, it has no individual metatable, and it is not collected (as it was - * never created). A light userdata is equal to "any" light userdata with - * the same C address. - */ - public void pushlightuserdata(Object p); - - /** - * Push string bytes onto the stack as a string. [-0, +1, - * m] - * - * Pushes the string pointed to by s with size - * len onto the stack. Lua makes (or reuses) an internal copy - * of the given string, so the memory at s can be freed or - * reused immediately after the function returns. The string can contain - * embedded zeros. - */ - public void pushlstring(byte[] bytes, int offset, int length); - - /** - * Push string bytes onto the stack as a string. [-0, +1, - * m] - * - * Pushes the bytes in byteArray onto the stack as a lua string. - */ - public void pushlstring(byte[] byteArray); - - /** - * Push an LValue onto the stack. [-0, +1, - * m] - */ - public void pushlvalue(LValue v); - - /** - * Push an LString onto the stack. [-0, +1, - * m] - */ - public void pushlstring(LString luaGetTypeName); - - /** - * Pushes a nil value onto the stack. [-0, +1, -] - * - */ - public void pushnil(); - - /** - * Pushes a number with value d onto the stack. [-0, +1, -] - * - */ - public void pushnumber(double d); - - /** - * Push a String onto the stack. [-0, +1, m] - * - *

- * Pushes the String s onto the stack. Lua makes (or reuses) - * an internal copy of the given string, so the memory at s - * can be freed or reused immediately after the function returns. The string - * cannot contain embedded zeros; it is assumed to end at the first zero. - */ - public void pushstring(String s); - - /** - * Push a thread onto the stack. [-0, +1, -] - * - * Pushes the thread represented by L onto the stack. Returns - * 1 if this thread is the main thread of its state. - */ - public void pushthread(); - - /** - * Push a value from the stack onto the stack. [-0, +1, - * -] - * - *

- * Pushes a copy of the element at the given valid index onto the stack. - */ - public void pushvalue(int index); - - /** - * Format and push a string. [-0, +1, m] - * - *

- * Equivalent to lua_pushfstring, - * except that it receives a va_list instead of a variable - * number of arguments. - */ - public void pushvfstring(String format, Object[] args); - - /** - * Test if two objects are the same object. [-0, +0, - * -] - * - *

- * Returns 1 if the two values in acceptable indices index1 - * and index2 are primitively equal (that is, without calling - * metamethods). Otherwise returns 0. Also returns 0 if any of the - * indices are non valid. - */ - public void rawequal(int index1, int index2); - - /** - * Do a table get without metadata calls. [-1, +1, - * -] - * - *

- * Similar to lua_gettable, but - * does a raw access (i.e., without metamethods). - */ - public void rawget(int index); - - /** - * Do a integer-key table get without metadata calls. [-0, +1, -] - * - *

- * Pushes onto the stack the value t[n], where - * t is the value at the given valid index. The access is - * raw; that is, it does not invoke metamethods. - */ - public void rawgeti(int index, int n); - - /** - * Do a table set without metadata calls. [-2, +0, - * m] - * - *

- * Similar to lua_settable, but - * does a raw assignment (i.e., without metamethods). - */ - public void rawset(int index); - - /** - * Do a integer-key table set without metadata calls. [-1, +0, m] - * - *

- * Does the equivalent of t[n] = v, where t - * is the value at the given valid index and v is the value - * at the top of the stack. - * - * - *

- * This function pops the value from the stack. The assignment is raw; that - * is, it does not invoke metamethods. - */ - public void rawseti(int index, int n); - - /** - * Register a JavaFunction with a specific name. [-0, +0, - * m] - * - *

- * Sets the C function f as the new value of global - * name. It is defined as a macro: - * - *

-	 * 	 #define lua_register(L,n,f) \
-	 * 	 (lua_pushcfunction(L, f), lua_setglobal(L, n))
-	 * 	
-	 * 
- */ - public void register(String name, JavaFunction f); - - /** - * Remove an element from the stack. [-1, +0, -] - * - *

- * Removes the element at the given valid index, shifting down the elements - * above this index to fill the gap. Cannot be called with a pseudo-index, - * because a pseudo-index is not an actual stack position. - */ - public void remove(int index); - - /** - * Replace an element on the stack. [-1, +0, -] - * - *

- * Moves the top element into the given position (and pops it), without - * shifting any element (therefore replacing the value at the given - * position). - */ - public void replace(int index); - - /** - * Starts and resumes a coroutine in a given thread. [-?, - * +?, -] - * - * - *

- * To start a coroutine, you first create a new thread (see lua_newthread); then you push - * onto its stack the main function plus any arguments; then you call lua_resume, with - * narg being the number of arguments. This call returns when - * the coroutine suspends or finishes its execution. When it returns, the - * stack contains all values passed to lua_yield, - * or all values returned by the body function. lua_resume - * returns LUA_YIELD if the - * coroutine yields, 0 if the coroutine finishes its execution without - * errors, or an error code in case of errors (see lua_pcall). - * In case of errors, the stack is not unwound, so you can use the debug API - * over it. The error message is on the top of the stack. To restart a - * coroutine, you put on its stack only the values to be passed as results - * from yield, and then call lua_resume. - */ - public void resume(int narg); - - /** - * Set the environment for a value. [-1, +0, -] - * - *

- * Pops a table from the stack and sets it as the new environment for the - * value at the given index. If the value at the given index is neither a - * function nor a thread nor a userdata, lua_setfenv - * returns 0. Otherwise it returns 1. - */ - public int setfenv(int index); - - /** - * Set the value of a table field. [-1, +0, e] - * - *

- * Does the equivalent to t[k] = v, where t - * is the value at the given valid index and v is the value - * at the top of the stack. - * - * - *

- * This function pops the value from the stack. As in Lua, this function may - * trigger a metamethod for the "newindex" event (see §2.8). - */ - public void setfield(int index, String k); - - /** - * Set the value of a global variable. [-1, +0, - * e] - * - *

- * Pops a value from the stack and sets it as the new value of global - * name. It is defined as a macro: - * - *

-	 * 	 #define lua_setglobal(L,s)   lua_setfield(L, LUA_GLOBALSINDEX, s)
-	 * 	
-	 * 
- */ - public void setglobal(String name); - - /** - * Set the metatable of a value. [-1, +0, -] - * - *

- * Pops a table from the stack and sets it as the new metatable for the - * value at the given acceptable index. - */ - public void setmetatable(int index); - - /** - * Set the value of a table for a key. [-2, +0, - * e] - * - *

- * Does the equivalent to t[k] = v, where t - * is the value at the given valid index, v is the value at - * the top of the stack, and k is the value just below the - * top. - * - * - *

- * This function pops both the key and the value from the stack. As in Lua, - * this function may trigger a metamethod for the "newindex" event (see §2.8). - */ - public void settable(int index); - - /** - * Set the top of the stack. [-?, +?, -] - * - *

- * Accepts any acceptable index, or 0, and sets the stack top to this - * index. If the new top is larger than the old one, then the new elements - * are filled with nil. If index is 0, then all - * stack elements are removed. - */ - public void settop(int index); - - /** - * Returns the status of the thread L. [-0, +0, -] - * - * - * - *

- * The status can be 0 for a normal thread, an error code if the thread - * finished its execution with an error, or LUA_YIELD - * if the thread is suspended. - * - */ - public void status(); - - /** - * Get a value as a boolean. [-0, +0, -] - * - *

- * Converts the Lua value at the given acceptable index to a C boolean - * value (0 or 1). Like all tests in Lua, lua_toboolean returns 1 for - * any Lua value different from false and nil; otherwise it - * returns 0. It also returns 0 when called with a non-valid index. (If you - * want to accept only actual boolean values, use lua_isboolean - * to test the value's type.) - * - */ - public boolean toboolean(int index); - - /** - * Get a value as a JavaFunction. - *


- *

lua_tocfunction

- *

- * [-0, +0, -] - * - *

-	 * lua_CFunction lua_tocfunction (lua_State *L, int index);
-	 * 
- * - *

- * Converts a value at the given acceptable index to a C function. That - * value must be a C function; otherwise, returns NULL. - */ - public JavaFunction tojavafunction(int index); - - /** - * Get a value as an int. [-0, +0, -] - * - *

- * Converts the Lua value at the given acceptable index to the signed - * integral type lua_Integer. - * The Lua value must be a number or a string convertible to a number (see - * §2.2.1); otherwise, lua_tointeger - * returns 0. - * - * - *

- * If the number is not an integer, it is truncated in some non-specified - * way. - */ - public int tointeger(int index); - - /** - * Gets the value of a string as byte array. [-0, +0, - * m] - * - *

- * Converts the Lua value at the given acceptable index to a C string. - * If len is not NULL, it also sets - * *len with the string length. The Lua value must be a - * string or a number; otherwise, the function returns NULL. - * If the value is a number, then lua_tolstring - * also changes the actual value in the stack to a string. (This - * change confuses lua_next when lua_tolstring is applied to - * keys during a table traversal.) - * - * - *

- * lua_tolstring returns a - * fully aligned pointer to a string inside the Lua state. This string - * always has a zero ('\0') after its last character (as - * in C), but may contain other zeros in its body. Because Lua has - * garbage collection, there is no guarantee that the pointer returned by lua_tolstring will be valid - * after the corresponding value is removed from the stack. - */ - public LString tolstring(int index); - - /** - * Convert a value to a double. [-0, +0, -] - * - *

- * Converts the Lua value at the given acceptable index to the C type - * lua_Number (see lua_Number). The Lua value must - * be a number or a string convertible to a number (see §2.2.1); otherwise, lua_tonumber - * returns 0. - * - */ - public double tonumber(int index); - - /** - * Get the raw Object at a stack location. [-0, +0, - * -] - * - *

- * Converts the value at the given acceptable index to a generic - * C pointer (void*). The value may be a userdata, a - * table, a thread, or a function; otherwise, lua_topointer - * returns NULL. Different objects will give different - * pointers. There is no way to convert the pointer back to its original - * value. - * - * - *

- * Typically this function is used only for debug information. - */ - public LValue topointer(int index); - - /** - * Get a stack value as a String. [-0, +0, m] - * - *

- * Equivalent to lua_tolstring - * with len equal to NULL. - */ - public String tostring(int index); - - /** - * Get a thread value from the stack. [-0, +0, -] - * - *

- * Converts the value at the given acceptable index to a Lua thread - * (represented as lua_State*). This value must be a thread; - * otherwise, the function returns NULL. - */ - public StackState tothread(int index); - - /** - * Get a value from the stack as a lua table. [-0, +0, -] - * - *

- * Converts the value at the given acceptable index to a Lua table - * This value must be a table otherwise, the function returns NIL. - */ - public LTable totable(int index); - - /** - * Get the Object from a userdata value. [-0, +0, - * -] - * - *

- * If the value at the given acceptable index is a full userdata, returns - * its block address. If the value is a light userdata, returns its pointer. - * Otherwise, returns NULL. - * - */ - public Object touserdata(int index); - - /** - * Get the type of a value. [-0, +0, -] - * - *

- * Returns the type of the value in the given acceptable index, or - * LUA_TNONE for a non-valid index (that is, an index to an - * "empty" stack position). The types returned by lua_type - * are coded by the following constants defined in lua.h: - * LUA_TNIL, LUA_TNUMBER, - * LUA_TBOOLEAN, LUA_TSTRING, - * LUA_TTABLE, LUA_TFUNCTION, - * LUA_TUSERDATA, LUA_TTHREAD, and - * LUA_TLIGHTUSERDATA. - */ - public int type(int index); - - /** - * Get the type name for a value. [-0, +0, -] - * - *

- * Returns the name of the type encoded by the value tp, - * which must be one the values returned by lua_type. - */ - public String typename(int tp); - - /** - * Exchange values between threads. [-?, +?, -] - * - *

- * Exchange values between different threads of the same global - * state. - * - * - *

- * This function pops n values from the stack - * from, and pushes them onto the stack to. - */ - public void xmove(VM to, int n); - - /** - * Yields a coroutine. [-?, +?, -] - * - * - *

- * This function should only be called as the return expression of a - * C function, as follows: - * - *

-	 * return lua_yield(L, nresults);
-	 * 
- * - *

- * When a C function calls lua_yield - * in that way, the running coroutine suspends its execution, and the call - * to lua_resume that started - * this coroutine returns. The parameter nresults is the - * number of values from the stack that are passed as results to lua_resume. - * - */ - public void yield(int nresults); - - // ============================= conversion to and from Java boxed types ==================== - - /** - * Push a Java Boolean value, or nil if the value is null. - * @param b Boolean value to convert, or null to to nil. - */ - public void pushboolean( Boolean b ); - - /** - * Push a Java Byte value, or nil if the value is null. - * @param b Byte value to convert, or null to to nil. - */ - public void pushinteger( Byte b ); - - /** - * Push a Java Character value, or nil if the value is null. - * @param c Character value to convert, or null to to nil. - */ - public void pushinteger( Character c ); - - /** - * Push a Java Double as a double, or nil if the value is null. - * @param d Double value to convert, or null to to nil. - */ - public void pushnumber( Double d ); - - /** - * Push a Java Float value, or nil if the value is null. - * @param f Float value to convert, or null to to nil. - */ - public void pushnumber( Float f ); - - /** - * Push a Java Integer value, or nil if the value is null. - * @param i Integer value to convert, or null to to nil. - */ - public void pushinteger( Integer i ); - - /** - * Push a Java Short value, or nil if the value is null. - * @param s Short value to convert, or null to to nil. - */ - public void pushinteger( Short s ); - - /** - * Push a Java Long value, or nil if the value is null. - * @param l Long value to convert, or null to to nil. - */ - public void pushnumber( Long l ); - - /** - * Push a Java Object as userdata, or nil if the value is null. - * @param o Object value to push, or null to to nil. - */ - public void pushuserdata( Object o ); - - - /** - * Convert a value to a Java Boolean value, or null if the value is nil. - * @param index index of the parameter to convert. - * @return Boolean value at the index, or null if the value was not a boolean. - */ - public Boolean toboxedboolean(int index); - - /** - * Convert a value to a Java Byte value, or null if the value is not a number. - * @param index index of the parameter to convert. - * @return Byte value at the index, or null if the value was not a number. - */ - public Byte toboxedbyte(int index); - - /** - * Convert a value to a Java Double value, or null if the value is not a number. - * @param index index of the parameter to convert. - * @return Double value at the index, or null if the value was not a number. - */ - public Double toboxeddouble(int index); - - /** - * Convert a value to a Java Float value, or null if the value is not a number. - * @param index index of the parameter to convert. - * @return Float value at the index, or null if the value was not a boolean. - */ - public Float toboxedfloat(int index); - - /** - * Convert a value to a Java Integer value, or null if the value is not a number. - * @param index index of the parameter to convert. - * @return Integer value at the index, or null if the value was not a number. - */ - public Integer toboxedinteger(int index); - - /** - * Convert a value to a Java Long value, or null if the value is nil. - * @param index index of the parameter to convert. - * @return Long value at the index, or null if the value was not a number. - */ - public Long toboxedlong(int index); - -} diff --git a/src/main/java/lua/debug/SerializationHelper.java b/src/main/java/lua/debug/SerializationHelper.java deleted file mode 100644 index 43774f20..00000000 --- a/src/main/java/lua/debug/SerializationHelper.java +++ /dev/null @@ -1,179 +0,0 @@ -package lua.debug; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import lua.debug.event.DebugEvent; -import lua.debug.event.DebugEventBreakpoint; -import lua.debug.event.DebugEventError; -import lua.debug.event.DebugEventType; -import lua.debug.request.DebugRequest; -import lua.debug.request.DebugRequestLineBreakpointToggle; -import lua.debug.request.DebugRequestStack; -import lua.debug.request.DebugRequestType; -import lua.debug.response.DebugResponseCallgraph; -import lua.debug.response.DebugResponseSimple; -import lua.debug.response.DebugResponseVariables; - -public class SerializationHelper { - - public static byte[] serialize(Serializable object) throws IOException { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - DataOutputStream dout = new DataOutputStream(bout); - - serialize(object, dout); - - byte[] data = bout.toByteArray(); - - bout.close(); - dout.close(); - - return data; - } - - public static Serializable deserialize(byte[] data) throws IOException { - ByteArrayInputStream bin = new ByteArrayInputStream(data); - DataInputStream din = new DataInputStream(bin); - - Serializable object = deserialize(din); - - bin.close(); - din.close(); - - return object; - } - - static final int SERIAL_TYPE_NullableString = 0; - static final int SERIAL_TYPE_TableVariable = 1; - static final int SERIAL_TYPE_Variable = 2; - static final int SERIAL_TYPE_DebugResponseCallgraph = 3; - static final int SERIAL_TYPE_DebugResponseVariables = 4; - static final int SERIAL_TYPE_DebugResponseSimple = 5; - static final int SERIAL_TYPE_StackFrame = 6; - static final int SERIAL_TYPE_DebugRequestType = 7; - static final int SERIAL_TYPE_DebugRequest = 8; - static final int SERIAL_TYPE_DebugRequestStack = 9; - static final int SERIAL_TYPE_DebugRequestLineBreakpointToggle = 10; - static final int SERIAL_TYPE_DebugEventType = 11; - static final int SERIAL_TYPE_DebugEvent = 12; - static final int SERIAL_TYPE_DebugEventBreakpoint = 13; - static final int SERIAL_TYPE_DebugEventError = 14; - - public static void serialize(Serializable object, DataOutputStream dout) - throws IOException { - if (object instanceof NullableString) { - dout.writeInt(SERIAL_TYPE_NullableString); - NullableString.serialize(dout, (NullableString) object); - } else if (object instanceof TableVariable) { - dout.writeInt(SERIAL_TYPE_TableVariable); - TableVariable.serialize(dout, (TableVariable) object); - } else if (object instanceof Variable) { - dout.writeInt(SERIAL_TYPE_Variable); - Variable.serialize(dout, (Variable) object); - } else if (object instanceof StackFrame) { - dout.writeInt(SERIAL_TYPE_StackFrame); - StackFrame.serialize(dout, (StackFrame) object); - } else if (object instanceof DebugResponseSimple) { - dout.writeInt(SERIAL_TYPE_DebugResponseSimple); - DebugResponseSimple.serialize(dout, (DebugResponseSimple) object); - } else if (object instanceof DebugResponseVariables) { - dout.writeInt(SERIAL_TYPE_DebugResponseVariables); - DebugResponseVariables.serialize(dout, (DebugResponseVariables) object); - } else if (object instanceof DebugResponseCallgraph) { - dout.writeInt(SERIAL_TYPE_DebugResponseCallgraph); - DebugResponseCallgraph.serialize(dout, - (DebugResponseCallgraph) object); - } else if (object instanceof DebugRequestType) { - dout.writeInt(SERIAL_TYPE_DebugRequestType); - DebugRequestType.serialize(dout, (DebugRequestType) object); - } else if (object instanceof DebugRequestStack) { - dout.writeInt(SERIAL_TYPE_DebugRequestStack); - DebugRequestStack.serialize(dout, (DebugRequestStack) object); - } else if (object instanceof DebugRequestLineBreakpointToggle) { - dout.writeInt(SERIAL_TYPE_DebugRequestLineBreakpointToggle); - DebugRequestLineBreakpointToggle.serialize(dout, - (DebugRequestLineBreakpointToggle) object); - } else if (object instanceof DebugRequest) { - dout.writeInt(SERIAL_TYPE_DebugRequest); - DebugRequest.serialize(dout, (DebugRequest) object); - } else if (object instanceof DebugEventType) { - dout.writeInt(SERIAL_TYPE_DebugEventType); - DebugEventType.serialize(dout, (DebugEventType) object); - } else if (object instanceof DebugEventBreakpoint) { - dout.writeInt(SERIAL_TYPE_DebugEventBreakpoint); - DebugEventBreakpoint.serialize(dout, (DebugEventBreakpoint) object); - } else if (object instanceof DebugEventError) { - dout.writeInt(SERIAL_TYPE_DebugEventError); - DebugEventError.serialize(dout, (DebugEventError) object); - } else if (object instanceof DebugEvent) { - dout.writeInt(SERIAL_TYPE_DebugEvent); - DebugEvent.serialize(dout, (DebugEvent) object); - } else { - // catch the errors: forgot to implement - // serialization/deserialization - throw new RuntimeException( - "serialization operation is not supported"); - } - } - - public static Serializable deserialize(DataInputStream din) - throws IOException { - Serializable object = null; - int type = din.readInt(); - switch (type) { - case SERIAL_TYPE_NullableString: - object = NullableString.deserialize(din); - break; - case SERIAL_TYPE_TableVariable: - object = TableVariable.deserialize(din); - break; - case SERIAL_TYPE_Variable: - object = Variable.deserialize(din); - break; - case SERIAL_TYPE_StackFrame: - object = StackFrame.deserialize(din); - break; - case SERIAL_TYPE_DebugResponseSimple: - object = DebugResponseSimple.deserialize(din); - break; - case SERIAL_TYPE_DebugResponseCallgraph: - object = DebugResponseCallgraph.deserialize(din); - break; - case SERIAL_TYPE_DebugResponseVariables: - object = DebugResponseVariables.deserialize(din); - break; - case SERIAL_TYPE_DebugRequestType: - object = DebugRequestType.deserialize(din); - break; - case SERIAL_TYPE_DebugRequestStack: - object = DebugRequestStack.deserialize(din); - break; - case SERIAL_TYPE_DebugRequestLineBreakpointToggle: - object = DebugRequestLineBreakpointToggle.deserialize(din); - break; - case SERIAL_TYPE_DebugRequest: - object = DebugRequest.deserialize(din); - break; - case SERIAL_TYPE_DebugEventType: - object = DebugEventType.deserialize(din); - break; - case SERIAL_TYPE_DebugEventBreakpoint: - object = DebugEventBreakpoint.deserialize(din); - break; - case SERIAL_TYPE_DebugEventError: - object = DebugEventError.deserialize(din); - break; - case SERIAL_TYPE_DebugEvent: - object = DebugEvent.deserialize(din); - break; - default: - throw new RuntimeException( - "deserialization operation is not supported"); - } - - return object; - } -} diff --git a/src/main/java/lua/value/LFunction.java b/src/main/java/lua/value/LFunction.java deleted file mode 100644 index 1fd95317..00000000 --- a/src/main/java/lua/value/LFunction.java +++ /dev/null @@ -1,32 +0,0 @@ -package lua.value; - -import lua.Lua; -import lua.VM; - - -public class LFunction extends LValue { - - public String toJavaString() { - return "function: "+hashCode(); - } - - public void luaSetTable(VM vm, LValue table, LValue key, LValue val) { - vm.pushlvalue( this ); - vm.pushlvalue( table ); - vm.pushlvalue( key ); - vm.pushlvalue( val ); - vm.call( 3, 0 ); - } - - public void luaGetTable(VM vm, LValue table, LValue key) { - vm.pushlvalue( this ); - vm.pushlvalue( table ); - vm.pushlvalue( key ); - vm.call( 2, 1 ); - } - - public int luaGetType() { - return Lua.LUA_TFUNCTION; - } - -} diff --git a/src/sample/org/luaj/sample/LuaRunner.java b/src/sample/org/luaj/sample/LuaRunner.java new file mode 100644 index 00000000..00941d0c --- /dev/null +++ b/src/sample/org/luaj/sample/LuaRunner.java @@ -0,0 +1,45 @@ +package org.luaj.sample; + +import java.io.IOException; +import java.io.InputStream; + +import org.luaj.lib.MathLib; +import org.luaj.lib.j2se.LuajavaLib; +import org.luaj.vm.LClosure; +import org.luaj.vm.LPrototype; +import org.luaj.vm.LValue; +import org.luaj.vm.LoadState; +import org.luaj.vm.LuaState; + + +/** + * Program to run a lua chunk + * + * @author jim_roseborough + */ +public class LuaRunner { + + public static void main( String[] args ) throws IOException { + + // new lua state + LuaState state = new LuaState(); + + // get script name + String script = (args.length>0? args[0]: "/test2.luac"); + System.out.println("loading '"+script+"'"); + + // add LuaCompat bindings + MathLib.install(state._G); + LuajavaLib.install(state._G); + + // load the file + InputStream is = LuaRunner.class.getResourceAsStream( script ); + LPrototype p = LoadState.undump(state, is, script); + + // create closure and execute + LClosure c = new LClosure( p, state._G ); + + // do the call + state.doCall( c, new LValue[0] ); + } +} diff --git a/src/test/java/LuaJavaAppRunner.java b/src/sample/org/luaj/sample/LuajavaRunner.java similarity index 51% rename from src/test/java/LuaJavaAppRunner.java rename to src/sample/org/luaj/sample/LuajavaRunner.java index 13202b8f..1fcd82a7 100644 --- a/src/test/java/LuaJavaAppRunner.java +++ b/src/sample/org/luaj/sample/LuajavaRunner.java @@ -1,15 +1,16 @@ +package org.luaj.sample; import java.io.IOException; import java.io.InputStream; -import lua.StackState; -import lua.addon.luacompat.LuaCompat; -import lua.addon.luajava.LuaJava; -import lua.io.Closure; -import lua.io.LoadState; -import lua.io.Proto; -import lua.value.LString; -import lua.value.LValue; +import org.luaj.lib.MathLib; +import org.luaj.lib.j2se.LuajavaLib; +import org.luaj.vm.LClosure; +import org.luaj.vm.LPrototype; +import org.luaj.vm.LValue; +import org.luaj.vm.LoadState; +import org.luaj.vm.LuaState; + /** * Program to run a compiled lua chunk for test purposes, @@ -17,29 +18,29 @@ import lua.value.LValue; * * @author jim_roseborough */ -public class LuaJavaAppRunner { +public class LuajavaRunner { public static void main( String[] args ) throws IOException { + // new lua state + LuaState state = new LuaState(); + // add LuaCompat bindings - LuaCompat.install(); + MathLib.install(state._G); // add LuaJava bindings - LuaJava.install(); + LuajavaLib.install(state._G); // get script name String script = (args.length>0? args[0]: "/swingapp.luac"); System.out.println("loading '"+script+"'"); - // new lua state - StackState state = new StackState(); - // load the file - InputStream is = LuaJavaAppRunner.class.getResourceAsStream( script ); - Proto p = LoadState.undump(state, is, script); + InputStream is = LuajavaRunner.class.getResourceAsStream( script ); + LPrototype p = LoadState.undump(state, is, script); // create closure and execute - Closure c = new Closure( state, p ); + LClosure c = new LClosure( p, state._G ); state.doCall(c, new LValue[0]); } diff --git a/src/test/java/SampleClass.java b/src/sample/org/luaj/sample/SampleClass.java similarity index 90% rename from src/test/java/SampleClass.java rename to src/sample/org/luaj/sample/SampleClass.java index 4c7c67be..d38f480a 100644 --- a/src/test/java/SampleClass.java +++ b/src/sample/org/luaj/sample/SampleClass.java @@ -1,3 +1,4 @@ +package org.luaj.sample; public class SampleClass { diff --git a/src/test/java/LuacRunner.java b/src/test/java/LuacRunner.java deleted file mode 100644 index 3a6c7536..00000000 --- a/src/test/java/LuacRunner.java +++ /dev/null @@ -1,46 +0,0 @@ - -import java.io.IOException; -import java.io.InputStream; - -import lua.StackState; -import lua.VM; -import lua.addon.luacompat.LuaCompat; -import lua.addon.luajava.LuaJava; -import lua.debug.DebugStackState; -import lua.io.Closure; -import lua.io.LoadState; -import lua.io.Proto; -import lua.value.LValue; - -/** - * Program to run a compiled lua chunk for test purposes - * - * @author jim_roseborough - */ -public class LuacRunner { - - public static void main( String[] args ) throws IOException { - - // get script name - String script = (args.length>0? args[0]: "/test2.luac"); - System.out.println("loading '"+script+"'"); - - // add LuaCompat bindings - LuaCompat.install(); - LuaJava.install(); - - // new lua state - StackState state = new StackState(); - VM vm = state; - - // load the file - InputStream is = LuacRunner.class.getResourceAsStream( script ); - Proto p = LoadState.undump(state, is, script); - - // create closure and execute - Closure c = new Closure( state, p ); - - // do the call - vm.doCall( c, new LValue[0] ); - } -} diff --git a/src/test/java/org/luaj/AllTests.java b/src/test/java/org/luaj/AllTests.java new file mode 100644 index 00000000..3a9241dd --- /dev/null +++ b/src/test/java/org/luaj/AllTests.java @@ -0,0 +1,42 @@ +package org.luaj; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class AllTests { + + public static Test suite() { + TestSuite suite = new TestSuite("Test for org.luaj"); + + // compiler tests + TestSuite compiler = new TestSuite("Compiler"); + compiler.addTestSuite(org.luaj.compiler.SimpleTests.class); + compiler.addTestSuite(org.luaj.compiler.RegressionTests.class); + compiler.addTestSuite(org.luaj.compiler.CompilerUnitTests.class); + suite.addTest(compiler); + + // debug tests + TestSuite debug = new TestSuite("Debug"); + debug.addTestSuite(org.luaj.debug.DebugEventTest.class); + debug.addTestSuite(org.luaj.debug.DebugRequestTest.class); + debug.addTestSuite(org.luaj.debug.DebugResponseTest.class); + debug.addTestSuite(org.luaj.debug.DebugStackStateTest.class); + debug.addTestSuite(org.luaj.debug.EnumTypeTest.class); + debug.addTestSuite(org.luaj.debug.StackFrameTest.class); + debug.addTestSuite(org.luaj.debug.TableVariableTest.class); + debug.addTestSuite(org.luaj.debug.VariableTest.class); + debug.addTestSuite(org.luaj.debug.j2se.LuaJVMTest.class); + suite.addTest(debug); + + // debug tests + TestSuite vm = new TestSuite("VM"); + vm.addTestSuite(org.luaj.vm.LoadStateTest.class); + vm.addTestSuite(org.luaj.vm.LStringTest.class); + vm.addTestSuite(org.luaj.vm.LTableTest.class); + vm.addTestSuite(org.luaj.vm.LuaJTest.class); + suite.addTest(vm); + + return suite; + } + +} diff --git a/src/test/java/lua/addon/compile/AbstractUnitTests.java b/src/test/java/org/luaj/compiler/AbstractUnitTests.java similarity index 78% rename from src/test/java/lua/addon/compile/AbstractUnitTests.java rename to src/test/java/org/luaj/compiler/AbstractUnitTests.java index 189c80e9..ac7a5bf2 100644 --- a/src/test/java/lua/addon/compile/AbstractUnitTests.java +++ b/src/test/java/org/luaj/compiler/AbstractUnitTests.java @@ -1,4 +1,4 @@ -package lua.addon.compile; +package org.luaj.compiler; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -9,14 +9,15 @@ import java.io.PrintStream; import java.io.Reader; import java.net.URL; +import org.luaj.compiler.Compiler; +import org.luaj.compiler.DumpState; +import org.luaj.debug.Print; +import org.luaj.vm.LoadState; +import org.luaj.vm.LPrototype; +import org.luaj.vm.LuaState; + import junit.framework.TestCase; -import lua.Print; -import lua.StackState; -import lua.addon.compile.Compiler; -import lua.addon.compile.DumpState; -import lua.io.LoadState; -import lua.io.Proto; abstract public class AbstractUnitTests extends TestCase { @@ -37,12 +38,12 @@ public class AbstractUnitTests extends TestCase { // compile in memory InputStream is = new ByteArrayInputStream( lua ); - Proto p = Compiler.compile(is, dir+"/"+file); + LPrototype p = Compiler.compile(is, dir+"/"+file); String actual = protoToString( p ); // load expected value from jar byte[] luac = bytesFromJar( path + "c" ); - Proto e = loadFromBytes( luac, file ); + LPrototype e = loadFromBytes( luac, file ); String expected = protoToString( e ); // compare results @@ -54,7 +55,7 @@ public class AbstractUnitTests extends TestCase { byte[] dumped = baos.toByteArray(); // re-undump - Proto p2 = loadFromBytes( dumped, file ); + LPrototype p2 = loadFromBytes( dumped, file ); String actual2 = protoToString( p2 ); // compare again @@ -77,13 +78,13 @@ public class AbstractUnitTests extends TestCase { return baos.toByteArray(); } - protected Proto loadFromBytes(byte[] bytes, String script) throws IOException { - StackState state = new StackState(); + protected LPrototype loadFromBytes(byte[] bytes, String script) throws IOException { + LuaState state = new LuaState(); InputStream is = new ByteArrayInputStream( bytes ); return LoadState.undump(state, is, script); } - protected String protoToString(Proto p) { + protected String protoToString(LPrototype p) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream( baos ); Print.ps = ps; diff --git a/src/test/java/lua/addon/compile/CompilerUnitTests.java b/src/test/java/org/luaj/compiler/CompilerUnitTests.java similarity index 98% rename from src/test/java/lua/addon/compile/CompilerUnitTests.java rename to src/test/java/org/luaj/compiler/CompilerUnitTests.java index cdd31e52..e4d323a1 100644 --- a/src/test/java/lua/addon/compile/CompilerUnitTests.java +++ b/src/test/java/org/luaj/compiler/CompilerUnitTests.java @@ -1,4 +1,4 @@ -package lua.addon.compile; +package org.luaj.compiler; public class CompilerUnitTests extends AbstractUnitTests { diff --git a/src/test/java/lua/addon/compile/RegressionTests.java b/src/test/java/org/luaj/compiler/RegressionTests.java similarity index 97% rename from src/test/java/lua/addon/compile/RegressionTests.java rename to src/test/java/org/luaj/compiler/RegressionTests.java index 2cdb2674..c6018389 100644 --- a/src/test/java/lua/addon/compile/RegressionTests.java +++ b/src/test/java/org/luaj/compiler/RegressionTests.java @@ -1,4 +1,4 @@ -package lua.addon.compile; +package org.luaj.compiler; /** * Framework to add regression tests as problem areas are found. diff --git a/src/test/java/lua/addon/compile/SimpleTests.java b/src/test/java/org/luaj/compiler/SimpleTests.java similarity index 79% rename from src/test/java/lua/addon/compile/SimpleTests.java rename to src/test/java/org/luaj/compiler/SimpleTests.java index 4e3825f9..e6c6f35a 100644 --- a/src/test/java/lua/addon/compile/SimpleTests.java +++ b/src/test/java/org/luaj/compiler/SimpleTests.java @@ -1,28 +1,30 @@ -package lua.addon.compile; +package org.luaj.compiler; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import org.luaj.compiler.Compiler; +import org.luaj.debug.Print; +import org.luaj.vm.LClosure; +import org.luaj.vm.LValue; +import org.luaj.vm.LPrototype; +import org.luaj.vm.LuaState; + import junit.framework.TestCase; -import lua.Print; -import lua.StackState; -import lua.io.Closure; -import lua.io.Proto; -import lua.value.LValue; public class SimpleTests extends TestCase { private void doTest( String script ) { try { InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") ); - Proto p = Compiler.compile( is, "script" ); + LPrototype p = Compiler.compile( is, "script" ); assertNotNull( p ); Print.printCode( p ); // try running the code! - StackState state = new StackState(); - Closure c = new Closure( state, p ); + LuaState state = new LuaState(); + LClosure c = new LClosure( state, p ); state.doCall( c, new LValue[0] ); } catch ( Exception e ) { fail("i/o exception: "+e ); diff --git a/src/test/java/lua/debug/DebugEventTest.java b/src/test/java/org/luaj/debug/DebugEventTest.java similarity index 83% rename from src/test/java/lua/debug/DebugEventTest.java rename to src/test/java/org/luaj/debug/DebugEventTest.java index 229a867f..88ff8d15 100644 --- a/src/test/java/lua/debug/DebugEventTest.java +++ b/src/test/java/org/luaj/debug/DebugEventTest.java @@ -1,11 +1,13 @@ -package lua.debug; +package org.luaj.debug; import java.io.IOException; +import org.luaj.debug.SerializationHelper; +import org.luaj.debug.event.DebugEvent; +import org.luaj.debug.event.DebugEventBreakpoint; +import org.luaj.debug.event.DebugEventType; + import junit.framework.TestCase; -import lua.debug.event.DebugEvent; -import lua.debug.event.DebugEventBreakpoint; -import lua.debug.event.DebugEventType; public class DebugEventTest extends TestCase { public void testDebugEventSerialization() { diff --git a/src/test/java/lua/debug/DebugRequestTest.java b/src/test/java/org/luaj/debug/DebugRequestTest.java similarity index 87% rename from src/test/java/lua/debug/DebugRequestTest.java rename to src/test/java/org/luaj/debug/DebugRequestTest.java index c79ce118..217c85d4 100644 --- a/src/test/java/lua/debug/DebugRequestTest.java +++ b/src/test/java/org/luaj/debug/DebugRequestTest.java @@ -1,12 +1,14 @@ -package lua.debug; +package org.luaj.debug; import java.io.IOException; +import org.luaj.debug.SerializationHelper; +import org.luaj.debug.request.DebugRequest; +import org.luaj.debug.request.DebugRequestLineBreakpointToggle; +import org.luaj.debug.request.DebugRequestStack; +import org.luaj.debug.request.DebugRequestType; + import junit.framework.TestCase; -import lua.debug.request.DebugRequest; -import lua.debug.request.DebugRequestLineBreakpointToggle; -import lua.debug.request.DebugRequestStack; -import lua.debug.request.DebugRequestType; public class DebugRequestTest extends TestCase { public void testDebugRequestSerialization() { diff --git a/src/test/java/lua/debug/DebugResponseTest.java b/src/test/java/org/luaj/debug/DebugResponseTest.java similarity index 85% rename from src/test/java/lua/debug/DebugResponseTest.java rename to src/test/java/org/luaj/debug/DebugResponseTest.java index b57b3946..e89eb368 100644 --- a/src/test/java/lua/debug/DebugResponseTest.java +++ b/src/test/java/org/luaj/debug/DebugResponseTest.java @@ -1,12 +1,17 @@ -package lua.debug; +package org.luaj.debug; import java.io.IOException; +import org.luaj.debug.SerializationHelper; +import org.luaj.debug.StackFrame; +import org.luaj.debug.TableVariable; +import org.luaj.debug.Variable; +import org.luaj.debug.response.DebugResponseCallgraph; +import org.luaj.debug.response.DebugResponseSimple; +import org.luaj.debug.response.DebugResponseStack; +import org.luaj.vm.Lua; + import junit.framework.TestCase; -import lua.Lua; -import lua.debug.response.DebugResponseCallgraph; -import lua.debug.response.DebugResponseSimple; -import lua.debug.response.DebugResponseVariables; public class DebugResponseTest extends TestCase { public void testDebugResponseSimpleSerialization() { @@ -46,10 +51,10 @@ public class DebugResponseTest extends TestCase { private void doTestDebugResponseStackSerialization(Variable[] variables) throws IOException { - DebugResponseVariables stackIn = new DebugResponseVariables(variables); + DebugResponseStack stackIn = new DebugResponseStack(variables); byte[] data = SerializationHelper.serialize(stackIn); - DebugResponseVariables stackOut - = (DebugResponseVariables) SerializationHelper.deserialize(data); + DebugResponseStack stackOut + = (DebugResponseStack) SerializationHelper.deserialize(data); Variable[] variablesIn = stackIn.getVariables(); Variable[] variablesOut = stackOut.getVariables(); assertNotNull(variablesIn); diff --git a/src/test/java/lua/debug/DebugStackStateTest.java b/src/test/java/org/luaj/debug/DebugStackStateTest.java similarity index 89% rename from src/test/java/lua/debug/DebugStackStateTest.java rename to src/test/java/org/luaj/debug/DebugStackStateTest.java index 424d6e44..7667dcbe 100644 --- a/src/test/java/lua/debug/DebugStackStateTest.java +++ b/src/test/java/org/luaj/debug/DebugStackStateTest.java @@ -19,16 +19,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug; +package org.luaj.debug; import java.io.IOException; import java.io.InputStream; +import org.luaj.debug.DebugStackState; +import org.luaj.vm.LClosure; +import org.luaj.vm.LValue; +import org.luaj.vm.LoadState; +import org.luaj.vm.LPrototype; + import junit.framework.TestCase; -import lua.io.Closure; -import lua.io.LoadState; -import lua.io.Proto; -import lua.value.LValue; public class DebugStackStateTest extends TestCase { @@ -38,10 +40,10 @@ public class DebugStackStateTest extends TestCase { // set up the vm final DebugStackState state = new DebugStackState(); InputStream is = getClass().getResourceAsStream( script ); - Proto p = LoadState.undump(state, is, script); + LPrototype p = LoadState.undump(state, is, script); // create closure and execute - final Closure c = new Closure( state, p ); + final LClosure c = new LClosure( state, p ); // suspend the vm right away state.suspend(); diff --git a/src/test/java/lua/debug/EnumTypeTest.java b/src/test/java/org/luaj/debug/EnumTypeTest.java similarity index 83% rename from src/test/java/lua/debug/EnumTypeTest.java rename to src/test/java/org/luaj/debug/EnumTypeTest.java index 07613ddd..81513c21 100644 --- a/src/test/java/lua/debug/EnumTypeTest.java +++ b/src/test/java/org/luaj/debug/EnumTypeTest.java @@ -1,8 +1,10 @@ -package lua.debug; +package org.luaj.debug; + +import org.luaj.debug.SerializationHelper; +import org.luaj.debug.event.DebugEventType; +import org.luaj.debug.request.DebugRequestType; import junit.framework.TestCase; -import lua.debug.event.DebugEventType; -import lua.debug.request.DebugRequestType; public class EnumTypeTest extends TestCase { public void testDebugRequestTypeSerialization() { diff --git a/src/test/java/lua/debug/StackFrameTest.java b/src/test/java/org/luaj/debug/StackFrameTest.java similarity index 82% rename from src/test/java/lua/debug/StackFrameTest.java rename to src/test/java/org/luaj/debug/StackFrameTest.java index f4da3ff7..61a9d9fa 100644 --- a/src/test/java/lua/debug/StackFrameTest.java +++ b/src/test/java/org/luaj/debug/StackFrameTest.java @@ -1,7 +1,10 @@ -package lua.debug; +package org.luaj.debug; import java.io.IOException; +import org.luaj.debug.SerializationHelper; +import org.luaj.debug.StackFrame; + import junit.framework.TestCase; public class StackFrameTest extends TestCase { diff --git a/src/test/java/lua/debug/TableVariableTest.java b/src/test/java/org/luaj/debug/TableVariableTest.java similarity index 93% rename from src/test/java/lua/debug/TableVariableTest.java rename to src/test/java/org/luaj/debug/TableVariableTest.java index a3069174..6f400229 100644 --- a/src/test/java/lua/debug/TableVariableTest.java +++ b/src/test/java/org/luaj/debug/TableVariableTest.java @@ -1,15 +1,18 @@ -package lua.debug; +package org.luaj.debug; import java.io.IOException; +import org.luaj.debug.SerializationHelper; +import org.luaj.debug.TableVariable; +import org.luaj.vm.LBoolean; +import org.luaj.vm.LDouble; +import org.luaj.vm.LInteger; +import org.luaj.vm.LNil; +import org.luaj.vm.LString; +import org.luaj.vm.LTable; +import org.luaj.vm.Lua; + import junit.framework.TestCase; -import lua.Lua; -import lua.value.LBoolean; -import lua.value.LDouble; -import lua.value.LInteger; -import lua.value.LNil; -import lua.value.LString; -import lua.value.LTable; public class TableVariableTest extends TestCase { public void testCreate() { diff --git a/src/test/java/lua/debug/VariableTest.java b/src/test/java/org/luaj/debug/VariableTest.java similarity index 84% rename from src/test/java/lua/debug/VariableTest.java rename to src/test/java/org/luaj/debug/VariableTest.java index a34c3be2..71a57fc0 100644 --- a/src/test/java/lua/debug/VariableTest.java +++ b/src/test/java/org/luaj/debug/VariableTest.java @@ -1,9 +1,12 @@ -package lua.debug; +package org.luaj.debug; import java.io.IOException; +import org.luaj.debug.SerializationHelper; +import org.luaj.debug.Variable; +import org.luaj.vm.Lua; + import junit.framework.TestCase; -import lua.Lua; public class VariableTest extends TestCase { public void testSerialization() { diff --git a/src/test/java/lua/debug/j2se/LuaJVMTest.java b/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java similarity index 97% rename from src/test/java/lua/debug/j2se/LuaJVMTest.java rename to src/test/java/org/luaj/debug/j2se/LuaJVMTest.java index 0329061e..f3ce59e4 100644 --- a/src/test/java/lua/debug/j2se/LuaJVMTest.java +++ b/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java @@ -19,14 +19,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ******************************************************************************/ -package lua.debug.j2se; +package org.luaj.debug.j2se; import java.io.IOException; import java.net.URL; +import org.luaj.debug.j2se.StandardLuaJVM; +import org.luaj.debug.j2se.StandardLuaJVM.ParseException; + import junit.framework.TestCase; -import lua.debug.j2se.StandardLuaJVM; -import lua.debug.j2se.StandardLuaJVM.ParseException; /** * Sanity test for StandardLuaJVM. diff --git a/src/test/java/lua/value/LStringTest.java b/src/test/java/org/luaj/vm/LStringTest.java similarity index 97% rename from src/test/java/lua/value/LStringTest.java rename to src/test/java/org/luaj/vm/LStringTest.java index 3bd07c01..2e5b4a16 100644 --- a/src/test/java/lua/value/LStringTest.java +++ b/src/test/java/org/luaj/vm/LStringTest.java @@ -1,8 +1,10 @@ -package lua.value; +package org.luaj.vm; import java.io.IOException; import java.io.InputStream; +import org.luaj.vm.LString; + import junit.framework.TestCase; public class LStringTest extends TestCase { diff --git a/src/test/java/lua/value/LTableTest.java b/src/test/java/org/luaj/vm/LTableTest.java similarity index 94% rename from src/test/java/lua/value/LTableTest.java rename to src/test/java/org/luaj/vm/LTableTest.java index 9e6bd892..6f2080b3 100644 --- a/src/test/java/lua/value/LTableTest.java +++ b/src/test/java/org/luaj/vm/LTableTest.java @@ -1,4 +1,11 @@ -package lua.value; +package org.luaj.vm; + +import org.luaj.vm.LDouble; +import org.luaj.vm.LInteger; +import org.luaj.vm.LNil; +import org.luaj.vm.LString; +import org.luaj.vm.LTable; +import org.luaj.vm.LValue; import junit.framework.TestCase; diff --git a/src/test/java/lua/io/LoadStateTest.java b/src/test/java/org/luaj/vm/LoadStateTest.java similarity index 94% rename from src/test/java/lua/io/LoadStateTest.java rename to src/test/java/org/luaj/vm/LoadStateTest.java index fac3afc6..4ab6788c 100644 --- a/src/test/java/lua/io/LoadStateTest.java +++ b/src/test/java/org/luaj/vm/LoadStateTest.java @@ -1,11 +1,13 @@ -package lua.io; +package org.luaj.vm; import java.util.Random; +import org.luaj.vm.LDouble; +import org.luaj.vm.LInteger; +import org.luaj.vm.LNumber; +import org.luaj.vm.LoadState; + import junit.framework.TestCase; -import lua.value.LDouble; -import lua.value.LInteger; -import lua.value.LNumber; public class LoadStateTest extends TestCase { double[] DOUBLE_VALUES = { diff --git a/src/test/java/lua/LuaJTest.java b/src/test/java/org/luaj/vm/LuaJTest.java similarity index 90% rename from src/test/java/lua/LuaJTest.java rename to src/test/java/org/luaj/vm/LuaJTest.java index c1ec49cc..ff1666a4 100644 --- a/src/test/java/lua/LuaJTest.java +++ b/src/test/java/org/luaj/vm/LuaJTest.java @@ -1,17 +1,14 @@ -package lua; +package org.luaj.vm; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import junit.framework.TestCase; -import lua.addon.luacompat.LuaCompat; -import lua.addon.luajava.LuaJava; -import lua.debug.DebugStackState; -import lua.io.Closure; -import lua.io.LoadState; -import lua.io.Proto; -import lua.value.LValue; + +import org.luaj.debug.DebugStackState; +import org.luaj.lib.MathLib; +import org.luaj.lib.j2se.LuajavaLib; public class LuaJTest extends TestCase { @@ -43,7 +40,7 @@ public class LuaJTest extends TestCase { public void testTest7() throws IOException, InterruptedException { runTest( "test7" ); } - + public void testAutoload() throws IOException, InterruptedException { runTest( "autoload" ); } @@ -111,30 +108,27 @@ public class LuaJTest extends TestCase { public void testUpvalues2() throws IOException, InterruptedException { runTest( "upvalues2" ); } - + private void runTest( String testName ) throws IOException, InterruptedException { - - // Reset the _G table just in case some test mucks with it - GlobalState.resetGlobals(); + + // new lua state + LuaState state = new DebugStackState(); // add LuaJava bindings - LuaJava.install(); + LuajavaLib.install(state._G); // add LuaCompat bindings - LuaCompat.install(); - - // new lua state - StackState state = new StackState(); + MathLib.install(state._G); // load the file - Proto p = loadScriptResource( state, testName ); + LPrototype p = loadScriptResource( state, testName ); // Replace System.out with a ByteArrayOutputStream ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - Builtin.redirectOutput( outputStream ); + BaseLib.redirectOutput( outputStream ); try { // create closure and execute - Closure c = new Closure( state, p ); + LClosure c = new LClosure( p, state._G ); state.doCall(c, new LValue[0]); final String actualOutput = new String( outputStream.toByteArray() ); @@ -142,12 +136,12 @@ public class LuaJTest extends TestCase { assertEquals( expectedOutput, actualOutput ); } finally { - Builtin.restoreStandardOutput(); + BaseLib.restoreStandardOutput(); outputStream.close(); } } - private Proto loadScriptResource( StackState state, String name ) throws IOException { + private LPrototype loadScriptResource( LuaState state, String name ) throws IOException { InputStream script = getClass().getResourceAsStream( "/"+name+".luac" ); if ( script == null ) { script = getClass().getResourceAsStream( "/"+name+".lua" ); diff --git a/src/test/java/lua/StandardTest.java b/src/test/java/org/luaj/vm/StandardTest.java similarity index 80% rename from src/test/java/lua/StandardTest.java rename to src/test/java/org/luaj/vm/StandardTest.java index 728db20b..91b0b1bf 100644 --- a/src/test/java/lua/StandardTest.java +++ b/src/test/java/org/luaj/vm/StandardTest.java @@ -1,4 +1,4 @@ -package lua; +package org.luaj.vm; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -12,16 +12,9 @@ import java.util.zip.ZipInputStream; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; -import lua.Builtin; -import lua.StackState; -import lua.addon.luacompat.LuaCompat; -import lua.debug.DebugStackState; -import lua.io.Closure; -import lua.io.LoadState; -import lua.io.Proto; -import lua.value.LNil; -import lua.value.LString; -import lua.value.LValue; + +import org.luaj.debug.DebugStackState; +import org.luaj.lib.MathLib; public class StandardTest extends TestCase { @@ -37,7 +30,7 @@ public class StandardTest extends TestCase { while ( ( file = testSuiteArchive.getNextEntry() ) != null ) { final String entryName = file.getName(); if ( entryName.endsWith( ".luac" ) ) { - Proto p = LoadState.undump( new StackState(), testSuiteArchive, entryName ); + LPrototype p = LoadState.undump( new LuaState(), testSuiteArchive, entryName ); tests.put( entryName.substring( 0, entryName.length() - 5 ), p ); } else if ( entryName.endsWith( ".out" ) ) { results.put( entryName.substring( 0, entryName.length() - 4 ), readString( testSuiteArchive ) ); @@ -51,7 +44,7 @@ public class StandardTest extends TestCase { for ( Iterator keys = tests.keySet().iterator(); keys.hasNext(); ) { String test = (String)keys.next(); - final Proto code = (Proto)tests.get( test ); + final LPrototype code = (LPrototype)tests.get( test ); final String expectedResult = (String)results.get( test ); if ( code != null && expectedResult != null ) { @@ -62,29 +55,28 @@ public class StandardTest extends TestCase { return suite; } - private final Proto code; + private final LPrototype code; private final String expectedResult; - public StandardTest( String name, Proto code, String expectedResult ) { + public StandardTest( String name, LPrototype code, String expectedResult ) { super( name ); this.code = code; this.expectedResult = expectedResult; } public void runTest() { - GlobalState.resetGlobals(); - LuaCompat.install(); + LuaState state = new DebugStackState(); + MathLib.install(state._G); // hack: it's unpleasant when the test cases fail to terminate; // unfortunately, there is a test in the standard suite that // relies on weak tables having their elements removed by // the garbage collector. Until we implement that, remove the // built-in collectgarbage function. - GlobalState.getGlobalsTable().put( "collectgarbage", LNil.NIL ); - StackState state = new DebugStackState(); - Closure c = new Closure( state, code ); + state._G.put( "collectgarbage", LNil.NIL ); + LClosure c = new LClosure( code, state._G ); ByteArrayOutputStream output = new ByteArrayOutputStream(); - Builtin.redirectOutput( output ); + BaseLib.redirectOutput( output ); try { try { state.doCall( c, new LValue[0] ); @@ -94,7 +86,7 @@ public class StandardTest extends TestCase { for ( int i = 0; i < ncalls; ++i ) { CallInfo call = state.calls[i]; - Proto p = call.closure.p; + LPrototype p = call.closure.p; int line = p.lineinfo[call.pc-1]; String func = call.closure.luaAsString().toJavaString(); stackTrace[ncalls - i - 1] = new StackTraceElement(getName(), func, getName()+".lua", line ); @@ -110,7 +102,7 @@ public class StandardTest extends TestCase { assertEquals( expectedResult, actualResult ); } finally { - Builtin.restoreStandardOutput(); + BaseLib.restoreStandardOutput(); } } diff --git a/src/test/res/test7.lua b/src/test/res/test7.lua index 2df50b50..eec647e7 100644 --- a/src/test/res/test7.lua +++ b/src/test/res/test7.lua @@ -1,7 +1,7 @@ obj = luajava.newInstance("java.lang.Object") print( obj ) -obj = luajava.newInstance("SampleClass") +obj = luajava.newInstance("org.luaj.sample.SampleClass") print( obj ) obj.s = "Hello" print( obj.s ) diff --git a/src/test/res/test7.luac b/src/test/res/test7.luac index 9c44ac41..3fb07c46 100644 Binary files a/src/test/res/test7.luac and b/src/test/res/test7.luac differ