I'm currently working on a project with LuaJ, a library which allows for Java to interpret Lua code and execute it. From what I have gathered thus far, I can only pass in values which Lua itself can understand, such as primitives, tables, userdata etc.
What I can't find is a way to, for example, call a function defined in Lua by passing in an object reference as an argument.
And this makes sense. Lua can't understand Java object references, that's fair enough.
The solution that I came up with was to essentially create a virtual memory manager. In the simplest implementation I can come up with, it would essentially be a Map<Integer,Object>
. The Integer bit gets used in the Lua scripts, something like doSomethingWithObject(object)
, where object is in fact a LuaValue of the Integer key from the map.
When doSomethingWithObject(object)
gets called ( and this is a function defined in Java, extended from OneArgFunction
), it would have to hook into the Map<Integer,Object>
, fetch the object represented by the integer value and do whatever it wants to with the object.
This has a smell to it. I'm not sure if there are better ways to do this. If there are, I would love to read up on any bits of information I can get my hands on.