diff --git a/README.html b/README.html index b970c1f7..28022618 100644 --- a/README.html +++ b/README.html @@ -16,7 +16,7 @@ Getting Started with LuaJ -James Roseborough, Ian Farmer, Version 2.0.1 +James Roseborough, Ian Farmer, Version 2.0.2

Copyright © 2009-2010 Luaj.org. @@ -106,7 +106,7 @@ in comparison with the standard C distribution. 16.794 11.274 Java - java -cp luaj-jse-2.0.1.jar;bcel-5.2.jar lua -b fannkuch.lua 10 + java -cp luaj-jse-2.0.2.jar;bcel-5.2.jar lua -b fannkuch.lua 10 @@ -116,7 +116,7 @@ in comparison with the standard C distribution. 16.701 13.789 - java -cp luaj-jse-2.0.1.jar lua -j fannkuch.lua 10 + java -cp luaj-jse-2.0.2.jar lua -j fannkuch.lua 10 @@ -126,7 +126,7 @@ in comparison with the standard C distribution. 36.894 15.163 - java -cp luaj-jse-2.0.1.jar lua -n fannkuch.lua 10 + java -cp luaj-jse-2.0.2.jar lua -n fannkuch.lua 10 lua 5.1.4 @@ -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:

-	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
 

@@ -197,8 +197,8 @@ You should see the following output: From the main distribution directory line type:

-	java -cp lib/luaj-jse-2.0.1.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 luac examples/lua/hello.lua
+	java -cp lib/luaj-jse-2.0.2.jar lua luac.out
 

@@ -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:

-	java -cp lib/luaj-jse-2.0.1.jar lua2java -s examples/lua -d . hello.lua
-	javac -cp lib/luaj-jse-2.0.1.jar hello.java
-	java -cp "lib/luaj-jse-2.0.1.jar;." lua -l hello
+	java -cp lib/luaj-jse-2.0.2.jar lua2java -s examples/lua -d . hello.lua
+	javac -cp lib/luaj-jse-2.0.2.jar hello.java
+	java -cp "lib/luaj-jse-2.0.2.jar;." lua -l hello
 

@@ -223,7 +223,7 @@ There are no additional dependencies for compiling or running source-to-source c

Lua scripts can also be run directly in this mode without precompiling using the lua command with the -j option when run in JDK 1.5 or higher:

-	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
 

Compile lua bytecode to java bytecode

@@ -233,8 +233,8 @@ Luaj can compile lua sources or binaries directly to java bytecode if the bcel l
 	ant bcel-lib
-	java -cp "lib/luaj-jse-2.0.1.jar;lib/bcel-5.2.jar" luajc -s examples/lua -d . hello.lua
-	java -cp "lib/luaj-jse-2.0.1.jar;." lua -l hello
+	java -cp "lib/luaj-jse-2.0.2.jar;lib/bcel-5.2.jar" luajc -s examples/lua -d . hello.lua
+	java -cp "lib/luaj-jse-2.0.2.jar;." lua -l hello
 

@@ -245,7 +245,7 @@ but the compiled classes must be in the class path at runtime, unless runtime ji

Lua scripts can also be run directly in this mode without precompiling using the lua command with the -b option and providing the bcel library in the class path:

-	java -cp "lib/luaj-jse-2.0.1.jar;lib/bcel-5.2.jar" lua -b examples/lua/hello.lua
+	java -cp "lib/luaj-jse-2.0.2.jar;lib/bcel-5.2.jar" lua -b examples/lua/hello.lua
 
@@ -270,7 +270,7 @@ A simple example may be found in

-You must include the library lib/luaj-jse-2.0.1.jar in your class path. +You must include the library lib/luaj-jse-2.0.2.jar in your class path.

Run a script in a MIDlet

@@ -297,7 +297,7 @@ A simple example may be found in

-You must include the library lib/luaj-jme-2.0.1.jar in your midlet jar. +You must include the library lib/luaj-jme-2.0.2.jar in your midlet jar.

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.

-You must include the library lib/luaj-jse-2.0.1.jar in your class path. +You must include the library lib/luaj-jse-2.0.2.jar in your class path.

A working example may be found in @@ -337,7 +337,7 @@ To compile and run it using Java 1.6 or higher:

 	javac examples/jse/ScriptEngineSample.java
-	java -cp "lib/luaj-jse-2.0.1.jar;examples/jse" ScriptEngineSample
+	java -cp "lib/luaj-jse-2.0.2.jar;examples/jse" ScriptEngineSample
 

Excluding the lua bytecode compiler

@@ -501,7 +501,7 @@ The following lua script will open a swiing frame on Java SE:

See a longer sample in examples/lua/swingapp.lua for details, or try running it using:

-	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
 

