Skip to content

Lua cjson library #558

@ivan-kripakov-m10

Description

@ivan-kripakov-m10

hi!
There are some pre-imported libraries available in the Redis Lua runtime context that are not available in the Jedis mock Lua runtime context (source)

The following standard Lua libraries are available to use:

The String Manipulation (string) library
The Table Manipulation (table) library
The Mathematical Functions (math) library
The Operating System Facilities (os) library

In addition, the following external libraries are loaded and accessible to scripts:

The struct library
The cjson library
The cmsgpack library
The bitop library

As I understand, the Lua context is configured here:

globals.set("unpack", globals.load("return table.unpack(...)").checkfunction());
globals.set("redis", globals.load(REDIS_LUA).call().checktable());
globals.set("KEYS", embedLuaListToValue(args.subList(0, keysNum)));
globals.set("ARGV", embedLuaListToValue(args.subList(keysNum, args.size())));
globals.set("_mock", CoerceJavaToLua.coerce(new LuaRedisCallback(state)));

And in this method implementation:

public static Globals standardGlobals() {
    Globals globals = new Globals();
    globals.load(new JseBaseLib());
    globals.load(new PackageLib());
    globals.load(new Bit32Lib());
    globals.load(new TableLib());
    globals.load(new StringLib());
    globals.load(new CoroutineLib());
    globals.load(new JseMathLib());
    globals.load(new JseIoLib());
    globals.load(new JseOsLib());
    globals.load(new LuajavaLib());
    LoadState.install(globals);
    LuaC.install(globals);
    return globals;
}

So, only external libs are unsupported at the moment.
I'm ready to add them, but I want to discuss the approach first

I would like to present the one I've found for cjson as an example:

  1. Extend the org.luaj.vm2.lib.TwoArgFunction:
class LuaCjsonLib extends TwoArgFunction {

    @Override
    public LuaValue call(LuaValue modname, LuaValue env) {
        LuaTable cjson = new LuaTable();
        cjson.set("encode", new encode());
        cjson.set("decode", new decode());
        env.set("cjson", cjson);
        env.get("package").get("loaded").set("cjson", cjson);
        return cjson;
    }

    static class encode extends OneArgFunction {

        @Override
        public Varargs invoke(LuaValue arg) {
            // details omitted
        }
    }

    static class decode extends OneArgFunction {
        @Override
        public Varargs invoke(LuaValue arg) {
            // details omitted
        }
    }
}
  1. Configure via:
globals.set("cjson", globals.load(new LuaCjsonLib()));

While this approach can work, it would require to map all possible org.luaj.vm2.LuaValue inheritors to Java types, which seems to me a little bit complicated, but possible and good enough.

Are there any other approaches I should consider?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions