Search in sources :

Example 46 with Varargs

use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.

the class TailcallVarargs method eval.

public Varargs eval() {
    while (result == null) {
        Varargs r = func.onInvoke(args);
        if (r.isTailcall()) {
            TailcallVarargs t = (TailcallVarargs) r;
            func = t.func;
            args = t.args;
        } else {
            result = r;
            func = null;
            args = null;
        }
    }
    return result;
}
Also used : ArrayPartVarargs(org.luaj.vm2.Varargs.ArrayPartVarargs) PairVarargs(org.luaj.vm2.Varargs.PairVarargs)

Example 47 with Varargs

use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.

the class StringLib method byte_.

/**
 * string.byte (s [, i [, j]])
 *
 * Returns the internal numerical codes of the
 * characters s[i], s[i+1], ..., s[j]. The default value for i is 1; the
 * default value for j is i.
 *
 * Note that numerical codes are not necessarily portable across platforms.
 *
 * @param args the calling args
 */
static Varargs byte_(Varargs args) {
    LuaString s = args.checkstring(1);
    int l = s.m_length;
    int posi = posrelat(args.optint(2, 1), l);
    int pose = posrelat(args.optint(3, posi), l);
    int n, i;
    if (posi <= 0)
        posi = 1;
    if (pose > l)
        pose = l;
    if (posi > pose)
        return NONE;
    /* empty interval; return no values */
    n = (int) (pose - posi + 1);
    if (posi + n <= pose)
        /* overflow? */
        error("string slice too long");
    LuaValue[] v = new LuaValue[n];
    for (i = 0; i < n; i++) v[i] = valueOf(s.luaByte(posi + i - 1));
    return varargsOf(v);
}
Also used : LuaString(org.luaj.vm2.LuaString) LuaValue(org.luaj.vm2.LuaValue)

Example 48 with Varargs

use of org.luaj.vm2.Varargs in project LuaViewSDK by alibaba.

the class StringLib method gsub.

/**
 * string.gsub (s, pattern, repl [, n])
 * Returns a copy of s in which all (or the first n, if given) occurrences of the
 * pattern have been replaced by a replacement string specified by repl, which
 * may be a string, a table, or a function. gsub also returns, as its second value,
 * the total number of matches that occurred.
 *
 * If repl is a string, then its value is used for replacement.
 * The character % works as an escape character: any sequence in repl of the form %n,
 * with n between 1 and 9, stands for the value of the n-th captured substring (see below).
 * The sequence %0 stands for the whole match. The sequence %% stands for a single %.
 *
 * If repl is a table, then the table is queried for every match, using the first capture
 * as the key; if the pattern specifies no captures, then the whole match is used as the key.
 *
 * If repl is a function, then this function is called every time a match occurs,
 * with all captured substrings passed as arguments, in order; if the pattern specifies
 * no captures, then the whole match is passed as a sole argument.
 *
 * If the value returned by the table query or by the function call is a string or a number,
 * then it is used as the replacement string; otherwise, if it is false or nil,
 * then there is no replacement (that is, the original match is kept in the string).
 *
 * Here are some examples:
 * 	     x = string.gsub("hello world", "(%w+)", "%1 %1")
 * 	     --> x="hello hello world world"
 *
 *	     x = string.gsub("hello world", "%w+", "%0 %0", 1)
 *	     --> x="hello hello world"
 *
 *	     x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
 *	     --> x="world hello Lua from"
 *
 *	     x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
 *	     --> x="home = /home/roberto, user = roberto"
 *
 *	     x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
 *	           return loadstring(s)()
 *       end)
 *	     --> x="4+5 = 9"
 *
 *	     local t = {name="lua", version="5.1"}
 *	     x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
 *	     --> x="lua-5.1.tar.gz"
 */
static Varargs gsub(Varargs args) {
    LuaString src = args.checkstring(1);
    final int srclen = src.length();
    LuaString p = args.checkstring(2);
    LuaValue repl = args.arg(3);
    int max_s = args.optint(4, srclen + 1);
    final boolean anchor = p.length() > 0 && p.charAt(0) == '^';
    Buffer lbuf = new Buffer(srclen);
    MatchState ms = new MatchState(args, src, p);
    int soffset = 0;
    int n = 0;
    while (n < max_s) {
        ms.reset();
        int res = ms.match(soffset, anchor ? 1 : 0);
        if (res != -1) {
            n++;
            ms.add_value(lbuf, soffset, res, repl);
        }
        if (res != -1 && res > soffset)
            soffset = res;
        else if (soffset < srclen)
            lbuf.append((byte) src.luaByte(soffset++));
        else
            break;
        if (anchor)
            break;
    }
    lbuf.append(src.substring(soffset, srclen));
    return varargsOf(lbuf.tostring(), valueOf(n));
}
Also used : Buffer(org.luaj.vm2.Buffer) LuaString(org.luaj.vm2.LuaString) LuaValue(org.luaj.vm2.LuaValue)

Aggregations

Varargs (org.luaj.vm2.Varargs)24 LuaTable (org.luaj.vm2.LuaTable)22 LuaValue (org.luaj.vm2.LuaValue)20 VarArgFunction (org.luaj.vm2.lib.VarArgFunction)16 LuaString (org.luaj.vm2.LuaString)10 LuaFunction (org.luaj.vm2.LuaFunction)5 LuaViewApi (com.taobao.luaview.fun.mapper.LuaViewApi)2 UDView (com.taobao.luaview.userdata.ui.UDView)2 List (java.util.List)2 LuanObjImage (net.schattenkind.androidLove.luan.obj.LuanObjImage)2 Buffer (org.luaj.vm2.Buffer)2 LuaThread (org.luaj.vm2.LuaThread)2 Point (android.graphics.Point)1 Sensor (android.hardware.Sensor)1 SoundPool (android.media.SoundPool)1 UDBitmap (com.taobao.luaview.userdata.kit.UDBitmap)1 UDData (com.taobao.luaview.userdata.kit.UDData)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1