Update javadoc comments related to library initialization and loading lua code.

This commit is contained in:
James Roseborough
2013-12-29 22:49:23 +00:00
parent 04fd646c87
commit de33943eaf
38 changed files with 494 additions and 254 deletions

View File

@@ -29,9 +29,7 @@ import java.io.InputStreamReader;
import java.util.Vector;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.Lua;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Print;
@@ -65,7 +63,7 @@ public class lua {
System.exit(-1);
}
private static Globals _G;
private static Globals globals;
private static boolean print = false;
private static String encoding = null;
@@ -137,8 +135,8 @@ public class lua {
System.out.println(version);
// new lua state
_G = nodebug? JsePlatform.standardGlobals(): JsePlatform.debugGlobals();
if ( luajc ) LuaJC.install(_G);
globals = nodebug? JsePlatform.standardGlobals(): JsePlatform.debugGlobals();
if ( luajc ) LuaJC.install(globals);
for ( int i=0, n=libs!=null? libs.size(): 0; i<n; i++ )
loadLibrary( (String) libs.elementAt(i) );
@@ -181,12 +179,12 @@ public class lua {
LuaValue slibname =LuaValue.valueOf(libname);
try {
// load via plain require
_G.get("require").call(slibname);
globals.get("require").call(slibname);
} catch ( Exception e ) {
try {
// load as java class
LuaValue v = (LuaValue) Class.forName(libname).newInstance();
v.call(slibname, _G);
v.call(slibname, globals);
} catch ( Exception f ) {
throw new IOException("loadLibrary("+libname+") failed: "+e+","+f );
}
@@ -198,21 +196,21 @@ public class lua {
LuaValue c;
try {
c = encoding != null?
_G.load(new InputStreamReader(script, encoding), chunkname):
_G.load(script, chunkname, "bt", _G);
globals.load(new InputStreamReader(script, encoding), chunkname):
globals.load(script, chunkname, "bt", globals);
} finally {
script.close();
}
if (print && c.isclosure())
Print.print(c.checkclosure().p);
Varargs scriptargs = setGlobalArg(chunkname, args, firstarg, _G);
Varargs scriptargs = setGlobalArg(chunkname, args, firstarg, globals);
c.invoke( scriptargs );
} catch ( Exception e ) {
e.printStackTrace( System.err );
}
}
private static Varargs setGlobalArg(String chunkname, String[] args, int i, LuaValue _G) {
private static Varargs setGlobalArg(String chunkname, String[] args, int i, LuaValue globals) {
if (args == null)
return LuaValue.NONE;
LuaTable arg = LuaValue.tableOf();
@@ -220,7 +218,7 @@ public class lua {
arg.set( j-i, LuaValue.valueOf(args[j]) );
arg.set(0, LuaValue.valueOf(chunkname));
arg.set(-1, LuaValue.valueOf("luaj"));
_G.set("arg", arg);
globals.set("arg", arg);
return arg.unpack();
}

View File

@@ -21,14 +21,12 @@
******************************************************************************/
package org.luaj.vm2.lib.jse;
import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;
import org.luaj.vm2.LuaDouble;
import org.luaj.vm2.LuaInteger;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaUserdata;
import org.luaj.vm2.LuaValue;

View File

@@ -22,18 +22,13 @@
package org.luaj.vm2.lib.jse;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
/**
* Helper class to coerce values from lua to Java within the luajava library.

View File

@@ -33,7 +33,6 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaValue;
/**

View File

@@ -50,7 +50,7 @@ import org.luaj.vm2.lib.LibFunction;
* <p>
* For special cases where the smallest possible footprint is desired,
* a minimal set of libraries could be loaded
* directly via {@link Glboals#load(LuaValue)} using code such as:
* directly via {@link Globals#load(LuaValue)} using code such as:
* <pre> {@code
* Globals globals = new Globals();
* globals.load(new JseBaseLib());

View File

@@ -35,18 +35,18 @@ import org.luaj.vm2.lib.LibFunction;
* {@link JsePlatform#standardGlobals()}
* <pre> {@code
* Globals globals = JsePlatform.standardGlobals();
* System.out.println( _G.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
* System.out.println( globals.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
* } </pre>
* <p>
* For special cases where the smallest possible footprint is desired,
* a minimal set of libraries could be loaded
* directly via {@link Glboals#load(LuaValue)} using code such as:
* directly via {@link Globals#load(LuaValue)} using code such as:
* <pre> {@code
* Globals globals = new Globals();
* globals.load(new JseBaseLib());
* globals.load(new PackageLib());
* globals.load(new JseMathLib());
* System.out.println( _G.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
* System.out.println( globals.get("math").get("sqrt").call( LuaValue.valueOf(2) ) );
* } </pre>
* <p>However, other libraries such as <em>CoroutineLib</em> are not loaded in this case.
* <p>

View File

@@ -48,18 +48,18 @@ import org.luaj.vm2.lib.LibFunction;
* {@link JsePlatform#standardGlobals()}
* <pre> {@code
* Globals globals = JsePlatform.standardGlobals();
* System.out.println( _G.get("os").get("time").call() );
* System.out.println( globals.get("os").get("time").call() );
* } </pre>
* <p>
* For special cases where the smallest possible footprint is desired,
* a minimal set of libraries could be loaded
* directly via {@link Glboals#load(LuaValue)} using code such as:
* directly via {@link Globals#load(LuaValue)} using code such as:
* <pre> {@code
* Globals globals = new Globals();
* globals.load(new JseBaseLib());
* globals.load(new PackageLib());
* globals.load(new JseOsLib());
* System.out.println( _G.get("os").get("time").call() );
* System.out.println( globals.get("os").get("time").call() );
* } </pre>
* <p>However, other libraries such as <em>MathLib</em> are not loaded in this case.
* <p>

View File

@@ -34,7 +34,6 @@ import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.LibFunction;
import org.luaj.vm2.lib.PackageLib;
import org.luaj.vm2.lib.VarArgFunction;
/**
@@ -43,33 +42,42 @@ import org.luaj.vm2.lib.VarArgFunction;
* Luajava is an approach to mixing lua and java using simple functions that bind
* java classes and methods to lua dynamically. The API is documented on the
* <a href="http://www.keplerproject.org/luajava/">luajava</a> documentation pages.
*
* <p>
* Typically, this library is included as part of a call to either
* Typically, this library is included as part of a call to
* {@link JsePlatform#standardGlobals()}
* <pre> {@code
* Globals globals = JsePlatform.standardGlobals();
* System.out.println( globals.get("luajava").get("bindClass").call( LuaValue.valueOf("java.lang.System") ).invokeMethod("currentTimeMillis") );
* } </pre>
* <p>
* To instantiate and use it directly,
* link it into your globals table via {@link LuaValue#load(LuaValue)} using code such as:
* link it into your globals table via {@link Globals#load} using code such as:
* <pre> {@code
* LuaTable _G = new LuaTable();
* LuaThread.setGlobals(_G);
* LuaC.install();
* _G.load(new BaseLib());
* _G.load(new PackageLib());
* _G.load(new LuajavaLib());
* _G.get("load").call( LuaValue.valueOf(
* "sys = luajava.bindClass('java.lang.System')\n"+
* "print ( sys:currentTimeMillis() )\n" ) ).call();
* Globals globals = new Globals();
* globals.load(new JseBaseLib());
* globals.load(new PackageLib());
* globals.load(new LuajavaLib());
* globals.load(
* "sys = luajava.bindClass('java.lang.System')\n"+
* "print ( sys:currentTimeMillis() )\n", "main.lua" ).call();
* } </pre>
* This example is not intended to be realistic - only to show how the {@link LuajavaLib}
* may be initialized by hand. In practice, the {@code luajava} library is available
* <p>
*
* The {@code luajava} library is available
* on all JSE platforms via the call to {@link JsePlatform#standardGlobals()}
* and the luajava api's are simply invoked from lua.
* and the luajava api's are simply invoked from lua.
* Because it makes extensive use of Java's reflection API, it is not available
* on JME, but can be used in Android applications.
* <p>
* This has been implemented to match as closely as possible the behavior in the corresponding library in C.
*
* @see LibFunction
* @see org.luaj.vm2.lib.jse.JsePlatform
* @see org.luaj.vm2.lib.jme.JmePlatform
* @see LuaC
* @see CoerceJavaToLua
* @see CoerceLuaToJava
* @see <a href="http://www.keplerproject.org/luajava/manual.html#luareference">http://www.keplerproject.org/luajava/manual.html#luareference</a>
*/
public class LuajavaLib extends VarArgFunction {

View File

@@ -27,6 +27,7 @@ import java.io.Reader;
import java.util.Hashtable;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Prototype;
@@ -37,19 +38,25 @@ import org.luaj.vm2.lib.BaseLib;
* Implementation of {@link LuaCompiler} which does direct
* lua-to-java-bytecode compiling.
* <p>
* By default, when using {@link JsePlatform} or {@JmePlatform}
* to construct globals, the plain compiler {@link LuaC} is installed and lua code
* will only be compiled into lua bytecode and execute as {@link LuaClosure}.
* <p>
* To override the default compiling behavior with {@link LuaJC}
* lua-to-java bytecode compiler, install it before undumping code,
* for example:
* <pre> {@code
* LuaValue globals = JsePlatform.standardGlobals();
* LuaJC.install(globals);
* LuaValue chunk = globals.load( "print('hello, world'), "main.lua");
* System.out.println(chunk.isclosure()); // Will be false when LuaJC is working.
* chunk.call();
* } </pre>
* <p>
* This requires the bcel library to be on the class path to work as expected.
* If the library is not found, the default {@link LuaC} lua-to-lua-bytecode
* compiler will be used.
* <p>
* The compiler should be installed as part of globals initialization,
* and before any scripts or lua code is executed.
* A typical example is to install it following the globals creation,
* as in the following:
* <pre> {@code
* LuaValue _G = JsePlatform.standardGlobals();
* LuaJC.install(_G);
* _G.loadString("print 'hello'").call();
* } </pre>
*
* @see LuaCompiler
* @see LuaC
* @see BaseLib

View File

@@ -3,7 +3,6 @@ package org.luaj.vm2.luajc;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Hashtable;
import java.util.Vector;
import org.luaj.vm2.Lua;
import org.luaj.vm2.LuaString;