LuaScriptEngineFactory.getScriptEngine() now returns new instance of lua script engine for each call.

This commit is contained in:
James Roseborough
2015-03-18 15:07:56 +00:00
parent cd9cdc496e
commit 237c384d55
3 changed files with 34 additions and 33 deletions

View File

@@ -978,6 +978,7 @@ Files are no longer hosted at LuaForge.
<li>Fix aliasing issue for some multiple assignments from varargs return values (fixes issue #38)</li> <li>Fix aliasing issue for some multiple assignments from varargs return values (fixes issue #38)</li>
<li>Let os.getenv() return System.getenv() values first for JSE, then fall back to properties (fixes issue #25)</li> <li>Let os.getenv() return System.getenv() values first for JSE, then fall back to properties (fixes issue #25)</li>
<li>Improve collection of orphaned coroutines when yielding from debug hook functions.</li> <li>Improve collection of orphaned coroutines when yielding from debug hook functions.</li>
<li>LuaScriptEngineFactory.getScriptEngine() now returns new instance of lua script engine for each call.</li>
</ul></td></tr> </ul></td></tr>
</table></td></tr></table> </table></td></tr></table>

View File

@@ -28,7 +28,10 @@ import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory; import javax.script.ScriptEngineFactory;
/** /**
* Jsr 223 scripting engine factory * Jsr 223 scripting engine factory.
*
* Exposes metadata to support the lua language, and constructs
* instances of LuaScriptEngine to handl lua scripts.
*/ */
public class LuaScriptEngineFactory implements ScriptEngineFactory { public class LuaScriptEngineFactory implements ScriptEngineFactory {
@@ -47,12 +50,9 @@ public class LuaScriptEngineFactory implements ScriptEngineFactory {
"luaj", "luaj",
}; };
private static final ThreadLocal<ScriptEngine> engines
= new ThreadLocal<ScriptEngine>();
private List<String> extensions; private List<String> extensions;
private List<String> mimeTypes; private List<String> mimeTypes;
private List<String> names; private List<String> names;
public LuaScriptEngineFactory() { public LuaScriptEngineFactory() {
extensions = Arrays.asList(EXTENSIONS); extensions = Arrays.asList(EXTENSIONS);
@@ -123,11 +123,6 @@ public class LuaScriptEngineFactory implements ScriptEngineFactory {
} }
public ScriptEngine getScriptEngine() { public ScriptEngine getScriptEngine() {
ScriptEngine eng = engines.get(); return new LuaScriptEngine();
if ( eng == null ) {
eng = new LuaScriptEngine();
engines.set(eng);
}
return eng;
} }
} }

View File

@@ -46,12 +46,13 @@ public class ScriptEngineTests extends TestSuite {
public static TestSuite suite() { public static TestSuite suite() {
TestSuite suite = new TestSuite("Script Engine Tests"); TestSuite suite = new TestSuite("Script Engine Tests");
suite.addTest( new TestSuite( LookupEngineTestCase.class, "Lookup Engine" ) ); suite.addTest( new TestSuite( LookupEngineTestCase.class, "Lookup Engine" ) );
suite.addTest( new TestSuite( SimpleBindingsTest.class, "Simple Bindings" ) ); suite.addTest( new TestSuite( DefaultBindingsTest.class, "Default Bindings" ) );
suite.addTest( new TestSuite( DefaultBindingsTest.class, "Default Bindings" ) ); suite.addTest( new TestSuite( SimpleBindingsTest.class, "Simple Bindings" ) );
suite.addTest( new TestSuite( LuaJCBindingsTest.class, "LuaJC Bindings" ) ); suite.addTest( new TestSuite( CompileClosureTest.class, "Compile Closure" ) );
suite.addTest( new TestSuite( UserContextTest.class, "User Context" ) ); suite.addTest( new TestSuite( CompileNonClosureTest.class, "Compile NonClosure" ) );
suite.addTest( new TestSuite( WriterTest.class, "Writer" ) ); suite.addTest( new TestSuite( UserContextTest.class, "User Context" ) );
suite.addTest( new TestSuite( WriterTest.class, "Writer" ) );
return suite; return suite;
} }
@@ -81,35 +82,39 @@ public class ScriptEngineTests extends TestSuite {
} }
} }
public static class SimpleBindingsTest extends EngineTestCase {
protected Bindings createBindings() {
return new SimpleBindings();
}
public void testCompiledFunctionIsClosure() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("return 'foo'");
assertTrue(((LuaScriptEngine.LuajCompiledScript)cs).function.isclosure());
}
}
public static class DefaultBindingsTest extends EngineTestCase { public static class DefaultBindingsTest extends EngineTestCase {
protected Bindings createBindings() { protected Bindings createBindings() {
return e.createBindings(); return e.createBindings();
} }
} }
public static class LuaJCBindingsTest extends EngineTestCase { public static class SimpleBindingsTest extends EngineTestCase {
static {
System.setProperty("org.luaj.luajc", "true");
}
protected Bindings createBindings() { protected Bindings createBindings() {
return new SimpleBindings(); return new SimpleBindings();
} }
public void setUp() { }
public static class CompileClosureTest extends DefaultBindingsTest {
protected void setUp() throws Exception {
System.setProperty("org.luaj.luajc", "false");
super.setUp();
}
public void testCompiledFunctionIsClosure() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("return 'foo'");
LuaValue value = ((LuaScriptEngine.LuajCompiledScript)cs).function;
assertTrue(value.isclosure());
}
}
public static class CompileNonClosureTest extends DefaultBindingsTest {
protected void setUp() throws Exception {
System.setProperty("org.luaj.luajc", "true");
super.setUp(); super.setUp();
} }
public void testCompiledFunctionIsNotClosure() throws ScriptException { public void testCompiledFunctionIsNotClosure() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("return 'foo'"); CompiledScript cs = ((Compilable)e).compile("return 'foo'");
assertFalse(((LuaScriptEngine.LuajCompiledScript)cs).function.isclosure()); LuaValue value = ((LuaScriptEngine.LuajCompiledScript)cs).function;
assertFalse(value.isclosure());
} }
} }
@@ -117,7 +122,7 @@ public class ScriptEngineTests extends TestSuite {
protected ScriptEngine e; protected ScriptEngine e;
protected Bindings b; protected Bindings b;
abstract protected Bindings createBindings(); abstract protected Bindings createBindings();
public void setUp() { protected void setUp() throws Exception {
this.e = new ScriptEngineManager().getEngineByName("luaj"); this.e = new ScriptEngineManager().getEngineByName("luaj");
this.b = createBindings(); this.b = createBindings();
} }