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(); }
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)); } }
/** * Load a library instance by calling it with and empty string as the modname, * and this Globals as the environment. This is normally used to iniitalize the * library instance and which may install itself into these globals. * @param library The callable {@link LuaValue} to load into {@code this} * @return {@link LuaValue} returned by the initialization call. */ public LuaValue load(LuaValue library) { return library.call(EMPTYSTRING, this); }
/** * Load a library instance by calling it with and empty string as the modname, * and this Globals as the environment. This is normally used to iniitalize the * library instance and which may install itself into these globals. * @param library The callable {@link LuaValue} to load into {@code this} * @return {@link LuaValue} returned by the initialization call. */ public LuaValue load(LuaValue library) { return library.call(EMPTYSTRING, this); }
String script = "argument = ...\n"+ "test_string = 'Hello World!'\n"+ "print(test_string)\n"+ "print(argument)\n"; Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.loadString(script, "myscript"); chunk.call( LuaValue.valueOf("some-arg-value") );
/** Unary minus: return negative value {@code (-this)} as defined by lua unary minus operator * @return boolean inverse as {@link LuaBoolean} if boolean or nil, * numeric inverse as {@link LuaNumber} if numeric, * or metatag processing result if {@link #UNM} metatag is defined * @throws LuaError if {@code this} is not a table or string, and has no {@link #UNM} metatag */ public LuaValue neg() { return checkmetatag(UNM, "attempt to perform arithmetic on ").call(this); }
/** Convenience function which calls a luavalue with a single, string argument. * @param arg String argument to the function. This will be converted to a LuaString. * @return return value of the invocation. * @see #call(LuaValue) */ public LuaValue call(String arg) { return call(valueOf(arg)); }
/** Convenience function which calls a luavalue with a single, string argument. * @param arg String argument to the function. This will be converted to a LuaString. * @return return value of the invocation. * @see #call(LuaValue) */ public LuaValue call(String arg) { return call(valueOf(arg)); }
@Override public void runFunction(String functionName) { LuaValue v = mGlobals.get(functionName); if (v != null) { v.call(); } }
/** Length operator: return lua length of object {@code (#this)} including metatag processing as java int * @return length as defined by the lua # operator * or metatag processing result * @throws LuaError if {@code this} is not a table or string, and has no {@link #LEN} metatag */ public LuaValue len() { return checkmetatag(LEN, "attempt to get length of ").call(this); }
/** Unary minus: return negative value {@code (-this)} as defined by lua unary minus operator * @return boolean inverse as {@link LuaBoolean} if boolean or nil, * numeric inverse as {@link LuaNumber} if numeric, * or metatag processing result if {@link #UNM} metatag is defined * @throws LuaError if {@code this} is not a table or string, and has no {@link #UNM} metatag */ public LuaValue neg() { return checkmetatag(UNM, "attempt to perform arithmetic on ").call(this); }
/** Length operator: return lua length of object {@code (#this)} including metatag processing as java int * @return length as defined by the lua # operator * or metatag processing result * @throws LuaError if {@code this} is not a table or string, and has no {@link #LEN} metatag */ public LuaValue len() { return checkmetatag(LEN, "attempt to get length of ").call(this); }
@Override public void run(String script) { HNLog.d(HNLog.SANDBOX, "Execute script \"" + script + "\"."); LuaValue l = mGlobals.load(script); l.call(); }
public Varargs invoke(Varargs args) { LuaValue tostring = globals.get("tostring"); for ( int i=1, n=args.narg(); i<=n; i++ ) { if ( i>1 ) globals.STDOUT.print( '\t' ); LuaString s = tostring.call( args.arg(i) ).strvalue(); globals.STDOUT.print(s.tojstring()); } globals.STDOUT.println(); return NONE; } }
public LuaValue len() { final LuaValue h = metatag(LEN); if (h.toboolean()) return h.call(this); return LuaInteger.valueOf(rawlen()); }
public LuaValue len() { final LuaValue h = metatag(LEN); if (h.toboolean()) return h.call(this); return LuaInteger.valueOf(rawlen()); }
public LuaValue call(LuaValue arg) { LuaValue h = arg.metatag(TOSTRING); if (!h.isnil()) { return h.call(arg); } LuaValue v = arg.tostring(); if (!v.isnil()) { return v; } return valueOf(arg.tojstring()); } }
public LuaValue call(LuaValue arg) { LuaValue h = arg.metatag(TOSTRING); if ( ! h.isnil() ) return h.call(arg); LuaValue v = arg.tostring(); if ( ! v.isnil() ) return v; return valueOf(arg.tojstring()); } }
@Test public void execute() throws Exception { Globals globals = JsePlatform.standardGlobals(); // globals.set("LCallback", new LCallback()); LuaValue value = globals.load(helloWorldScript); value.call(); }