Move static variables used by libraries into explicit Globals object for better thread safety.
This commit is contained in:
@@ -67,11 +67,14 @@ import org.luaj.vm2.lib.ResourceFinder;
|
||||
|
||||
public class JseBaseLib extends org.luaj.vm2.lib.BaseLib {
|
||||
|
||||
/** Construct a JSE base library instance */
|
||||
public JseBaseLib() {
|
||||
STDIN = System.in;
|
||||
/** Extend the library loading to set the default value for {@link Globals.STDIN} */
|
||||
public LuaValue call(LuaValue env) {
|
||||
super.call(env);
|
||||
env.checkglobals().STDIN = System.in;
|
||||
return env;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Try to open a file in the current working directory,
|
||||
* or fall back to base opener if not found.
|
||||
|
||||
@@ -67,16 +67,12 @@ import org.luaj.vm2.lib.LibFunction;
|
||||
*/
|
||||
public class JseIoLib extends IoLib {
|
||||
|
||||
public JseIoLib() {
|
||||
super();
|
||||
}
|
||||
|
||||
protected File wrapStdin() throws IOException {
|
||||
return new FileImpl(BaseLib.instance.STDIN);
|
||||
return new FileImpl(globals.STDIN);
|
||||
}
|
||||
|
||||
protected File wrapStdout() throws IOException {
|
||||
return new FileImpl(BaseLib.instance.STDOUT);
|
||||
return new FileImpl(globals.STDOUT);
|
||||
}
|
||||
|
||||
protected File openFile( String filename, boolean readMode, boolean appendMode, boolean updateMode, boolean binaryMode ) throws IOException {
|
||||
|
||||
@@ -21,14 +21,16 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.lib.jse;
|
||||
|
||||
import org.luaj.vm2.compiler.LuaC;
|
||||
import org.luaj.vm2.Globals;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaThread;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.compiler.LuaC;
|
||||
import org.luaj.vm2.lib.Bit32Lib;
|
||||
import org.luaj.vm2.lib.CoroutineLib;
|
||||
import org.luaj.vm2.lib.DebugLib;
|
||||
import org.luaj.vm2.lib.PackageLib;
|
||||
import org.luaj.vm2.lib.ResourceFinder;
|
||||
import org.luaj.vm2.lib.StringLib;
|
||||
import org.luaj.vm2.lib.TableLib;
|
||||
|
||||
@@ -40,13 +42,13 @@ import org.luaj.vm2.lib.TableLib;
|
||||
* <p>
|
||||
* A simple example of initializing globals and using them from Java is:
|
||||
* <pre> {@code
|
||||
* LuaValue _G = JsePlatform.standardGlobals();
|
||||
* Globals _G = JsePlatform.standardGlobals();
|
||||
* _G.get("print").call(LuaValue.valueOf("hello, world"));
|
||||
* } </pre>
|
||||
* <p>
|
||||
* Once globals are created, a simple way to load and run a script is:
|
||||
* <pre> {@code
|
||||
* LoadState.load( new FileInputStream("main.lua"), "main.lua", _G ).call();
|
||||
* _G.load( new FileInputStream("main.lua"), "main.lua" ).call();
|
||||
* } </pre>
|
||||
* <p>
|
||||
* although {@code require} could also be used:
|
||||
@@ -58,6 +60,7 @@ import org.luaj.vm2.lib.TableLib;
|
||||
* <p>
|
||||
* The standard globals will contain all standard libraries plus {@code luajava}:
|
||||
* <ul>
|
||||
* <li>{@link Globals}</li>
|
||||
* <li>{@link JseBaseLib}</li>
|
||||
* <li>{@link PackageLib}</li>
|
||||
* <li>{@link Bit32Lib}</li>
|
||||
@@ -87,9 +90,8 @@ public class JsePlatform {
|
||||
* @see JsePlatform
|
||||
* @see JmePlatform
|
||||
*/
|
||||
public static LuaTable standardGlobals() {
|
||||
LuaTable _G = new LuaTable();
|
||||
LuaValue._G = _G;
|
||||
public static Globals standardGlobals() {
|
||||
Globals _G = new Globals();
|
||||
_G.load(new JseBaseLib());
|
||||
_G.load(new PackageLib());
|
||||
_G.load(new Bit32Lib());
|
||||
@@ -101,6 +103,7 @@ public class JsePlatform {
|
||||
_G.load(new JseOsLib());
|
||||
_G.load(new LuajavaLib());
|
||||
LuaC.install();
|
||||
_G.compiler = LuaC.instance;
|
||||
return _G;
|
||||
}
|
||||
|
||||
@@ -112,8 +115,8 @@ public class JsePlatform {
|
||||
* @see JmePlatform
|
||||
* @see DebugLib
|
||||
*/
|
||||
public static LuaTable debugGlobals() {
|
||||
LuaTable _G = standardGlobals();
|
||||
public static Globals debugGlobals() {
|
||||
Globals _G = standardGlobals();
|
||||
_G.load(new DebugLib());
|
||||
return _G;
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ public class LuajavaLib extends VarArgFunction {
|
||||
LuaTable t = new LuaTable();
|
||||
bind( t, LuajavaLib.class, NAMES, BINDCLASS );
|
||||
env.set("luajava", t);
|
||||
PackageLib.instance.LOADED.set("luajava", t);
|
||||
env.get("package").get("loaded").set("luajava", t);
|
||||
return t;
|
||||
}
|
||||
case BINDCLASS: {
|
||||
|
||||
@@ -35,18 +35,14 @@ public class JavaLoader extends ClassLoader {
|
||||
public JavaLoader() {
|
||||
}
|
||||
|
||||
public LuaFunction load( Prototype p, String classname, String filename ) {
|
||||
public LuaFunction load( Prototype p, String classname, String filename, LuaValue env ) {
|
||||
JavaGen jg = new JavaGen( p, classname, filename );
|
||||
return load( jg );
|
||||
return load( jg, env );
|
||||
}
|
||||
|
||||
public LuaFunction load( JavaGen jg ) {
|
||||
public LuaFunction load( JavaGen jg, LuaValue env ) {
|
||||
include( jg );
|
||||
return load( jg.classname );
|
||||
}
|
||||
|
||||
public LuaFunction load(String classname) {
|
||||
return load(classname, LuaValue._G);
|
||||
return load( jg.classname, env );
|
||||
}
|
||||
|
||||
public LuaFunction load(String classname, LuaValue env) {
|
||||
|
||||
@@ -100,7 +100,7 @@ public class LuaJC implements LuaCompiler {
|
||||
String classname = toStandardJavaClassName( name );
|
||||
String luaname = toStandardLuaFileName( name );
|
||||
JavaLoader loader = new JavaLoader();
|
||||
return loader.load(p, classname, luaname);
|
||||
return loader.load(p, classname, luaname, env);
|
||||
}
|
||||
|
||||
private static String toStandardJavaClassName( String luachunkname ) {
|
||||
|
||||
Reference in New Issue
Block a user