use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class LuaPrint method buildState.
/**
* Print the state of a {@link LuaClosure} that is being executed
*
* @param cl the {@link LuaClosure}
* @param pc the program counter
* @param stack the stack of {@link LuaValue}
* @param top the top of the stack
* @param varargs any {@link Varargs} value that may apply
*/
public LuaPrint buildState(LuaClosure cl, int pc, LuaValue[] stack, int top, Varargs varargs) {
// print opcode into buffer
StringBuffer previous = ps;
ps = new StringBuffer();
buildOpCode(cl.p, pc);
LogUtil.i(ps);
ps = previous;
format(ps.toString(), 50);
// print stack
ps.append('[');
for (int i = 0; i < stack.length; i++) {
LuaValue v = stack[i];
if (v == null)
ps.append(STRING_FOR_NULL);
else
switch(v.type()) {
case LuaValue.TSTRING:
LuaString s = v.checkstring();
ps.append(s.length() < 48 ? s.tojstring() : s.substring(0, 32).tojstring() + "...+" + (s.length() - 32) + "b");
break;
case LuaValue.TFUNCTION:
ps.append(v.tojstring());
break;
case LuaValue.TUSERDATA:
Object o = v.touserdata();
if (o != null) {
String n = o.getClass().getName();
n = n.substring(n.lastIndexOf('.') + 1);
ps.append(n + ": " + Integer.toHexString(o.hashCode()));
} else {
ps.append(v.toString());
}
break;
default:
ps.append(v.tojstring());
}
if (i + 1 == top)
ps.append(']');
ps.append(" | ");
}
ps.append(varargs);
ps.append("\n");
return this;
}
use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class StringLib method sub.
/**
* string.sub (s, i [, j])
*
* Returns the substring of s that starts at i and continues until j;
* i and j may be negative. If j is absent, then it is assumed to be equal to -1
* (which is the same as the string length). In particular, the call
* string.sub(s,1,j)
* returns a prefix of s with length j, and
* string.sub(s, -i)
* returns a suffix of s with length i.
*/
static Varargs sub(Varargs args) {
final LuaString s = args.checkstring(1);
final int l = s.length();
int start = posrelat(args.checkint(2), l);
int end = posrelat(args.optint(3, -1), l);
if (start < 1)
start = 1;
if (end > l)
end = l;
if (start <= end) {
return s.substring(start - 1, end);
} else {
return EMPTYSTRING;
}
}
use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class StringLib method gmatch.
/**
* string.gmatch (s, pattern)
*
* Returns an iterator function that, each time it is called, returns the next captures
* from pattern over string s. If pattern specifies no captures, then the
* whole match is produced in each call.
*
* As an example, the following loop
* s = "hello world from Lua"
* for w in string.gmatch(s, "%a+") do
* print(w)
* end
*
* will iterate over all the words from string s, printing one per line.
* The next example collects all pairs key=value from the given string into a table:
* t = {}
* s = "from=world, to=Lua"
* for k, v in string.gmatch(s, "(%w+)=(%w+)") do
* t[k] = v
* end
*
* For this function, a '^' at the start of a pattern does not work as an anchor,
* as this would prevent the iteration.
*/
static Varargs gmatch(Varargs args) {
LuaString src = args.checkstring(1);
LuaString pat = args.checkstring(2);
return new GMatchAux(args, src, pat);
}
use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class StringLib method str_find_aux.
/**
* This utility method implements both string.find and string.match.
*/
static Varargs str_find_aux(Varargs args, boolean find) {
LuaString s = args.checkstring(1);
LuaString pat = args.checkstring(2);
int init = args.optint(3, 1);
if (init > 0) {
init = Math.min(init - 1, s.length());
} else if (init < 0) {
init = Math.max(0, s.length() + init);
}
boolean fastMatch = find && (args.arg(4).toboolean() || pat.indexOfAny(SPECIALS) == -1);
if (fastMatch) {
int result = s.indexOf(pat, init);
if (result != -1) {
return varargsOf(valueOf(result + 1), valueOf(result + pat.length()));
}
} else {
MatchState ms = new MatchState(args, s, pat);
boolean anchor = false;
int poff = 0;
if (pat.luaByte(0) == '^') {
anchor = true;
poff = 1;
}
int soff = init;
do {
int res;
ms.reset();
if ((res = ms.match(soff, poff)) != -1) {
if (find) {
return varargsOf(valueOf(soff + 1), valueOf(res), ms.push_captures(false, soff, res));
} else {
return ms.push_captures(true, soff, res);
}
}
} while (soff++ < s.length() && !anchor);
}
return NIL;
}
use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.
the class StringLib method rep.
/**
* string.rep (s, n)
*
* Returns a string that is the concatenation of n copies of the string s.
*/
static Varargs rep(Varargs args) {
LuaString s = args.checkstring(1);
int n = args.checkint(2);
final byte[] bytes = new byte[s.length() * n];
int len = s.length();
for (int offset = 0; offset < bytes.length; offset += len) {
s.copyInto(0, bytes, offset, len);
}
return LuaString.valueOf(bytes);
}
Aggregations