Improve JSR-223 bindings especially for values that are not primitives.

This commit is contained in:
James Roseborough
2010-12-03 01:37:47 +00:00
parent 7a24e6148a
commit 31f6ffde88
4 changed files with 83 additions and 76 deletions

View File

@@ -16,7 +16,7 @@
Getting Started with LuaJ Getting Started with LuaJ
</h1> </h1>
James Roseborough, Ian Farmer, Version 2.0.1 James Roseborough, Ian Farmer, Version 2.0.2
<p> <p>
<small> <small>
Copyright &copy; 2009-2010 Luaj.org. Copyright &copy; 2009-2010 Luaj.org.
@@ -106,7 +106,7 @@ in comparison with the standard C distribution.
<td>16.794</td> <td>16.794</td>
<td>11.274</td> <td>11.274</td>
<td>Java</td> <td>Java</td>
<td>java -cp luaj-jse-2.0.1.jar;bcel-5.2.jar lua <b>-b</b> fannkuch.lua 10</td></tr> <td>java -cp luaj-jse-2.0.2.jar;bcel-5.2.jar lua <b>-b</b> fannkuch.lua 10</td></tr>
<tr valign="top"> <tr valign="top">
<td></td> <td></td>
<td></td> <td></td>
@@ -116,7 +116,7 @@ in comparison with the standard C distribution.
<td>16.701</td> <td>16.701</td>
<td>13.789</td> <td>13.789</td>
<td></td> <td></td>
<td>java -cp luaj-jse-2.0.1.jar lua <b>-j</b> fannkuch.lua 10</td></tr> <td>java -cp luaj-jse-2.0.2.jar lua <b>-j</b> fannkuch.lua 10</td></tr>
<tr valign="top"> <tr valign="top">
<td></td> <td></td>
<td></td> <td></td>
@@ -126,7 +126,7 @@ in comparison with the standard C distribution.
<td>36.894</td> <td>36.894</td>
<td>15.163</td> <td>15.163</td>
<td></td> <td></td>
<td>java -cp luaj-jse-2.0.1.jar lua -n fannkuch.lua 10</td></tr> <td>java -cp luaj-jse-2.0.2.jar lua -n fannkuch.lua 10</td></tr>
<tr valign="top"> <tr valign="top">
<td>lua</td> <td>lua</td>
<td>5.1.4</td> <td>5.1.4</td>
@@ -182,7 +182,7 @@ It is also faster than Java-lua implementations Jill, Kahlua, and Mochalua for a
From the main distribution directory line type: From the main distribution directory line type:
<pre> <pre>
java -cp lib/luaj-jse-2.0.1.jar lua examples/lua/hello.lua java -cp lib/luaj-jse-2.0.2.jar lua examples/lua/hello.lua
</pre> </pre>
<p> <p>
@@ -197,8 +197,8 @@ You should see the following output:
From the main distribution directory line type: From the main distribution directory line type:
<pre> <pre>
java -cp lib/luaj-jse-2.0.1.jar luac examples/lua/hello.lua java -cp lib/luaj-jse-2.0.2.jar luac examples/lua/hello.lua
java -cp lib/luaj-jse-2.0.1.jar lua luac.out java -cp lib/luaj-jse-2.0.2.jar lua luac.out
</pre> </pre>
<p> <p>
@@ -210,9 +210,9 @@ The compiled output "luac.out" is lua bytecode and should run and produce the sa
Luaj can compile to lua source code to Java source code: Luaj can compile to lua source code to Java source code:
<pre> <pre>
java -cp lib/luaj-jse-2.0.1.jar lua2java -s examples/lua -d . hello.lua java -cp lib/luaj-jse-2.0.2.jar lua2java -s examples/lua -d . hello.lua
javac -cp lib/luaj-jse-2.0.1.jar hello.java javac -cp lib/luaj-jse-2.0.2.jar hello.java
java -cp &quot;lib/luaj-jse-2.0.1.jar;.&quot; lua -l hello java -cp &quot;lib/luaj-jse-2.0.2.jar;.&quot; lua -l hello
</pre> </pre>
<p> <p>
@@ -223,7 +223,7 @@ There are no additional dependencies for compiling or running source-to-source c
<p> <p>
Lua scripts can also be run directly in this mode without precompiling using the <em>lua</em> command with the <b><em>-j</em></b> option when run in JDK 1.5 or higher: Lua scripts can also be run directly in this mode without precompiling using the <em>lua</em> command with the <b><em>-j</em></b> option when run in JDK 1.5 or higher:
<pre> <pre>
java -cp lib/luaj-jse-2.0.1.jar lua -j examples/lua/hello.lua java -cp lib/luaj-jse-2.0.2.jar lua -j examples/lua/hello.lua
</pre> </pre>
<h2>Compile lua bytecode to java bytecode</h2> <h2>Compile lua bytecode to java bytecode</h2>
@@ -233,8 +233,8 @@ Luaj can compile lua sources or binaries directly to java bytecode if the bcel l
<pre> <pre>
ant bcel-lib ant bcel-lib
java -cp &quot;lib/luaj-jse-2.0.1.jar;lib/bcel-5.2.jar&quot; luajc -s examples/lua -d . hello.lua java -cp &quot;lib/luaj-jse-2.0.2.jar;lib/bcel-5.2.jar&quot; luajc -s examples/lua -d . hello.lua
java -cp &quot;lib/luaj-jse-2.0.1.jar;.&quot; lua -l hello java -cp &quot;lib/luaj-jse-2.0.2.jar;.&quot; lua -l hello
</pre> </pre>
<p> <p>
@@ -245,7 +245,7 @@ but the compiled classes must be in the class path at runtime, unless runtime ji
<p> <p>
Lua scripts can also be run directly in this mode without precompiling using the <em>lua</em> command with the <b><em>-b</em></b> option and providing the <em>bcel</em> library in the class path: Lua scripts can also be run directly in this mode without precompiling using the <em>lua</em> command with the <b><em>-b</em></b> option and providing the <em>bcel</em> library in the class path:
<pre> <pre>
java -cp &quot;lib/luaj-jse-2.0.1.jar;lib/bcel-5.2.jar&quot; lua -b examples/lua/hello.lua java -cp &quot;lib/luaj-jse-2.0.2.jar;lib/bcel-5.2.jar&quot; lua -b examples/lua/hello.lua
</pre> </pre>
@@ -270,7 +270,7 @@ A simple example may be found in
</pre> </pre>
<p> <p>
You must include the library <b>lib/luaj-jse-2.0.1.jar</b> in your class path. You must include the library <b>lib/luaj-jse-2.0.2.jar</b> in your class path.
<h2>Run a script in a MIDlet</h2> <h2>Run a script in a MIDlet</h2>
@@ -297,7 +297,7 @@ A simple example may be found in
</pre> </pre>
<p> <p>
You must include the library <b>lib/luaj-jme-2.0.1.jar</b> in your midlet jar. You must include the library <b>lib/luaj-jme-2.0.2.jar</b> in your midlet jar.
<p> <p>
An ant script to build and run the midlet is in An ant script to build and run the midlet is in
@@ -325,7 +325,7 @@ The standard use of JSR-223 scripting engines may be used:
All standard aspects of script engines including compiled statements should be supported. All standard aspects of script engines including compiled statements should be supported.
<p> <p>
You must include the library <b>lib/luaj-jse-2.0.1.jar</b> in your class path. You must include the library <b>lib/luaj-jse-2.0.2.jar</b> in your class path.
<p> <p>
A working example may be found in A working example may be found in
@@ -337,7 +337,7 @@ To compile and run it using Java 1.6 or higher:
<pre> <pre>
javac examples/jse/ScriptEngineSample.java javac examples/jse/ScriptEngineSample.java
java -cp &quot;lib/luaj-jse-2.0.1.jar;examples/jse&quot; ScriptEngineSample java -cp &quot;lib/luaj-jse-2.0.2.jar;examples/jse&quot; ScriptEngineSample
</pre> </pre>
<h2>Excluding the lua bytecode compiler</h2> <h2>Excluding the lua bytecode compiler</h2>
@@ -501,7 +501,7 @@ The following lua script will open a swiing frame on Java SE:
<p> <p>
See a longer sample in <em>examples/lua/swingapp.lua</em> for details, or try running it using: See a longer sample in <em>examples/lua/swingapp.lua</em> for details, or try running it using:
<pre> <pre>
java -cp lib/luaj-jse-2.0.1.jar lua examples/lua/swingapp.lua java -cp lib/luaj-jse-2.0.2.jar lua examples/lua/swingapp.lua
</pre> </pre>
<p> <p>
@@ -716,6 +716,9 @@ and LuaForge:
<li>Fix nan-related error in constant folding logic that was failing on some JVMs <li>Fix nan-related error in constant folding logic that was failing on some JVMs
<li>JSR-223 fixes: add META-INF/services entry in jse jar, improve bindings implementation <li>JSR-223 fixes: add META-INF/services entry in jse jar, improve bindings implementation
</ul></td></tr> </ul></td></tr>
<tr valign="top"><td>&nbsp;&nbsp;<b>2.0.2</b></td><td><ul>
<li>JSR-223 bindings will pass values that are not primitives as LuaValue
</ul></td></tr>
</table> </table>
<h2>Known Issues</h2> <h2>Known Issues</h2>

