Search in sources :

Example 76 with LuaValue

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

the class StringLib method call.

public LuaValue call(LuaValue modname, LuaValue env) {
    LuaTable t = new LuaTable();
    bind(t, StringLib1.class, new String[] { "dump", "len", "lower", "reverse", "upper" });
    bind(t, StringLibV.class, new String[] { "byte", "char", "find", "format", "gmatch", "gsub", "match", "rep", "sub" });
    env.set("string", t);
    instance = t;
    if (LuaString.s_metatable == null)
        LuaString.s_metatable = tableOf(new LuaValue[] { INDEX, t });
    env.get("package").get("loaded").set("string", t);
    return t;
}
Also used : LuaTable(org.luaj.vm2.LuaTable)

Example 77 with LuaValue

use of org.luaj.vm2.LuaValue 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 78 with LuaValue

use of org.luaj.vm2.LuaValue 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)

Example 79 with LuaValue

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

the class Bit32Lib method call.

public LuaValue call(LuaValue modname, LuaValue env) {
    LuaTable t = new LuaTable();
    bind(t, Bit32LibV.class, new String[] { "band", "bnot", "bor", "btest", "bxor", "extract", "replace" });
    bind(t, Bit32Lib2.class, new String[] { "arshift", "lrotate", "lshift", "rrotate", "rshift" });
    env.set("bit32", t);
    env.get("package").get("loaded").set("bit32", t);
    return t;
}
Also used : LuaTable(org.luaj.vm2.LuaTable)

Example 80 with LuaValue

use of org.luaj.vm2.LuaValue in project aerospike-client-java by aerospike.

the class QueryAggregateCommand method parseRow.

@Override
protected void parseRow(Key key) throws IOException {
    if (opCount != 1) {
        throw new AerospikeException("Query aggregate expected exactly one bin.  Received " + opCount);
    }
    // Parse aggregateValue.
    readBytes(8);
    int opSize = Buffer.bytesToInt(dataBuffer, 0);
    byte particleType = dataBuffer[5];
    byte nameSize = dataBuffer[7];
    readBytes(nameSize);
    String name = Buffer.utf8ToString(dataBuffer, 0, nameSize);
    int particleBytesSize = (int) (opSize - (4 + nameSize));
    readBytes(particleBytesSize);
    if (!name.equals("SUCCESS")) {
        if (name.equals("FAILURE")) {
            Object value = Buffer.bytesToParticle(particleType, dataBuffer, 0, particleBytesSize);
            throw new AerospikeException(ResultCode.QUERY_GENERIC, value.toString());
        } else {
            throw new AerospikeException(ResultCode.PARSE_ERROR, "Query aggregate expected bin name SUCCESS.  Received " + name);
        }
    }
    LuaValue aggregateValue = instance.getLuaValue(particleType, dataBuffer, 0, particleBytesSize);
    if (!valid) {
        throw new AerospikeException.QueryTerminated();
    }
    if (aggregateValue != null) {
        try {
            inputQueue.put(aggregateValue);
        } catch (InterruptedException ie) {
        }
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) LuaValue(org.luaj.vm2.LuaValue)

Aggregations

LuaValue (org.luaj.vm2.LuaValue)51 LuaTable (org.luaj.vm2.LuaTable)35 Varargs (org.luaj.vm2.Varargs)12 LuaString (org.luaj.vm2.LuaString)9 VarArgFunction (org.luaj.vm2.lib.VarArgFunction)7 UDView (com.taobao.luaview.userdata.ui.UDView)5 LuaFunction (org.luaj.vm2.LuaFunction)5 View (android.view.View)4 LuaError (org.luaj.vm2.LuaError)4 Point (android.graphics.Point)3 ILVView (com.taobao.luaview.view.interfaces.ILVView)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 HorizontalScrollView (android.widget.HorizontalScrollView)2 AerospikeException (com.aerospike.client.AerospikeException)2 UDLuaTable (com.taobao.luaview.userdata.base.UDLuaTable)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Field (java.lang.reflect.Field)2