Search in sources :

Example 41 with LuaValue

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

the class FuncState method constfolding.

boolean constfolding(int op, expdesc e1, expdesc e2) {
    LuaValue v1, v2, r;
    if (!e1.isnumeral() || !e2.isnumeral())
        return false;
    if ((op == OP_DIV || op == OP_MOD) && e2.u.nval().eq_b(LuaValue.ZERO))
        return false;
    /* do not attempt to divide by 0 */
    v1 = e1.u.nval();
    v2 = e2.u.nval();
    switch(op) {
        case OP_ADD:
            r = v1.add(v2);
            break;
        case OP_SUB:
            r = v1.sub(v2);
            break;
        case OP_MUL:
            r = v1.mul(v2);
            break;
        case OP_DIV:
            r = v1.div(v2);
            break;
        case OP_MOD:
            r = v1.mod(v2);
            break;
        case OP_POW:
            r = v1.pow(v2);
            break;
        case OP_UNM:
            r = v1.neg();
            break;
        case OP_LEN:
            // break;
            return false;
        /* no constant folding for 'len' */
        default:
            _assert(false);
            r = null;
            break;
    }
    if (Double.isNaN(r.todouble()))
        return false;
    /* do not attempt to produce NaN */
    e1.u.setNval(r);
    return true;
}
Also used : LuaValue(org.luaj.vm2.LuaValue)

Example 42 with LuaValue

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

the class DumpState method dumpConstants.

void dumpConstants(final Prototype f) throws IOException {
    final LuaValue[] k = f.k;
    int i, n = k.length;
    dumpInt(n);
    for (i = 0; i < n; i++) {
        final LuaValue o = k[i];
        switch(o.type()) {
            case LuaValue.TNIL:
                writer.write(LuaValue.TNIL);
                break;
            case LuaValue.TBOOLEAN:
                writer.write(LuaValue.TBOOLEAN);
                dumpChar(o.toboolean() ? 1 : 0);
                break;
            case LuaValue.TNUMBER:
                switch(NUMBER_FORMAT) {
                    case NUMBER_FORMAT_FLOATS_OR_DOUBLES:
                        writer.write(LuaValue.TNUMBER);
                        dumpDouble(o.todouble());
                        break;
                    case NUMBER_FORMAT_INTS_ONLY:
                        if (!ALLOW_INTEGER_CASTING && !o.isint())
                            throw new java.lang.IllegalArgumentException("not an integer: " + o);
                        writer.write(LuaValue.TNUMBER);
                        dumpInt(o.toint());
                        break;
                    case NUMBER_FORMAT_NUM_PATCH_INT32:
                        if (o.isint()) {
                            writer.write(LuaValue.TINT);
                            dumpInt(o.toint());
                        } else {
                            writer.write(LuaValue.TNUMBER);
                            dumpDouble(o.todouble());
                        }
                        break;
                    default:
                        throw new IllegalArgumentException("number format not supported: " + NUMBER_FORMAT);
                }
                break;
            case LuaValue.TSTRING:
                writer.write(LuaValue.TSTRING);
                dumpString((LuaString) o);
                break;
            default:
                throw new IllegalArgumentException("bad type for " + o);
        }
    }
    n = f.p.length;
    dumpInt(n);
    for (i = 0; i < n; i++) dumpFunction(f.p[i]);
}
Also used : LuaValue(org.luaj.vm2.LuaValue)

Example 43 with LuaValue

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

the class OsLib method call.

public LuaValue call(LuaValue modname, LuaValue env) {
    globals = env.checkglobals();
    LuaTable os = new LuaTable();
    for (int i = 0; i < NAMES.length; ++i) os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
    env.set("os", os);
    env.get("package").get("loaded").set("os", os);
    return os;
}
Also used : LuaTable(org.luaj.vm2.LuaTable)

Example 44 with LuaValue

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

the class TableLib method call.

public LuaValue call(LuaValue modname, LuaValue env) {
    LuaTable table = new LuaTable();
    table.set("concat", new concat());
    table.set("insert", new insert());
    table.set("pack", new pack());
    table.set("remove", new remove());
    table.set("sort", new sort());
    table.set("unpack", new unpack());
    // TODO yesong添加的函数
    table.set("getn", new getn());
    env.set("table", table);
    env.get("package").get("loaded").set("table", table);
    return NIL;
}
Also used : LuaTable(org.luaj.vm2.LuaTable)

Example 45 with LuaValue

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

the class JsePlatform method luaMain.

/**
 * Simple wrapper for invoking a lua function with command line arguments.
 * The supplied function is first given a new Globals object,
 * then the program is run with arguments.
 */
public static void luaMain(LuaValue mainChunk, String[] args) {
    Globals g = standardGlobals();
    int n = args.length;
    LuaValue[] vargs = new LuaValue[args.length];
    for (int i = 0; i < n; ++i) vargs[i] = LuaValue.valueOf(args[i]);
    LuaValue arg = LuaValue.listOf(vargs);
    arg.set("n", n);
    g.set("arg", arg);
    mainChunk.initupvalue1(g);
    mainChunk.invoke(LuaValue.varargsOf(vargs));
}
Also used : Globals(org.luaj.vm2.Globals) LuaValue(org.luaj.vm2.LuaValue)

Aggregations

LuaValue (org.luaj.vm2.LuaValue)55 LuaTable (org.luaj.vm2.LuaTable)37 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)6 LuaError (org.luaj.vm2.LuaError)6 LuaFunction (org.luaj.vm2.LuaFunction)6 View (android.view.View)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 LuaViewApi (com.taobao.luaview.fun.mapper.LuaViewApi)2 UDLuaTable (com.taobao.luaview.userdata.base.UDLuaTable)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2