Initial sources for planned 2.0 luaj vm release. Most interpreter features and library functions working.
This commit is contained in:
34
examples/jme/SampleMIDlet.java
Normal file
34
examples/jme/SampleMIDlet.java
Normal 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() {
|
||||
}
|
||||
|
||||
}
|
||||
20
examples/jse/SampleJseMain.java
Normal file
20
examples/jse/SampleJseMain.java
Normal 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) );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
50
examples/jse/ScriptEngineSample.java
Normal file
50
examples/jse/ScriptEngineSample.java
Normal 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
1
examples/lua/hello.lua
Normal file
@@ -0,0 +1 @@
|
||||
print( 'hello, world' )
|
||||
29
examples/lua/swingapp.lua
Normal file
29
examples/lua/swingapp.lua
Normal 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)
|
||||
Reference in New Issue
Block a user