Refactor library initialization code.
This commit is contained in:
@@ -47,12 +47,14 @@ import org.luaj.vm2.Varargs;
|
||||
*
|
||||
* @see org.luaj.vm2.lib.jse.JseBaseLib
|
||||
*/
|
||||
public class BaseLib extends LuaTable implements ResourceFinder {
|
||||
public class BaseLib extends OneArgFunction implements ResourceFinder {
|
||||
public static final String VERSION = "Luaj 2.0";
|
||||
|
||||
public static InputStream STDIN = null;
|
||||
public static PrintStream STDOUT = System.out;
|
||||
public static PrintStream STDERR = System.err;
|
||||
public static BaseLib instance;
|
||||
|
||||
public InputStream STDIN = null;
|
||||
public PrintStream STDOUT = System.out;
|
||||
public PrintStream STDERR = System.err;
|
||||
|
||||
/**
|
||||
* Singleton file opener for this Java ClassLoader realm.
|
||||
@@ -61,25 +63,32 @@ public class BaseLib extends LuaTable implements ResourceFinder {
|
||||
*/
|
||||
public static ResourceFinder FINDER;
|
||||
|
||||
private LuaValue next;
|
||||
private LuaValue inext;
|
||||
|
||||
/**
|
||||
* Construct a base libarary instance and initialize the functions in it.
|
||||
* Construct a base libarary instance.
|
||||
*/
|
||||
public BaseLib() {
|
||||
this.set( "_G", this );
|
||||
this.set( "_VERSION", VERSION );
|
||||
LibFunction.bind( this, new BaseFunc1().getClass(), new String[] {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
env.set( "_G", env );
|
||||
env.set( "_VERSION", VERSION );
|
||||
bind1( env, new String[] {
|
||||
"getfenv", // ( [f] ) -> env
|
||||
"getmetatable", // ( object ) -> table
|
||||
"tostring", // (e) -> value
|
||||
} );
|
||||
LibFunction.bind( this, new BaseFunc2().getClass(), new String[] {
|
||||
bind2( env, new String[] {
|
||||
"collectgarbage", // ( opt [,arg] ) -> value
|
||||
"error", // ( message [,level] ) -> ERR
|
||||
"rawequal", // (v1, v2) -> boolean
|
||||
"setfenv", // (f, table) -> void
|
||||
"tonumber", // (e [,base]) -> value
|
||||
} );
|
||||
LibFunction.bind( this, new BaseFuncV().getClass(), new String[] {
|
||||
bindv( env, new String[] {
|
||||
"assert", // ( v [,message] ) -> v, message | ERR
|
||||
"dofile", // ( filename ) -> result1, ...
|
||||
"load", // ( func [,chunkname] ) -> chunk | nil, msg
|
||||
@@ -94,22 +103,20 @@ public class BaseLib extends LuaTable implements ResourceFinder {
|
||||
"rawget", // (table, index) -> value
|
||||
"rawset", // (table, index, value) -> table
|
||||
"setmetatable", // (table, metatable) -> table
|
||||
"pairs", // "pairs" (t) -> iter-func, t, nil
|
||||
"ipairs", // "ipairs", // (t) -> iter-func, t, 0
|
||||
"next", // "next" ( table, [index] ) -> next-index, next-value
|
||||
"__inext", // "inext" ( table, [int-index] ) -> next-index, next-value
|
||||
} );
|
||||
|
||||
// pairs and ipars need iterator functions
|
||||
// "next", // ( table, [index] ) -> next-index, next-value
|
||||
// "inext", // not public ( table, [int-index] ) -> next-index, next-value
|
||||
// "ipairs", // (t) -> iter-func, t, 0
|
||||
// "pairs", // (t) -> iter-func, t, nil
|
||||
LuaValue next = new BaseIter(0,"next",null);
|
||||
LuaValue inext = new BaseIter(1,"inext",null);
|
||||
this.set( "pairs", new BaseIter(2,"pairs",next) );
|
||||
this.set( "ipairs", new BaseIter(3,"ipairs",inext) );
|
||||
this.set( "next", next );
|
||||
// remember next, and inext for use in pairs and ipairs
|
||||
next = env.get("next");
|
||||
inext = env.get("__inext");
|
||||
|
||||
// set the default resource finder if not set already
|
||||
if ( FINDER == null )
|
||||
FINDER = this;
|
||||
return env;
|
||||
}
|
||||
|
||||
/** ResourceFinder implementation
|
||||
@@ -121,8 +128,7 @@ public class BaseLib extends LuaTable implements ResourceFinder {
|
||||
return c.getResourceAsStream(filename.startsWith("/")? filename: "/"+filename);
|
||||
}
|
||||
|
||||
public static class BaseFunc1 extends OneArgFunction {
|
||||
public LuaValue call(LuaValue arg) {
|
||||
protected LuaValue oncall1(int opcode, LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case 0: { // "getfenv", // ( [f] ) -> env
|
||||
if ( ! arg.isfunction() ) {
|
||||
@@ -141,10 +147,8 @@ public class BaseLib extends LuaTable implements ResourceFinder {
|
||||
}
|
||||
return NIL;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BaseFunc2 extends TwoArgFunction {
|
||||
public LuaValue call(LuaValue arg1,LuaValue arg2) {
|
||||
protected LuaValue oncall2(int opcode, LuaValue arg1, LuaValue arg2) {
|
||||
switch ( opcode ) {
|
||||
case 0: // "collectgarbage", // ( opt [,arg] ) -> value
|
||||
String s = arg1.optString("collect");
|
||||
@@ -190,17 +194,8 @@ public class BaseLib extends LuaTable implements ResourceFinder {
|
||||
}
|
||||
return NIL;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BaseFuncV extends VarArgFunction {
|
||||
private final BaseLib lib;
|
||||
public BaseFuncV() {
|
||||
this.lib = null;
|
||||
}
|
||||
public BaseFuncV(BaseLib lib) {
|
||||
this.lib = lib;
|
||||
}
|
||||
public Varargs invoke(Varargs args) {
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case 0: // "assert", // ( v [,message] ) -> v, message | ERR
|
||||
if ( !args.arg1().toboolean() ) error("assertion failed!");
|
||||
@@ -331,6 +326,14 @@ public class BaseLib extends LuaTable implements ResourceFinder {
|
||||
t.setmetatable(mt.isnil()? null: mt.checktable());
|
||||
return t;
|
||||
}
|
||||
case 14: // "pairs" (t) -> iter-func, t, nil
|
||||
return varargsOf( next, args.checktable(1) );
|
||||
case 15: // "ipairs", // (t) -> iter-func, t, 0
|
||||
return varargsOf( inext, args.checktable(1), ZERO );
|
||||
case 16: // "next" ( table, [index] ) -> next-index, next-value
|
||||
return args.arg1().next(args.arg(2));
|
||||
case 17: // "inext" ( table, [int-index] ) -> next-index, next-value
|
||||
return args.arg1().inext(args.arg(2));
|
||||
}
|
||||
return NONE;
|
||||
}
|
||||
@@ -345,29 +348,7 @@ public class BaseLib extends LuaTable implements ResourceFinder {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class BaseIter extends VarArgFunction {
|
||||
final LuaValue aux;
|
||||
BaseIter(int opcode, String name, LuaValue aux) {
|
||||
this.name = name;
|
||||
this.opcode = opcode;
|
||||
this.aux = aux;
|
||||
}
|
||||
public Varargs invoke(Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case 0: // "next" ( table, [index] ) -> next-index, next-value
|
||||
return args.arg1().next(args.arg(2));
|
||||
case 1: // "inext" ( table, [int-index] ) -> next-index, next-value
|
||||
return args.arg1().inext(args.arg(2));
|
||||
case 2: // "pairs" (t) -> iter-func, t, nil
|
||||
return varargsOf( aux, args.checktable(1), NIL );
|
||||
case 3: // "ipairs", // (t) -> iter-func, t, 0
|
||||
return varargsOf( aux, args.checktable(1), ZERO );
|
||||
}
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class StringInputStream extends InputStream {
|
||||
|
||||
@@ -21,24 +21,13 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.lib;
|
||||
|
||||
import org.luaj.vm2.LuaThread;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaThread;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.Varargs;
|
||||
|
||||
public class CoroutineLib extends VarArgFunction {
|
||||
public class CoroutineLib extends ZeroArgFunction {
|
||||
|
||||
private static final String[] NAMES = {
|
||||
"create",
|
||||
"resume",
|
||||
"running",
|
||||
"status",
|
||||
"yield",
|
||||
"wrap",
|
||||
"wrapped"
|
||||
};
|
||||
|
||||
private static final int INIT = -1;
|
||||
private static final int CREATE = 0;
|
||||
private static final int RESUME = 1;
|
||||
private static final int RUNNING = 2;
|
||||
@@ -48,21 +37,18 @@ public class CoroutineLib extends VarArgFunction {
|
||||
private static final int WRAPPED = 6;
|
||||
|
||||
public CoroutineLib() {
|
||||
name = "coroutine";;
|
||||
opcode = INIT;
|
||||
}
|
||||
|
||||
private CoroutineLib(String name, int opcode, LuaThread thread) {
|
||||
super(name, opcode, thread);
|
||||
}
|
||||
|
||||
public Varargs invoke(Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case INIT: {
|
||||
public LuaValue call() {
|
||||
LuaTable t = new LuaTable();
|
||||
LibFunction.bind(t, this.getClass(), NAMES);
|
||||
bindv(t, new String[] {
|
||||
"create", "resume", "running", "status", "yield", "wrap" });
|
||||
env.set("coroutine", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case CREATE: {
|
||||
final LuaValue func = args.checkfunction(1);
|
||||
return new LuaThread(func, func.getfenv() );
|
||||
@@ -87,7 +73,9 @@ public class CoroutineLib extends VarArgFunction {
|
||||
case WRAP: {
|
||||
final LuaValue func = args.checkfunction(1);
|
||||
final LuaThread thread = new LuaThread(func, func.getfenv());
|
||||
return new CoroutineLib("wrapped",WRAPPED,thread);
|
||||
CoroutineLib cl = new CoroutineLib();
|
||||
cl.setfenv(thread);
|
||||
return cl.bindv("wrapped",WRAPPED);
|
||||
}
|
||||
case WRAPPED: {
|
||||
final LuaThread t = (LuaThread) env;
|
||||
|
||||
@@ -21,20 +21,19 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.lib;
|
||||
|
||||
|
||||
import org.luaj.vm2.LuaClosure;
|
||||
import org.luaj.vm2.Lua;
|
||||
import org.luaj.vm2.LuaClosure;
|
||||
import org.luaj.vm2.LuaError;
|
||||
import org.luaj.vm2.LuaFunction;
|
||||
import org.luaj.vm2.LuaThread;
|
||||
import org.luaj.vm2.Print;
|
||||
import org.luaj.vm2.Prototype;
|
||||
import org.luaj.vm2.LuaString;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaThread;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.Print;
|
||||
import org.luaj.vm2.Prototype;
|
||||
import org.luaj.vm2.Varargs;
|
||||
|
||||
public class DebugLib extends VarArgFunction {
|
||||
public class DebugLib extends OneArgFunction {
|
||||
public static final boolean CALLS = (null != System.getProperty("CALLS"));
|
||||
public static final boolean TRACE = (null != System.getProperty("TRACE"));
|
||||
|
||||
@@ -59,7 +58,6 @@ public class DebugLib extends VarArgFunction {
|
||||
"traceback",
|
||||
};
|
||||
|
||||
private static final int INIT = -1;
|
||||
private static final int DEBUG = 0;
|
||||
private static final int GETFENV = 1;
|
||||
private static final int GETHOOK = 2;
|
||||
@@ -105,49 +103,34 @@ public class DebugLib extends VarArgFunction {
|
||||
private static final LuaString ACTIVELINES = valueOf("activelines");
|
||||
|
||||
public DebugLib() {
|
||||
name = "debug";
|
||||
opcode = INIT;
|
||||
}
|
||||
|
||||
public Varargs invoke(Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case INIT: {
|
||||
LuaTable t = new LuaTable(0,20);
|
||||
LibFunction.bind(t, this.getClass(), NAMES);
|
||||
public LuaValue call(LuaValue arg) {
|
||||
LuaTable t = new LuaTable();
|
||||
bindv(t, NAMES);
|
||||
env.set("debug", t);
|
||||
if ( ! DEBUG_ENABLED )
|
||||
DEBUG_ENABLED = true;
|
||||
return t;
|
||||
}
|
||||
case DEBUG:
|
||||
return _debug(args);
|
||||
case GETFENV:
|
||||
return _getfenv(args);
|
||||
case GETHOOK:
|
||||
return _gethook(args);
|
||||
case GETINFO:
|
||||
return _getinfo(args);
|
||||
case GETLOCAL:
|
||||
return _getlocal(args);
|
||||
case GETMETATABLE:
|
||||
return _getmetatable(args);
|
||||
case GETREGISTRY:
|
||||
return _getregistry(args);
|
||||
case GETUPVALUE:
|
||||
return _getupvalue(args);
|
||||
case SETFENV:
|
||||
return _setfenv(args);
|
||||
case SETHOOK:
|
||||
return _sethook(args);
|
||||
case SETLOCAL:
|
||||
return _setlocal(args);
|
||||
case SETMETATABLE:
|
||||
return _setmetatable(args);
|
||||
case SETUPVALUE:
|
||||
return _setupvalue(args);
|
||||
case TRACEBACK:
|
||||
return _traceback(args);
|
||||
default:
|
||||
return NONE;
|
||||
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case DEBUG: return _debug(args);
|
||||
case GETFENV: return _getfenv(args);
|
||||
case GETHOOK: return _gethook(args);
|
||||
case GETINFO: return _getinfo(args);
|
||||
case GETLOCAL: return _getlocal(args);
|
||||
case GETMETATABLE: return _getmetatable(args);
|
||||
case GETREGISTRY: return _getregistry(args);
|
||||
case GETUPVALUE: return _getupvalue(args);
|
||||
case SETFENV: return _setfenv(args);
|
||||
case SETHOOK: return _sethook(args);
|
||||
case SETLOCAL: return _setlocal(args);
|
||||
case SETMETATABLE: return _setmetatable(args);
|
||||
case SETUPVALUE: return _setupvalue(args);
|
||||
case TRACEBACK: return _traceback(args);
|
||||
default: return NONE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.luaj.vm2.Varargs;
|
||||
|
||||
|
||||
abstract
|
||||
public class IoLib extends VarArgFunction {
|
||||
public class IoLib extends OneArgFunction {
|
||||
|
||||
abstract
|
||||
protected class File extends LuaValue{
|
||||
@@ -56,7 +56,7 @@ public class IoLib extends VarArgFunction {
|
||||
|
||||
// delegate method access to file methods table
|
||||
public LuaValue get( LuaValue key ) {
|
||||
return env.get(FILEMETHODS).get(key);
|
||||
return filemethods.get(key);
|
||||
}
|
||||
|
||||
// essentially a userdata instance
|
||||
@@ -122,34 +122,31 @@ public class IoLib extends VarArgFunction {
|
||||
private static final LuaValue STDERR = valueOf("stderr");
|
||||
private static final LuaValue FILE = valueOf("file");
|
||||
private static final LuaValue CLOSED_FILE = valueOf("closed file");
|
||||
private static final LuaValue FILEMETHODS = valueOf("__filemethods");
|
||||
|
||||
private static final int INIT = 0;
|
||||
private static final int IO_INDEX = 1;
|
||||
private static final int IO_CLOSE = 2;
|
||||
private static final int IO_FLUSH = 3;
|
||||
private static final int IO_INPUT = 4;
|
||||
private static final int IO_LINES = 5;
|
||||
private static final int IO_OPEN = 6;
|
||||
private static final int IO_OUTPUT = 7;
|
||||
private static final int IO_POPEN = 8;
|
||||
private static final int IO_READ = 9;
|
||||
private static final int IO_TMPFILE = 10;
|
||||
private static final int IO_TYPE = 11;
|
||||
private static final int IO_WRITE = 12;
|
||||
private static final int IO_CLOSE = 0;
|
||||
private static final int IO_FLUSH = 1;
|
||||
private static final int IO_INPUT = 2;
|
||||
private static final int IO_LINES = 3;
|
||||
private static final int IO_OPEN = 4;
|
||||
private static final int IO_OUTPUT = 5;
|
||||
private static final int IO_POPEN = 6;
|
||||
private static final int IO_READ = 7;
|
||||
private static final int IO_TMPFILE = 8;
|
||||
private static final int IO_TYPE = 9;
|
||||
private static final int IO_WRITE = 10;
|
||||
|
||||
private static final int FILE_CLOSE = 13;
|
||||
private static final int FILE_FLUSH = 14;
|
||||
private static final int FILE_LINES = 15;
|
||||
private static final int FILE_READ = 16;
|
||||
private static final int FILE_SEEK = 17;
|
||||
private static final int FILE_SETVBUF = 18;
|
||||
private static final int FILE_WRITE = 19;
|
||||
private static final int FILE_CLOSE = 11;
|
||||
private static final int FILE_FLUSH = 12;
|
||||
private static final int FILE_LINES = 13;
|
||||
private static final int FILE_READ = 14;
|
||||
private static final int FILE_SEEK = 15;
|
||||
private static final int FILE_SETVBUF = 16;
|
||||
private static final int FILE_WRITE = 17;
|
||||
|
||||
private static final int LINES_ITER = 20;
|
||||
private static final int IO_INDEX = 18;
|
||||
private static final int LINES_ITER = 19;
|
||||
|
||||
public static final String[] IO_NAMES = {
|
||||
"__index",
|
||||
"close",
|
||||
"flush",
|
||||
"input",
|
||||
@@ -173,39 +170,35 @@ public class IoLib extends VarArgFunction {
|
||||
"write",
|
||||
};
|
||||
|
||||
LuaTable filemethods;
|
||||
|
||||
public IoLib() {}
|
||||
|
||||
protected LuaTable init() {
|
||||
public LuaValue call(LuaValue arg) {
|
||||
|
||||
// io lib functions
|
||||
LuaTable t = new LuaTable();
|
||||
LibFunction.bind(t, this.getClass(), IO_NAMES, IO_INDEX );
|
||||
|
||||
// let t be its own metatable
|
||||
t.setmetatable( t );
|
||||
bindv(t, IO_NAMES );
|
||||
|
||||
// create file methods table
|
||||
LuaTable filemethods = new LuaTable();
|
||||
LibFunction.bind(filemethods, this.getClass(), FILE_NAMES, FILE_CLOSE );
|
||||
t.set(FILEMETHODS, filemethods);
|
||||
filemethods = new LuaTable();
|
||||
bind(filemethods, FILE_NAMES, -1, FILE_CLOSE );
|
||||
|
||||
// set up file metatable
|
||||
LuaTable mt = tableOf( new LuaValue[] { INDEX, bindv("__index",IO_INDEX) });
|
||||
t.setmetatable( mt );
|
||||
|
||||
// return the table
|
||||
env.set("io", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
public Varargs invoke(Varargs args) {
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
File f;
|
||||
int n;
|
||||
LuaValue v;
|
||||
try {
|
||||
switch ( opcode ) {
|
||||
case INIT: // init
|
||||
return init();
|
||||
case IO_INDEX: // __index, returns a field
|
||||
v = args.arg(2);
|
||||
return v.equals(STDOUT)?output():
|
||||
v.equals(STDIN)? input():
|
||||
v.equals(STDERR)? errput(): NIL;
|
||||
case IO_FLUSH: // io.flush() -> bool
|
||||
checkopen(output());
|
||||
outfile.flush();
|
||||
@@ -245,6 +238,7 @@ public class IoLib extends VarArgFunction {
|
||||
case IO_WRITE: // io.write(...) -> void
|
||||
checkopen(output());
|
||||
return iowrite(outfile,args);
|
||||
|
||||
case FILE_CLOSE: // file:close() -> void
|
||||
return ioclose(checkfile(args.arg1()));
|
||||
case FILE_FLUSH: // file:flush() -> void
|
||||
@@ -266,6 +260,12 @@ public class IoLib extends VarArgFunction {
|
||||
case FILE_WRITE: // file:write(...) -> void
|
||||
f = checkfile(args.arg1());
|
||||
return iowrite(f,args.subargs(2));
|
||||
|
||||
case IO_INDEX: // __index, returns a field
|
||||
v = args.arg(2);
|
||||
return v.equals(STDOUT)?output():
|
||||
v.equals(STDIN)? input():
|
||||
v.equals(STDERR)? errput(): NIL;
|
||||
case LINES_ITER: // lines iterator(s,var) -> var'
|
||||
f = checkfile(env);
|
||||
return freadline(f);
|
||||
@@ -321,10 +321,8 @@ public class IoLib extends VarArgFunction {
|
||||
|
||||
private Varargs lines(final File f) throws Exception {
|
||||
IoLib iter = (IoLib) getClass().newInstance();
|
||||
iter.opcode = LINES_ITER;
|
||||
iter.name = "lnext";
|
||||
iter.env = f;
|
||||
return iter;
|
||||
iter.setfenv(f);
|
||||
return iter.bindv("lnext",LINES_ITER);
|
||||
}
|
||||
|
||||
private static Varargs iowrite(File f, Varargs args) throws IOException {
|
||||
|
||||
@@ -22,59 +22,264 @@
|
||||
package org.luaj.vm2.lib;
|
||||
|
||||
import org.luaj.vm2.LuaFunction;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
;
|
||||
|
||||
import org.luaj.vm2.Varargs;
|
||||
|
||||
abstract public class LibFunction extends LuaFunction {
|
||||
|
||||
protected int opcode;
|
||||
protected String name;
|
||||
|
||||
public LibFunction() {
|
||||
protected LibFunction() {
|
||||
}
|
||||
protected void bind0(LuaValue env, String[] names) {
|
||||
bind(env, names, 0, 0);
|
||||
}
|
||||
protected void bind1(LuaValue env, String[] names) {
|
||||
bind(env, names, 1, 0);
|
||||
}
|
||||
protected void bind2(LuaValue env, String[] names) {
|
||||
bind(env, names, 2, 0);
|
||||
}
|
||||
protected void bind3(LuaValue env, String[] names) {
|
||||
bind(env, names, 3, 0);
|
||||
}
|
||||
protected void bindv(LuaValue env, String[] names) {
|
||||
bind(env, names, -1, 0);
|
||||
}
|
||||
protected void bind(LuaValue env, String[] names, int numargs, int firstopcode ) {
|
||||
for ( int i=0, n=names.length; i<n; i++ ) {
|
||||
int opcode = firstopcode + i;
|
||||
String name = names[i];
|
||||
LibFunction binding;
|
||||
switch( numargs ) {
|
||||
case 0: binding = bind0(name, opcode); break;
|
||||
case 1: binding = bind1(name, opcode); break;
|
||||
case 2: binding = bind2(name, opcode); break;
|
||||
case 3: binding = bind3(name, opcode); break;
|
||||
default: binding = bindv(name, opcode); break;
|
||||
}
|
||||
env.set(names[i], binding);
|
||||
}
|
||||
}
|
||||
|
||||
public LibFunction(String name, int opcode, LuaValue env) {
|
||||
super(env);
|
||||
protected LibFunction bind0(String name, int opcode) {
|
||||
return new ZeroArgBinding(name, opcode, this);
|
||||
}
|
||||
protected LibFunction bind1(String name, int opcode) {
|
||||
return new OneArgBinding(name, opcode, this);
|
||||
}
|
||||
protected LibFunction bind2(String name, int opcode) {
|
||||
return new TwoArgBinding(name, opcode, this);
|
||||
}
|
||||
protected LibFunction bind3(String name, int opcode) {
|
||||
return new ThreeArgBinding(name, opcode, this);
|
||||
}
|
||||
protected LibFunction bindv(String name, int opcode) {
|
||||
return new VarArgBinding(name, opcode, this);
|
||||
}
|
||||
|
||||
/** called when a zero-arg function is invoked */
|
||||
protected LuaValue oncall0(int opcode) {
|
||||
return NIL;
|
||||
}
|
||||
|
||||
/** called when a one-arg function is invoked */
|
||||
protected LuaValue oncall1(int opcode, LuaValue arg) {
|
||||
return NIL;
|
||||
}
|
||||
/** called when a two-arg function is invoked */
|
||||
protected LuaValue oncall2(int opcode, LuaValue arg1, LuaValue arg2) {
|
||||
return NIL;
|
||||
}
|
||||
/** called when a three-arg function is invoked */
|
||||
protected LuaValue oncall3(int opcode, LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||
return NIL;
|
||||
}
|
||||
/** called when a var-arg function is invoked */
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
return NONE;
|
||||
}
|
||||
|
||||
/** Binding to a one-arg function */
|
||||
private static class ZeroArgBinding extends LibFunction {
|
||||
private final String name;
|
||||
private final int opcode;
|
||||
private final LibFunction delegate;
|
||||
|
||||
private ZeroArgBinding(String name, int opcode, LibFunction delegate) {
|
||||
this.name = name;
|
||||
this.opcode = opcode;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name!=null? name: super.toString();
|
||||
return name;
|
||||
}
|
||||
|
||||
public static LuaTable bind( LuaTable table, Class libFuncClass, String[] names ) {
|
||||
return bind( table, libFuncClass, names, 0 );
|
||||
public LuaValue call() {
|
||||
return delegate.oncall0(opcode);
|
||||
}
|
||||
|
||||
/** Bind a set of names to class instances, put values into the table. */
|
||||
public static LuaTable bind( LuaTable table, Class libFuncClass, String[] names, int firstOpcode ) {
|
||||
try {
|
||||
for ( int i=0, n=names.length; i<n; i++ ) {
|
||||
LibFunction f = (LibFunction) libFuncClass.newInstance();
|
||||
f.opcode = firstOpcode + i;
|
||||
f.name = names[i];
|
||||
f.env = table;
|
||||
table.set( names[i], f );
|
||||
}
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException(e.toString());
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e.toString());
|
||||
}
|
||||
return table;
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return delegate.oncall0(opcode);
|
||||
}
|
||||
|
||||
/** Bind a complete set of names and classes , put values into the table. */
|
||||
public static LuaTable bind( LuaTable table, Class[] libFuncClasses, String[][] nameLists ) {
|
||||
for ( int j=0, n=libFuncClasses.length; j<n; j++ ) {
|
||||
bind( table, libFuncClasses[j], nameLists[j] );
|
||||
}
|
||||
return table;
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||
return delegate.oncall0(opcode);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||
return delegate.oncall0(opcode);
|
||||
}
|
||||
|
||||
public Varargs invoke(Varargs varargs) {
|
||||
return delegate.oncall0(opcode);
|
||||
}
|
||||
}
|
||||
|
||||
/** Binding to a one-arg function */
|
||||
private static class OneArgBinding extends LibFunction {
|
||||
private final String name;
|
||||
private final int opcode;
|
||||
private final LibFunction delegate;
|
||||
|
||||
private OneArgBinding(String name, int opcode, LibFunction delegate) {
|
||||
this.name = name;
|
||||
this.opcode = opcode;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public LuaValue call() {
|
||||
return delegate.oncall1(opcode,NIL);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return delegate.oncall1(opcode,arg);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||
return delegate.oncall1(opcode,arg1);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||
return delegate.oncall1(opcode,arg1);
|
||||
}
|
||||
|
||||
public Varargs invoke(Varargs varargs) {
|
||||
return delegate.oncall1(opcode,varargs.arg1());
|
||||
}
|
||||
}
|
||||
|
||||
/** Binding to a two-arg function */
|
||||
private static class TwoArgBinding extends LibFunction {
|
||||
private final String name;
|
||||
private final int opcode;
|
||||
private final LibFunction delegate;
|
||||
|
||||
private TwoArgBinding(String name, int opcode, LibFunction delegate) {
|
||||
this.name = name;
|
||||
this.opcode = opcode;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public LuaValue call() {
|
||||
return delegate.oncall2(opcode,NIL,NIL);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return delegate.oncall2(opcode,arg,NIL);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||
return delegate.oncall2(opcode,arg1,arg2);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||
return delegate.oncall2(opcode,arg1,arg2);
|
||||
}
|
||||
|
||||
public Varargs invoke(Varargs varargs) {
|
||||
return delegate.oncall2(opcode,varargs.arg1(),varargs.arg(2));
|
||||
}
|
||||
}
|
||||
|
||||
/** Binding to a three-arg function */
|
||||
private static class ThreeArgBinding extends LibFunction {
|
||||
private final String name;
|
||||
private final int opcode;
|
||||
private final LibFunction delegate;
|
||||
|
||||
private ThreeArgBinding(String name, int opcode, LibFunction delegate) {
|
||||
this.name = name;
|
||||
this.opcode = opcode;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public LuaValue call() {
|
||||
return delegate.oncall3(opcode,NIL,NIL,NIL);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return delegate.oncall3(opcode,arg,NIL,NIL);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||
return delegate.oncall3(opcode,arg1,arg2,NIL);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||
return delegate.oncall3(opcode,arg1,arg2,arg3);
|
||||
}
|
||||
|
||||
public Varargs invoke(Varargs varargs) {
|
||||
return delegate.oncall3(opcode,varargs.arg1(),varargs.arg(2),varargs.arg(3));
|
||||
}
|
||||
}
|
||||
|
||||
/** Binding to a var-arg function */
|
||||
private static class VarArgBinding extends LibFunction {
|
||||
private final String name;
|
||||
private final int opcode;
|
||||
private final LibFunction delegate;
|
||||
|
||||
private VarArgBinding(String name, int opcode, LibFunction delegate) {
|
||||
this.name = name;
|
||||
this.opcode = opcode;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public LuaValue call() {
|
||||
return delegate.oncallv(opcode,NONE).arg1();
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return delegate.oncallv(opcode,arg).arg1();
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||
return delegate.oncallv(opcode,varargsOf(arg1,arg2)).arg1();
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||
return delegate.oncallv(opcode,varargsOf(arg1,arg2,arg3)).arg1();
|
||||
}
|
||||
|
||||
public Varargs invoke(Varargs varargs) {
|
||||
return delegate.oncallv(opcode,varargs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,34 +39,31 @@ public class MathLib extends OneArgFunction {
|
||||
|
||||
public static MathLib MATHLIB = null;
|
||||
|
||||
private static final LuaValue RANDOM = valueOf("__random");
|
||||
private Random random;
|
||||
|
||||
public MathLib() {
|
||||
name = "math";
|
||||
opcode = -1;
|
||||
MATHLIB = this;
|
||||
}
|
||||
|
||||
protected LuaTable init() {
|
||||
public LuaValue call(LuaValue arg) {
|
||||
LuaTable t = new LuaTable(0,30);
|
||||
t.set( "pi", Math.PI );
|
||||
t.set( "huge", LuaDouble.POSINF );
|
||||
LibFunction.bind( t, new MathLib().getClass(), new String[] {
|
||||
bind1( t, new String[] {
|
||||
"abs", "ceil", "cos", "deg",
|
||||
"exp", "floor", "rad", "sin",
|
||||
"sqrt", "tan" } );
|
||||
LibFunction.bind( t, new MathFunc2().getClass(), new String[] {
|
||||
"fmod", "ldexp", "pow", "random",
|
||||
} );
|
||||
LibFunction.bind( t, new MathFuncV().getClass(), new String[] {
|
||||
bind2( t, new String[] {
|
||||
"fmod", "ldexp", "pow", "random", } );
|
||||
bindv( t, new String[] {
|
||||
"frexp", "max", "min", "modf",
|
||||
"randomseed" } );
|
||||
env.set("math", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
public LuaValue oncall1(int opcode, LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case -1: return init();
|
||||
case 0: return valueOf(Math.abs(arg.todouble()));
|
||||
case 1: return valueOf(Math.ceil(arg.todouble()));
|
||||
case 2: return valueOf(Math.cos(arg.todouble()));
|
||||
@@ -81,8 +78,7 @@ public class MathLib extends OneArgFunction {
|
||||
return NIL;
|
||||
}
|
||||
|
||||
public static class MathFunc2 extends TwoArgFunction {
|
||||
public LuaValue call(LuaValue arg1,LuaValue arg2) {
|
||||
public LuaValue oncall2(int opcode,LuaValue arg1,LuaValue arg2) {
|
||||
switch ( opcode ) {
|
||||
case 0: { // fmod
|
||||
double x = arg1.checkdouble();
|
||||
@@ -101,9 +97,8 @@ public class MathLib extends OneArgFunction {
|
||||
return dpow(arg1.todouble(), arg2.todouble());
|
||||
}
|
||||
case 3: { // random
|
||||
Random random = (Random) env.get(RANDOM).optuserdata(Random.class, null);
|
||||
if ( random == null )
|
||||
env.set(RANDOM,userdataOf(random = new Random()));
|
||||
random = new Random();
|
||||
if ( arg1.isnil() )
|
||||
return valueOf( random.nextDouble() );
|
||||
int m = arg1.toint();
|
||||
@@ -116,8 +111,6 @@ public class MathLib extends OneArgFunction {
|
||||
return NIL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** compute power using installed math library, or default if there is no math library installed */
|
||||
public static LuaValue dpow(double a, double b) {
|
||||
return LuaDouble.valueOf(
|
||||
@@ -155,8 +148,7 @@ public class MathLib extends OneArgFunction {
|
||||
return p;
|
||||
}
|
||||
|
||||
public static class MathFuncV extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
public Varargs oncallv(int opcode,Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case 0: { // frexp
|
||||
double x = args.checkdouble(1);
|
||||
@@ -186,11 +178,10 @@ public class MathLib extends OneArgFunction {
|
||||
}
|
||||
case 4: { // randomseed
|
||||
long seed = args.checklong(1);
|
||||
env.set(RANDOM,userdataOf(new Random(seed)));
|
||||
random = new Random(seed);
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,10 +35,6 @@ abstract public class OneArgFunction extends LibFunction {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
public OneArgFunction( String name, int opcode, LuaValue env ) {
|
||||
super(name, opcode, env);
|
||||
}
|
||||
|
||||
public final LuaValue call() {
|
||||
return call(NIL);
|
||||
}
|
||||
|
||||
@@ -46,11 +46,10 @@ import org.luaj.vm2.Varargs;
|
||||
*
|
||||
* @see org.luaj.vm2.lib.jse.JseOsLib
|
||||
*/
|
||||
public class OsLib extends VarArgFunction {
|
||||
public class OsLib extends OneArgFunction {
|
||||
public static String TMP_PREFIX = ".luaj";
|
||||
public static String TMP_SUFFIX = "tmp";
|
||||
|
||||
private static final int INIT = -1;
|
||||
private static final int CLOCK = 0;
|
||||
private static final int DATE = 1;
|
||||
private static final int DIFFTIME = 2;
|
||||
@@ -84,18 +83,18 @@ public class OsLib extends VarArgFunction {
|
||||
* Create and OsLib instance.
|
||||
*/
|
||||
public OsLib() {
|
||||
name = "os";
|
||||
opcode = INIT;
|
||||
}
|
||||
|
||||
public Varargs invoke(Varargs args) {
|
||||
try {
|
||||
switch ( opcode ) {
|
||||
case INIT: {
|
||||
public LuaValue call(LuaValue arg) {
|
||||
LuaTable t = new LuaTable();
|
||||
LibFunction.bind(t, getClass(), NAMES);
|
||||
bindv(t, NAMES);
|
||||
env.set("os", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
try {
|
||||
switch ( opcode ) {
|
||||
case CLOCK:
|
||||
return valueOf(clock());
|
||||
case DATE: {
|
||||
|
||||
@@ -32,75 +32,78 @@ import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.Varargs;
|
||||
|
||||
|
||||
public class PackageLib extends LuaTable {
|
||||
public class PackageLib extends OneArgFunction {
|
||||
|
||||
public static String DEFAULT_LUA_PATH = "?.lua";
|
||||
|
||||
public LuaValue _G = null;
|
||||
public InputStream STDIN = null;
|
||||
public PrintStream STDOUT = System.out;
|
||||
public LuaTable LOADED = null;
|
||||
public LuaTable PACKAGE = null;
|
||||
public LuaTable LOADED;
|
||||
public LuaTable PACKAGE;
|
||||
|
||||
private static final LuaString _M = LuaString.valueOf("_M");
|
||||
private static final LuaString _NAME = LuaString.valueOf("_NAME");
|
||||
private static final LuaString _PACKAGE = LuaString.valueOf("_PACKAGE");
|
||||
private static final LuaString _DOT = LuaString.valueOf(".");
|
||||
private static final LuaString _LOADERS = LuaString.valueOf("loaders");
|
||||
private static final LuaString _LOADED = LuaString.valueOf("loaded");
|
||||
private static final LuaString _LOADLIB = LuaString.valueOf("loadlib");
|
||||
private static final LuaString _PRELOAD = LuaString.valueOf("preload");
|
||||
private static final LuaString _PATH = LuaString.valueOf("path");
|
||||
private static final LuaString _SEEALL = LuaString.valueOf("seeall");
|
||||
private static final LuaValue _SENTINEL = EMPTYSTRING;
|
||||
private static final LuaString _M = valueOf("_M");
|
||||
private static final LuaString _NAME = valueOf("_NAME");
|
||||
private static final LuaString _PACKAGE = valueOf("_PACKAGE");
|
||||
private static final LuaString _DOT = valueOf(".");
|
||||
private static final LuaString _LOADERS = valueOf("loaders");
|
||||
private static final LuaString _LOADED = valueOf("loaded");
|
||||
private static final LuaString _LOADLIB = valueOf("loadlib");
|
||||
private static final LuaString _PRELOAD = valueOf("preload");
|
||||
private static final LuaString _PATH = valueOf("path");
|
||||
private static final LuaString _SEEALL = valueOf("seeall");
|
||||
private static final LuaString _LOADFILE = valueOf("loadfile");
|
||||
private static final LuaString _SENTINEL = valueOf("\u0001");
|
||||
|
||||
private static final int MODULE = 1;
|
||||
private static final int REQUIRE = 2;
|
||||
private static final int LOADLIB = 3;
|
||||
private static final int SEEALL = 4;
|
||||
private static final int PRELOAD_LOADER = 5;
|
||||
private static final int LUA_LOADER = 6;
|
||||
private static final int JAVA_LOADER = 7;
|
||||
private static final int MODULE = 0;
|
||||
private static final int REQUIRE = 1;
|
||||
private static final int LOADLIB = 2;
|
||||
private static final int SEEALL = 3;
|
||||
private static final int PRELOAD_LOADER = 4;
|
||||
private static final int LUA_LOADER = 5;
|
||||
private static final int JAVA_LOADER = 6;
|
||||
|
||||
public PackageLib() {
|
||||
}
|
||||
|
||||
public PackageLib(LuaValue _G) {
|
||||
this._G = _G;
|
||||
_G.set( "module", new PackageFuncV(MODULE, "module", _G) );
|
||||
_G.set( "require", new PackageFuncV(REQUIRE, "require",_G) );
|
||||
_G.set( "package", PACKAGE = tableOf( new LuaValue[] {
|
||||
public LuaValue call(LuaValue arg) {
|
||||
env.set("require", bind1("require",REQUIRE));
|
||||
env.set("module", bindv("module",MODULE));
|
||||
env.set( "package", PACKAGE=tableOf( new LuaValue[] {
|
||||
_LOADED, LOADED=tableOf(),
|
||||
_PRELOAD, tableOf(),
|
||||
_PATH, valueOf(DEFAULT_LUA_PATH),
|
||||
_LOADLIB, new PackageFuncV(LOADLIB, "loadlib",_G),
|
||||
_SEEALL, new PackageFuncV(SEEALL, "seeall",_G),
|
||||
_LOADLIB, bindv("loadlib",LOADLIB),
|
||||
_SEEALL, bind1("seeall",SEEALL),
|
||||
_LOADERS, listOf(new LuaValue[] {
|
||||
new PackageFuncV(PRELOAD_LOADER,"preload_loader",_G),
|
||||
new PackageFuncV(LUA_LOADER, "lua_loader",_G),
|
||||
new PackageFuncV(JAVA_LOADER, "java_loader",_G),
|
||||
}),
|
||||
} ) );
|
||||
bindv("preload_loader", PRELOAD_LOADER),
|
||||
bindv("lua_loader", LUA_LOADER),
|
||||
bindv("java_loader", JAVA_LOADER),
|
||||
}) }) );
|
||||
return env;
|
||||
}
|
||||
|
||||
private class PackageFuncV extends VarArgFunction {
|
||||
public PackageFuncV(int opcode, String name, LuaValue env) {
|
||||
super(name, opcode, env);
|
||||
}
|
||||
public Varargs invoke(Varargs args) {
|
||||
protected LuaValue oncall1(int opcode, LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case MODULE:
|
||||
return module(args);
|
||||
case REQUIRE:
|
||||
return require(args);
|
||||
case LOADLIB:
|
||||
return loadlib(args);
|
||||
return require(arg);
|
||||
case SEEALL: {
|
||||
LuaTable t = args.checktable(1);
|
||||
LuaTable t = arg.checktable();
|
||||
LuaValue m = t.getmetatable();
|
||||
if ( m == null )
|
||||
t.setmetatable(m=tableOf());
|
||||
m.set( INDEX, env );
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
return NIL;
|
||||
}
|
||||
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case MODULE:
|
||||
return module(args);
|
||||
case LOADLIB:
|
||||
return loadlib(args);
|
||||
case PRELOAD_LOADER: {
|
||||
return loader_preload(args);
|
||||
}
|
||||
@@ -113,7 +116,6 @@ public class PackageLib extends LuaTable {
|
||||
}
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
|
||||
/** Allow packages to mark themselves as loaded */
|
||||
public void setIsLoaded(String name, LuaTable value) {
|
||||
@@ -159,7 +161,7 @@ public class PackageLib extends LuaTable {
|
||||
if ( ! value.istable() ) { /* not found? */
|
||||
|
||||
/* try global variable (and create one if it does not exist) */
|
||||
module = findtable( _G, modname );
|
||||
module = findtable( env, modname );
|
||||
if ( module == null )
|
||||
error( "name conflict for module '"+modname+"'" );
|
||||
LOADED.set(modname, module);
|
||||
@@ -249,8 +251,8 @@ public class PackageLib extends LuaTable {
|
||||
* 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 Varargs require( Varargs args ) {
|
||||
LuaString name = args.checkstring(1);
|
||||
public LuaValue require( LuaValue arg ) {
|
||||
LuaString name = arg.checkstring();
|
||||
LuaValue loaded = LOADED.get(name);
|
||||
if ( loaded.toboolean() ) {
|
||||
if ( loaded == _SENTINEL )
|
||||
@@ -304,7 +306,7 @@ public class PackageLib extends LuaTable {
|
||||
InputStream is = null;
|
||||
|
||||
// try to use loadfile for the file
|
||||
LuaValue loadfile = _G.get("loadfile");
|
||||
LuaValue loadfile = env.get(_LOADFILE);
|
||||
if ( ! loadfile.isfunction() )
|
||||
return valueOf("loadfile is not a function" );
|
||||
|
||||
@@ -357,7 +359,7 @@ public class PackageLib extends LuaTable {
|
||||
try {
|
||||
c = Class.forName(classname);
|
||||
v = (LuaValue) c.newInstance();
|
||||
v.setfenv(_G);
|
||||
v.setfenv(env);
|
||||
return v;
|
||||
} catch ( ClassNotFoundException cnfe ) {
|
||||
return valueOf("\n\tno class '"+classname+"'" );
|
||||
|
||||
@@ -37,23 +37,23 @@ public class StringLib extends OneArgFunction {
|
||||
public static LuaTable instance;
|
||||
|
||||
public StringLib() {
|
||||
name = "string";
|
||||
opcode = -1;
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case -1: {
|
||||
LuaTable t = new LuaTable();
|
||||
LibFunction.bind( t, getClass(), new String[] {
|
||||
bind1(t, new String[] {
|
||||
"dump", "len", "lower", "reverse", "upper", } );
|
||||
LibFunction.bind( t, new StringFuncV().getClass(), new String[] {
|
||||
bindv(t, new String[] {
|
||||
"byte", "char", "find", "format",
|
||||
"gmatch", "gsub", "match", "rep",
|
||||
"sub"} );
|
||||
env.set("string", t);
|
||||
instance = t;
|
||||
return t;
|
||||
}
|
||||
|
||||
protected LuaValue oncall1(int opcode, LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case 0: return dump(arg); // dump (function)
|
||||
case 1: return len(arg); // len (function)
|
||||
case 2: return lower(arg); // lower (function)
|
||||
@@ -63,8 +63,8 @@ public class StringLib extends OneArgFunction {
|
||||
return NIL;
|
||||
}
|
||||
|
||||
public static class StringFuncV extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case 0: return StringLib.byte_( args );
|
||||
case 1: return StringLib.char_( args );
|
||||
@@ -78,7 +78,6 @@ public class StringLib extends OneArgFunction {
|
||||
}
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* string.byte (s [, i [, j]])
|
||||
|
||||
@@ -28,19 +28,15 @@ import org.luaj.vm2.Varargs;
|
||||
public class TableLib extends OneArgFunction {
|
||||
|
||||
public TableLib() {
|
||||
name = "table";
|
||||
opcode = -1;
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case -1: {
|
||||
LuaTable t = new LuaTable();
|
||||
LibFunction.bind( t, this.getClass(), new String[] {
|
||||
bind1(t, new String[] {
|
||||
"getn", // (table) -> number
|
||||
"maxn", // (table) -> number
|
||||
} );
|
||||
LibFunction.bind( t, new TableFuncV().getClass(), new String[] {
|
||||
bindv(t, new String[] {
|
||||
"remove", // (table [, pos]) -> removed-ele
|
||||
"concat", // (table [, sep [, i [, j]]]) -> string
|
||||
"insert", // (table, [pos,] value) -> prev-ele
|
||||
@@ -48,16 +44,19 @@ public class TableLib extends OneArgFunction {
|
||||
"foreach", // (table, func) -> void
|
||||
"foreachi", // (table, func) -> void
|
||||
} );
|
||||
env.set("table", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
protected LuaValue oncall1(int opcode, LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case 0: return arg.checktable().getn();
|
||||
case 1: return valueOf( arg.checktable().maxn());
|
||||
}
|
||||
return NIL;
|
||||
}
|
||||
|
||||
public static class TableFuncV extends VarArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
switch ( opcode ) {
|
||||
case 0: { // "remove" (table [, pos]) -> removed-ele
|
||||
LuaTable table = args.checktable(1);
|
||||
@@ -92,4 +91,3 @@ public class TableLib extends OneArgFunction {
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,10 +35,6 @@ abstract public class ThreeArgFunction extends LibFunction {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
public ThreeArgFunction( String name, int opcode, LuaValue env ) {
|
||||
super(name, opcode, env);
|
||||
}
|
||||
|
||||
public final LuaValue call() {
|
||||
return call(NIL, NIL, NIL);
|
||||
}
|
||||
|
||||
@@ -35,10 +35,6 @@ abstract public class TwoArgFunction extends LibFunction {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
public TwoArgFunction( String name, int opcode, LuaValue env ) {
|
||||
super(name, opcode, env);
|
||||
}
|
||||
|
||||
public final LuaValue call() {
|
||||
return call(NIL, NIL);
|
||||
}
|
||||
|
||||
@@ -33,10 +33,6 @@ abstract public class VarArgFunction extends LibFunction {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
public VarArgFunction( String name, int opcode, LuaValue env ) {
|
||||
super(name, opcode, env);
|
||||
}
|
||||
|
||||
public final LuaValue call() {
|
||||
return invoke(NONE).arg1();
|
||||
}
|
||||
|
||||
@@ -35,10 +35,6 @@ abstract public class ZeroArgFunction extends LibFunction {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
public ZeroArgFunction( String name, int opcode, LuaValue env ) {
|
||||
super(name, opcode, env);
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
return call();
|
||||
}
|
||||
|
||||
@@ -22,7 +22,12 @@
|
||||
package org.luaj.vm2.lib;
|
||||
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.jme.JmeIoLib;
|
||||
import org.luaj.vm2.lib.jse.JseBaseLib;
|
||||
import org.luaj.vm2.lib.jse.JseIoLib;
|
||||
import org.luaj.vm2.lib.jse.JseMathLib;
|
||||
import org.luaj.vm2.lib.jse.JseOsLib;
|
||||
import org.luaj.vm2.lib.jse.LuajavaLib;
|
||||
|
||||
public class JmePlatform {
|
||||
|
||||
@@ -32,27 +37,24 @@ public class JmePlatform {
|
||||
* @return Table of globals initialized with the standard JME libraries
|
||||
*/
|
||||
public static LuaTable standardGlobals() {
|
||||
LuaTable _G = new BaseLib();
|
||||
new org.luaj.vm2.lib.PackageLib(_G);
|
||||
set(_G, "coroutine", new org.luaj.vm2.lib.CoroutineLib() );
|
||||
set(_G, "io", new org.luaj.vm2.lib.jme.JseIoLib() );
|
||||
set(_G, "math", new org.luaj.vm2.lib.MathLib() );
|
||||
set(_G, "os", new org.luaj.vm2.lib.OsLib() );
|
||||
set(_G, "table", new org.luaj.vm2.lib.TableLib() );
|
||||
set(_G, "string", new org.luaj.vm2.lib.StringLib() );
|
||||
LuaTable _G = new LuaTable();
|
||||
init(_G, new BaseLib());
|
||||
init(_G, new PackageLib());
|
||||
init(_G, new TableLib());
|
||||
init(_G, new StringLib());
|
||||
init(_G, new CoroutineLib());
|
||||
init(_G, new JmeIoLib());
|
||||
return _G;
|
||||
}
|
||||
|
||||
public static LuaTable debugGlobals() {
|
||||
LuaTable _G = standardGlobals();
|
||||
set(_G, "string", new org.luaj.vm2.lib.DebugLib() );
|
||||
init(_G, new DebugLib());
|
||||
return _G;
|
||||
}
|
||||
|
||||
private static void set( LuaTable _G, String name, LuaValue chunk ) {
|
||||
chunk.setfenv(_G);
|
||||
LuaValue pkg = chunk.call(LuaValue.valueOf(name));
|
||||
_G.set( name, pkg );
|
||||
private static void init(LuaTable _G, LibFunction lib) {
|
||||
lib.setfenv(_G);
|
||||
lib.call();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,18 +37,18 @@ import org.luaj.vm2.lib.IoLib;
|
||||
*
|
||||
* Seek is not supported.
|
||||
*/
|
||||
public class JseIoLib extends IoLib {
|
||||
public class JmeIoLib extends IoLib {
|
||||
|
||||
public JseIoLib() {
|
||||
public JmeIoLib() {
|
||||
super();
|
||||
}
|
||||
|
||||
protected File wrapStdin() throws IOException {
|
||||
return new FileImpl(BaseLib.STDIN);
|
||||
return new FileImpl(BaseLib.instance.STDIN);
|
||||
}
|
||||
|
||||
protected File wrapStdout() throws IOException {
|
||||
return new FileImpl(BaseLib.STDOUT);
|
||||
return new FileImpl(BaseLib.instance.STDOUT);
|
||||
}
|
||||
|
||||
protected File openFile( String filename, boolean readMode, boolean appendMode, boolean updateMode, boolean binaryMode ) throws IOException {
|
||||
@@ -22,8 +22,6 @@
|
||||
package org.luaj.vm2.lib;
|
||||
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.CoroutineLib;
|
||||
import org.luaj.vm2.lib.jse.JseBaseLib;
|
||||
import org.luaj.vm2.lib.jse.JseIoLib;
|
||||
import org.luaj.vm2.lib.jse.JseMathLib;
|
||||
@@ -38,22 +36,22 @@ public class JsePlatform {
|
||||
* @return Table of globals initialized with the standard JSE libraries
|
||||
*/
|
||||
public static LuaTable standardGlobals() {
|
||||
LuaTable _G = new JseBaseLib();
|
||||
new org.luaj.vm2.lib.PackageLib(_G);
|
||||
set(_G, "table", new org.luaj.vm2.lib.TableLib() );
|
||||
set(_G, "string", new org.luaj.vm2.lib.StringLib() );
|
||||
set(_G, "coroutine", new org.luaj.vm2.lib.CoroutineLib() );
|
||||
set(_G, "debug", new org.luaj.vm2.lib.DebugLib() );
|
||||
set(_G, "math", new org.luaj.vm2.lib.jse.JseMathLib() );
|
||||
set(_G, "io", new org.luaj.vm2.lib.jse.JseIoLib() );
|
||||
set(_G, "os", new org.luaj.vm2.lib.jse.JseOsLib() );
|
||||
set(_G, "luajava", new org.luaj.vm2.lib.jse.LuajavaLib() );
|
||||
LuaTable _G = new LuaTable();
|
||||
init(_G, new JseBaseLib());
|
||||
init(_G, new PackageLib());
|
||||
init(_G, new TableLib());
|
||||
init(_G, new StringLib());
|
||||
init(_G, new CoroutineLib());
|
||||
init(_G, new DebugLib());
|
||||
init(_G, new JseMathLib());
|
||||
init(_G, new JseIoLib());
|
||||
init(_G, new JseOsLib());
|
||||
init(_G, new LuajavaLib());
|
||||
return _G;
|
||||
}
|
||||
|
||||
private static void set( LuaTable _G, String name, LuaValue chunk ) {
|
||||
chunk.setfenv(_G);
|
||||
LuaValue pkg = chunk.call(LuaValue.valueOf(name));
|
||||
_G.set( name, pkg );
|
||||
private static void init(LuaTable _G, LibFunction lib) {
|
||||
lib.setfenv(_G);
|
||||
lib.call();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,12 +38,9 @@ import java.io.InputStream;
|
||||
*/
|
||||
public class JseBaseLib extends org.luaj.vm2.lib.BaseLib {
|
||||
|
||||
static {
|
||||
STDIN = System.in;
|
||||
}
|
||||
|
||||
/** Construct a JSE base library instance */
|
||||
public JseBaseLib() {
|
||||
STDIN = System.in;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.lib.jse;
|
||||
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -29,6 +30,7 @@ import java.io.RandomAccessFile;
|
||||
|
||||
import org.luaj.vm2.LuaError;
|
||||
import org.luaj.vm2.LuaString;
|
||||
import org.luaj.vm2.lib.BaseLib;
|
||||
import org.luaj.vm2.lib.IoLib;
|
||||
|
||||
/**
|
||||
@@ -42,11 +44,11 @@ public class JseIoLib extends IoLib {
|
||||
}
|
||||
|
||||
protected File wrapStdin() throws IOException {
|
||||
return new FileImpl(JseBaseLib.STDIN);
|
||||
return new FileImpl(BaseLib.instance.STDIN);
|
||||
}
|
||||
|
||||
protected File wrapStdout() throws IOException {
|
||||
return new FileImpl(JseBaseLib.STDOUT);
|
||||
return new FileImpl(BaseLib.instance.STDOUT);
|
||||
}
|
||||
|
||||
protected File openFile( String filename, boolean readMode, boolean appendMode, boolean updateMode, boolean binaryMode ) throws IOException {
|
||||
|
||||
@@ -21,10 +21,8 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.lib.jse;
|
||||
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.LibFunction;
|
||||
import org.luaj.vm2.lib.TwoArgFunction;
|
||||
import org.luaj.vm2.lib.MathLib;
|
||||
|
||||
/**
|
||||
* Math library implementation for use on JSE platform.
|
||||
@@ -36,20 +34,21 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib {
|
||||
|
||||
public JseMathLib() {}
|
||||
|
||||
protected LuaTable init() {
|
||||
LuaTable t = super.init();
|
||||
LibFunction.bind( t, this.getClass(), new String[] {
|
||||
public LuaValue call(LuaValue arg) {
|
||||
MathLib ml = new MathLib();
|
||||
ml.setfenv(env);
|
||||
LuaValue t = ml.call(arg);
|
||||
bind1( t, new String[] {
|
||||
"acos", "asin", "atan", "cosh",
|
||||
"exp", "log", "log10", "sinh",
|
||||
"tanh" } );
|
||||
LibFunction.bind( t, new J2seMathFunc2().getClass(), new String[] {
|
||||
bind2( t, new String[] {
|
||||
"atan2", "pow", } );
|
||||
return t;
|
||||
}
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
public LuaValue oncall1(int opcode, LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case -1: return init();
|
||||
case 0: return valueOf(Math.acos(arg.todouble()));
|
||||
case 1: return valueOf(Math.asin(arg.todouble()));
|
||||
case 2: return valueOf(Math.atan(arg.todouble()));
|
||||
@@ -63,15 +62,13 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib {
|
||||
return NIL;
|
||||
}
|
||||
|
||||
public static class J2seMathFunc2 extends TwoArgFunction {
|
||||
public LuaValue call(LuaValue arg1,LuaValue arg2) {
|
||||
public LuaValue oncall2(int opcode, LuaValue arg1, LuaValue arg2) {
|
||||
switch ( opcode ) {
|
||||
case 0: return valueOf(Math.atan2(arg1.todouble(), arg2.todouble()));
|
||||
case 1: return valueOf(Math.pow(arg1.todouble(), arg2.todouble()));
|
||||
}
|
||||
return NIL;
|
||||
}
|
||||
}
|
||||
|
||||
/** Faster, better version of pow() used by arithmetic operator ^ */
|
||||
public double dpow_d(double a, double b) {
|
||||
|
||||
@@ -43,23 +43,20 @@ import org.luaj.vm2.LuaUserdata;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.Varargs;
|
||||
import org.luaj.vm2.lib.LibFunction;
|
||||
import org.luaj.vm2.lib.OneArgFunction;
|
||||
import org.luaj.vm2.lib.ThreeArgFunction;
|
||||
import org.luaj.vm2.lib.TwoArgFunction;
|
||||
import org.luaj.vm2.lib.VarArgFunction;
|
||||
|
||||
public class LuajavaLib extends VarArgFunction {
|
||||
public class LuajavaLib extends OneArgFunction {
|
||||
|
||||
private static final String LIBNAME = "luajava";
|
||||
|
||||
private static final int INIT = 0;
|
||||
private static final int BINDCLASS = 1;
|
||||
private static final int NEWINSTANCE = 2;
|
||||
private static final int NEW = 3;
|
||||
private static final int CREATEPROXY = 4;
|
||||
private static final int LOADLIB = 5;
|
||||
private static final int BINDCLASS = 0;
|
||||
private static final int NEWINSTANCE = 1;
|
||||
private static final int NEW = 2;
|
||||
private static final int CREATEPROXY = 3;
|
||||
private static final int LOADLIB = 4;
|
||||
|
||||
private static final String[] NAMES = {
|
||||
LIBNAME,
|
||||
"bindClass",
|
||||
"newInstance",
|
||||
"new",
|
||||
@@ -73,18 +70,18 @@ public class LuajavaLib extends VarArgFunction {
|
||||
}
|
||||
|
||||
public LuajavaLib() {
|
||||
name = LIBNAME;
|
||||
opcode = INIT;
|
||||
}
|
||||
|
||||
public Varargs invoke(final Varargs args) {
|
||||
try {
|
||||
switch ( opcode ) {
|
||||
case INIT: {
|
||||
LuaTable t = new LuaTable(0,8);
|
||||
LibFunction.bind( t, this.getClass(), NAMES );
|
||||
public LuaValue call(LuaValue arg) {
|
||||
LuaTable t = new LuaTable();
|
||||
bindv( t, NAMES );
|
||||
env.set("luajava", t);
|
||||
return t;
|
||||
}
|
||||
|
||||
protected Varargs oncallv(int opcode, Varargs args) {
|
||||
try {
|
||||
switch ( opcode ) {
|
||||
case BINDCLASS: {
|
||||
final Class clazz = Class.forName(args.checkString(1));
|
||||
return toUserdata( clazz, clazz );
|
||||
|
||||
@@ -55,7 +55,7 @@ public class LuaOperationsTest extends TestCase {
|
||||
private final LuaValue stringlong = LuaValue.valueOf(samplestringlong);
|
||||
private final LuaValue stringdouble = LuaValue.valueOf(samplestringdouble);
|
||||
private final LuaTable table = LuaValue.listOf( new LuaValue[] { LuaValue.valueOf("aaa"), LuaValue.valueOf("bbb") } );
|
||||
private final LuaFunction somefunc = new ZeroArgFunction("sample",0,table) { public LuaValue call() { return NONE;}};
|
||||
private final LuaValue somefunc = new ZeroArgFunction(table) { public LuaValue call() { return NONE;}};
|
||||
private final LuaThread thread = new LuaThread(somefunc,table);
|
||||
private final Prototype proto = new Prototype();
|
||||
private final LuaClosure someclosure = new LuaClosure(proto,table);
|
||||
@@ -207,7 +207,7 @@ public class LuaOperationsTest extends TestCase {
|
||||
|
||||
// function tests
|
||||
{
|
||||
LuaFunction f = new ZeroArgFunction("f",0,_G) { public LuaValue call() { return env.get("a");}};
|
||||
LuaFunction f = new ZeroArgFunction(_G) { public LuaValue call() { return env.get("a");}};
|
||||
assertEquals( aaa, f.call() );
|
||||
f.setfenv(newenv);
|
||||
assertEquals( newenv, f.getfenv() );
|
||||
|
||||
@@ -70,9 +70,9 @@ public class ScriptDrivenTest extends TestCase {
|
||||
|
||||
// override print()
|
||||
final ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
final PrintStream oldps = BaseLib.STDOUT;
|
||||
final PrintStream oldps = BaseLib.instance.STDOUT;
|
||||
final PrintStream ps = new PrintStream( output );
|
||||
BaseLib.STDOUT = ps;
|
||||
BaseLib.instance.STDOUT = ps;
|
||||
|
||||
// run the script
|
||||
try {
|
||||
@@ -85,7 +85,7 @@ public class ScriptDrivenTest extends TestCase {
|
||||
|
||||
assertEquals(expectedOutput, actualOutput);
|
||||
} finally {
|
||||
BaseLib.STDOUT = oldps;
|
||||
BaseLib.instance.STDOUT = oldps;
|
||||
ps.close();
|
||||
}
|
||||
} catch ( IOException ioe ) {
|
||||
|
||||
Reference in New Issue
Block a user