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

@@ -849,6 +849,11 @@ Files are no longer hosted at LuaForge.
<li>Fix pluggable scripting engine lookup, simplify implementation, and add unit tests.</li> <li>Fix pluggable scripting engine lookup, simplify implementation, and add unit tests.</li>
<li>Coerce script engine eval() return values to Java.</li> <li>Coerce script engine eval() return values to Java.</li>
<li>Fix Lua to Java coercion directly on Java classes.</li> <li>Fix Lua to Java coercion directly on Java classes.</li>
<li>Fix Globals.load() to call the library with an empty modname and the globals as the environment.</li>
<li>Fix hash codes of double.</li>
<li>Fix bug in luajava overload resolution.</li>
<li>Fix luastring bug where parsing did not check for overflow.</li>
<li>Fix luastring bug where circular dependency randomly caused NullPointerException.</li>
</ul></td></tr> </ul></td></tr>
</table></td></tr></table> </table></td></tr></table>

View File

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

View File

@@ -72,11 +72,11 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform * @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> * @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; Globals globals;
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals(); globals = env.checkglobals();
globals.FINDER = this; globals.FINDER = this;
globals.baselib = 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. * Subclass of LibFunction that implements the Lua standard {@code bit32} library.
*/ */
public class Bit32Lib extends OneArgFunction { public class Bit32Lib extends TwoArgFunction {
public Bit32Lib() { public Bit32Lib() {
} }
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable t = new LuaTable(); LuaTable t = new LuaTable();
bind(t, Bit32LibV.class, new String[] { bind(t, Bit32LibV.class, new String[] {
"band", "bnot", "bor", "btest", "bxor", "extract", "replace" "band", "bnot", "bor", "btest", "bxor", "extract", "replace"

View File

@@ -56,7 +56,7 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform * @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> * @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; static long thread_orphan_check_interval = 30000;
@@ -64,7 +64,7 @@ public class CoroutineLib extends OneArgFunction {
Globals globals; Globals globals;
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals(); globals = env.checkglobals();
LuaTable coroutine = new LuaTable(); LuaTable coroutine = new LuaTable();
coroutine.set("create", new create()); coroutine.set("create", new create());

View File

@@ -66,7 +66,7 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform * @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> * @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 CALLS = (null != System.getProperty("CALLS"));
public static final boolean TRACE = (null != System.getProperty("TRACE")); public static final boolean TRACE = (null != System.getProperty("TRACE"));
@@ -94,7 +94,7 @@ public class DebugLib extends OneArgFunction {
Globals globals; Globals globals;
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals(); globals = env.checkglobals();
globals.debuglib = this; globals.debuglib = this;
LuaTable debug = new LuaTable(); 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> * @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 abstract
public class IoLib extends OneArgFunction { public class IoLib extends TwoArgFunction {
abstract abstract
protected class File extends LuaValue{ protected class File extends LuaValue{
@@ -230,7 +230,7 @@ public class IoLib extends OneArgFunction {
protected Globals globals; protected Globals globals;
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals(); globals = env.checkglobals();
// io lib functions // io lib functions

View File

@@ -73,7 +73,7 @@ import org.luaj.vm2.Varargs;
* @see JseMathLib * @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> * @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; public static MathLib MATHLIB = null;
@@ -81,7 +81,7 @@ public class MathLib extends OneArgFunction {
MATHLIB = this; MATHLIB = this;
} }
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable math = new LuaTable(0,30); LuaTable math = new LuaTable(0,30);
math.set("abs", new abs()); math.set("abs", new abs());
math.set("ceil", new ceil()); math.set("ceil", new ceil());

View File

@@ -73,7 +73,7 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform * @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> * @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_PREFIX = ".luaj";
public static String TMP_SUFFIX = "tmp"; public static String TMP_SUFFIX = "tmp";
@@ -114,7 +114,7 @@ public class OsLib extends OneArgFunction {
public OsLib() { public OsLib() {
} }
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
globals = env.checkglobals(); globals = env.checkglobals();
LuaTable os = new LuaTable(); LuaTable os = new LuaTable();
for (int i = 0; i < NAMES.length; ++i) for (int i = 0; i < NAMES.length; ++i)

View File

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

View File

@@ -59,14 +59,14 @@ import org.luaj.vm2.compiler.DumpState;
* @see JmePlatform * @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> * @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 static LuaTable instance;
public StringLib() { public StringLib() {
} }
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
LuaTable t = new LuaTable(); LuaTable t = new LuaTable();
bind(t, StringLib1.class, new String[] { bind(t, StringLib1.class, new String[] {
"dump", "len", "lower", "reverse", "upper", } ); "dump", "len", "lower", "reverse", "upper", } );

View File

@@ -56,9 +56,9 @@ import org.luaj.vm2.Varargs;
* @see JmePlatform * @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> * @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(); LuaTable table = new LuaTable();
table.set("concat", new concat()); table.set("concat", new concat());
table.set("insert", new insert()); 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 { public class JseBaseLib extends org.luaj.vm2.lib.BaseLib {
/** Extend the library loading to set the default value for {@link Globals.STDIN} */ /** Extend the library loading to set the default value for {@link Globals.STDIN} */
public LuaValue call(LuaValue env) { public LuaValue call(LuaValue modname, LuaValue env) {
super.call(env); super.call(modname, env);
env.checkglobals().STDIN = System.in; env.checkglobals().STDIN = System.in;
return env; return env;
} }

View File

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

View File

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

View File

@@ -2,8 +2,7 @@ package org.luaj.vm2;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.luaj.vm2.lib.MathLib; import org.luaj.vm2.lib.jme.JmePlatform;
import org.luaj.vm2.lib.jse.JseMathLib;
import org.luaj.vm2.lib.jse.JsePlatform; import org.luaj.vm2.lib.jse.JsePlatform;
public class MathLibTest extends TestCase { public class MathLibTest extends TestCase {
@@ -13,10 +12,8 @@ public class MathLibTest extends TestCase {
private boolean supportedOnJ2me; private boolean supportedOnJ2me;
public MathLibTest() { public MathLibTest() {
LuaValue g = JsePlatform.standardGlobals(); j2se = JsePlatform.standardGlobals().get("math");
j2se = g.get("math"); j2me = JmePlatform.standardGlobals().get("math");
g.load( new MathLib() );
j2me = g.get("math");
} }
protected void setUp() throws Exception { protected void setUp() throws Exception {

View File

@@ -16,12 +16,17 @@ public class RequireClassTest extends TestCase {
globals = JsePlatform.standardGlobals(); globals = JsePlatform.standardGlobals();
require = globals.get("require"); require = globals.get("require");
} }
public void testLoadClass() {
LuaValue result = globals.load(new org.luaj.vm2.require.RequireSampleSuccess());
assertEquals( "require-sample-success-", result.tojstring() );
}
public void testRequireClassSuccess() { public void testRequireClassSuccess() {
LuaValue result = require.call( LuaValue.valueOf("org.luaj.vm2.require.RequireSampleSuccess") ); LuaValue result = require.call( LuaValue.valueOf("org.luaj.vm2.require.RequireSampleSuccess") );
assertEquals( "require-sample-success", result.tojstring() ); assertEquals( "require-sample-success-org.luaj.vm2.require.RequireSampleSuccess", result.tojstring() );
result = require.call( LuaValue.valueOf("org.luaj.vm2.require.RequireSampleSuccess") ); result = require.call( LuaValue.valueOf("org.luaj.vm2.require.RequireSampleSuccess") );
assertEquals( "require-sample-success", result.tojstring() ); assertEquals( "require-sample-success-org.luaj.vm2.require.RequireSampleSuccess", result.tojstring() );
} }
public void testRequireClassLoadLuaError() { public void testRequireClassLoadLuaError() {

View File

@@ -1,17 +1,18 @@
package org.luaj.vm2.require; package org.luaj.vm2.require;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.ZeroArgFunction; import org.luaj.vm2.lib.TwoArgFunction;
/** /**
* This should succeed as a library that can be loaded dynamically via "require()" * This should succeed as a library that can be loaded dynamically via "require()"
*/ */
public class RequireSampleSuccess extends ZeroArgFunction { public class RequireSampleSuccess extends TwoArgFunction {
public RequireSampleSuccess() { public RequireSampleSuccess() {
} }
public LuaValue call() { public LuaValue call(LuaValue modname, LuaValue env) {
return LuaValue.valueOf("require-sample-success"); env.checkglobals();
return LuaValue.valueOf("require-sample-success-"+modname.tojstring());
} }
} }