Fix Globals.load() to call the library with an empty modname and the globals as the environment. Change standard libraries to be called with two arguments, a modname and an environment.

This commit is contained in:
James Roseborough
2013-07-04 15:54:36 +00:00
parent 120ac758c4
commit 9b59234327
18 changed files with 52 additions and 53 deletions

View File

@@ -1357,24 +1357,14 @@ public class LuaValue extends Varargs {
public Varargs inext(LuaValue index) { return typerror("table"); }
/**
* Load a library instance by setting its environment to the calling object,
* and calling it, which should iniitalize the library instance and
* install itself into this instance. The calling object should be the
* {@link Globals} environment to associate wtih the library.
* Load a library instance by calling it with and empty string as the modname,
* and this Globals as the environment. This is normally used to iniitalize the
* library instance and which may install itself into these globals.
* @param library The callable {@link LuaValue} to load into {@code this}
* @return {@link LuaValue._G} containing the result of the initialization call.
* @param string
* @return {@link LuaValue} returned by the initialization call.
*/
public LuaValue load(LuaValue library) { return load(library, this); }
/**
* Load a library instance by setting its environment to {@code env}
* and calling it, which should iniitalize the library instance and
* install itself into this instance.
* @param library The callable {@link LuaValue} to load into {@code this}
* @param env The {@link LuaValue} to use as the environment for the library.
* @return {@link LuaValue} containing the result of the initialization call.
*/
public LuaValue load(LuaValue library, LuaValue env) { return library.call(env); }
public LuaValue load(LuaValue library) { return library.call(EMPTYSTRING, this); }
// varargs references
public LuaValue arg(int index) { return index==1? this: NIL; }

View File

@@ -72,11 +72,11 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.1">http://www.lua.org/manual/5.1/manual.html#5.1</a>
*/
public class BaseLib extends OneArgFunction implements ResourceFinder {
public class BaseLib extends TwoArgFunction implements ResourceFinder {
Globals globals;
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
globals.FINDER = this;
globals.baselib = this;

View File

@@ -28,12 +28,12 @@ import org.luaj.vm2.Varargs;
/**
* Subclass of LibFunction that implements the Lua standard {@code bit32} library.
*/
public class Bit32Lib extends OneArgFunction {
public class Bit32Lib extends TwoArgFunction {
public Bit32Lib() {
}
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable t = new LuaTable();
bind(t, Bit32LibV.class, new String[] {
"band", "bnot", "bor", "btest", "bxor", "extract", "replace"

View File

@@ -56,7 +56,7 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.2">http://www.lua.org/manual/5.1/manual.html#5.2</a>
*/
public class CoroutineLib extends OneArgFunction {
public class CoroutineLib extends TwoArgFunction {
static long thread_orphan_check_interval = 30000;
@@ -64,7 +64,7 @@ public class CoroutineLib extends OneArgFunction {
Globals globals;
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
LuaTable coroutine = new LuaTable();
coroutine.set("create", new create());

View File

@@ -66,7 +66,7 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.9">http://www.lua.org/manual/5.1/manual.html#5.9</a>
*/
public class DebugLib extends OneArgFunction {
public class DebugLib extends TwoArgFunction {
public static final boolean CALLS = (null != System.getProperty("CALLS"));
public static final boolean TRACE = (null != System.getProperty("TRACE"));
@@ -94,7 +94,7 @@ public class DebugLib extends OneArgFunction {
Globals globals;
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
globals.debuglib = this;
LuaTable debug = new LuaTable();

View File

@@ -71,7 +71,7 @@ import org.luaj.vm2.Varargs;
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.7">http://www.lua.org/manual/5.1/manual.html#5.7</a>
*/
abstract
public class IoLib extends OneArgFunction {
public class IoLib extends TwoArgFunction {
abstract
protected class File extends LuaValue{
@@ -230,7 +230,7 @@ public class IoLib extends OneArgFunction {
protected Globals globals;
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
// io lib functions

View File

@@ -73,7 +73,7 @@ import org.luaj.vm2.Varargs;
* @see JseMathLib
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.6">http://www.lua.org/manual/5.1/manual.html#5.6</a>
*/
public class MathLib extends OneArgFunction {
public class MathLib extends TwoArgFunction {
public static MathLib MATHLIB = null;
@@ -81,7 +81,7 @@ public class MathLib extends OneArgFunction {
MATHLIB = this;
}
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable math = new LuaTable(0,30);
math.set("abs", new abs());
math.set("ceil", new ceil());

View File

@@ -73,7 +73,7 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.8">http://www.lua.org/manual/5.1/manual.html#5.8</a>
*/
public class OsLib extends OneArgFunction {
public class OsLib extends TwoArgFunction {
public static String TMP_PREFIX = ".luaj";
public static String TMP_SUFFIX = "tmp";
@@ -114,7 +114,7 @@ public class OsLib extends OneArgFunction {
public OsLib() {
}
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
LuaTable os = new LuaTable();
for (int i = 0; i < NAMES.length; ++i)

View File

@@ -61,7 +61,7 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.3">http://www.lua.org/manual/5.1/manual.html#5.3</a>
*/
public class PackageLib extends OneArgFunction {
public class PackageLib extends TwoArgFunction {
/** The default value to use for package.path. This can be set with the system property
* "luaj.package.path", and is "?.lua" by default. */
@@ -99,7 +99,7 @@ public class PackageLib extends OneArgFunction {
public PackageLib() {}
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals();
globals.set("require", new require());
package_ = new LuaTable();

View File

@@ -59,14 +59,14 @@ import org.luaj.vm2.compiler.DumpState;
* @see JmePlatform
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.4">http://www.lua.org/manual/5.1/manual.html#5.4</a>
*/
public class StringLib extends OneArgFunction {
public class StringLib extends TwoArgFunction {
public static LuaTable instance;
public StringLib() {
}
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable t = new LuaTable();
bind(t, StringLib1.class, new String[] {
"dump", "len", "lower", "reverse", "upper", } );

View File

@@ -56,9 +56,9 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform
* @see <a href="http://www.lua.org/manual/5.1/manual.html#5.5">http://www.lua.org/manual/5.1/manual.html#5.5</a>
*/
public class TableLib extends OneArgFunction {
public class TableLib extends TwoArgFunction {
public LuaValue call(LuaValue env) {
public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable table = new LuaTable();
table.set("concat", new concat());
table.set("insert", new insert());

View File

@@ -68,8 +68,8 @@ import org.luaj.vm2.lib.ResourceFinder;
public class JseBaseLib extends org.luaj.vm2.lib.BaseLib {
/** Extend the library loading to set the default value for {@link Globals.STDIN} */
public LuaValue call(LuaValue env) {
super.call(env);
public LuaValue call(LuaValue modname, LuaValue env) {
super.call(modname, env);
env.checkglobals().STDIN = System.in;
return env;
}

View File

@@ -58,8 +58,8 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib {
public JseMathLib() {}
public LuaValue call(LuaValue env) {
super.call(env);
public LuaValue call(LuaValue modname, LuaValue env) {
super.call(modname, env);
LuaValue math = env.get("math");
math.set("acos", new acos());
math.set("asin", new asin());

View File

@@ -98,7 +98,8 @@ public class LuajavaLib extends VarArgFunction {
try {
switch ( opcode ) {
case INIT: {
LuaValue env = args.arg1();
// LuaValue modname = args.arg1();
LuaValue env = args.arg(2);
LuaTable t = new LuaTable();
bind( t, LuajavaLib.class, NAMES, BINDCLASS );
env.set("luajava", t);