View File

@@ -43,6 +43,18 @@ public class ScriptEngineSample {
System.out.println( "eval: "+cs.eval(sb) ); System.out.println( "eval: "+cs.eval(sb) );
System.out.println( "y="+sb.get("y") ); System.out.println( "y="+sb.get("y") );
cs = ((Compilable)e).compile(
"print( 'luajava', luajava )\n" +
// "_G.lua2java = require( 'org.luaj.vm2.lib.jse.LuajavaLib' )\n" +
// "print( 'lua2java', lua2java )\n" +
// "print( 'lua2java.newInstance', lua2java.newInstance )\n" +
"test = luajava.newInstance(\"java.lang.String\", \"test\")\n" +
"return test:toString()");
b = e.createBindings();
System.out.println( "eval: "+cs.eval(b) );
Object t = b.get("test");
System.out.println( "t="+t );
try { try {
e.eval("\n\nbogus example\n\n"); e.eval("\n\nbogus example\n\n");
} catch ( ScriptException se ) { } catch ( ScriptException se ) {

View File

@@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.util.Iterator;
import javax.script.Bindings; import javax.script.Bindings;
import javax.script.Compilable; import javax.script.Compilable;
@@ -44,8 +45,7 @@ import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue; import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Prototype; import org.luaj.vm2.Prototype;
import org.luaj.vm2.lib.ThreeArgFunction; import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.TwoArgFunction;
import org.luaj.vm2.lib.jse.JsePlatform; import org.luaj.vm2.lib.jse.JsePlatform;
/** /**
@@ -168,9 +168,11 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
return new CompiledScript() { return new CompiledScript() {
public Object eval(ScriptContext context) throws ScriptException { public Object eval(ScriptContext context) throws ScriptException {
Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE); Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE);
LuaValue env = newBindingsBackedEnv(b); BindingsEnv env = new BindingsEnv(b);
LuaClosure c = new LuaClosure( p, env ); LuaClosure c = new LuaClosure( p, env.t );
return c.invoke(LuaValue.NONE); Varargs result = c.invoke(LuaValue.NONE);
env.copyGlobalsToBindings();
return result;
} }
public ScriptEngine getEngine() { public ScriptEngine getEngine() {
return LuaScriptEngine.this; return LuaScriptEngine.this;
@@ -181,15 +183,17 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
return new CompiledScript() { return new CompiledScript() {
public Object eval(ScriptContext context) throws ScriptException { public Object eval(ScriptContext context) throws ScriptException {
Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE); Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE);
LuaValue env = newBindingsBackedEnv(b); BindingsEnv env = new BindingsEnv(b);
LuaFunction lf; LuaFunction lf;
try { try {
lf = (LuaFunction) c.newInstance(); lf = (LuaFunction) c.newInstance();
} catch (Exception e) { } catch (Exception e) {
throw new ScriptException("instantiation failed: "+e.toString()); throw new ScriptException("instantiation failed: "+e.toString());
} }
lf.setfenv(env); lf.setfenv(env.t);
return lf.invoke(LuaValue.NONE); Varargs result = lf.invoke(LuaValue.NONE);
env.copyGlobalsToBindings();
return result;
} }
public ScriptEngine getEngine() { public ScriptEngine getEngine() {
return LuaScriptEngine.this; return LuaScriptEngine.this;
@@ -207,58 +211,25 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
} }
// ------ lua bindings ----- private static final class BindingsBackedEnv extends LuaTable { // ------ lua bindings ----- private static final class BindingsBackedEnv extends LuaTable {
public LuaTable newBindingsBackedEnv(Bindings b) { public class BindingsEnv {
LuaTable t = new LuaTable(); public final Bindings b;
LuaTable mt = new LuaTable(0,2); public final LuaTable t;
mt.set(LuaValue.INDEX, new BindindsIndexFunction(b)); public BindingsEnv( Bindings b ) {
mt.set(LuaValue.NEWINDEX, new BindingsNewindexFunction(b));
t.setmetatable(mt);
return t;
}
private static final class BindingsNewindexFunction extends ThreeArgFunction {
private final Bindings b;
private BindingsNewindexFunction(Bindings b) {
this.b = b; this.b = b;
this.t = new LuaTable();
t.setmetatable(LuaTable.tableOf(new LuaValue[] { LuaValue.INDEX, _G }));
this.copyBindingsToGlobals();
} }
public void copyBindingsToGlobals() {
public LuaValue call(LuaValue tbl, LuaValue key, LuaValue val) { for ( Iterator<String> i = b.keySet().iterator(); i.hasNext(); ) {
String k = key.tojstring(); String key = i.next();
if ( val.isnil() ) Object val = b.get(key);
b.remove(k); LuaValue luakey = toLua(key);
else LuaValue luaval = toLua(val);
b.put(k, toJava(val)); t.set(luakey, luaval);
return NIL; i.remove();
}
private Object toJava(LuaValue v) {
switch ( v.type() ) {
case LuaValue.TNIL: return null;
case LuaValue.TSTRING: return v.tojstring();
case LuaValue.TUSERDATA: return v.checkuserdata(Object.class);
case LuaValue.TNUMBER: return v.isinttype()? (Object) new Integer(v.toint()): (Object) new Double(v.todouble());
default:
throw new java.lang.UnsupportedOperationException(
"LuaBindings cannot convert lua type '"+v.typename()+"' to Java");
} }
} }
}
private final class BindindsIndexFunction extends TwoArgFunction {
private final Bindings b;
private BindindsIndexFunction(Bindings b) {
this.b = b;
}
public LuaValue call(LuaValue tbl, LuaValue key) {
String k = key.tojstring();
if ( b.containsKey(k) )
return toLua(b.get(k));
return _G.get(key);
}
private LuaValue toLua(Object javaValue) { private LuaValue toLua(Object javaValue) {
if ( javaValue instanceof Number ) { if ( javaValue instanceof Number ) {
return LuaValue.valueOf(((Number)javaValue).doubleValue()); return LuaValue.valueOf(((Number)javaValue).doubleValue());
@@ -266,10 +237,31 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
return LuaValue.valueOf(javaValue.toString()); return LuaValue.valueOf(javaValue.toString());
} else if ( javaValue == null ) { } else if ( javaValue == null ) {
return LuaValue.NIL; return LuaValue.NIL;
} else if ( javaValue instanceof LuaValue ) {
return (LuaValue) javaValue;
} else { } else {
return LuaValue.userdataOf(javaValue); return LuaValue.userdataOf(javaValue);
} }
} }
public void copyGlobalsToBindings() {
LuaValue[] keys = t.keys();
for ( int i=0; i<keys.length; i++ ) {
LuaValue luakey = keys[i];
LuaValue luaval = t.get(luakey);
String key = luakey.tojstring();
Object val = toJava( luaval );
b.put(key,val);
}
}
private Object toJava(LuaValue v) {
switch ( v.type() ) {
case LuaValue.TNIL: return null;
case LuaValue.TSTRING: return v.tojstring();
case LuaValue.TUSERDATA: return v.checkuserdata(Object.class);
case LuaValue.TNUMBER: return v.isinttype()? (Object) new Integer(v.toint()): (Object) new Double(v.todouble());
default: return v;
}
}
} }
// ------ convert char stream to byte stream for lua compiler ----- // ------ convert char stream to byte stream for lua compiler -----

View File

@@ -1 +1 @@
version: 2.0.1 version: 2.0.2