Search in sources :

Example 6 with Prototype

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

the class LuaC method luaY_parser.

/**
 * Parse the input
 */
private Prototype luaY_parser(InputStream z, String name, boolean standardSyntax) throws IOException {
    LexState lexstate = new LexState(this, z, standardSyntax);
    FuncState funcstate = new FuncState();
    // lexstate.buff = buff;
    lexstate.fs = funcstate;
    lexstate.setinput(this, z.read(), z, (LuaString) LuaValue.valueOf(name));
    /* main func. is always vararg */
    funcstate.f = new Prototype();
    funcstate.f.source = (LuaString) LuaValue.valueOf(name);
    lexstate.mainfunc(funcstate);
    LuaC._assert(funcstate.prev == null);
    /* all scopes should be correctly finished */
    LuaC._assert(lexstate.dyd == null || (lexstate.dyd.n_actvar == 0 && lexstate.dyd.n_gt == 0 && lexstate.dyd.n_label == 0));
    return funcstate.f;
}
Also used : Prototype(org.luaj.vm2.Prototype)

Example 7 with Prototype

use of org.luaj.vm2.Prototype 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 8 with Prototype

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

the class LuaCache method loadPackageFromResource.

/**
 * Load lua package from a resource.
 */
public static final Prototype loadPackageFromResource(ClassLoader resourceLoader, String resourcePath, String packageName) throws AerospikeException {
    Prototype prototype = Packages.get(packageName);
    if (prototype == null) {
        try {
            InputStream is = resourceLoader.getResourceAsStream(resourcePath);
            if (is == null) {
                throw new Exception();
            }
            prototype = compile(packageName, is);
            Packages.put(packageName, prototype);
        } catch (Exception e) {
            throw new AerospikeException("Failed to read resource: " + resourcePath);
        }
    }
    return prototype;
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Prototype(org.luaj.vm2.Prototype) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AerospikeException(com.aerospike.client.AerospikeException)

Example 9 with Prototype

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

the class LuaCache method loadPackageFromFile.

/**
 * Load lua package from a file.
 */
public static final Prototype loadPackageFromFile(String packageName) throws AerospikeException {
    Prototype prototype = Packages.get(packageName);
    if (prototype == null) {
        File source = new File(LuaConfig.SourceDirectory, packageName + ".lua");
        try {
            InputStream is = new FileInputStream(source);
            prototype = compile(packageName, is);
            Packages.put(packageName, prototype);
        } catch (Exception e) {
            throw new AerospikeException("Failed to read file: " + source.getAbsolutePath());
        }
    }
    return prototype;
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Prototype(org.luaj.vm2.Prototype) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) AerospikeException(com.aerospike.client.AerospikeException)

Example 10 with Prototype

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

the class LuaInstance method loadSystemPackage.

private void loadSystemPackage(ClassLoader resourceLoader, String packageName) {
    String resourcePath = "udf/" + packageName + ".lua";
    Prototype prototype = LuaCache.loadPackageFromResource(resourceLoader, resourcePath, packageName);
    LuaClosure function = new LuaClosure(prototype, globals);
    function.invoke();
    loadedTable.set(packageName, LuaValue.TRUE);
}
Also used : Prototype(org.luaj.vm2.Prototype) LuaClosure(org.luaj.vm2.LuaClosure) LuaString(org.luaj.vm2.LuaString)

Aggregations

Prototype (org.luaj.vm2.Prototype)12 LuaString (org.luaj.vm2.LuaString)4 LuaPrint (org.luaj.vm2.log.LuaPrint)3 AerospikeException (com.aerospike.client.AerospikeException)2 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 LocVars (org.luaj.vm2.LocVars)2 LuaClosure (org.luaj.vm2.LuaClosure)2 LuaValue (org.luaj.vm2.LuaValue)2 File (java.io.File)1 Hashtable (java.util.Hashtable)1 LuaInteger (org.luaj.vm2.LuaInteger)1