Refactor library initialization code.
This commit is contained in:
@@ -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()));
|
||||
@@ -62,15 +61,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) {
|
||||
switch ( opcode ) {
|
||||
case 0: return valueOf(Math.atan2(arg1.todouble(), arg2.todouble()));
|
||||
case 1: return valueOf(Math.pow(arg1.todouble(), arg2.todouble()));
|
||||
}
|
||||
return NIL;
|
||||
|
||||
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 ^ */
|
||||
|
||||
@@ -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) {
|
||||
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 INIT: {
|
||||
LuaTable t = new LuaTable(0,8);
|
||||
LibFunction.bind( t, this.getClass(), NAMES );
|
||||
return t;
|
||||
}
|
||||
case BINDCLASS: {
|
||||
final Class clazz = Class.forName(args.checkString(1));
|
||||
return toUserdata( clazz, clazz );
|
||||
|
||||
Reference in New Issue
Block a user