Add LuaValue.load() function for library initialization. Change unit tests to use JavaBytecodeCompiler for lua->Java conversion.

This commit is contained in:
James Roseborough
2010-04-03 03:48:53 +00:00
parent d903a85578
commit 46a9527701
7 changed files with 26 additions and 40 deletions

View File

@@ -56,7 +56,7 @@
<javac destdir="build/jse/classes" encoding="utf-8" source="1.5" target="1.5" <javac destdir="build/jse/classes" encoding="utf-8" source="1.5" target="1.5"
classpath="build/core/classes;lib/bcel-5.2.jar" classpath="build/core/classes;lib/bcel-5.2.jar"
srcdir="src/jse" srcdir="src/jse"
excludes="**/antlr/**,**/lst/**,**/JavaCodeGenerator.java" /> excludes="**/antlr/**,**/lst/**,**/JavaCodeGenerator.java,**/LuaJCompiler.java" />
</target> </target>
<target name="jar-jme" depends="compile"> <target name="jar-jme" depends="compile">
@@ -107,6 +107,7 @@
<exclude name="**/antlr/**"/> <exclude name="**/antlr/**"/>
<exclude name="**/lst/**"/> <exclude name="**/lst/**"/>
<exclude name="**/JavaCodeGenerator.java"/> <exclude name="**/JavaCodeGenerator.java"/>
<exclude name="**/LuaJCompiler.java"/>
</fileset> </fileset>
</copy> </copy>
<copy todir="build/luaj-${version}/test"> <copy todir="build/luaj-${version}/test">

View File

@@ -181,6 +181,7 @@ public class LuaValue extends Varargs {
public void presize( int i) { unimplemented("presize"); } public void presize( int i) { unimplemented("presize"); }
public Varargs next(LuaValue index) { unimplemented("next"); return null; } public Varargs next(LuaValue index) { unimplemented("next"); return null; }
public Varargs inext(LuaValue index) { unimplemented("inext"); return null; } public Varargs inext(LuaValue index) { unimplemented("inext"); return null; }
public LuaValue load(LuaValue library) { library.setfenv(this); return library.call(); }
// 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

@@ -401,7 +401,5 @@ public class PackageLib extends OneArgFunction {
default: default:
return false; return false;
} }
} }
} }

View File

@@ -23,11 +23,6 @@ package org.luaj.vm2.lib;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.lib.jme.JmeIoLib; 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 { public class JmePlatform {
@@ -38,23 +33,20 @@ public class JmePlatform {
*/ */
public static LuaTable standardGlobals() { public static LuaTable standardGlobals() {
LuaTable _G = new LuaTable(); LuaTable _G = new LuaTable();
init(_G, new BaseLib()); _G.load(new BaseLib());
init(_G, new PackageLib()); _G.load(new PackageLib());
init(_G, new TableLib()); _G.load(new OsLib());
init(_G, new StringLib()); _G.load(new MathLib());
init(_G, new CoroutineLib()); _G.load(new TableLib());
init(_G, new JmeIoLib()); _G.load(new StringLib());
_G.load(new CoroutineLib());
_G.load(new JmeIoLib());
return _G; return _G;
} }
public static LuaTable debugGlobals() { public static LuaTable debugGlobals() {
LuaTable _G = standardGlobals(); LuaTable _G = standardGlobals();
init(_G, new DebugLib()); _G.load(new DebugLib());
return _G; return _G;
} }
private static void init(LuaTable _G, LibFunction lib) {
lib.setfenv(_G);
lib.call();
}
} }

View File

@@ -37,21 +37,16 @@ public class JsePlatform {
*/ */
public static LuaTable standardGlobals() { public static LuaTable standardGlobals() {
LuaTable _G = new LuaTable(); LuaTable _G = new LuaTable();
init(_G, new JseBaseLib()); _G.load(new JseBaseLib());
init(_G, new PackageLib()); _G.load(new PackageLib());
init(_G, new TableLib()); _G.load(new TableLib());
init(_G, new StringLib()); _G.load(new StringLib());
init(_G, new CoroutineLib()); _G.load(new CoroutineLib());
init(_G, new DebugLib()); _G.load(new DebugLib());
init(_G, new JseMathLib()); _G.load(new JseMathLib());
init(_G, new JseIoLib()); _G.load(new JseIoLib());
init(_G, new JseOsLib()); _G.load(new JseOsLib());
init(_G, new LuajavaLib()); _G.load(new LuajavaLib());
return _G; return _G;
} }
private static void init(LuaTable _G, LibFunction lib) {
lib.setfenv(_G);
lib.call();
}
} }

View File

@@ -87,7 +87,7 @@ public class CompatibiltyTest {
} }
public static class JseBytecodeTest extends CompatibiltyTestSuite { public static class JseBytecodeTest extends CompatibiltyTestSuite {
public JseBytecodeTest() { public JseBytecodeTest() {
super(ScriptDrivenTest.PlatformType.JSE); super(ScriptDrivenTest.PlatformType.LUAJIT);
} }
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();

View File

@@ -32,8 +32,7 @@ import java.io.PrintStream;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.luaj.vm2.lib.BaseLib; import org.luaj.vm2.lib.BaseLib;
import org.luaj.vm2.lib.DebugLib; import org.luaj.vm2.luajc.JavaBytecodeCompiler;
import org.luaj.vm2.luajc.LuaJCompiler;
abstract abstract
public class ScriptDrivenTest extends TestCase { public class ScriptDrivenTest extends TestCase {
@@ -112,7 +111,7 @@ public class ScriptDrivenTest extends TestCase {
return c; return c;
} else { } else {
script = new FileInputStream(file); script = new FileInputStream(file);
return LuaJCompiler.compile( script, name, _G); return JavaBytecodeCompiler.load( script, name, _G);
} }
default: default:
script = new FileInputStream(file); script = new FileInputStream(file);