如何将对象传递给一个暴露的luaj函数? [英] How can I pass objects to an exposed luaj function?

查看:647
本文介绍了如何将对象传递给一个暴露的luaj函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Luaj + java构建一个控制器。我有以下的java类

  public class Duck {
public void talk(){System.out.println 鸭acks!); }
public void walk(){System.out.println(Duck walking! }
}

public class Person {
public void talk(){System.out.println(Person talks! }
public void walk(){System.out.println(Person walks! }
}

和以下控制器的lua脚本:

  onTalk(obj)
obj:talk();
end

onWalk(obj)
obj:walk();
end



我希望定义一个控制器保留所有程序的逻辑,我想将API从该控制器暴露到我的java代码。我试图使用以下方法:

  ScriptEngineManager sem = new ScriptEngineManager(); 
ScriptEngine engine = sem.getEngineByExtension(。lua);
ScriptEngineFactory factory = engine.getFactory();

//上面定义的脚本
CompiledScript cs =((Compilable)engine).compile(MY_LUA_SCRIPT);
SimpleBindings b = new SimpleBindings();

b = newSimpletBindings();

LuaValue onWalkHandler =(LuaValue)b.get(onWalk);
//func.call(LuaValue.valueOf(duck)); //传递鸭子对象不工作???

我无法将对象传递给LuaValue。如何将一个java对象传递给lua脚本?



PS:一般来说,当使用Java和嵌入脚本时,人们在一个脚本中捆绑函数,

我正在搜索 luaj 条目,找到您的未回答。你的问题很有趣,让我搜索。然后我意识到,它是在2年前被问的...我希望我的答案可以对别人有用!基于luaj-3.0-alpha1的代码。



我们需要一个有效的Lua脚本(您忘记了 function 关键字) :

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

函数onWalk )
javaObj:walk()
return 1,km
end


$ b b

我添加了一些跟踪...
我也做了类似于你的类:

  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
{
字符串名称;
Cat(String n){name = n; }
public void talk(){System.out.println(Cat+ name +meows! }
public void walk(){System.out.println(Cat+ name +walks ...); }
}

添加一个字段进行测试。对于我的测试,我只是在方法中声明了创建他们的实例的类:

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

我首先尝试使用 LuaValue luaDog = LuaValue.userdataOf(dog); 但它不工作:我们确实有userdata,如trace所示,但没有metatable,所以我们不能调用方法或访问字段。



经过很多搜索,我发现了正确的咒语:

  CompiledScript脚本=((Compilable)scriptEngine).compile(阅读器); 
Bindings sb = new SimpleBindings();
script.eval(sb); //将Lua函数放入sb环境
LuaValue luaDog = CoerceJavaToLua.coerce(dog); // Java to Lua
LuaFunction onTalk =(LuaFunction)sb.get(onTalk); // Get Lua function
LuaValue b = onTalk.call(luaDog); //调用函数
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返回:+ dist);



我欣赏Luaj API ... :-)可能更多的Java程序员,而其他库似乎以更多地瞄准Lua / C程序员...


I am trying to build a controller using Luaj + java. I have the following java classes

public class Duck {
  public void talk() { System.out.println("Duck quacks!"); }
  public void walk() { System.out.println("Duck walks!"); }
}

public class Person {
  public void talk() { System.out.println("Person talks!"); }
  public void walk() { System.out.println("Person walks!"); }
}

and the following lua script for the controller:

onTalk(obj) 
  obj:talk();
end

onWalk(obj)
   obj:walk();
end

I would ideally like to define one controller (written in lua) where I will keep all of the program's logic, and I would like to expose API from that controller to my java code. I was trying to use the following approach:

ScriptEngineManager sem     = new ScriptEngineManager();
ScriptEngine        engine  = sem.getEngineByExtension(".lua");
ScriptEngineFactory factory = engine.getFactory();

// Script defined above
CompiledScript cs = ((Compilable)engine).compile(MY_LUA_SCRIPT);
SimpleBindings b = new SimpleBindings();

b = newSimpletBindings();

LuaValue onWalkHandler = (LuaValue)b.get("onWalk");
//func.call(LuaValue.valueOf(duck)); // Passing duck object does not work ???

I am not able to pass the object to the LuaValue. How can I pass a java object to the lua script?

PS: In general, when using Java and embedded scripting, do people bundle functions in one script, or is there a separate script for every callback?

解决方案

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...

这篇关于如何将对象传递给一个暴露的luaj函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