How can I pass objects to an exposed luaj function?

℡╲_俬逩灬. 提交于 2019-11-30 19:13:09

I was searching entries and found your unanswered. Your question was interesting and made me search. Then I realized that it was asked 2 years ago... I hope my answer can be useful to somebody else! Code based on luaj-3.0-alpha1.

We need a valid Lua script (you forgot the function keyword):

function onTalk(javaObj)
  print(type(javaObj) .. " " .. tostring(javaObj))
  print(javaObj.name)
  javaObj:talk()
  return true
end

function onWalk(javaObj)
  javaObj:walk()
  return 1, "km"
end

I added a bit of trace... I also made classes similar to your:

class Dog
{
  public String name;
  Dog(String n) { name = n; }
  public void talk() { System.out.println("Dog " + name + " barks!"); }
  public void walk() { System.out.println("Dog " + name + " walks..."); }
}
class Cat
{
  String name;
  Cat(String n) { name = n; }
  public void talk() { System.out.println("Cat " + name + " meows!"); }
  public void walk() { System.out.println("Cat " + name + " walks..."); }
}

Adding a field to test this too. For my test, I just declared the classes inside the method creating their instances:

Dog dog = new Dog("Rex");
Cat cat = new Cat("Felix");

I first tried to convert these Java objects to Lua, using LuaValue luaDog = LuaValue.userdataOf(dog); but it doesn't work: we indeed have userdata, as shown by the traces, but no metatable, so we cannot call the methods nor access the fields.

After searching a lot, I found out the right incantation:

CompiledScript script = ((Compilable) scriptEngine).compile(reader);
Bindings sb = new SimpleBindings();
script.eval(sb); // Put the Lua functions into the sb environment
LuaValue luaDog = CoerceJavaToLua.coerce(dog); // Java to Lua
LuaFunction onTalk = (LuaFunction) sb.get("onTalk"); // Get Lua function
LuaValue b = onTalk.call(luaDog); // Call the function
System.out.println("onTalk answered: " + b);
LuaFunction onWalk = (LuaFunction) sb.get("onWalk");
LuaValue[] dogs = { luaDog };
Varargs dist = onWalk.invoke(LuaValue.varargsOf(dogs)); // Alternative
System.out.println("onWalk returned: " + dist);

I appreciate the Luaj API... :-) Probably more made for Java programmers while other libraries seem to aim more at Lua / C programmers...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!