@@ -716,6 +716,9 @@ and LuaForge:

  • Fix nan-related error in constant folding logic that was failing on some JVMs
  • JSR-223 fixes: add META-INF/services entry in jse jar, improve bindings implementation +  2.0.2

    Known Issues

    diff --git a/examples/jse/ScriptEngineSample.java b/examples/jse/ScriptEngineSample.java index 131180a4..b4f7b7dc 100644 --- a/examples/jse/ScriptEngineSample.java +++ b/examples/jse/ScriptEngineSample.java @@ -43,6 +43,18 @@ public class ScriptEngineSample { System.out.println( "eval: "+cs.eval(sb) ); 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 { e.eval("\n\nbogus example\n\n"); } catch ( ScriptException se ) { diff --git a/src/jse/org/luaj/vm2/script/LuaScriptEngine.java b/src/jse/org/luaj/vm2/script/LuaScriptEngine.java index 2a64a17c..ca93d654 100644 --- a/src/jse/org/luaj/vm2/script/LuaScriptEngine.java +++ b/src/jse/org/luaj/vm2/script/LuaScriptEngine.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.io.StringReader; +import java.util.Iterator; import javax.script.Bindings; import javax.script.Compilable; @@ -44,8 +45,7 @@ import org.luaj.vm2.LuaFunction; import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaValue; import org.luaj.vm2.Prototype; -import org.luaj.vm2.lib.ThreeArgFunction; -import org.luaj.vm2.lib.TwoArgFunction; +import org.luaj.vm2.Varargs; import org.luaj.vm2.lib.jse.JsePlatform; /** @@ -168,9 +168,11 @@ public class LuaScriptEngine implements ScriptEngine, Compilable { return new CompiledScript() { public Object eval(ScriptContext context) throws ScriptException { Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE); - LuaValue env = newBindingsBackedEnv(b); - LuaClosure c = new LuaClosure( p, env ); - return c.invoke(LuaValue.NONE); + BindingsEnv env = new BindingsEnv(b); + LuaClosure c = new LuaClosure( p, env.t ); + Varargs result = c.invoke(LuaValue.NONE); + env.copyGlobalsToBindings(); + return result; } public ScriptEngine getEngine() { return LuaScriptEngine.this; @@ -181,15 +183,17 @@ public class LuaScriptEngine implements ScriptEngine, Compilable { return new CompiledScript() { public Object eval(ScriptContext context) throws ScriptException { Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE); - LuaValue env = newBindingsBackedEnv(b); + BindingsEnv env = new BindingsEnv(b); LuaFunction lf; try { lf = (LuaFunction) c.newInstance(); } catch (Exception e) { throw new ScriptException("instantiation failed: "+e.toString()); } - lf.setfenv(env); - return lf.invoke(LuaValue.NONE); + lf.setfenv(env.t); + Varargs result = lf.invoke(LuaValue.NONE); + env.copyGlobalsToBindings(); + return result; } public ScriptEngine getEngine() { return LuaScriptEngine.this; @@ -207,58 +211,25 @@ public class LuaScriptEngine implements ScriptEngine, Compilable { } // ------ lua bindings ----- private static final class BindingsBackedEnv extends LuaTable { - public LuaTable newBindingsBackedEnv(Bindings b) { - LuaTable t = new LuaTable(); - LuaTable mt = new LuaTable(0,2); - mt.set(LuaValue.INDEX, new BindindsIndexFunction(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) { + public class BindingsEnv { + public final Bindings b; + public final LuaTable t; + public BindingsEnv( Bindings b ) { this.b = b; + this.t = new LuaTable(); + t.setmetatable(LuaTable.tableOf(new LuaValue[] { LuaValue.INDEX, _G })); + this.copyBindingsToGlobals(); } - - public LuaValue call(LuaValue tbl, LuaValue key, LuaValue val) { - String k = key.tojstring(); - if ( val.isnil() ) - b.remove(k); - else - b.put(k, toJava(val)); - return NIL; - } - - 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"); + public void copyBindingsToGlobals() { + for ( Iterator i = b.keySet().iterator(); i.hasNext(); ) { + String key = i.next(); + Object val = b.get(key); + LuaValue luakey = toLua(key); + LuaValue luaval = toLua(val); + t.set(luakey, luaval); + i.remove(); } } - } - - 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) { if ( javaValue instanceof Number ) { return LuaValue.valueOf(((Number)javaValue).doubleValue()); @@ -266,10 +237,31 @@ public class LuaScriptEngine implements ScriptEngine, Compilable { return LuaValue.valueOf(javaValue.toString()); } else if ( javaValue == null ) { return LuaValue.NIL; + } else if ( javaValue instanceof LuaValue ) { + return (LuaValue) javaValue; } else { return LuaValue.userdataOf(javaValue); } } + public void copyGlobalsToBindings() { + LuaValue[] keys = t.keys(); + for ( int i=0; i