Refine search
/** Perform boolean {@code and} with another operand, based on lua rules for boolean evaluation. * This returns either {@code this} or {@code rhs} depending on the boolean value for {@code this}. * * @param rhs The right-hand-side value to perform the operation with * @return {@code this} if {@code this.toboolean()} is false, {@code rhs} otherwise. */ public LuaValue and( LuaValue rhs ) { return this.toboolean()? rhs: 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 converted to java int using {@link #toint()} * @throws LuaError if {@code this} is not a table or string, and has no {@link #LEN} metatag */ public int length() { return len().toint(); }
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)); } }
private String formatQuery(LuaValue page, LuaValue query) { if (query.isstring()) { return wgScript + "?title="+page.checkstring()+"&"+query.checkjstring(); } else if (query.istable()) { LuaTable params = query.checktable(); String base = wgScript + "?title="+page.checkstring()+"&"; for (LuaValue key : params.keys()) { base += (key.tojstring() + "=" + params.get(key).tojstring()); } return base; } else { throw new AssertionError("unexpected type: "+query); } } }
@Override public Varargs invoke(Varargs args) { LuaFunction chunk = args.arg(1).checkfunction(); LuaValue name = args.arg(2); final LuaValue res = chunk.call(); if (name.isnil()) { return LuaValue.varargsOf(new LuaValue[]{LuaValue.TRUE, res}); } else { if (!res.istable()) { return LuaValue.varargsOf(new LuaValue[]{FALSE, toLuaString(res.typename())}); } else { return LuaValue.varargsOf(new LuaValue[]{LuaValue.TRUE, res.checktable().get(name)}); } } } });
/** caller must ensure key is not nil */ public void set( LuaValue key, LuaValue value ) { if (!key.isvalidkey() && !metatag(NEWINDEX).isfunction()) typerror("table index"); if ( m_metatable==null || ! rawget(key).isnil() || ! settable(this,key,value) ) rawset(key, value); }
/** get value from metatable operations, or NIL if not defined by metatables */ protected static LuaValue gettable(LuaValue t, LuaValue key) { LuaValue tm; int loop = 0; do { if (t.istable()) { LuaValue res = t.rawget(key); if ((!res.isnil()) || (tm = t.metatag(INDEX)).isnil()) return res; } else if ((tm = t.metatag(INDEX)).isnil()) t.indexerror(); if (tm.isfunction()) return tm.call(t, key); t = tm; } while ( ++loop < MAXTAGLOOP ); error("loop in gettable"); return NIL; }
@Override public LuaValue call(LuaValue table, LuaValue compare) { table.checktable().sort(compare.isnil()? NIL: compare.checkfunction()); return NONE; } }
public Varargs _io_output(LuaValue filename) { outfile = filename.isnil()? output(): filename.isstring()? ioopenfile(FTYPE_NAMED, filename.checkjstring(),"w"): checkfile(filename); return outfile; }
public Varargs invoke(Varargs args) { LuaValue ld = args.arg1(); args.argcheck(ld.isstring() || ld.isfunction(), 1, "ld must be string or function"); String source = args.optjstring(2, ld.isstring()? ld.tojstring(): "=(load)"); String mode = args.optjstring(3, "bt"); LuaValue env = args.optvalue(4, globals); return loadStream(ld.isstring()? ld.strvalue().toInputStream(): new StringInputStream(ld.checkfunction()), source, mode, env); } }
/** Construct a Metatable instance from the given LuaValue */ protected static Metatable metatableOf(LuaValue mt) { if ( mt != null && mt.istable() ) { LuaValue mode = mt.rawget(MODE); if ( mode.isstring() ) { String m = mode.tojstring(); boolean weakkeys = m.indexOf('k') >= 0; boolean weakvalues = m.indexOf('v') >= 0; if ( weakkeys || weakvalues ) { return new WeakTable(weakkeys, weakvalues, mt); } } return (LuaTable)mt; } else if ( mt != null ) { return new NonTableMetatable( mt ); } else { return null; } }
public Object coerce(LuaValue value) { if ( value.isnil() ) return null; if ( targetType == TARGET_TYPE_STRING ) return value.tojstring(); LuaString s = value.checkstring(); byte[] b = new byte[s.m_length]; s.copyInto(0, b, 0, b.length); return b; } }
private HashMap<?, ?> convertToMap(LuaTable table) { HashMap<Object, Object> map = new HashMap<>(); LuaValue[] rootKeys = table.keys(); for (LuaValue k : rootKeys) { if (table.get(k).istable()) { map.put(k, this.convertToMap(table.get(k).checktable())); } else { map.put(k, table.get(k).touserdata()); } } return map; }
public LuaValue call(LuaValue table, LuaValue metatable) { final LuaValue mt0 = table.checktable().getmetatable(); if (mt0 != null && !mt0.rawget(METATABLE).isnil()) { error("cannot change a protected metatable"); } return table.setmetatable(metatable.isnil() ? null : metatable.checktable()); } }