Search in sources :

Example 6 with LuaString

use of org.luaj.vm2.LuaString 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);
}
Also used : LuaString(org.luaj.vm2.LuaString)

Example 7 with LuaString

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

the class StringLib method format.

/**
 * string.format (formatstring, ...)
 *
 * Returns a formatted version of its variable number of arguments following
 * the description given in its first argument (which must be a string).
 * The format string follows the same rules as the printf family of standard C functions.
 * The only differences are that the options/modifiers *, l, L, n, p, and h are not supported
 * and that there is an extra option, q. The q option formats a string in a form suitable
 * to be safely read back by the Lua interpreter: the string is written between double quotes,
 * and all double quotes, newlines, embedded zeros, and backslashes in the string are correctly
 * escaped when written. For instance, the call
 *   string.format('%q', 'a string with "quotes" and \n new line')
 *
 * will produce the string:
 *    "a string with \"quotes\" and \
 *    new line"
 *
 * The options c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument,
 * whereas q and s expect a string.
 *
 * This function does not accept string values containing embedded zeros,
 * except as arguments to the q option.
 */
static Varargs format(Varargs args) {
    LuaString fmt = args.checkstring(1);
    final int n = fmt.length();
    Buffer result = new Buffer(n);
    int arg = 1;
    int c;
    for (int i = 0; i < n; ) {
        switch(c = fmt.luaByte(i++)) {
            case '\n':
                result.append("\n");
                break;
            default:
                result.append((byte) c);
                break;
            case L_ESC:
                if (i < n) {
                    if ((c = fmt.luaByte(i)) == L_ESC) {
                        ++i;
                        result.append((byte) L_ESC);
                    } else {
                        arg++;
                        FormatDesc fdsc = new FormatDesc(args, fmt, i);
                        i += fdsc.length;
                        switch(fdsc.conversion) {
                            case 'c':
                                fdsc.format(result, (byte) args.checkint(arg));
                                break;
                            case 'i':
                            case 'd':
                                fdsc.format(result, args.checkint(arg));
                                break;
                            case 'o':
                            case 'u':
                            case 'x':
                            case 'X':
                                fdsc.format(result, args.checklong(arg));
                                break;
                            case 'e':
                            case 'E':
                            case 'f':
                            case 'g':
                            case 'G':
                                fdsc.format(result, args.checkdouble(arg), fdsc.precision);
                                break;
                            case 'q':
                                addquoted(result, args.checkstring(arg));
                                break;
                            case 's':
                                {
                                    LuaString s = args.checkstring(arg);
                                    if (fdsc.precision == -1 && s.length() >= 100) {
                                        result.append(s);
                                    } else {
                                        fdsc.format(result, s);
                                    }
                                }
                                break;
                            default:
                                error("invalid option '%" + (char) fdsc.conversion + "' to 'format'");
                                break;
                        }
                    }
                }
        }
    }
    return result.tostring();
}
Also used : Buffer(org.luaj.vm2.Buffer) LuaString(org.luaj.vm2.LuaString)

Example 8 with LuaString

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

the class DebugLib method getfuncname.

// Return the name info if found, or null if no useful information could be found.
static NameWhat getfuncname(DebugLib.CallFrame frame) {
    if (!frame.f.isclosure())
        return new NameWhat(frame.f.classnamestub(), "Java");
    Prototype p = frame.f.checkclosure().p;
    int pc = frame.pc;
    int i = p.code[pc];
    /* calling instruction */
    LuaString tm;
    switch(Lua.GET_OPCODE(i)) {
        case Lua.OP_CALL:
        case Lua.OP_TAILCALL:
            /* get function name */
            return getobjname(p, pc, Lua.GETARG_A(i));
        case Lua.OP_TFORCALL:
            /* for iterator */
            return new NameWhat("(for iterator)", "(for iterator");
        /* all other instructions can call only through metamethods */
        case Lua.OP_SELF:
        case Lua.OP_GETTABUP:
        case Lua.OP_GETTABLE:
            tm = LuaValue.INDEX;
            break;
        case Lua.OP_SETTABUP:
        case Lua.OP_SETTABLE:
            tm = LuaValue.NEWINDEX;
            break;
        case Lua.OP_EQ:
            tm = LuaValue.EQ;
            break;
        case Lua.OP_ADD:
            tm = LuaValue.ADD;
            break;
        case Lua.OP_SUB:
            tm = LuaValue.SUB;
            break;
        case Lua.OP_MUL:
            tm = LuaValue.MUL;
            break;
        case Lua.OP_DIV:
            tm = LuaValue.DIV;
            break;
        case Lua.OP_MOD:
            tm = LuaValue.MOD;
            break;
        case Lua.OP_POW:
            tm = LuaValue.POW;
            break;
        case Lua.OP_UNM:
            tm = LuaValue.UNM;
            break;
        case Lua.OP_LEN:
            tm = LuaValue.LEN;
            break;
        case Lua.OP_LT:
            tm = LuaValue.LT;
            break;
        case Lua.OP_LE:
            tm = LuaValue.LE;
            break;
        case Lua.OP_CONCAT:
            tm = LuaValue.CONCAT;
            break;
        default:
            return null;
    }
    return new NameWhat(tm.tojstring(), "metamethod");
}
Also used : Prototype(org.luaj.vm2.Prototype) LuaString(org.luaj.vm2.LuaString) LuaPrint(org.luaj.vm2.log.LuaPrint)

Example 9 with LuaString

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

the class LexState method forstat.

void forstat(int line) {
    /* forstat -> FOR (fornum | forlist) END */
    FuncState fs = this.fs;
    LuaString varname;
    BlockCnt bl = new BlockCnt();
    fs.enterblock(bl, true);
    /* scope for loop and control variables */
    this.next();
    /* skip `for' */
    varname = this.str_checkname();
    /* first variable name */
    switch(this.t.token) {
        case '=':
            this.fornum(varname, line);
            break;
        case ',':
        case TK_IN:
            this.forlist(varname);
            break;
        default:
            this.syntaxerror(LUA_QL("=") + " or " + LUA_QL("in") + " expected");
    }
    this.check_match(TK_END, TK_FOR, line);
    fs.leaveblock();
/* loop scope (`break' jumps to this point) */
}
Also used : BlockCnt(org.luaj.vm2.compiler.FuncState.BlockCnt) LuaString(org.luaj.vm2.LuaString)

Example 10 with LuaString

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

the class LexState method new_localvarliteral.

void new_localvarliteral(String v) {
    LuaString ts = newstring(v);
    new_localvar(ts);
}
Also used : LuaString(org.luaj.vm2.LuaString)

Aggregations

LuaString (org.luaj.vm2.LuaString)25 LuaValue (org.luaj.vm2.LuaValue)5 Prototype (org.luaj.vm2.Prototype)3 Buffer (org.luaj.vm2.Buffer)2 LuaPrint (org.luaj.vm2.log.LuaPrint)2 LocVars (org.luaj.vm2.LocVars)1 Upvaldesc (org.luaj.vm2.Upvaldesc)1 BlockCnt (org.luaj.vm2.compiler.FuncState.BlockCnt)1