Initial sources for planned 2.0 luaj vm release. Most interpreter features and library functions working.
This commit is contained in:
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user