From 9b5923432712ca0a2e551e187af9184bc144400e Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Thu, 4 Jul 2013 15:54:36 +0000 Subject: [PATCH] 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. --- README.html | 5 +++++ src/core/org/luaj/vm2/LuaValue.java | 22 +++++-------------- src/core/org/luaj/vm2/lib/BaseLib.java | 4 ++-- src/core/org/luaj/vm2/lib/Bit32Lib.java | 4 ++-- src/core/org/luaj/vm2/lib/CoroutineLib.java | 4 ++-- src/core/org/luaj/vm2/lib/DebugLib.java | 4 ++-- src/core/org/luaj/vm2/lib/IoLib.java | 4 ++-- src/core/org/luaj/vm2/lib/MathLib.java | 4 ++-- src/core/org/luaj/vm2/lib/OsLib.java | 4 ++-- src/core/org/luaj/vm2/lib/PackageLib.java | 4 ++-- src/core/org/luaj/vm2/lib/StringLib.java | 4 ++-- src/core/org/luaj/vm2/lib/TableLib.java | 4 ++-- src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java | 4 ++-- src/jse/org/luaj/vm2/lib/jse/JseMathLib.java | 4 ++-- src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java | 3 ++- test/junit/org/luaj/vm2/MathLibTest.java | 9 +++----- test/junit/org/luaj/vm2/RequireClassTest.java | 9 ++++++-- .../vm2/require/RequireSampleSuccess.java | 9 ++++---- 18 files changed, 52 insertions(+), 53 deletions(-) diff --git a/README.html b/README.html index 2509a51a..64ee12de 100644 --- a/README.html +++ b/README.html @@ -849,6 +849,11 @@ Files are no longer hosted at LuaForge.
  • Fix pluggable scripting engine lookup, simplify implementation, and add unit tests.
  • Coerce script engine eval() return values to Java.
  • Fix Lua to Java coercion directly on Java classes.
  • +
  • Fix Globals.load() to call the library with an empty modname and the globals as the environment.
  • +
  • Fix hash codes of double.
  • +
  • Fix bug in luajava overload resolution.
  • +
  • Fix luastring bug where parsing did not check for overflow.
  • +
  • Fix luastring bug where circular dependency randomly caused NullPointerException.
  • diff --git a/src/core/org/luaj/vm2/LuaValue.java b/src/core/org/luaj/vm2/LuaValue.java index a11edb69..4a0b4fb9 100644 --- a/src/core/org/luaj/vm2/LuaValue.java +++ b/src/core/org/luaj/vm2/LuaValue.java @@ -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; } diff --git a/src/core/org/luaj/vm2/lib/BaseLib.java b/src/core/org/luaj/vm2/lib/BaseLib.java index 1266cbb1..7be31f69 100644 --- a/src/core/org/luaj/vm2/lib/BaseLib.java +++ b/src/core/org/luaj/vm2/lib/BaseLib.java @@ -72,11 +72,11 @@ import org.luaj.vm2.Varargs; * @see JmePlatform * @see http://www.lua.org/manual/5.1/manual.html#5.1 */ -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; diff --git a/src/core/org/luaj/vm2/lib/Bit32Lib.java b/src/core/org/luaj/vm2/lib/Bit32Lib.java index a3279ed0..71cc2a50 100644 --- a/src/core/org/luaj/vm2/lib/Bit32Lib.java +++ b/src/core/org/luaj/vm2/lib/Bit32Lib.java @@ -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" diff --git a/src/core/org/luaj/vm2/lib/CoroutineLib.java b/src/core/org/luaj/vm2/lib/CoroutineLib.java index abd6c566..b8c38d4f 100644 --- a/src/core/org/luaj/vm2/lib/CoroutineLib.java +++ b/src/core/org/luaj/vm2/lib/CoroutineLib.java @@ -56,7 +56,7 @@ import org.luaj.vm2.Varargs; * @see JmePlatform * @see http://www.lua.org/manual/5.1/manual.html#5.2 */ -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()); diff --git a/src/core/org/luaj/vm2/lib/DebugLib.java b/src/core/org/luaj/vm2/lib/DebugLib.java index e5483573..83a5048e 100644 --- a/src/core/org/luaj/vm2/lib/DebugLib.java +++ b/src/core/org/luaj/vm2/lib/DebugLib.java @@ -66,7 +66,7 @@ import org.luaj.vm2.Varargs; * @see JmePlatform * @see http://www.lua.org/manual/5.1/manual.html#5.9 */ -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(); diff --git a/src/core/org/luaj/vm2/lib/IoLib.java b/src/core/org/luaj/vm2/lib/IoLib.java index d510ebc2..c1aac248 100644 --- a/src/core/org/luaj/vm2/lib/IoLib.java +++ b/src/core/org/luaj/vm2/lib/IoLib.java @@ -71,7 +71,7 @@ import org.luaj.vm2.Varargs; * @see http://www.lua.org/manual/5.1/manual.html#5.7 */ 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 diff --git a/src/core/org/luaj/vm2/lib/MathLib.java b/src/core/org/luaj/vm2/lib/MathLib.java index 125f16bc..2d975496 100644 --- a/src/core/org/luaj/vm2/lib/MathLib.java +++ b/src/core/org/luaj/vm2/lib/MathLib.java @@ -73,7 +73,7 @@ import org.luaj.vm2.Varargs; * @see JseMathLib * @see http://www.lua.org/manual/5.1/manual.html#5.6 */ -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()); diff --git a/src/core/org/luaj/vm2/lib/OsLib.java b/src/core/org/luaj/vm2/lib/OsLib.java index 529cf55f..4f6484b8 100644 --- a/src/core/org/luaj/vm2/lib/OsLib.java +++ b/src/core/org/luaj/vm2/lib/OsLib.java @@ -73,7 +73,7 @@ import org.luaj.vm2.Varargs; * @see JmePlatform * @see http://www.lua.org/manual/5.1/manual.html#5.8 */ -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) diff --git a/src/core/org/luaj/vm2/lib/PackageLib.java b/src/core/org/luaj/vm2/lib/PackageLib.java index 937aa135..65960eb2 100644 --- a/src/core/org/luaj/vm2/lib/PackageLib.java +++ b/src/core/org/luaj/vm2/lib/PackageLib.java @@ -61,7 +61,7 @@ import org.luaj.vm2.Varargs; * @see JmePlatform * @see http://www.lua.org/manual/5.1/manual.html#5.3 */ -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(); diff --git a/src/core/org/luaj/vm2/lib/StringLib.java b/src/core/org/luaj/vm2/lib/StringLib.java index a5a7b324..b56caa3d 100644 --- a/src/core/org/luaj/vm2/lib/StringLib.java +++ b/src/core/org/luaj/vm2/lib/StringLib.java @@ -59,14 +59,14 @@ import org.luaj.vm2.compiler.DumpState; * @see JmePlatform * @see http://www.lua.org/manual/5.1/manual.html#5.4 */ -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", } ); diff --git a/src/core/org/luaj/vm2/lib/TableLib.java b/src/core/org/luaj/vm2/lib/TableLib.java index eeec59b5..253be18d 100644 --- a/src/core/org/luaj/vm2/lib/TableLib.java +++ b/src/core/org/luaj/vm2/lib/TableLib.java @@ -56,9 +56,9 @@ import org.luaj.vm2.Varargs; * @see JmePlatform * @see http://www.lua.org/manual/5.1/manual.html#5.5 */ -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()); diff --git a/src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java b/src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java index 7d23496c..0ff68d9a 100644 --- a/src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java +++ b/src/jse/org/luaj/vm2/lib/jse/JseBaseLib.java @@ -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; } diff --git a/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java b/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java index 5e0e9531..deadb647 100644 --- a/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java +++ b/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java @@ -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()); diff --git a/src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java b/src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java index 8975077f..a396f1bb 100644 --- a/src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java +++ b/src/jse/org/luaj/vm2/lib/jse/LuajavaLib.java @@ -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); diff --git a/test/junit/org/luaj/vm2/MathLibTest.java b/test/junit/org/luaj/vm2/MathLibTest.java index d9bba861..ba3b290b 100644 --- a/test/junit/org/luaj/vm2/MathLibTest.java +++ b/test/junit/org/luaj/vm2/MathLibTest.java @@ -2,8 +2,7 @@ package org.luaj.vm2; import junit.framework.TestCase; -import org.luaj.vm2.lib.MathLib; -import org.luaj.vm2.lib.jse.JseMathLib; +import org.luaj.vm2.lib.jme.JmePlatform; import org.luaj.vm2.lib.jse.JsePlatform; public class MathLibTest extends TestCase { @@ -13,10 +12,8 @@ public class MathLibTest extends TestCase { private boolean supportedOnJ2me; public MathLibTest() { - LuaValue g = JsePlatform.standardGlobals(); - j2se = g.get("math"); - g.load( new MathLib() ); - j2me = g.get("math"); + j2se = JsePlatform.standardGlobals().get("math"); + j2me = JmePlatform.standardGlobals().get("math"); } protected void setUp() throws Exception { diff --git a/test/junit/org/luaj/vm2/RequireClassTest.java b/test/junit/org/luaj/vm2/RequireClassTest.java index 3f074354..e617a5c4 100644 --- a/test/junit/org/luaj/vm2/RequireClassTest.java +++ b/test/junit/org/luaj/vm2/RequireClassTest.java @@ -16,12 +16,17 @@ public class RequireClassTest extends TestCase { globals = JsePlatform.standardGlobals(); 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() { 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") ); - assertEquals( "require-sample-success", result.tojstring() ); + assertEquals( "require-sample-success-org.luaj.vm2.require.RequireSampleSuccess", result.tojstring() ); } public void testRequireClassLoadLuaError() { diff --git a/test/junit/org/luaj/vm2/require/RequireSampleSuccess.java b/test/junit/org/luaj/vm2/require/RequireSampleSuccess.java index edd0c549..766d3eb8 100644 --- a/test/junit/org/luaj/vm2/require/RequireSampleSuccess.java +++ b/test/junit/org/luaj/vm2/require/RequireSampleSuccess.java @@ -1,17 +1,18 @@ package org.luaj.vm2.require; 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()" */ -public class RequireSampleSuccess extends ZeroArgFunction { +public class RequireSampleSuccess extends TwoArgFunction { public RequireSampleSuccess() { } - public LuaValue call() { - return LuaValue.valueOf("require-sample-success"); + public LuaValue call(LuaValue modname, LuaValue env) { + env.checkglobals(); + return LuaValue.valueOf("require-sample-success-"+modname.tojstring()); } }