luaj

Java调用Lua脚本(热载实现)

可紊 提交于 2020-11-10 01:01:16
前言:   Lua作为解析执行的脚本语言, 往往是易变逻辑编写的首选语言, 尤其是在游戏领域. C/C++和Lua的结合, 往往了标配. 比如Redis, Nginx其对Lua的支持, 也是杠杠的. 当然Lua也可以作为规则引擎中的规则编写语言. 本文对Java调用Lua(Luaj)的实现机制, 做下简单的介绍. Luaj简介:   Luaj是Java调用Lua的一种实现方式, 其是构建一个虚拟机解析执行Lua脚本来实现的, 这和Groovy的方式有所不同.   这是Luaj的 官网 , http://www.luaj.org/luaj/3.0/README.html.   它是针对 5.2.x 的lua版本的解析器, 其Luaj库的编写是通过JavaCC来实现的. 简单示例:   集合Luaj, 可以通过Maven进行如下配置: <dependency> <groupId>org.luaj</groupId> <artifactId>luaj-jse</artifactId> <version>3.0.1</version> </dependency>   Luaj的一个简单的示例程序: import org.luaj.vm2.Globals; import org.luaj.vm2.LuaValue; import org.luaj.vm2.lib.jse

How can I abandon a LuaJ coroutine LuaThread?

被刻印的时光 ゝ 提交于 2019-12-23 12:55:00
问题 I am experimenting with a game mechanic in which players can run scripts on in-game computers. Script execution will be resource limited at a gameplay level to some amount of instructions per tick. The following proof-of-concept demonstrates a basic level of sandboxing and throttling of arbitrary user code. It successfully runs ~250 instructions of poorly crafted 'user input' and then discards the coroutine. Unfortunately, the Java process never terminates. A little investigation in shows

Lua / Java / LuaJ - Handling or Interrupting Infinite Loops and Threads

丶灬走出姿态 提交于 2019-12-22 08:30:14
问题 I'm using LuaJ to run user-created Lua scripts in Java. However, running a Lua script that never returns causes the Java thread to freeze. This also renders the thread uninterruptible. I run the Lua script with: JsePlatform.standardGlobals().loadFile("badscript.lua").call(); badscript.lua contains while true do end . I'd like to be able to automatically terminate scripts which are stuck in unyielding loops and also allow users to manually terminate their Lua scripts while they are running. I

Lua / Java / LuaJ - Handling or Interrupting Infinite Loops and Threads

[亡魂溺海] 提交于 2019-12-22 08:29:00
问题 I'm using LuaJ to run user-created Lua scripts in Java. However, running a Lua script that never returns causes the Java thread to freeze. This also renders the thread uninterruptible. I run the Lua script with: JsePlatform.standardGlobals().loadFile("badscript.lua").call(); badscript.lua contains while true do end . I'd like to be able to automatically terminate scripts which are stuck in unyielding loops and also allow users to manually terminate their Lua scripts while they are running. I

LuaJ: Unable to call 'require' function in Lua script

扶醉桌前 提交于 2019-12-22 06:56:17
问题 There is a really good chance that I am doing something bizarre that is causing this error. The following simple example fails: --> thingy.lua function doThing() print( "Thing has been done." ); end and --> test.lua require( "thingy" ); When thingy.lua is executed, there are no problems. When test.lua is executed, I see the following error: script:2 module 'thingy' not found: thingy no field package.preload['thingy'] thingy.lua no class 'thingy' Both of these files exist in the same directory

How can I pass objects to an exposed luaj function?

谁都会走 提交于 2019-12-18 23:37:44
问题 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

LuaJ loading two functions with the same name from two different LuaScripts

陌路散爱 提交于 2019-12-13 18:02:35
问题 I have two Lua Scripts containing functions with the same name: luaScriptA: function init() print( 'This function was run from Script A' ) end luaScriptB: function init() print( 'This function was run from Script B' ) end I would like to load both these functions using LuaJ into the globals environnment, for one script I usually do it as follows: LuaValue chunk = globals.load(new FileInputStream(luaScriptA), scriptName, "t", globals); chunk.call(); This will load the function init() into

LuaJ How to create instance of Java object in Lua script?

折月煮酒 提交于 2019-12-12 02:59:59
问题 I have Java class: class SomeClass{ private int i; public SomeClass(int i){ this.i = i; } } And I need to create instance of this class in Lua script and pass it to Java function, using LuaJ library. How I can do it? 回答1: This is some example code found on lua.org: jframe = luajava.bindClass( "javax.swing.JFrame" ) frame = luajava.newInstance( "javax.swing.JFrame", "Texts" ); frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE) frame:setSize(300,400) frame:setVisible(true) source: http://www

Making global environment access-only (Lua)

别等时光非礼了梦想. 提交于 2019-12-11 05:57:35
问题 I embedded Lua and want scripts to be able to read the global table but not automatically write to it so two scripts can write variables with the same name without overwriting eachother but still being able to add stuff to the global table. I can't really explain it better then this: Script 1 var1 = "foo" _G.var2 = "bar" Script 2 print(var1) -- Prints nil print(var2) -- Prints 'bar' How I tried to accomplish this is by doing something like this (The 'scripts' being a function) newScript =

Luaj: How to Import or Require a Lua Function Library

风流意气都作罢 提交于 2019-12-08 15:50:31
问题 In the Java LuaJ library I would like to know how to require or import a lua script of functions in another lua script called by a lua closure through Java. For example this does not work: public static LuaValue runInputStreamLua(InputStream inputStream) throws Exception { Prototype luaScriptPrototype = LuaC.instance.compile(inputStream, ""); Globals luaScriptStandardGlobals = JsePlatform.standardGlobals(); luaScriptStandardGlobals.loadfile("mycoolmathfunctions.lua"); LuaClosure luaClosure =