Question

I have Java/Scala code that is calling out to Lua via LuaJava. I would like to mimic some of the object hierarchy from the J/S pipeline to the Lua pipeline; basically to have companion objects in the languages. In other words, if I create a 'Cracker' in J/S, I would create a 'Cracker' object in Lua. Then when I call a method like "eatCracker" in J/S I could also call the Lua companion object's "eatCracker".

Problem is, I can't figure out how to create and store the Lua object and then how to call methods on a specific object.

Anybody able to help?

No correct solution

OTHER TIPS

When you push an object to the Lua interpreter from J/S and create a global reference to it, a Lua script that is then run and accesses the object will access the same object as in the J/S side, not a copy of it. So once the script ends, the Java/Scala sees the object with state (data members) as modified by the Lua script. If the J/S then modifies it, and another script run that accesses that object, that script will see the new state as modified by the J/S.

So there is no mimicking when calling methods: a method is a function bound to an object, so calling it from Lua or from Java/Scala causes the same object (the same memory block) to be accessed.

However, mimicking is required when constructing: if you construct an object in J/S, you must explicitly push it to the Lua interpreter for it to become available; if a Lua script run by the interpreter creates a new object, the J/S must pull it (via a global variable that references the object). For disposal, I wager that the object will be automatically finalized when there are no references left to it on either side (J/S or Lua).

So all you need to do is ensure that the objects you care to have in both environments are pushed and pulled on the J/S side (the Lua side is "slave"). I don't think it is a good idea to automate this: explicitly push/pull only what is required on both sides, no more no less.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top