Initial sources for planned 2.0 luaj vm release. Most interpreter features and library functions working.

This commit is contained in:
James Roseborough
2009-10-27 06:12:24 +00:00
parent d16fad00e8
commit 3863ff8e46
116 changed files with 33600 additions and 122 deletions

View File

@@ -0,0 +1,34 @@
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
import org.luaj.vm2.compiler.LuaC;
public class SampleMIDlet extends MIDlet {
// the script will be loaded as a resource
private static final String DEFAULT_SCRIPT = "hello.lua";
protected void startApp() throws MIDletStateChangeException {
// get the script as an app property
String script = this.getAppProperty("script");
if ( script == null )
script = DEFAULT_SCRIPT;
// create an environment to run in
LuaC.install();
LuaValue _G = JmePlatform.standardGlobals();
_G.get("dofile").call( LuaValue.valueOf(script) );
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}

View File

@@ -0,0 +1,20 @@
import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
import org.luaj.vm2.compiler.LuaC;
public class SampleJseMain {
public static void main(String[] args) throws Exception {
String script = "examples/lua/hello.lua";
// create an environment to run in
LuaC.install();
LuaValue _G = JsePlatform.standardGlobals();
_G.get("dofile").call( LuaValue.valueOf(script) );
}
}

View File

@@ -0,0 +1,50 @@
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class ScriptEngineSample {
public static void main(String [] args) {
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine e = sem.getEngineByExtension(".lua");
ScriptEngineFactory f = e.getFactory();
System.out.println( "Engine name: " +f.getEngineName() );
System.out.println( "Engine Version: " +f.getEngineVersion() );
System.out.println( "LanguageName: " +f.getLanguageName() );
System.out.println( "Language Version: " +f.getLanguageVersion() );
String statement = f.getOutputStatement("\"hello, world\"");
System.out.println(statement);
try {
e.eval(statement);
e.put("x", 25);
e.eval("y = math.sqrt(x)");
System.out.println( "y="+e.get("y") );
e.put("x", 2);
e.eval("y = math.sqrt(x)");
System.out.println( "y="+e.get("y") );
CompiledScript cs = ((Compilable)e).compile("y = math.sqrt(x); return y");
Bindings b = e.createBindings();
b.put("x", 3);
System.out.println( "eval: "+cs.eval(b) );
System.out.println( "y="+b.get("y") );
try {
e.eval("\n\nbogus example\n\n");
} catch ( ScriptException se ) {
System.out.println("script threw ScriptException as expected, message is '"+se.getMessage()+"'");
}
} catch (ScriptException ex) {
ex.printStackTrace();
}
}
}

1
examples/lua/hello.lua Normal file
View File

@@ -0,0 +1 @@
print( 'hello, world' )

29
examples/lua/swingapp.lua Normal file
View File

@@ -0,0 +1,29 @@
frame = luajava.newInstance( "javax.swing.JFrame", "Texts" );
pane = luajava.newInstance( "javax.swing.JPanel" );
borderFactory = luajava.bindClass( "javax.swing.BorderFactory" )
border = borderFactory:createEmptyBorder( 30, 30, 10, 30 )
pane:setBorder( border )
label = luajava.newInstance( "javax.swing.JLabel", "This is a Label" );
layout = luajava.newInstance( "java.awt.GridLayout", 2, 2 );
pane:setLayout( layout )
pane:add( label )
pane:setBounds( 20, 30, 10, 30 )
borderLayout = luajava.bindClass( "java.awt.BorderLayout" )
frame:getContentPane():add(pane, borderLayout.CENTER )
jframe = luajava.bindClass( "javax.swing.JFrame" )
frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE)
frame:pack()
frame:setVisible(true)
local listener = luajava.createProxy("java.awt.event.MouseListener",
{
mouseClicked = function(me)
print("clicked!", me)
end
})
frame:addMouseListener(listener)