Pass Java HashMap to Lua script

358 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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()));