java – 使用luaj将参数传递给lua函数
发布时间:2020-12-14 19:33:28 所属栏目:Java 来源:网络整理
导读:我正在尝试使用LuaJ在 Java程序中调用lua函数.当我没有向闭包传递任何参数时,它工作正常: String script = "print 'Hello World!'";InputStream input = new ByteArrayInputStream(script.getBytes());Prototype prototype = LuaC.compile(input,"script");
我正在尝试使用LuaJ在
Java程序中调用lua函数.当我没有向闭包传递任何参数时,它工作正常:
String script = "print 'Hello World!'"; InputStream input = new ByteArrayInputStream(script.getBytes()); Prototype prototype = LuaC.compile(input,"script"); LuaValue globals = JsePlatform.standardGlobals(); LuaClosure closure = new LuaClosure(prototype,globals); closure.call(); 但是现在我正在尝试一个带有顶级函数的lua脚本,该函数接受一个参数而我无法弄清楚如何从Java中传入参数.这是我到目前为止所得到的: String script = "function something(argument)n"+ "test_string = 'Hello World!'n"+ "print(test_string)n"+ "print(argument)n"+ "end"; InputStream input = new ByteArrayInputStream(script.getBytes()); Prototype prototype = LuaC.compile(input,globals); closure.invokemethod("something",CoerceJavaToLua.coerce("Foo")); 这会导致invoke方法行出现异常:
谢谢你的帮助! 解决方法
在lua中,顶级作用域是具有可变参数的匿名函数.这些是使用…访问的.在您的示例中,您不需要名为something的函数,块本身可以用作未命名的函数.
例如,这个代码在luaj-3.0-beta1中 String script = "argument = ...n"+ "test_string = 'Hello World!'n"+ "print(test_string)n"+ "print(argument)n"; Globals globals = JsePlatform.standardGlobals(); LuaValue chunk = globals.loadString(script,"myscript"); chunk.call( LuaValue.valueOf("some-arg-value") ); 为我制作了这个结果: Hello World! some-arg-value 您可以通过这种方式传递任意数量的参数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – HashMap中的NULL键的Hashcode
- JAAS authentication in Tomcat example--reference
- eclipse中导入项目外部的jar包时出现.classpath can`t be w
- MyBatis的 config.xml标签
- 如何使用基于JAX-RS的Restlet自动将@DefaultValue映射到枚举
- Java压缩或解压文件实现
- java – 如何创建包含字符串和对象的Hashtable条目的JList?
- Java – 添加到Lambda表达式中的字符串
- java – 如何实现我可以回放的OutputStream?
- java – JPA继承不是DRY