CompiledScript script = ((Compilable) scriptEngine).compile(reader); Bindings sb = new SimpleBindings(); script.eval(sb); // Put the Lua functions into the sb environment LuaValue luaDog = CoerceJavaToLua.coerce(dog); // Java to Lua LuaFunction onTalk = (LuaFunction) sb.get("onTalk"); // Get Lua function LuaValue b = onTalk.call(luaDog); // Call the function System.out.println("onTalk answered: " + b); LuaFunction onWalk = (LuaFunction) sb.get("onWalk"); LuaValue[] dogs = { luaDog }; Varargs dist = onWalk.invoke(LuaValue.varargsOf(dogs)); // Alternative System.out.println("onWalk returned: " + dist);
public static LuaValue runInputStreamLua(InputStream inputStream) throws Exception { Prototype luaScriptPrototype = LuaC.instance.compile(inputStream, ""); Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.load("require 'com.example.import-me';"); chunk.call(); LuaClosure luaClosure = new LuaClosure(luaScriptPrototype, globals); return luaClosure.call(); }
protected static Entry defaultEntry(LuaValue key, LuaValue value) { if ( key.isinttype() ) { return new IntKeyEntry( key.toint(), value ); } else if (value.type() == TNUMBER) { return new NumberValueEntry( key, value.todouble() ); } else { return new NormalEntry( key, value ); } }
public Varargs invoke(Varargs args) { args.arg1().checktable().sort( args.arg(2).isnil()? NIL: args.arg(2).checkfunction()); return NONE; } }
import org.luaj.vm2.LuaValue; import org.luaj.vm2.lib.jse.JsePlatform; public class test { public static void main(String[] args) { //run the lua script defining your function LuaValue _G = JsePlatform.standardGlobals(); _G.get("dofile").call( LuaValue.valueOf("./test.lua")); //call the function MyAdd with two parameters 5, and 5 LuaValue MyAdd = _G.get("MyAdd"); LuaValue retvals = MyAdd.call(LuaValue.valueOf(5), LuaValue.valueOf(5)); //print out the result from the lua function System.out.println(retvals.tojstring(1)); } }
/** Concatenate this buffer onto a {@link LuaNumber} * <p> * The {@link LuaNumber} will be converted to a string before concatenating. * @param lhs the left-hand-side value onto which we are concatenating {@code this} * @return {@link Buffer} for use in call chaining. */ public Buffer concatTo(LuaNumber lhs) { return value!=null&&!value.isstring()? setvalue(lhs.concat(value)): prepend(lhs.strvalue()); }
/** Return argument i as a double, or throw an error if it cannot be converted to one. * @param i the index of the argument to test, 1 is the first argument * @return java double value if argument i is a number or string that converts to a number * @exception LuaError if the argument is not a number * */ public double checkdouble(int i) { return arg(i).checknumber().todouble(); }
/** Return argument i as a java long value, discarding any fractional part, or throw an error if not a number. * @param i the index of the argument to test, 1 is the first argument * @return long value with fraction discarded and truncated if necessary if argument i is number * @exception LuaError if the argument is not a number * */ public long checklong(int i) { return arg(i).checknumber().tolong(); }
/** Function which yields the current thread. * @param args Arguments to supply as return values in the resume function of the resuming thread. * @return Values supplied as arguments to the resume() call that reactivates this thread. */ public Varargs yield(Varargs args) { if (running == null || running.isMainThread()) throw new LuaError("cannot yield main thread"); final LuaThread.State s = running.state; return s.lua_yield(args); }
protected static Entry defaultEntry(LuaValue key, LuaValue value) { if ( key.isinttype() ) { return new IntKeyEntry( key.toint(), value ); } else if (value.type() == TNUMBER) { return new NumberValueEntry( key, value.todouble() ); } else { return new NormalEntry( key, value ); } }