diff --git a/src/main/java/lua/GlobalState.java b/src/main/java/lua/GlobalState.java index 84723cf0..35f6e1cf 100644 --- a/src/main/java/lua/GlobalState.java +++ b/src/main/java/lua/GlobalState.java @@ -26,6 +26,10 @@ public class GlobalState { private static LTable _G; static { + resetGlobals(); + } + + static void resetGlobals() { _G = new LTable(); _G .put( "_G", _G ); Builtin.addBuiltins( _G ); diff --git a/src/test/java/lua/LuaJTest.java b/src/test/java/lua/LuaJTest.java index 1f0c08dc..fd566be2 100644 --- a/src/test/java/lua/LuaJTest.java +++ b/src/test/java/lua/LuaJTest.java @@ -43,6 +43,10 @@ public class LuaJTest extends TestCase { runTest( "test7" ); } + public void testAutoload() throws IOException, InterruptedException { + runTest( "autoload" ); + } + public void testBoolean() throws IOException, InterruptedException { runTest( "boolean" ); } @@ -68,6 +72,10 @@ public class LuaJTest extends TestCase { } private void runTest( String testName ) throws IOException, InterruptedException { + + // Reset the _G table just in case some test mucks with it + GlobalState.resetGlobals(); + // add LuaJava bindings LuaJava.install(); diff --git a/src/test/res/autoload.lua b/src/test/res/autoload.lua new file mode 100644 index 00000000..723e53ed --- /dev/null +++ b/src/test/res/autoload.lua @@ -0,0 +1,11 @@ +local function autoload(table, key) + local chunk = loadfile("/"..key..".luac") + table[key] = chunk() +end + +autoload_mt = { __index = autoload } + +setmetatable(_G, autoload_mt) + +print("square root of 9.0 is ", math.sqrt(9.0)) +print("math.pi=", math.pi); diff --git a/src/test/res/autoload.luac b/src/test/res/autoload.luac new file mode 100644 index 00000000..e49e9fc4 Binary files /dev/null and b/src/test/res/autoload.luac differ diff --git a/src/test/res/math.lua b/src/test/res/math.lua new file mode 100644 index 00000000..13dc6b8d --- /dev/null +++ b/src/test/res/math.lua @@ -0,0 +1,7 @@ +local mathClass = luajava.bindClass("java.lang.Math") + +local function sqrt(x) + return mathClass:sqrt(x) +end + +return { sqrt = sqrt; pi = mathClass.PI } diff --git a/src/test/res/math.luac b/src/test/res/math.luac new file mode 100644 index 00000000..6152d02f Binary files /dev/null and b/src/test/res/math.luac differ