javalualuaj

Pass Java HashMap to Lua script


I want to pass a Java HashMap to a Lua script from Java code in LuaJ. But all I see is we have chunk.call() and chunk.invoke() where we can pass the arguments or array of LuaValue.valueOf(), which allow us either int, byte, double, string and boolean.


Solution

  • You're looking for LuaValue.tableOf() to create an empty table. You can then call LuaValue.set to insert your HashMap entries. Example:

    LuaValue table = LuaValue.tableOf();
    // assuming map is a HashMap of the "primitive" Lua types valueOf supports
    for (Entry e : map.entrySet())
        table.set(LuaValue.valueOf(e.getKey()), LuaValue.valueOf(e.getValue()));