Major refactoring of package names, class names

This commit is contained in:
James Roseborough
2007-11-07 23:56:06 +00:00
parent 2281d056ec
commit ff66779330
99 changed files with 1151 additions and 2739 deletions

View File

@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/addon/java"/>
<classpathentry kind="src" path="src/core"/>
<classpathentry kind="src" path="src/j2se"/>
<classpathentry kind="src" path="src/debug"/>
<classpathentry kind="src" path="src/sample"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="src" path="src/test/res"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>

View File

@@ -15,9 +15,8 @@
<target name="compile">
<mkdir dir="build/all/classes"/>
<javac destdir="build/all/classes" encoding="utf-8" source="1.3" target="1.1">
<src path="src/main/java" />
<src path="src/addon/java" />
<exclude name="lua/debug/j2me/**"/>
<src path="src/core" />
<src path="src/j2se" />
</javac>
</target>
@@ -53,11 +52,10 @@
<mkdir dir="build/core/classes"/>
<javac destdir="build/core/classes" encoding="utf-8" source="1.3" target="1.1"
bootclasspathref="wtk-libs">
<src path="src/main/java"/>
<src path="src/addon/java"/>
<exclude name="lua/debug/j2se/**"/>
<exclude name="lua/debug/j2me/**"/>
<exclude name="lua/addon/luajava/**"/>
<src path="src/core" />
<src path="src/debug" />
<exclude name="org/luaj/debug/j2se/**"/>
<exclude name="org/luaj/debug/j2me/**"/>
</javac>
</target>
@@ -65,10 +63,10 @@
<mkdir dir="build/extras-j2se/classes"/>
<javac destdir="build/extras-j2se/classes" encoding="utf-8" source="1.3" target="1.1"
classpath="build/core/classes">
<src path="src/main/java"/>
<src path="src/addon/java"/>
<include name="lua/debug/j2se/**"/>
<include name="lua/addon/luajava/**"/>
<src path="src/debug" />
<src path="src/j2se" />
<include name="org/luaj/debug/j2se/**"/>
<include name="org/luaj/lib/j2se/**"/>
</javac>
</target>
@@ -76,9 +74,8 @@
<mkdir dir="build/extras-j2me/classes"/>
<javac destdir="build/extras-j2me/classes" encoding="utf-8" source="1.3" target="1.1"
bootclasspathref="wtk-libs" classpath="build/core/classes">
<src path="src/main/java"/>
<src path="src/addon/java"/>
<include name="lua/debug/j2me/**"/>
<src path="src/debug" />
<exclude name="org/luaj/debug/j2se/**"/>
</javac>
</target>

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -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<n; i++ )
dumpInt( f.code[i] );
}
void dumpConstants(final Proto f) throws IOException {
void dumpConstants(final LPrototype f) throws IOException {
int i, n = f.k.length;
dumpInt(n);
for (i = 0; i < n; i++) {
@@ -119,7 +120,7 @@ public class DumpState {
dumpFunction(f.p[i], f.source);
}
void dumpDebug(final Proto f) throws IOException {
void dumpDebug(final LPrototype f) throws IOException {
int i, n;
n = (strip) ? 0 : f.lineinfo.length;
dumpInt(n);
@@ -138,7 +139,7 @@ public class DumpState {
dumpString(f.upvalues[i]);
}
void dumpFunction(final Proto f, final LString string) throws IOException {
void dumpFunction(final LPrototype f, final LString string) throws IOException {
if ( f.source == null || f.source.equals(string) || strip )
dumpInt(0);
else
@@ -169,14 +170,14 @@ public class DumpState {
/*
** dump Lua function as precompiled chunk
*/
public static int dump( Proto f, OutputStream w, boolean strip ) throws IOException {
public static int dump( LPrototype f, OutputStream w, boolean strip ) throws IOException {
DumpState D = new DumpState(w,strip);
D.dumpHeader();
D.dumpFunction(f,null);
return D.status;
}
public static int dump(Proto f, OutputStream w, boolean strip, boolean intonly, boolean littleendian) throws IOException {
public static int dump(LPrototype f, OutputStream w, boolean strip, boolean intonly, boolean littleendian) throws IOException {
DumpState D = new DumpState(w,strip);
D.IS_LITTLE_ENDIAN = littleendian;
D.IS_NUMBER_INTEGRAL = intonly;

View File

@@ -1,19 +1,20 @@
package lua.addon.compile;
package org.luaj.compiler;
import java.util.Hashtable;
import lua.Lua;
import lua.addon.compile.LexState.ConsControl;
import lua.addon.compile.LexState.expdesc;
import lua.io.LocVars;
import lua.io.Proto;
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;
import org.luaj.compiler.LexState.ConsControl;
import org.luaj.compiler.LexState.expdesc;
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.LValue;
import org.luaj.vm.LocVars;
import org.luaj.vm.Lua;
import org.luaj.vm.LPrototype;
public class FuncState extends LuaC {
class upvaldesc {
@@ -29,7 +30,7 @@ public class FuncState extends LuaC {
boolean isbreakable; /* true if `block' is a loop */
};
Proto f; /* current function header */
LPrototype f; /* current function header */
// LTable h; /* table to find (and reuse) elements in `k' */
Hashtable htable; /* table to find (and reuse) elements in `k' */
FuncState prev; /* enclosing function */
@@ -418,7 +419,7 @@ public class FuncState extends LuaC {
} else {
idx = this.nk;
this.htable.put(v, new Integer(idx));
final Proto f = this.f;
final LPrototype f = this.f;
if (f.k == null || nk + 1 >= 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)

View File

@@ -1,5 +1,5 @@
package lua.addon.compile;
package org.luaj.compiler;
class InstructionPtr {
final int[] code;

View File

@@ -1,4 +1,4 @@
package lua.addon.compile;
package org.luaj.compiler;
public class IntPtr {
int i;

View File

@@ -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? */

View File

@@ -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;

View File

@@ -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

View File

@@ -1,6 +1,6 @@
package lua.addon.luacompat;
package org.luaj.lib;
import lua.value.LString;
import org.luaj.vm.LString;
public class LBuffer {

View File

@@ -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<names.length; i++ )
table.put( names[i], new LuaCompat(indexBase+i) );
table.put( names[i], new MathLib(indexBase+i) );
}
public static final String[] GLOBAL_NAMES = {
@@ -183,22 +182,22 @@ public class LuaCompat extends LFunction {
private final int id;
private LuaCompat( int id ) {
private MathLib( int id ) {
this.id = id;
}
private static void setResult( VM vm, LValue value ) {
private static void setResult( LuaState vm, LValue value ) {
vm.settop(0);
vm.pushlvalue( value );
}
private static void setErrorResult( VM vm, String message ) {
private static void setErrorResult( LuaState vm, String message ) {
vm.settop(0);
vm.pushnil();
vm.pushstring( message );
}
public boolean luaStackCall( VM vm ) {
public boolean luaStackCall( LuaState vm ) {
switch ( id ) {
case LOADFILE:
loadfile(vm, vm.tostring(2));
@@ -226,7 +225,7 @@ public class LuaCompat extends LFunction {
}
} break;
case SETFENV:
setfenv( (StackState) vm );
setfenv( (LuaState) vm );
break;
case SELECT:
select( vm );
@@ -291,46 +290,46 @@ public class LuaCompat extends LFunction {
// String functions
case BYTE:
StrLib.byte_( vm );
StringLib.byte_( vm );
break;
case CHAR:
StrLib.char_( vm );
StringLib.char_( vm );
break;
case DUMP:
StrLib.dump( vm );
StringLib.dump( vm );
break;
case FIND:
StrLib.find( vm );
StringLib.find( vm );
break;
case FORMAT:
StrLib.format( vm );
StringLib.format( vm );
break;
case GMATCH:
StrLib.gmatch( vm );
StringLib.gmatch( vm );
break;
case GSUB:
StrLib.gsub( vm );
StringLib.gsub( vm );
break;
case LEN:
StrLib.len( vm );
StringLib.len( vm );
break;
case LOWER:
StrLib.lower( vm );
StringLib.lower( vm );
break;
case MATCH:
StrLib.match( vm );
StringLib.match( vm );
break;
case REP:
StrLib.rep( vm );
StringLib.rep( vm );
break;
case REVERSE:
StrLib.reverse( vm );
StringLib.reverse( vm );
break;
case SUB:
StrLib.sub( vm );
StringLib.sub( vm );
break;
case UPPER:
StrLib.upper( vm );
StringLib.upper( vm );
break;
// package functions
@@ -364,7 +363,7 @@ public class LuaCompat extends LFunction {
return false;
}
private void select( VM vm ) {
private void select( LuaState vm ) {
LValue arg = vm.topointer(2);
if ( arg instanceof LNumber ) {
final int start = Math.min(arg.toJavaInt(),vm.gettop());
@@ -389,7 +388,7 @@ public class LuaCompat extends LFunction {
return rhs.luaBinCmpUnknown( Lua.OP_LT, lhs ) ? lhs: rhs;
}
private void modf( VM vm ) {
private void modf( LuaState vm ) {
LValue arg = vm.topointer(2);
double v = arg.toJavaDouble();
double intPart = ( v > 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 );

View File

@@ -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<nargs; i++ )
@@ -66,7 +67,7 @@ public class StrLib {
*
* TODO: port dumping code as optional add-on
*/
static void dump( VM vm ) {
static void dump( LuaState vm ) {
vm.error("dump() not supported");
}
@@ -86,7 +87,7 @@ public class StrLib {
* If the pattern has captures, then in a successful match the captured values
* are also returned, after the two indices.
*/
static void find( VM vm ) {
static void find( LuaState vm ) {
str_find_aux( vm, true );
}
@@ -113,7 +114,7 @@ public class StrLib {
* This function does not accept string values containing embedded zeros,
* except as arguments to the q option.
*/
static void format( VM vm ) {
static void format( LuaState vm ) {
vm.settop(0);
vm.pushstring( "" );
}
@@ -142,7 +143,7 @@ public class StrLib {
* For this function, a '^' at the start of a pattern does not work as an anchor,
* as this would prevent the iteration.
*/
static void gmatch( VM vm ) {
static void gmatch( LuaState vm ) {
vm.settop(0);
vm.pushlvalue( new GMatchAux(vm) );
}
@@ -152,14 +153,14 @@ public class StrLib {
private final int srclen;
private final MatchState ms;
private int soffset;
public GMatchAux(VM vm) {
public GMatchAux(LuaState vm) {
this.src = vm.tolstring(2);
this.pat = vm.tolstring(3);
this.srclen = src.length();
this.ms = new MatchState(vm, src, pat);
this.soffset = 0;
}
public boolean luaStackCall(VM vm) {
public boolean luaStackCall(LuaState vm) {
vm.settop(0);
for ( ; soffset<srclen; soffset++ ) {
int res = ms.match(soffset, 0);
@@ -221,7 +222,7 @@ public class StrLib {
* x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
* --> 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;

View File

@@ -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<NAMES.length; i++ )
table.put( NAMES[i], new Builtin(i) );
globals.put( NAMES[i], new BaseLib(i) );
}
private static final String[] NAMES = {
@@ -40,7 +39,7 @@ final class Builtin extends JavaFunction {
private static PrintStream stdout = System.out;
private int id;
private Builtin( int id ) {
private BaseLib( int id ) {
this.id = id;
}
@@ -52,7 +51,7 @@ final class Builtin extends JavaFunction {
/**
* Invoke a builtin
*/
public int invoke(VM vm) {
public int invoke(LuaState vm) {
switch ( id ) {
case PRINT: {
int n = vm.gettop();
@@ -110,11 +109,11 @@ final class Builtin extends JavaFunction {
}
}
static void redirectOutput( OutputStream newStdOut ) {
public static void redirectOutput( OutputStream newStdOut ) {
stdout = new PrintStream( newStdOut );
}
static void restoreStandardOutput() {
public static void restoreStandardOutput() {
stdout = System.out;
}

View File

@@ -1,17 +1,16 @@
package lua;
package org.luaj.vm;
import lua.io.Closure;
public class CallInfo {
public Closure closure;
public LClosure closure;
public int base;
public int top;
public int pc;
public int resultbase;
public int nresults;
public CallInfo(Closure c, int base, int top, int resultoff, int nresults) {
public CallInfo(LClosure c, int base, int top, int resultoff, int nresults) {
this.closure = c;
this.base = base;
this.top = top;

View File

@@ -1,6 +1,5 @@
package lua.value;
package org.luaj.vm;
import lua.Lua;
public final class LBoolean extends LValue {

View File

@@ -1,13 +1,10 @@
package lua.io;
package org.luaj.vm;
import lua.StackState;
import lua.VM;
import lua.value.LFunction;
import lua.value.LTable;
public class Closure extends LFunction {
public class LClosure extends LFunction {
public LTable env;
public Proto p;
public LPrototype p;
public UpVal[] upVals;
/**
@@ -15,7 +12,7 @@ public class Closure extends LFunction {
* @param state
* @param p
*/
public Closure(StackState state, Proto p) {
public LClosure(LuaState state, LPrototype p) {
this.env = state._G;
this.p = p;
upVals = new UpVal[p.nups];
@@ -26,7 +23,7 @@ public class Closure extends LFunction {
* @param p
* @param env
*/
public Closure(Proto p, LTable env) {
public LClosure(LPrototype p, LTable env) {
this.p = p;
this.env = env;
upVals = new UpVal[p.nups];
@@ -35,7 +32,7 @@ public class Closure extends LFunction {
// called by vm when there is an OP_CALL
// in this case, we are on the stack,
// and simply need to cue the VM to treat it as a stack call
public boolean luaStackCall(VM vm) {
public boolean luaStackCall(LuaState vm) {
vm.prepStackCall();
return true;
}

View File

@@ -1,6 +1,5 @@
package lua.value;
package org.luaj.vm;
import lua.Lua;
public class LDouble extends LNumber {

View File

@@ -0,0 +1,59 @@
package org.luaj.vm;
abstract
public class LFunction extends LValue {
public String toJavaString() {
return "function: "+hashCode();
}
public void luaSetTable(LuaState 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(LuaState 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;
}
/**
* Set up a Java invocation, and leave the results on the stack
* starting at base. The default implementation for LFunction
* delegates to the VM which provides convenience.
*/
public boolean luaStackCall(LuaState vm) {
vm.invokeJavaFunction( this );
return false;
}
/**
* 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.
*
* Arguments to the function will be in position 1-n.
* Return values can be pushed onto the stack, and will be
* copied down to the appropriate location by the calling LuaState.
*
*
* @param lua the LuaState calling this function.
* @return number of results pushed onto the stack.
*/
public int invoke( LuaState lua ) {
return 0;
}
}

View File

@@ -1,6 +1,5 @@
package lua.value;
package org.luaj.vm;
import lua.Lua;
public class LInteger extends LNumber {
private final int m_value;

View File

@@ -1,6 +1,5 @@
package lua.value;
package org.luaj.vm;
import lua.Lua;
public final class LNil extends LValue {
public static final LNil NIL = new LNil();

View File

@@ -1,6 +1,5 @@
package lua.value;
package org.luaj.vm;
import lua.Lua;
abstract
public class LNumber extends LValue {

View File

@@ -1,21 +1,19 @@
package lua.io;
package org.luaj.vm;
import lua.StackState;
import lua.value.LValue;
import lua.value.LString;
/*
** Function Prototypes
*/
public class Proto {
public Proto() {
public class LPrototype {
public LPrototype() {
}
/* constants used by the function */
public LValue[] k;
public int[] code;
/* functions defined inside the function */
public Proto[] p;
public LPrototype[] p;
/* map from opcodes to source lines */
public int[] lineinfo;
/* information about local variables */

View File

@@ -1,12 +1,10 @@
package lua.value;
package org.luaj.vm;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import lua.Lua;
/**
* A String implementation for Lua using bytes instead of chars.
@@ -386,54 +384,6 @@ public class LString extends LValue {
return h;
}
private static byte[] stringToUtf8Bytes( final String string ) {
final int strlen = string.length();
byte[] bytes = new byte[ strlen ];
byte b1 = 0, b2 = 0, b3 = 0;
int j = 0;
for ( int i = 0; i < strlen; ++i ) {
int c = string.charAt( i );
// TODO: combine 2-character combinations
int count;
if ( c > 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;
}

View File

@@ -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]);

View File

@@ -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 ) {

View File

@@ -1,6 +1,5 @@
package lua.value;
package org.luaj.vm;
import lua.Lua;
public class LUserData extends LValue {

View File

@@ -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 );

View File

@@ -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; i<n; i++ )
@@ -132,7 +125,7 @@ public class LoadState {
f.code = code;
}
void loadConstants(Proto f) throws IOException {
void loadConstants(LPrototype f) throws IOException {
int n = loadInt();
LValue[] values = new LValue[n];
for ( int i=0; i<n; i++ ) {
@@ -156,13 +149,13 @@ public class LoadState {
f.k = values;
n = loadInt();
Proto[] protos = new Proto[n];
LPrototype[] protos = new LPrototype[n];
for ( int i=0; i<n; i++ )
protos[i] = loadFunction(f.source);
f.p = protos;
}
void loadDebug( Proto f ) throws IOException {
void loadDebug( LPrototype f ) throws IOException {
int n = loadInt();
f.lineinfo = new int[n];
for ( int i=0; i<n; i++ )
@@ -184,8 +177,8 @@ public class LoadState {
}
}
public Proto loadFunction(LString p) throws IOException {
Proto f = new Proto();
public LPrototype loadFunction(LString p) throws IOException {
LPrototype f = new LPrototype();
// this.L.push(f);
f.source = loadString();
if ( f.source == null )
@@ -227,10 +220,10 @@ public class LoadState {
luacIsNumberIntegral = (0 != is.readByte());
}
public static Proto undump( VM L, InputStream stream, String name ) throws IOException {
public static LPrototype undump( LuaState L, InputStream stream, String name ) throws IOException {
// is this a source file?
Proto p = lua.addon.compile.Compiler.compile(stream, name);
LPrototype p = org.luaj.compiler.Compiler.compile(stream, name);
if ( p != null )
return p;
@@ -259,7 +252,7 @@ public class LoadState {
}
/** Private constructor for create a load state */
private LoadState( VM L, InputStream stream, String name ) {
private LoadState( LuaState L, InputStream stream, String name ) {
this.L = L;
this.name = name;
this.is = new DataInputStream( stream );

View File

@@ -1,6 +1,5 @@
package lua.io;
package org.luaj.vm;
import lua.value.LString;
public class LocVars {
public LString varname;

View File

@@ -1,25 +1,16 @@
package lua;
package org.luaj.vm;
/**
* Constants for lua limits and opcodes
*
* @author jim_roseborough
*
*
* @deprecated - this class will go away. Constants will probably move to LuaState
*
*/
public class Lua {
// from llimits.h
/** maximum stack for a Lua function */
public static final int MAXSTACK = 250;
/** minimum size for the string table (must be power of 2) */
public static final int MINSTRTABSIZE = 32;
/** minimum size for string buffer */
public static final int LUA_MINBUFFER = 32;
/** use return values from previous op */
public static final int LUA_MULTRET = -1;
@@ -312,50 +303,6 @@ public class Lua {
return 0 != (luaP_opmodes[m] & (1 << 7));
}
/** opcode names */
public 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,
};
// type constants
public static final int LUA_TNONE = (-1);

View File

@@ -1,26 +1,14 @@
package lua;
package org.luaj.vm;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Stack;
import lua.io.Closure;
import lua.io.LoadState;
import lua.io.LocVars;
import lua.io.Proto;
import lua.io.UpVal;
import lua.value.LBoolean;
import lua.value.LDouble;
import lua.value.LInteger;
import lua.value.LNil;
import lua.value.LString;
import lua.value.LTable;
import lua.value.LUserData;
import lua.value.LValue;
public class StackState extends Lua implements VM {
private static final boolean TRACE = (null != System.getProperty("TRACE"));
public class LuaState extends Lua {
/* thread status; 0 is OK */
private static final int LUA_YIELD = 1;
@@ -35,13 +23,13 @@ public class StackState extends Lua implements VM {
public int base = 0;
protected int top = 0;
protected int nresults = -1;
protected LValue[] stack = new LValue[LUA_MINSTACK];
public LValue[] stack = new LValue[LUA_MINSTACK];
public int cc = -1;
protected CallInfo[] calls = new CallInfo[LUA_MINCALLS];
public CallInfo[] calls = new CallInfo[LUA_MINCALLS];
protected Stack upvals = new Stack();
protected JavaFunction panic;
protected LFunction panic;
public LTable _G = GlobalState.getGlobalsTable();
public LTable _G;
// main debug hook, overridden by DebugStackState
protected void debugHooks(int pc) {
@@ -64,13 +52,20 @@ public class StackState extends Lua implements VM {
* second argument, <code>ud</code>, 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<n; i++ )
@@ -116,15 +111,14 @@ public class StackState extends Lua implements VM {
base = (cc>=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;
}

View File

@@ -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;

View File

@@ -1,7 +1,5 @@
package lua.io;
package org.luaj.vm;
import lua.value.LString;
import lua.value.LValue;
public class UpVal {

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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";
}

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -1,4 +1,4 @@
package lua.debug;
package org.luaj.debug;
public class VMException extends RuntimeException {
private static final long serialVersionUID = 7876955153693775429L;

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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;

View File

@@ -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];

View File

@@ -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);
}
}

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -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 {}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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<NAMES.length; i++ )
luajava.put( NAMES[i], new LuaJava(i) );
GlobalState.getGlobalsTable().put( "luajava", luajava );
luajava.put( NAMES[i], new LuajavaLib(i) );
globals.put( "luajava", luajava );
}
private static final int NEWINSTANCE = 0;
@@ -45,7 +44,7 @@ public final class LuaJava extends LFunction {
"loadLib" };
private int id;
private LuaJava( int id ) {
private LuajavaLib( int id ) {
this.id = id;
}
@@ -54,7 +53,7 @@ public final class LuaJava extends LFunction {
}
// perform a lua call
public boolean luaStackCall(VM vm) {
public boolean luaStackCall(LuaState vm) {
String className;
switch ( id ) {
case BINDCLASS:
@@ -97,7 +96,7 @@ public final class LuaJava extends LFunction {
public final LValue[] values;
public final Class[] classes;
public int hash;
ParamsList( VM vm ) {
ParamsList( LuaState vm ) {
int n = vm.gettop()-2;
values = new LValue[n];
classes = new Class[n];
@@ -123,7 +122,7 @@ public final class LuaJava extends LFunction {
super(o);
this.clazz = clazz;
}
public void luaGetTable(VM vm, LValue table, LValue key) {
public void luaGetTable(LuaState vm, LValue table, LValue key) {
final String s = key.toJavaString();
try {
Field f = clazz.getField(s);
@@ -136,7 +135,7 @@ public final class LuaJava extends LFunction {
throw new RuntimeException(e);
}
}
public void luaSetTable(VM vm, LValue table, LValue key, LValue val) {
public void luaSetTable(LuaState vm, LValue table, LValue key, LValue val) {
Class c = m_instance.getClass();
String s = key.toJavaString();
try {
@@ -149,7 +148,7 @@ public final class LuaJava extends LFunction {
}
}
public boolean luaStackCall(VM vm) {
public boolean luaStackCall(LuaState vm) {
// TODO Auto-generated method stub
return super.luaStackCall(vm);
}
@@ -168,7 +167,7 @@ public final class LuaJava extends LFunction {
public String toString() {
return clazz.getName()+"."+s+"()";
}
public boolean luaStackCall(VM vm) {
public boolean luaStackCall(LuaState vm) {
try {
// find the method
ParamsList params = new ParamsList( vm );

View File

@@ -1,41 +0,0 @@
package lua;
import java.util.Hashtable;
import lua.value.LTable;
/**
** `global state', shared by all threads of this state
*/
public class GlobalState {
// typedef struct global_State {
Hashtable strt; /* hash table for strings */
StringBuffer buff; /* temporary buffer for string concatentation */
// lu_mem totalbytes; /* number of bytes currently allocated */
// lu_mem estimate; /* an estimate of number of bytes actually in use */
// lua_CFunction panic; /* to be called in unprotected errors */
// TValue l_registry;
// struct lua_State *mainthread;
StackState mainthread;
// UpVal uvhead; /* head of double-linked list of all open upvalues */
// struct Table *mt[NUM_TAGS]; /* metatables for basic types */
// TString *tmname[TM_N]; /* array with tag-method names */
// } global_State;
//
private static LTable _G;
static {
resetGlobals();
}
static public void resetGlobals() {
_G = new LTable();
_G .put( "_G", _G );
Builtin.addBuiltins( _G );
}
public static LTable getGlobalsTable() {
return _G;
}
}

View File

@@ -1,74 +0,0 @@
package lua;
import lua.value.LFunction;
/**
Type for Java&nbsp;functions.
<p>
In order to communicate properly with Lua,
a Java&nbsp;function must use the following protocol,
which defines the way parameters and results are passed:
a Java&nbsp;function receives its arguments from Lua in its stack
in direct order (the first argument is pushed first).
So, when the function starts,
<code>lua_gettop(L)</code> 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 <code>lua_gettop(L)</code>.
To return values to Lua, a Java&nbsp;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&nbsp;function called by Lua can also return
many results.
<p>
As an example, the following function receives a variable number
of numerical arguments and returns their average and sum:
<pre><code>
int foo (VM lua) {
int n = lua.gettop(); // number of arguments
double sum = 0;
int i;
for (i = 1; i &lt;= 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
}
</code></pre>
*/
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;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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] );
}
}

View File

@@ -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]);
}

View File

@@ -1,3 +1,4 @@
package org.luaj.sample;
public class SampleClass {

View File

@@ -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] );
}
}

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -1,4 +1,4 @@
package lua.addon.compile;
package org.luaj.compiler;
public class CompilerUnitTests extends AbstractUnitTests {

View File

@@ -1,4 +1,4 @@
package lua.addon.compile;
package org.luaj.compiler;
/**
* Framework to add regression tests as problem areas are found.

View File

@@ -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 );

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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);

View File

@@ -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();

View File

@@ -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() {

View File

@@ -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 {

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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.

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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 = {

View File

@@ -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" );

View File

@@ -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();
}
}

View File

@@ -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 )

Binary file not shown.