/** Tests if argument i is a table. * @param i the index of the argument to test, 1 is the first argument * @return true if the argument exists and is a lua table, false otherwise * @see LuaValue#TTABLE * */ public boolean istable(int i) { return arg(i).istable(); }
/** Tests if argument i is a table. * @param i the index of the argument to test, 1 is the first argument * @return true if the argument exists and is a lua table, false otherwise * @see LuaValue.TTABLE * */ public boolean istable(int i) { return arg(i).istable(); }
/** Tests if argument i is a table. * @param i the index of the argument to test, 1 is the first argument * @return true if the argument exists and is a lua table, false otherwise * @see LuaValue#TTABLE * */ public boolean istable(int i) { return arg(i).istable(); }
public boolean isTable(String name) { return env.get(name).istable(); }
@Override public LuaValue call(LuaValue cls, LuaValue args) { String classPath = cls.checkjstring(); try { if (classPath.startsWith("$")) classPath = "org.bukkit" + classPath.substring(1); if (classPath.startsWith("#")) classPath = "nz.co.jammehcow.lukkit.environment" + classPath.substring(1); if (args.isnil()) { return CoerceJavaToLua.coerce(Class.forName(classPath).newInstance()); } else if (args.istable()) { List<Object> argList = (List<Object>) Utilities.convertTable(args.checktable()); List<Class<?>> typesList = new ArrayList<>(); argList.forEach(o -> typesList.add(o.getClass())); return CoerceJavaToLua.coerce(Class.forName(classPath).getDeclaredConstructor( typesList.toArray(new Class<?>[0])) .newInstance(argList.toArray(new Object[0]))); } } catch (Exception e) { e.printStackTrace(); } return NIL; } });
public boolean eq_b( LuaValue val ) { if ( this == val ) return true; if ( m_metatable == null || !val.istable() ) return false; LuaValue valmt = val.getmetatable(); return valmt!=null && LuaValue.eqmtcall(this, m_metatable.toLuaValue(), val, valmt); }
@Override public boolean eq_b( LuaValue val ) { if ( this == val ) return true; if ( m_metatable == null || !val.istable() ) return false; LuaValue valmt = val.getmetatable(); return valmt!=null && LuaValue.eqmtcall(this, m_metatable.toLuaValue(), val, valmt); }
public boolean eq_b( LuaValue val ) { if ( this == val ) return true; if ( m_metatable == null || !val.istable() ) return false; LuaValue valmt = val.getmetatable(); return valmt!=null && LuaValue.eqmtcall(this, m_metatable.toLuaValue(), val, valmt); }
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; }
/** 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; } }
/** 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; } }
/** 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; } }
@Override public List<String> getCommands() { List<String> cmds = Lists.newArrayList(); if (!lv.isNil(LuaFields.COMMANDS)) { Iterator<LuaPair> pairsIter = lv.getTable(LuaFields.COMMANDS).arrayIterator(); while (pairsIter.hasNext()) { LuaValue c = pairsIter.next().value; if (c.isstring()) { cmds.add(c.tojstring()); } else if (c.istable()) { cmds.add(toCommand(new LuaWrapper(c.checktable()))); } } } else if (!lv.isNil(LuaFields.COMMAND)) { if (lv.isTable(LuaFields.COMMAND)) { cmds.add(toCommand(lv.getTable(LuaFields.COMMAND))); } else { cmds.add(lv.getString(LuaFields.COMMAND)); } } if (cmds.isEmpty()) { LOG.fatal("No commands found in container!"); } return cmds; }
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); } } }
private static Object toJavaValue(LuaValue aValue) { if (aValue.isnil()) { return null; } if (aValue.istable()) { LuaTable theTable = (LuaTable) aValue; LuaValue theObject = theTable.get(LuaString.valueOf("javaobject")); if (theObject.isuserdata()) { return theObject.checkuserdata(); } throw new IllegalArgumentException("Cannot convert " + aValue+" to java object"); } if (aValue.isint()) { return aValue.toint(); } if (aValue.islong()) { return aValue.tolong(); } if (aValue.isstring()) { return aValue.toString(); } return aValue.toString(); }
/** 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; }
/** 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; }
/** 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 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)}); } } } });
/** * Gets the Java object from a LuaValue. * * @param value the LuaValue * @return the Java object */ public static Object getObjectFromLuavalue(LuaValue value) { if (value.istable()) { return convertTable(value.checktable()); } else if (value.isint()) { return value.checkint(); } else if (value.islong()) { return value.checklong(); } else if (value.isnumber()) { return value.checkdouble(); } else if (value.isstring()) { return value.checkjstring(); } else if (value.isboolean()) { return value.checkboolean(); } else if (value.isnil()) { return null; } else { return value.checkuserdata(); } }