diff --git a/README.html b/README.html index 6c7f0fe2..a80cc443 100644 --- a/README.html +++ b/README.html @@ -241,15 +241,22 @@ Lua scripts can also be run directly in this mode without precompiling using the
-The following pattern is used within Java SE +A simple hello, world example in luaj is:
import org.luaj.vm2.*;
import org.luaj.vm2.lib.jse.*;
- String script = "examples/lua/hello.lua";
- LuaValue _G = JsePlatform.standardGlobals();
- _G.get("dofile").call( LuaValue.valueOf(script) );
+ Globals globals = JsePlatform.standardGlobals();
+ LuaValue chunk = globals.load("print 'hello, world'");
+ chunk.call();
+
+
+
+Loading from a file is done via Globals.loadFile():
+
+
+ LuaValue chunk = globals.loadfile("examples/lua/hello.lua");
@@ -270,13 +277,13 @@ The for MIDlets the JmePlatform is used instead: import org.luaj.vm2.*; import org.luaj.vm2.lib.jme.*; - String script = "examples/lua/hello.lua"; - LuaValue _G = JmePlatform.standardGlobals(); - _G.get("dofile").call( LuaValue.valueOf(script) ); + Globals globals = JmePlatform.standardGlobals(); + LuaValue chunk = globals.loadfile("examples/lua/hello.lua"); + chunk.call();
-The file must be a resource within within the midlet jar for dofile() to find it. +The file must be a resource within within the midlet jar for the loader to find it. Any files included via require() must also be part of the midlet resources.
@@ -310,6 +317,8 @@ The standard use of JSR-223 scripting engines may be used: System.out.println( "y="+e.get("y") ); +You can also look up the engine by language "lua" or mimetypes "text/lua" or "application/lua". +
All standard aspects of script engines including compiled statements should be supported. @@ -341,7 +350,7 @@ To exclude the lua-to-lua-bytecode compiler, do not call but instead initialize globals with including only those libraries that are needed and omitting the line:
- org.luaj.vm2.compiler.LuaC.install(); + org.luaj.vm2.compiler.LuaC.install(globals);@@ -349,10 +358,10 @@ that are needed and omitting the line:
To compile from lua to Java bytecode for all lua loaded at runtime, -install the LuaJC compiler after globals have been created using: +install the LuaJC compiler into a globals object use:
- org.luaj.vm2.jse.luajc.LuaJC.install(); + org.luaj.vm2.jse.luajc.LuaJC.install(globals);
@@ -389,8 +398,8 @@ Luaj 3.0 can be run in multiple threads, with the following restrictions:
* A simple pattern for loading and executing code is *
{@code
-* LuaValue _G = JsePlatform.standardGlobals();
-* LoadState.load( new FileInputStream("main.lua"), "main.lua", _G ).call();
+* Globals _G = JsePlatform.standardGlobals();
+* _G.load(new FileReader("main.lua"), "main.lua", _G ).call();
* }
-* This should work regardless of which {@link LuaCompiler}
+* This should work regardless of which {@link Globals.Compiler}
* has been installed.
* * @@ -52,7 +52,7 @@ import java.io.InputStream; * for example: *
{@code
* LuaValue _G = JsePlatform.standardGlobals();
-* LuaJC.install();
+* LuaJC.install(_G);
* LoadState.load( new FileInputStream("main.lua"), "main.lua", _G ).call();
* }
*
@@ -64,7 +64,10 @@ import java.io.InputStream;
* @see LuaC
* @see LuaJC
*/
-public class LoadState {
+public class LoadState implements Globals.Undumper {
+
+ /** Shared instance of Globals.Undumper to use loading prototypes from binary lua files */
+ public static final Globals.Undumper instance = new LoadState();
/** format corresponding to non-number-patched lua, all numbers are floats or doubles */
public static final int NUMBER_FORMAT_FLOATS_OR_DOUBLES = 0;
@@ -89,22 +92,9 @@ public class LoadState {
public static final int LUA_TTHREAD = 8;
public static final int LUA_TVALUE = 9;
- /** Interface for the compiler, if it is installed.
- * - * See the {@link LuaClosure} documentation for examples of how to use the compiler. - * @see LuaClosure - * @see #load(InputStream, String, LuaValue) - * */ - public interface LuaCompiler { - - /** Load into a Closure or LuaFunction from a Stream and initializes the environment - * @throws IOException */ - public LuaFunction load(InputStream stream, String filename, LuaValue env) throws IOException; - } - - /** Compiler instance, if installed */ - public static LuaCompiler compiler = null; - + /** The character encoding to use for file encoding. Null means the default encoding */ + public static String encoding = null; + /** Signature byte indicating the file is a compiled binary chunk */ public static final byte[] LUA_SIGNATURE = { '\033', 'L', 'u', 'a' }; @@ -150,7 +140,11 @@ public class LoadState { /** Read buffer */ private byte[] buf = new byte[512]; - + + /** Install this class as the standard Globals.Undumper for the supplied Globals */ + public static void install(Globals globals) { + globals.undumper = instance; + } /** Load a 4-byte int value from the input stream * @return the int value laoded. @@ -369,60 +363,22 @@ public class LoadState { } /** - * Load lua in either binary or text from an input stream. - * @param firstByte the first byte of the input stream + * Load input stream as a lua binary chunk if the first 4 bytes are the lua binary signature. * @param stream InputStream to read, after having read the first byte already * @param name Name to apply to the loaded chunk - * @param mode "b" for binary only, "t" for text only, "bt" for binary or text. - * @return {@link Prototype} that was loaded - * @throws IllegalArgumentException if the signature is bac + * @return {@link Prototype} that was loaded, or null if the first 4 bytes were not the lua signature. * @throws IOException if an IOException occurs */ - public static LuaFunction load( InputStream stream, String name, String mode, LuaValue env ) throws IOException { - if ( compiler != null ) - return compiler.load(stream, name, env); - else { - int firstByte = stream.read(); - if ( firstByte != LUA_SIGNATURE[0] ) - throw new LuaError("no compiler"); - Prototype p = loadBinaryChunk( firstByte, stream, name ); - return new LuaClosure( p, env ); - } - } - - /** - * Load lua in the default mode "bt" from an input stream. - * @param firstByte the first byte of the input stream - * @param stream InputStream to read, after having read the first byte already - * @param name Name to apply to the loaded chunk - * @return {@link Prototype} that was loaded - * @throws IllegalArgumentException if the signature is bac - * @throws IOException if an IOException occurs - */ - public static LuaFunction load( InputStream stream, String name, LuaValue env ) throws IOException { - return load(stream, name, "bt", env); - } - - /** - * Load lua thought to be a binary chunk from its first byte from an input stream. - * @param firstByte the first byte of the input stream - * @param stream InputStream to read, after having read the first byte already - * @param name Name to apply to the loaded chunk - * @return {@link Prototype} that was loaded - * @throws IllegalArgumentException if the signature is bac - * @throws IOException if an IOException occurs - */ - public static Prototype loadBinaryChunk( int firstByte, InputStream stream, String name ) throws IOException { - + public Prototype undump(InputStream stream, String chunkname) throws IOException { // check rest of signature - if ( firstByte != LUA_SIGNATURE[0] + if ( stream.read() != LUA_SIGNATURE[0] || stream.read() != LUA_SIGNATURE[1] || stream.read() != LUA_SIGNATURE[2] || stream.read() != LUA_SIGNATURE[3] ) - throw new IllegalArgumentException("bad signature"); + return null; // load file as a compiled chunk - String sname = getSourceName(name); + String sname = getSourceName(chunkname); LoadState s = new LoadState( stream, sname ); s.loadHeader(); @@ -457,4 +413,9 @@ public class LoadState { this.name = name; this.is = new DataInputStream( stream ); } + + private LoadState() { + this.name = ""; + this.is = null; + } } diff --git a/src/core/org/luaj/vm2/LuaString.java b/src/core/org/luaj/vm2/LuaString.java index 1557e3a2..6bac4180 100644 --- a/src/core/org/luaj/vm2/LuaString.java +++ b/src/core/org/luaj/vm2/LuaString.java @@ -26,6 +26,7 @@ import java.io.ByteArrayInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.PrintStream; import org.luaj.vm2.lib.MathLib; import org.luaj.vm2.lib.StringLib; @@ -48,7 +49,7 @@ import org.luaj.vm2.lib.StringLib; * When Java Strings are used to initialize {@link LuaString} data, the UTF8 encoding is assumed. * The functions * {@link LuaString#lengthAsUtf8(char[]), - * {@link LuaString#encodeToUtf8(char[], byte[], int)}, and + * {@link LuaString#encodeToUtf8(char[], int, byte[], int)}, and * {@link LuaString#decodeAsUtf8(byte[], int, int) * are used to convert back and forth between UTF8 byte arrays and character arrays. * @@ -108,7 +109,7 @@ public class LuaString extends LuaValue { public static LuaString valueOf(String string) { char[] c = string.toCharArray(); byte[] b = new byte[lengthAsUtf8(c)]; - encodeToUtf8(c, b, 0); + encodeToUtf8(c, c.length, b, 0); return valueOf(b, 0, b.length); } @@ -143,20 +144,31 @@ public class LuaString extends LuaValue { /** Construct a {@link LuaString} using the supplied characters as byte values. *
- * Only th elow-order 8-bits of each character are used, the remainder is ignored. + * Only the low-order 8-bits of each character are used, the remainder is ignored. *
* This is most useful for constructing byte sequences that do not conform to UTF8.
* @param bytes array of char, whose values are truncated at 8-bits each and put into a byte array.
* @return {@link LuaString} wrapping a copy of the byte buffer
*/
public static LuaString valueOf(char[] bytes) {
- int n = bytes.length;
- byte[] b = new byte[n];
- for ( int i=0; i
+ * This is most useful for constructing byte sequences that do not conform to UTF8.
+ * @param bytes array of char, whose values are truncated at 8-bits each and put into a byte array.
+ * @return {@link LuaString} wrapping a copy of the byte buffer
+ */
+ public static LuaString valueOf(char[] bytes, int off, int len) {
+ byte[] b = new byte[len];
+ for ( int i=0; i
- * Implements the {@link LuaCompiler} interface for loading
+ * Implements the {@link Globals.Compiler} interface for loading
* initialized chunks, which is an interface common to
* lua bytecode compiling and java bytecode compiling.
*
@@ -66,7 +66,7 @@ import org.luaj.vm2.LoadState.LuaCompiler;
* @see LuaCompiler
* @see Prototype
*/
-public class LuaC extends Lua implements LuaCompiler {
+public class LuaC extends Lua implements Globals.Compiler, Globals.Loader {
public static final LuaC instance = new LuaC();
@@ -74,8 +74,9 @@ public class LuaC extends Lua implements LuaCompiler {
* try to use it when handed bytes that are
* not already a compiled lua chunk.
*/
- public static void install() {
- org.luaj.vm2.LoadState.compiler = instance;
+ public static void install(Globals g) {
+ g.compiler = instance;
+ g.loader = instance;
}
protected static void _assert(boolean b) {
@@ -213,6 +214,13 @@ public class LuaC extends Lua implements LuaCompiler {
return a;
}
+ static char[] realloc(char[] v, int n) {
+ char[] a = new char[n];
+ if ( v != null )
+ System.arraycopy(v, 0, a, 0, Math.min(v.length,n));
+ return a;
+ }
+
public int nCcalls;
Hashtable strings;
@@ -221,29 +229,25 @@ public class LuaC extends Lua implements LuaCompiler {
private LuaC(Hashtable strings) {
this.strings = strings;
}
-
- /** Load into a Closure or LuaFunction, with the supplied initial environment */
- public LuaFunction load(InputStream stream, String name, LuaValue env) throws IOException {
- Prototype p = compile( stream, name );
- return new LuaClosure( p, env );
- }
- /** Compile a prototype or load as a binary chunk */
- public static Prototype compile(InputStream stream, String name) throws IOException {
- int firstByte = stream.read();
- return ( firstByte == '\033' )?
- LoadState.loadBinaryChunk(firstByte, stream, name):
- (new LuaC(new Hashtable())).luaY_parser(firstByte, stream, name);
+ /** Compile lua source into a Prototype.
+ * @param stream InputStream representing the text source conforming to lua source syntax.
+ * @param chunkname String name of the chunk to use.
+ * @return Prototype representing the lua chunk for this source.
+ * @throws IOException
+ */
+ public Prototype compile(InputStream stream, String chunkname) throws IOException {
+ return (new LuaC(new Hashtable())).luaY_parser(stream, chunkname);
}
/** Parse the input */
- private Prototype luaY_parser(int firstByte, InputStream z, String name) {
+ private Prototype luaY_parser(InputStream z, String name) throws IOException{
LexState lexstate = new LexState(this, z);
FuncState funcstate = new FuncState();
// lexstate.buff = buff;
lexstate.fs = funcstate;
- lexstate.setinput( this, firstByte, z, (LuaString) LuaValue.valueOf(name) );
+ lexstate.setinput( this, z.read(), z, (LuaString) LuaValue.valueOf(name) );
/* main func. is always vararg */
funcstate.f = new Prototype();
funcstate.f.source = (LuaString) LuaValue.valueOf(name);
@@ -256,25 +260,28 @@ public class LuaC extends Lua implements LuaCompiler {
}
// look up and keep at most one copy of each string
- public LuaString newTString(byte[] bytes, int offset, int len) {
- LuaString tmp = LuaString.valueOf(bytes, offset, len);
- LuaString v = (LuaString) strings.get(tmp);
- if ( v == null ) {
- // must copy bytes, since bytes could be from reusable buffer
- byte[] copy = new byte[len];
- System.arraycopy(bytes, offset, copy, 0, len);
- v = LuaString.valueOf(copy);
- strings.put(v, v);
- }
- return v;
+ public LuaString newTString(String s) {
+ return cachedLuaString(LuaString.valueOf(s));
+ }
+
+ // look up and keep at most one copy of each string
+ public LuaString newTString(LuaString s) {
+ return cachedLuaString(s);
+ }
+
+ public LuaString cachedLuaString(LuaString s) {
+ LuaString c = (LuaString) strings.get(s);
+ if (c != null)
+ return c;
+ strings.put(s, s);
+ return s;
}
public String pushfstring(String string) {
return string;
}
- public LuaFunction load(Prototype p, String filename, LuaValue env) {
- return new LuaClosure( p, env );
+ public LuaFunction load(Prototype prototype, String chunkname, LuaValue env) throws IOException {
+ return new LuaClosure(prototype, env);
}
-
}
diff --git a/src/core/org/luaj/vm2/lib/BaseLib.java b/src/core/org/luaj/vm2/lib/BaseLib.java
index 7be31f69..c246def7 100644
--- a/src/core/org/luaj/vm2/lib/BaseLib.java
+++ b/src/core/org/luaj/vm2/lib/BaseLib.java
@@ -228,9 +228,9 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
public Varargs invoke(Varargs args) {
LuaValue tostring = globals.get("tostring");
for ( int i=1, n=args.narg(); i<=n; i++ ) {
- if ( i>1 ) globals.STDOUT.write( '\t' );
+ if ( i>1 ) globals.STDOUT.print( '\t' );
LuaString s = tostring.call( args.arg(i) ).strvalue();
- globals.STDOUT.write( s.m_bytes, s.m_offset, s.m_length );
+ globals.STDOUT.print(s.tojstring());
}
globals.STDOUT.println();
return NONE;
@@ -438,7 +438,7 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
try {
if ( is == null )
return varargsOf(NIL, valueOf("not found: "+chunkname));
- return LoadState.load(is, chunkname, mode, env);
+ return globals.load(is, chunkname, mode, env);
} catch (Exception e) {
return varargsOf(NIL, valueOf(e.getMessage()));
}
diff --git a/src/core/org/luaj/vm2/lib/DebugLib.java b/src/core/org/luaj/vm2/lib/DebugLib.java
index 83a5048e..82fa0857 100644
--- a/src/core/org/luaj/vm2/lib/DebugLib.java
+++ b/src/core/org/luaj/vm2/lib/DebugLib.java
@@ -22,7 +22,6 @@
package org.luaj.vm2.lib;
import org.luaj.vm2.Globals;
-import org.luaj.vm2.LoadState.LuaCompiler;
import org.luaj.vm2.Lua;
import org.luaj.vm2.LuaBoolean;
import org.luaj.vm2.LuaClosure;
diff --git a/src/core/org/luaj/vm2/lib/OsLib.java b/src/core/org/luaj/vm2/lib/OsLib.java
index 632eff4c..1e23e112 100644
--- a/src/core/org/luaj/vm2/lib/OsLib.java
+++ b/src/core/org/luaj/vm2/lib/OsLib.java
@@ -151,7 +151,7 @@ public class OsLib extends TwoArgFunction {
tbl.set("min", LuaValue.valueOf(d.get(Calendar.MINUTE)));
tbl.set("sec", LuaValue.valueOf(d.get(Calendar.SECOND)));
tbl.set("wday", LuaValue.valueOf(d.get(Calendar.DAY_OF_WEEK)));
- tbl.set("yday", LuaValue.valueOf(d.get(Calendar.DAY_OF_YEAR)));
+ tbl.set("yday", LuaValue.valueOf(d.get(0x6))); // Day of year
tbl.set("isdst", LuaValue.valueOf(isDaylightSavingsTime(d)));
return tbl;
}
diff --git a/src/core/org/luaj/vm2/lib/PackageLib.java b/src/core/org/luaj/vm2/lib/PackageLib.java
index 65960eb2..bfdae8a3 100644
--- a/src/core/org/luaj/vm2/lib/PackageLib.java
+++ b/src/core/org/luaj/vm2/lib/PackageLib.java
@@ -240,7 +240,7 @@ public class PackageLib extends TwoArgFunction {
LuaString filename = v.arg1().strvalue();
// Try to load the file.
- v = globals.loadFile(filename.tojstring());
+ v = globals.loadfile(filename.tojstring());
if ( v.arg1().isfunction() )
return LuaValue.varargsOf(v.arg1(), filename);
diff --git a/src/jme/org/luaj/vm2/lib/jme/JmePlatform.java b/src/jme/org/luaj/vm2/lib/jme/JmePlatform.java
index ff9c793f..fedc6b09 100644
--- a/src/jme/org/luaj/vm2/lib/jme/JmePlatform.java
+++ b/src/jme/org/luaj/vm2/lib/jme/JmePlatform.java
@@ -24,7 +24,6 @@ package org.luaj.vm2.lib.jme;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
-import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaThread;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.BaseLib;
@@ -114,8 +113,8 @@ public class JmePlatform {
_G.load(new StringLib());
_G.load(new CoroutineLib());
_G.load(new JmeIoLib());
- LuaC.install();
- _G.compiler = LuaC.instance;
+ LoadState.install(_G);
+ LuaC.install(_G);
return _G;
}
diff --git a/src/jse/lua.java b/src/jse/lua.java
index eb4896e2..aa161799 100644
--- a/src/jse/lua.java
+++ b/src/jse/lua.java
@@ -56,6 +56,7 @@ public class lua {
" -b use luajc bytecode-to-bytecode compiler (requires bcel on class path)\n" +
" -n nodebug - do not load debug library by default\n" +
" -p print the prototype\n" +
+ " -c enc use the supplied encoding 'enc' for input files\n" +
" -- stop handling options\n" +
" - execute stdin and stop handling options";
@@ -66,6 +67,7 @@ public class lua {
private static Globals _G;
private static boolean print = false;
+ private static String encoding = null;
public static void main( String[] args ) throws IOException {
@@ -113,6 +115,11 @@ public class lua {
case 'p':
print = true;
break;
+ case 'c':
+ if ( ++i >= args.length )
+ usageExit();
+ encoding = args[i];
+ break;
case '-':
if ( args[i].length() > 2 )
usageExit();
@@ -131,7 +138,7 @@ public class lua {
// new lua state
_G = nodebug? JsePlatform.standardGlobals(): JsePlatform.debugGlobals();
- if ( luajc ) LuaJC.install();
+ if ( luajc ) LuaJC.install(_G);
for ( int i=0, n=libs!=null? libs.size(): 0; i
* If createDebugGlobals is set, the globals
* created will be a debug globals that includes the debug
@@ -73,11 +75,15 @@ public class LuajContext extends SimpleScriptContext implements ScriptContext {
* have negative impact on performance.
* @param createDebugGlobals true to create debug globals,
* false for standard globals.
+ * @param useLuaJCCompiler true to use the luajc compiler,
+ * reqwuires bcel to be on the class path.
*/
- public LuajContext(boolean createDebugGlobals) {
+ public LuajContext(boolean createDebugGlobals, boolean useLuaJCCompiler) {
globals = createDebugGlobals?
JsePlatform.debugGlobals():
JsePlatform.standardGlobals();
+ if (useLuaJCCompiler)
+ LuaJC.install(globals);
stdin = globals.STDIN;
stdout = globals.STDOUT;
stderr = globals.STDERR;
diff --git a/test/java/org/luaj/luajc/TestLuaJ.java b/test/java/org/luaj/luajc/TestLuaJ.java
index c72a7c71..2c670577 100644
--- a/test/java/org/luaj/luajc/TestLuaJ.java
+++ b/test/java/org/luaj/luajc/TestLuaJ.java
@@ -21,14 +21,10 @@
******************************************************************************/
package org.luaj.luajc;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-
-import org.luaj.vm2.LuaTable;
+import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Print;
import org.luaj.vm2.Prototype;
-import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.jse.JsePlatform;
/** Test the plain old bytecode interpreter */
@@ -51,11 +47,10 @@ public class TestLuaJ {
System.out.println(script);
// create an environment to run in
- LuaTable _G = JsePlatform.standardGlobals();
+ Globals _G = JsePlatform.standardGlobals();
// compile into a chunk, or load as a class
- InputStream is = new ByteArrayInputStream( script.getBytes() );
- LuaValue chunk = LuaC.instance.load(is, "script", _G);
+ LuaValue chunk = _G.load(script, "script");
// The loaded chunk should be a closure, which contains the prototype.
print( chunk.checkclosure().p );
diff --git a/test/java/org/luaj/luajc/TestLuaJC.java b/test/java/org/luaj/luajc/TestLuaJC.java
index fd62b085..b7650244 100644
--- a/test/java/org/luaj/luajc/TestLuaJC.java
+++ b/test/java/org/luaj/luajc/TestLuaJC.java
@@ -51,14 +51,14 @@ public class TestLuaJC {
_G = JsePlatform.standardGlobals();
// print the chunk as a closure, and pretty-print the closure.
- LuaValue f = _G.loadFile(filename).arg1();
+ LuaValue f = _G.loadfile(filename).arg1();
Prototype p = f.checkclosure().p;
Print.print(p);
// load into a luajc java-bytecode based chunk by installing the LuaJC compiler first
if ( ! (args.length>0 && args[0].equals("nocompile")) ) {
- LuaJC.install();
- f = _G.loadFile(filename).arg1();
+ LuaJC.install(_G);
+ f = _G.loadfile(filename).arg1();
}
// call with arguments
@@ -80,7 +80,7 @@ public class TestLuaJC {
String destdir = ".";
InputStream is = _G.FINDER.findResource(filename);
- Hashtable t = LuaJC.getInstance().compileAll(is, filename, filename, true);
+ Hashtable t = LuaJC.instance.compileAll(is, filename, filename, _G, true);
// write out the chunk
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
diff --git a/test/junit/org/luaj/vm2/CompatibiltyTest.java b/test/junit/org/luaj/vm2/CompatibiltyTest.java
index 2f0eadf5..36ae746b 100644
--- a/test/junit/org/luaj/vm2/CompatibiltyTest.java
+++ b/test/junit/org/luaj/vm2/CompatibiltyTest.java
@@ -107,7 +107,7 @@ public class CompatibiltyTest extends TestSuite {
protected void setUp() throws Exception {
super.setUp();
System.setProperty("JME", "false");
- LuaJC.install();
+ LuaJC.install(globals);
}
// not supported on this platform - don't test
public void testDebugLib() {}
diff --git a/test/junit/org/luaj/vm2/FragmentsTest.java b/test/junit/org/luaj/vm2/FragmentsTest.java
index 3972fc9e..ab33ba29 100644
--- a/test/junit/org/luaj/vm2/FragmentsTest.java
+++ b/test/junit/org/luaj/vm2/FragmentsTest.java
@@ -21,13 +21,13 @@
******************************************************************************/
package org.luaj.vm2;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.luaj.vm2.compiler.LuaC;
+import org.luaj.vm2.lib.jse.JsePlatform;
import org.luaj.vm2.luajc.LuaJC;
/**
@@ -64,16 +64,18 @@ public class FragmentsTest extends TestSuite {
public void runFragment( Varargs expected, String script ) {
try {
String name = getName();
- Globals _G = org.luaj.vm2.lib.jse.JsePlatform.debugGlobals();
- InputStream is = new ByteArrayInputStream(script.getBytes("UTF-8"));
+ Globals _G = JsePlatform.debugGlobals();
+ Reader reader = new StringReader(script);
LuaValue chunk ;
switch ( TEST_TYPE ) {
case TEST_TYPE_LUAJC:
- chunk = LuaJC.getInstance().load(is,name,_G);
+ LuaJC.install(_G);
+ chunk = _G.load(reader, name);
break;
default:
- chunk = LuaC.instance.load( is, name, _G );
- Print.print(((LuaClosure)chunk).p);
+ Prototype p = _G.compilePrototype(reader, name);
+ chunk = new LuaClosure(p, _G);
+ Print.print(p);
break;
}
Varargs actual = chunk.invoke();
diff --git a/test/junit/org/luaj/vm2/LuaOperationsTest.java b/test/junit/org/luaj/vm2/LuaOperationsTest.java
index 8adc4574..03d0144c 100644
--- a/test/junit/org/luaj/vm2/LuaOperationsTest.java
+++ b/test/junit/org/luaj/vm2/LuaOperationsTest.java
@@ -21,8 +21,8 @@
******************************************************************************/
package org.luaj.vm2;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import junit.framework.TestCase;
@@ -130,9 +130,9 @@ public class LuaOperationsTest extends TestCase {
public Prototype createPrototype( String script, String name ) {
try {
- LuaTable _G = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
- InputStream is = new ByteArrayInputStream(script.getBytes("UTF-8"));
- return LuaC.instance.compile(is, name);
+ Globals _G = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
+ Reader reader = new StringReader(script);
+ return _G.compilePrototype(reader, name);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
diff --git a/test/junit/org/luaj/vm2/OrphanedThreadTest.java b/test/junit/org/luaj/vm2/OrphanedThreadTest.java
index 81c96a1b..14cecb4c 100644
--- a/test/junit/org/luaj/vm2/OrphanedThreadTest.java
+++ b/test/junit/org/luaj/vm2/OrphanedThreadTest.java
@@ -21,15 +21,10 @@
******************************************************************************/
package org.luaj.vm2;
-import java.io.ByteArrayInputStream;
import java.lang.ref.WeakReference;
import junit.framework.TestCase;
-import org.luaj.vm2.LoadState;
-import org.luaj.vm2.LuaValue;
-import org.luaj.vm2.Varargs;
-import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.jse.JsePlatform;
@@ -74,8 +69,7 @@ public class OrphanedThreadTest extends TestCase {
"arg = coroutine.yield(0)\n" +
"print('leakage in closure.3, arg is '..arg)\n" +
"return 'done'\n";
- LuaC.install();
- function = LoadState.load(new ByteArrayInputStream(script.getBytes()), "script", "bt", globals);
+ function = globals.load(script, "script");
doTest(LuaValue.TRUE, LuaValue.ZERO);
}
@@ -90,8 +84,7 @@ public class OrphanedThreadTest extends TestCase {
" return 'done'\n" +
"end\n" +
"print( 'pcall-closre.result:', pcall( f, ... ) )\n";
- LuaC.install();
- function = LoadState.load(new ByteArrayInputStream(script.getBytes()), "script", "bt", globals);
+ function = globals.load(script, "script");
doTest(LuaValue.TRUE, LuaValue.ZERO);
}
@@ -107,8 +100,7 @@ public class OrphanedThreadTest extends TestCase {
" return t[i]\n" +
"end\n" +
"load(f)()\n";
- LuaC.install();
- function = LoadState.load(new ByteArrayInputStream(script.getBytes()), "script", "bt", globals);
+ function = globals.load(script, "script");
doTest(LuaValue.TRUE, LuaValue.ONE);
}
diff --git a/test/junit/org/luaj/vm2/ScriptDrivenTest.java b/test/junit/org/luaj/vm2/ScriptDrivenTest.java
index 2ae2a476..3e0860a4 100644
--- a/test/junit/org/luaj/vm2/ScriptDrivenTest.java
+++ b/test/junit/org/luaj/vm2/ScriptDrivenTest.java
@@ -177,7 +177,7 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
}
}
- protected LuaValue loadScript(String name, LuaTable _G) throws IOException {
+ protected LuaValue loadScript(String name, Globals _G) throws IOException {
InputStream script = this.findResource(name+".lua");
if ( script == null )
fail("Could not load script for test case: " + name);
@@ -188,10 +188,11 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
LuaValue c = (LuaValue) Class.forName(name).newInstance();
return c;
} else {
- return LuaJC.getInstance().load( script, name, _G);
+ LuaJC.install(_G);
+ return _G.load(script, name, "bt", _G);
}
default:
- return LoadState.load(script, "@"+name+".lua", "bt", _G);
+ return _G.load(script, "@"+name+".lua", "bt", _G);
}
} catch ( Exception e ) {
e.printStackTrace();
diff --git a/test/junit/org/luaj/vm2/compiler/AbstractUnitTests.java b/test/junit/org/luaj/vm2/compiler/AbstractUnitTests.java
index 9695506a..8f8de5e3 100644
--- a/test/junit/org/luaj/vm2/compiler/AbstractUnitTests.java
+++ b/test/junit/org/luaj/vm2/compiler/AbstractUnitTests.java
@@ -11,8 +11,8 @@ import java.net.URL;
import junit.framework.TestCase;
+import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
-import org.luaj.vm2.LuaTable;
import org.luaj.vm2.Print;
import org.luaj.vm2.Prototype;
import org.luaj.vm2.lib.jse.JsePlatform;
@@ -21,7 +21,7 @@ abstract public class AbstractUnitTests extends TestCase {
private final String dir;
private final String jar;
- private LuaTable _G;
+ private Globals _G;
public AbstractUnitTests(String zipdir, String zipfile, String dir) {
URL zip = null;
@@ -67,7 +67,7 @@ abstract public class AbstractUnitTests extends TestCase {
// compile in memory
InputStream is = new ByteArrayInputStream(lua);
- Prototype p = LuaC.instance.compile(is, "@" + file);
+ Prototype p = _G.loadPrototype(is, "@" + file, "bt");
String actual = protoToString(p);
// load expected value from jar
@@ -109,7 +109,7 @@ abstract public class AbstractUnitTests extends TestCase {
protected Prototype loadFromBytes(byte[] bytes, String script)
throws IOException {
InputStream is = new ByteArrayInputStream(bytes);
- return LoadState.loadBinaryChunk(is.read(), is, script);
+ return _G.loadPrototype(is, script, "b");
}
protected String protoToString(Prototype p) {
diff --git a/test/junit/org/luaj/vm2/compiler/DumpLoadEndianIntTest.java b/test/junit/org/luaj/vm2/compiler/DumpLoadEndianIntTest.java
index 7b5d9e0a..00c6d776 100644
--- a/test/junit/org/luaj/vm2/compiler/DumpLoadEndianIntTest.java
+++ b/test/junit/org/luaj/vm2/compiler/DumpLoadEndianIntTest.java
@@ -6,13 +6,15 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
import junit.framework.TestCase;
+import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.LuaClosure;
import org.luaj.vm2.LuaFunction;
-import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Prototype;
import org.luaj.vm2.lib.jse.JsePlatform;
@@ -28,7 +30,7 @@ public class DumpLoadEndianIntTest extends TestCase {
private static final String withdoubles = "1234-#!-23.75";
private static final String withints = "1234-#!-23";
- private LuaTable _G;
+ private Globals _G;
protected void setUp() throws Exception {
super.setUp();
@@ -83,8 +85,8 @@ public class DumpLoadEndianIntTest extends TestCase {
try {
// compile into prototype
- InputStream is = new ByteArrayInputStream(script.getBytes());
- Prototype p = LuaC.instance.compile(is, "script");
+ Reader reader = new StringReader(script);
+ Prototype p = _G.compilePrototype(reader, "script");
// double check script result before dumping
LuaFunction f = new LuaClosure(p, _G);
@@ -107,8 +109,8 @@ public class DumpLoadEndianIntTest extends TestCase {
byte[] dumped = baos.toByteArray();
// load again using compiler
- is = new ByteArrayInputStream(dumped);
- f = LoadState.load(is, "dumped", "bt", _G);
+ InputStream is = new ByteArrayInputStream(dumped);
+ f = _G.load(is, "dumped", "b", _G).checkfunction();
r = f.call();
actual = r.tojstring();
assertEquals( expectedPostDump, actual );
diff --git a/test/junit/org/luaj/vm2/compiler/SimpleTests.java b/test/junit/org/luaj/vm2/compiler/SimpleTests.java
index e8b2ae1f..638f4c34 100644
--- a/test/junit/org/luaj/vm2/compiler/SimpleTests.java
+++ b/test/junit/org/luaj/vm2/compiler/SimpleTests.java
@@ -1,20 +1,16 @@
package org.luaj.vm2.compiler;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-
import junit.framework.TestCase;
+import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaDouble;
-import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaInteger;
-import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
public class SimpleTests extends TestCase {
- private LuaTable _G;
+ private Globals _G;
protected void setUp() throws Exception {
super.setUp();
@@ -23,8 +19,7 @@ public class SimpleTests extends TestCase {
private void doTest( String script ) {
try {
- InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") );
- LuaFunction c = LuaC.instance.load( is, "script", _G );
+ LuaValue c = _G.load(script, "script");
c.call();
} catch ( Exception e ) {
fail("i/o exception: "+e );
diff --git a/test/junit/org/luaj/vm2/lib/jse/LuajavaAccessibleMembersTest.java b/test/junit/org/luaj/vm2/lib/jse/LuajavaAccessibleMembersTest.java
index 1bb2d99c..c0dccfe4 100644
--- a/test/junit/org/luaj/vm2/lib/jse/LuajavaAccessibleMembersTest.java
+++ b/test/junit/org/luaj/vm2/lib/jse/LuajavaAccessibleMembersTest.java
@@ -1,20 +1,13 @@
package org.luaj.vm2.lib.jse;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.security.Permission;
-
import junit.framework.TestCase;
-import org.luaj.vm2.LuaFunction;
-import org.luaj.vm2.LuaTable;
+import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
-import org.luaj.vm2.compiler.LuaC;
-import org.luaj.vm2.lib.jse.JsePlatform;
public class LuajavaAccessibleMembersTest extends TestCase {
- private LuaTable _G;
+ private Globals _G;
protected void setUp() throws Exception {
super.setUp();
@@ -23,8 +16,7 @@ public class LuajavaAccessibleMembersTest extends TestCase {
private String invokeScript(String script) {
try {
- InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") );
- LuaFunction c = LuaC.instance.load( is, "script", _G );
+ LuaValue c = _G.load(script, "script");
return c.call().tojstring();
} catch ( Exception e ) {
fail("exception: "+e );
diff --git a/test/junit/org/luaj/vm2/script/ScriptEngineTests.java b/test/junit/org/luaj/vm2/script/ScriptEngineTests.java
index 4d2f3842..031896c0 100644
--- a/test/junit/org/luaj/vm2/script/ScriptEngineTests.java
+++ b/test/junit/org/luaj/vm2/script/ScriptEngineTests.java
@@ -21,7 +21,6 @@
******************************************************************************/
package org.luaj.vm2.script;
-import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.Reader;
@@ -100,8 +99,8 @@ public class ScriptEngineTests extends TestSuite {
return new SimpleBindings();
}
public void setUp() {
+ System.setProperty("org.luaj.luajc", "true");
super.setUp();
- org.luaj.vm2.luajc.LuaJC.install();
}
public void testCompiledFunctionIsNotClosure() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("return 'foo'");