From 46a9527701f574ffa55ff9e2c87d2896b147a3ed Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Sat, 3 Apr 2010 03:48:53 +0000 Subject: [PATCH] Add LuaValue.load() function for library initialization. Change unit tests to use JavaBytecodeCompiler for lua->Java conversion. --- build.xml | 3 ++- src/core/org/luaj/vm2/LuaValue.java | 1 + src/core/org/luaj/vm2/lib/PackageLib.java | 4 +-- src/jme/org/luaj/vm2/lib/JmePlatform.java | 26 +++++++------------ src/jse/org/luaj/vm2/lib/JsePlatform.java | 25 +++++++----------- test/junit/org/luaj/vm2/CompatibiltyTest.java | 2 +- test/junit/org/luaj/vm2/ScriptDrivenTest.java | 5 ++-- 7 files changed, 26 insertions(+), 40 deletions(-) diff --git a/build.xml b/build.xml index 2e288e47..de8bdbd0 100644 --- a/build.xml +++ b/build.xml @@ -56,7 +56,7 @@ + excludes="**/antlr/**,**/lst/**,**/JavaCodeGenerator.java,**/LuaJCompiler.java" /> @@ -107,6 +107,7 @@ + diff --git a/src/core/org/luaj/vm2/LuaValue.java b/src/core/org/luaj/vm2/LuaValue.java index 99aeb05d..d3fcce9b 100644 --- a/src/core/org/luaj/vm2/LuaValue.java +++ b/src/core/org/luaj/vm2/LuaValue.java @@ -181,6 +181,7 @@ public class LuaValue extends Varargs { public void presize( int i) { unimplemented("presize"); } public Varargs next(LuaValue index) { unimplemented("next"); return null; } public Varargs inext(LuaValue index) { unimplemented("inext"); return null; } + public LuaValue load(LuaValue library) { library.setfenv(this); return library.call(); } // varargs references public LuaValue arg(int index) { return index==1? this: NIL; } diff --git a/src/core/org/luaj/vm2/lib/PackageLib.java b/src/core/org/luaj/vm2/lib/PackageLib.java index 4a5d9442..efb8d0dc 100644 --- a/src/core/org/luaj/vm2/lib/PackageLib.java +++ b/src/core/org/luaj/vm2/lib/PackageLib.java @@ -401,7 +401,5 @@ public class PackageLib extends OneArgFunction { default: return false; } - } - - + } } diff --git a/src/jme/org/luaj/vm2/lib/JmePlatform.java b/src/jme/org/luaj/vm2/lib/JmePlatform.java index c00a1b82..a4e2ee97 100644 --- a/src/jme/org/luaj/vm2/lib/JmePlatform.java +++ b/src/jme/org/luaj/vm2/lib/JmePlatform.java @@ -23,11 +23,6 @@ package org.luaj.vm2.lib; import org.luaj.vm2.LuaTable; import org.luaj.vm2.lib.jme.JmeIoLib; -import org.luaj.vm2.lib.jse.JseBaseLib; -import org.luaj.vm2.lib.jse.JseIoLib; -import org.luaj.vm2.lib.jse.JseMathLib; -import org.luaj.vm2.lib.jse.JseOsLib; -import org.luaj.vm2.lib.jse.LuajavaLib; public class JmePlatform { @@ -38,23 +33,20 @@ public class JmePlatform { */ public static LuaTable standardGlobals() { LuaTable _G = new LuaTable(); - init(_G, new BaseLib()); - init(_G, new PackageLib()); - init(_G, new TableLib()); - init(_G, new StringLib()); - init(_G, new CoroutineLib()); - init(_G, new JmeIoLib()); + _G.load(new BaseLib()); + _G.load(new PackageLib()); + _G.load(new OsLib()); + _G.load(new MathLib()); + _G.load(new TableLib()); + _G.load(new StringLib()); + _G.load(new CoroutineLib()); + _G.load(new JmeIoLib()); return _G; } public static LuaTable debugGlobals() { LuaTable _G = standardGlobals(); - init(_G, new DebugLib()); + _G.load(new DebugLib()); return _G; } - - private static void init(LuaTable _G, LibFunction lib) { - lib.setfenv(_G); - lib.call(); - } } diff --git a/src/jse/org/luaj/vm2/lib/JsePlatform.java b/src/jse/org/luaj/vm2/lib/JsePlatform.java index 22b15b1b..6e1930bb 100644 --- a/src/jse/org/luaj/vm2/lib/JsePlatform.java +++ b/src/jse/org/luaj/vm2/lib/JsePlatform.java @@ -37,21 +37,16 @@ public class JsePlatform { */ public static LuaTable standardGlobals() { 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()); + _G.load(new JseBaseLib()); + _G.load(new PackageLib()); + _G.load(new TableLib()); + _G.load(new StringLib()); + _G.load(new CoroutineLib()); + _G.load(new DebugLib()); + _G.load(new JseMathLib()); + _G.load(new JseIoLib()); + _G.load(new JseOsLib()); + _G.load(new LuajavaLib()); return _G; } - - private static void init(LuaTable _G, LibFunction lib) { - lib.setfenv(_G); - lib.call(); - } } diff --git a/test/junit/org/luaj/vm2/CompatibiltyTest.java b/test/junit/org/luaj/vm2/CompatibiltyTest.java index 0af9f249..e3722920 100644 --- a/test/junit/org/luaj/vm2/CompatibiltyTest.java +++ b/test/junit/org/luaj/vm2/CompatibiltyTest.java @@ -87,7 +87,7 @@ public class CompatibiltyTest { } public static class JseBytecodeTest extends CompatibiltyTestSuite { public JseBytecodeTest() { - super(ScriptDrivenTest.PlatformType.JSE); + super(ScriptDrivenTest.PlatformType.LUAJIT); } protected void setUp() throws Exception { super.setUp(); diff --git a/test/junit/org/luaj/vm2/ScriptDrivenTest.java b/test/junit/org/luaj/vm2/ScriptDrivenTest.java index 53f2cc1f..47ecfcc3 100644 --- a/test/junit/org/luaj/vm2/ScriptDrivenTest.java +++ b/test/junit/org/luaj/vm2/ScriptDrivenTest.java @@ -32,8 +32,7 @@ import java.io.PrintStream; import junit.framework.TestCase; import org.luaj.vm2.lib.BaseLib; -import org.luaj.vm2.lib.DebugLib; -import org.luaj.vm2.luajc.LuaJCompiler; +import org.luaj.vm2.luajc.JavaBytecodeCompiler; abstract public class ScriptDrivenTest extends TestCase { @@ -112,7 +111,7 @@ public class ScriptDrivenTest extends TestCase { return c; } else { script = new FileInputStream(file); - return LuaJCompiler.compile( script, name, _G); + return JavaBytecodeCompiler.load( script, name, _G); } default: script = new FileInputStream(file);