Lightweight, fast, Java-centric Lua interpreter written for JME and JSE, with string, table, package, math, io, os, debug, coroutine & luajava…
sourceforge.net
Can anyone tell me how I can run my Lua script from a file using this LuaJ virtual machine from the B4J application and if this is possible then also B4A?
Can anyone tell me how I can run my Lua script from a file using this LuaJ virtual machine from the B4J application and if this is possible then also B4A?
Why did you use version 2.0.3 and not 3.0.1 or just as an example and can you use the latest 3.0.1?
A question arose about execution, as I understand it, the example is executed in interpreter mode, how can I run it in compiler mode?
I checked in version 3.0.1 a lot of errors do not work... I would like to know why, I am attaching a photo with errors. There is also a bug in version 1.0.5, it only works with 2.0.3, although there is a warning...
'Non-UI application (console / server application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#AdditionalJar: C:\B4JExtraLibs\luaj-jse-3.0.1.jar
#End Region
Sub Process_Globals
Dim jsePlatforrm As JavaObject
Dim standardGlobals As JavaObject
Dim chunk As JavaObject
End Sub
Sub AppStart (Args() As String)
jsePlatforrm.InitializeStatic("org.luaj.vm2.lib.jse.JsePlatform")
standardGlobals = jsePlatforrm.RunMethod("standardGlobals",Null)
chunk.InitializeStatic("org.luaj.vm2.LuaValue")
chunk = standardGlobals.RunMethod("load",Array As String("print 'hello world'"))
chunk.RunMethod("call",Null)
End Sub
jsePlatforrm.InitializeStatic("org.luaj.vm2.lib.jse.JsePlatform")
standardGlobals = jsePlatforrm.RunMethod("standardGlobals",Null)
chunk.InitializeStatic("org.luaj.vm2.LuaValue")
chunk = standardGlobals.RunMethod("load",Array As String("return 4*4"))
Dim myanswer As Int = chunk.RunMethod("call",Null)
Log(myanswer)
I would like to understand how I can pass any values or variables to a script that is launched from a file, for example test.lua, and then the script, when executed, could pass something to the B4J variables
Ok tiny B4XPages example. (I'm just showing the extra code, just use a 'blank' b4xpages project to start with) uses JavaObject Library
In Main
B4X:
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#AdditionalJar: C:\B4JExtraLibs\luaj-jse-3.0.1.jar
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim scriptEngine As JavaObject
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.Show
scriptEngine = (Me).As(JavaObject).RunMethod("setUp",Null)
Log(scriptEngine) ' if this is Null it cant find the scriptengine
Dim PagesManager As B4XPagesManager
PagesManager.Initialize(MainForm)
End Sub
..
#if java
import javax.script.*;
public static ScriptEngine setUp(){
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine e = mgr.getEngineByName("luaj");
return e;
}
#End If
In B4XMainPage change the button click sub to
B4X:
Private Sub Button1_Click
Dim x As Int = 25
Dim y As Double 'ignore
Main.scriptEngine.RunMethod("put",Array("x", x)) ' pass b4x variable to lua (lua name , b4x variable)
Main.scriptEngine.RunMethod("eval",Array("y = math.sqrt(x)")) ' run the lua
y = Main.scriptEngine.RunMethod("get",Array("y")) ' get result back from lua using the lua name
xui.MsgboxAsync($"y= ${y}"$, "B4X") 'display
End Sub
You can also make an alias to Main.scrptEngine to make the code easier to read. (and far less typing)
in B4XMainPage add
B4X:
' in Class_Globals
Dim se As JavaObject = Main.scriptEngine
'in button_click the code become etc
se.RunMethod("put",Array("x", x)) ' pass b4x variable to lua (lua name , b4x variable)