Move static variables used by libraries into explicit Globals object for better thread safety.

This commit is contained in:
James Roseborough
2012-09-14 04:12:50 +00:00
parent 9f3aef6403
commit f786802bf1
30 changed files with 739 additions and 1000 deletions

View File

@@ -24,9 +24,6 @@ package org.luaj.vm2;
import java.io.IOException;
import java.io.InputStream;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.BaseLib;
/**
* Test argument type check errors
@@ -47,7 +44,7 @@ public class ErrorsTest extends ScriptDrivenTest {
}
public void testBaseLibArgs() {
BaseLib.instance.STDIN = new InputStream() {
globals.STDIN = new InputStream() {
public int read() throws IOException {
return -1;
}

View File

@@ -64,15 +64,15 @@ public class FragmentsTest extends TestSuite {
public void runFragment( Varargs expected, String script ) {
try {
String name = getName();
org.luaj.vm2.lib.jse.JsePlatform.debugGlobals();
Globals _G = org.luaj.vm2.lib.jse.JsePlatform.debugGlobals();
InputStream is = new ByteArrayInputStream(script.getBytes("UTF-8"));
LuaValue chunk ;
switch ( TEST_TYPE ) {
case TEST_TYPE_LUAJC:
chunk = LuaJC.getInstance().load(is,name,LuaValue._G);
chunk = LuaJC.getInstance().load(is,name,_G);
break;
default:
chunk = LuaC.instance.load( is, name, LuaValue._G );
chunk = LuaC.instance.load( is, name, _G );
Print.print(((LuaClosure)chunk).p);
break;
}

View File

@@ -56,7 +56,7 @@ public class LuaOperationsTest extends TestCase {
private final LuaValue stringdouble = LuaValue.valueOf(samplestringdouble);
private final LuaTable table = LuaValue.listOf( new LuaValue[] { LuaValue.valueOf("aaa"), LuaValue.valueOf("bbb") } );
private final LuaValue somefunc = new ZeroArgFunction() { public LuaValue call() { return NONE;}};
private final LuaThread thread = new LuaThread(somefunc);
private final LuaThread thread = new LuaThread(new Globals(), somefunc);
private final Prototype proto = new Prototype(1);
private final LuaClosure someclosure = new LuaClosure(proto,table);
private final LuaUserdata userdataobj = LuaValue.userdataOf(sampleobject);
@@ -146,7 +146,7 @@ public class LuaOperationsTest extends TestCase {
// set up suitable environments for execution
LuaValue aaa = LuaValue.valueOf("aaa");
LuaValue eee = LuaValue.valueOf("eee");
LuaTable _G = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
final Globals _G = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
LuaTable newenv = LuaValue.tableOf( new LuaValue[] {
LuaValue.valueOf("a"), LuaValue.valueOf("aaa"),
LuaValue.valueOf("b"), LuaValue.valueOf("bbb"), } );

View File

@@ -38,8 +38,8 @@ public class MetatableTest extends TestCase {
private final LuaValue string = LuaValue.valueOf(samplestring);
private final LuaTable table = LuaValue.tableOf();
private final LuaFunction function = new ZeroArgFunction() { public LuaValue call() { return NONE;}};
private final LuaThread thread = new LuaThread(function);
private final LuaClosure closure = new LuaClosure(new Prototype());
private final LuaThread thread = new LuaThread(new Globals(), function);
private final LuaClosure closure = new LuaClosure(new Prototype(), new LuaTable());
private final LuaUserdata userdata = LuaValue.userdataOf(sampleobject);
private final LuaUserdata userdatamt = LuaValue.userdataOf(sampledata,table);

View File

@@ -36,6 +36,7 @@ import org.luaj.vm2.lib.jse.JsePlatform;
public class OrphanedThreadTest extends TestCase {
Globals globals;
LuaThread luathread;
WeakReference luathr_ref;
LuaValue function;
@@ -113,7 +114,8 @@ public class OrphanedThreadTest extends TestCase {
}
private void doTest(LuaValue status2, LuaValue value2) throws Exception {
luathread = new LuaThread(function);
globals = JsePlatform.standardGlobals();
luathread = new LuaThread(globals, function);
luathr_ref = new WeakReference(luathread);
func_ref = new WeakReference(function);
assertNotNull(luathr_ref.get());
@@ -142,40 +144,40 @@ public class OrphanedThreadTest extends TestCase {
}
static class NormalFunction extends OneArgFunction {
class NormalFunction extends OneArgFunction {
public LuaValue call(LuaValue arg) {
System.out.println("in normal.1, arg is "+arg);
arg = LuaThread.yield(ONE).arg1();
arg = globals.yield(ONE).arg1();
System.out.println("in normal.2, arg is "+arg);
arg = LuaThread.yield(ZERO).arg1();
arg = globals.yield(ZERO).arg1();
System.out.println("in normal.3, arg is "+arg);
return NONE;
}
}
static class EarlyCompletionFunction extends OneArgFunction {
class EarlyCompletionFunction extends OneArgFunction {
public LuaValue call(LuaValue arg) {
System.out.println("in early.1, arg is "+arg);
arg = LuaThread.yield(ONE).arg1();
arg = globals.yield(ONE).arg1();
System.out.println("in early.2, arg is "+arg);
return ZERO;
}
}
static class AbnormalFunction extends OneArgFunction {
class AbnormalFunction extends OneArgFunction {
public LuaValue call(LuaValue arg) {
System.out.println("in abnormal.1, arg is "+arg);
arg = LuaThread.yield(ONE).arg1();
arg = globals.yield(ONE).arg1();
System.out.println("in abnormal.2, arg is "+arg);
error("abnormal condition");
return ZERO;
}
}
static class ClosureFunction extends OneArgFunction {
class ClosureFunction extends OneArgFunction {
public LuaValue call(LuaValue arg) {
System.out.println("in abnormal.1, arg is "+arg);
arg = LuaThread.yield(ONE).arg1();
arg = globals.yield(ONE).arg1();
System.out.println("in abnormal.2, arg is "+arg);
error("abnormal condition");
return ZERO;

View File

@@ -47,7 +47,7 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
private final PlatformType platform;
private final String subdir;
private LuaTable _G;
protected Globals globals;
static final String zipdir = "test/lua/";
static final String zipfile = "luaj3.0-tests.zip";
@@ -63,10 +63,10 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
default:
case JSE:
case LUAJIT:
_G = org.luaj.vm2.lib.jse.JsePlatform.debugGlobals();
globals = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
break;
case JME:
_G = org.luaj.vm2.lib.jme.JmePlatform.debugGlobals();
globals = org.luaj.vm2.lib.jme.JmePlatform.standardGlobals();
break;
}
}
@@ -75,7 +75,7 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
protected void setUp() throws Exception {
super.setUp();
initGlobals();
BaseLib.FINDER = this;
globals.FINDER = this;
}
// ResourceFinder implementation.
@@ -147,13 +147,13 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
try {
// override print()
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final PrintStream oldps = BaseLib.instance.STDOUT;
final PrintStream oldps = globals.STDOUT;
final PrintStream ps = new PrintStream( output );
BaseLib.instance.STDOUT = ps;
globals.STDOUT = ps;
// run the script
try {
LuaValue chunk = loadScript(testName, _G);
LuaValue chunk = loadScript(testName, globals);
chunk.call(LuaValue.valueOf(platform.toString()));
ps.flush();
@@ -164,7 +164,7 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
assertEquals(expectedOutput, actualOutput);
} finally {
BaseLib.instance.STDOUT = oldps;
globals.STDOUT = oldps;
ps.close();
}
} catch ( IOException ioe ) {

View File

@@ -56,8 +56,8 @@ public class TypeTest extends TestCase {
private final LuaValue stringdouble = LuaValue.valueOf(samplestringdouble);
private final LuaTable table = LuaValue.tableOf();
private final LuaFunction somefunc = new ZeroArgFunction() { public LuaValue call() { return NONE;}};
private final LuaThread thread = new LuaThread(somefunc);
private final LuaClosure someclosure = new LuaClosure(new Prototype());
private final LuaThread thread = new LuaThread(new Globals(), somefunc);
private final LuaClosure someclosure = new LuaClosure(new Prototype(), new LuaTable());
private final LuaUserdata userdataobj = LuaValue.userdataOf(sampleobject);
private final LuaUserdata userdatacls = LuaValue.userdataOf(sampledata);