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)); } }
/** Get a value in a table including metatag processing using {@link #INDEX}. * @param key the key to look up * @return {@link LuaValue} for that key, or {@link #NIL} if not found * @throws LuaError if {@code this} is not a table, * or there is no {@link #INDEX} metatag * @see #get(LuaValue) * @see #rawget(int) */ public LuaValue get( int key ) { return get(LuaInteger.valueOf(key)); }
/** Get a value in a table including metatag processing using {@link #INDEX}. * @param key the key to look up * @return {@link LuaValue} for that key, or {@link #NIL} if not found * @throws LuaError if {@code this} is not a table, * or there is no {@link #INDEX} metatag * @see #get(LuaValue) * @see #rawget(int) */ public LuaValue get( int key ) { return get(LuaInteger.valueOf(key)); }
/** Get a value in a table including metatag processing using {@link #INDEX}. * @param key the key to look up, must not be null * @return {@link LuaValue} for that key, or {@link #NIL} if not found * @throws LuaError if {@code this} is not a table, * or there is no {@link #INDEX} metatag * @see #get(LuaValue) * @see #rawget(String) */ public LuaValue get( String key ) { return get(valueOf(key)); }
/** Get a value in a table including metatag processing using {@link INDEX}. * @param key the key to look up, must not be null * @return {@link LuaValue} for that key, or {@link NIL} if not found * @throws LuaError if {@code this} is not a table, * or there is no {@link INDEX} metatag * @see #get(LuaValue) * @see #rawget(String) */ public LuaValue get( String key ) { return get(valueOf(key)); }
/** Get a value in a table including metatag processing using {@link #INDEX}. * @param key the key to look up, must not be null * @return {@link LuaValue} for that key, or {@link #NIL} if not found * @throws LuaError if {@code this} is not a table, * or there is no {@link #INDEX} metatag * @see #get(LuaValue) * @see #rawget(String) */ public LuaValue get( String key ) { return get(valueOf(key)); }
LuaValue globals = JsePlatform.standardGlobals(); globals.get("dofile").call( LuaValue.valueOf(yourScriptfile)); globals.set("callMyFunction", new MyZeroArgImpl());
/** Get a value in a table including metatag processing using {@link INDEX}. * @param key the key to look up * @return {@link LuaValue} for that key, or {@link NIL} if not found * @throws LuaError if {@code this} is not a table, * or there is no {@link INDEX} metatag * @see #get(LuaValue) * @see #rawget(int) */ public LuaValue get( int key ) { return get(LuaInteger.valueOf(key)); }
Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.load( "t = {"+ " subTitle = 'Haircut',"+ "};"+ "return t"); LuaValue result = chunk.call(); // Executes the chunk and returns 't' String text = result.get("subTitle").tojstring(); System.out.println(text);
@Override public LuaValue call(LuaValue frameId, LuaValue function, LuaValue args) { if ("filepath".equals(function.checkjstring())) { String path = args.get(1).checkjstring(); return toLuaString(path); } return LuaValue.NIL; } };
protected String executeFunctionChunk(LuaValue luaFunction, Frame frame) { assertFunction(luaFunction); try { currentFrame = frame; LuaValue executeFunction = globals.get("mw").get("executeFunction"); logger.trace("executing "+luaFunction); final LuaString result = executeFunction.call(luaFunction).checkstring(); return new String(result.m_bytes, result.m_offset, result.m_length, UTF8); } finally { currentFrame = null; } }
public int score(LuaValue value) { switch ( value.type() ) { case LuaValue.TTABLE: return value.length()==0? 0: componentCoercion.score( value.get(1) ); case LuaValue.TUSERDATA: return inheritanceLevels( componentType, value.touserdata().getClass().getComponentType() ); case LuaValue.TNIL: return SCORE_NULL_VALUE; default: return SCORE_UNCOERCIBLE; } } public Object coerce(LuaValue value) {
public class MyLib extends TwoArgFunction { public static MyLib MYLIB = null; public MyLib() { MYLIB = this; } public LuaValue call(LuaValue modname, LuaValue env) { LuaTable mine = new LuaTable(0,30); mine.set("someFunc", new SomeFunc()); env.set("mine", mine); env.get("package").get("loaded").set("mine", mine); return mine; } }
@Override public LuaValue call(LuaValue modname, LuaValue env) { LuaTable t = new LuaTable(); bind(t, new Bit32LibV(), new String[] { "band", "bnot", "bor", "btest", "bxor", "extract", "replace" }); bind(t, new Bit32Lib2(), new String[] { "arshift", "lrotate", "lshift", "rrotate", "rshift" }); env.set("bit32", t); env.get("package").get("loaded").set("bit32", t); return t; }
public Varargs invoke(Varargs args) { LuaString name = args.checkstring(1); LuaValue val = package_.get(_PRELOAD).get(name); return val.isnil()? valueOf("\n\tno field package.preload['"+name+"']"): val; } }
public Varargs invoke(Varargs args) { LuaString name = args.checkstring(1); LuaValue val = package_.get(_PRELOAD).get(name); return val.isnil()? valueOf("\n\tno field package.preload['"+name+"']"): val; } }
@Override public LuaValue call(LuaValue var1, LuaValue var2) { LuaTable functions = new LuaTable(); functions.set("getRapidView", new GetPhotonView()); functions.set("getJavaBridge", new GetJavaBridge()); functions.set("getEnvironment", new GetEnvironment()); var2.set("rapid", functions); var2.get("package").get("loaded").set("rapid", functions); return functions; }
protected LuaValue loadFunction(String functionName, Prototype prototype, Frame frame) throws ScribuntoException { try { currentFrame = frame; LuaValue function = new LuaClosure(prototype, globals).checkfunction().call().get(functionName); if (function.isnil()) { throw new ScribuntoException("no such function '"+functionName+"'"); } return function; } catch (LuaError e) { throw new ScribuntoException(e); } finally { currentFrame = null; } }