B4J Question Question about using third-party software

sseaand

Member
Licensed User
Longtime User
I've been asking about Lua for a long time, but I still haven't found a solution on how to use my old work from Lua in B4X.
I recently found LuaJ,

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?
 

teddybear

Well-Known Member
Licensed User
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?
It's not difficult to run a Lua script from a file using inline java.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Copy luaj-jse-2.0.3.jar to Additional Libraries folder, and then run the example.
 

Attachments

  • lua.zip
    2.2 KB · Views: 52
  • luaj-jse-2.0.3.jar
    282.7 KB · Views: 43
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
teddy, using this method can Lua call B4X functions? If yes it is a good script engine to be embedded.
 
Upvote 0

sseaand

Member
Licensed User
Longtime User
Copy luaj-jse-2.0.3.jar to Additional Libraries folder, and then run the example.
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?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
В любом случае, производительность скриптового языка, к тому же очень простого, впечатляет.
This is the english part of the forum. Write, at least, english here.
 
Upvote 0

sseaand

Member
Licensed User
Longtime User
teddy, using this method can Lua call B4X functions? If yes it is a good script engine to be embedded.

In any case, the performance of the scripting language, which is also very simple, is impressive.
 

Attachments

  • Screenshot_20231030-181327~2.png
    Screenshot_20231030-181327~2.png
    180.1 KB · Views: 50
Upvote 0

sseaand

Member
Licensed User
Longtime User
Copy luaj-jse-2.0.3.jar to Additional Libraries folder, and then run the example.
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...
IMG_20231030_190955~6.jpg
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I had Lua 3.0.1 running in a non-ui program fine.
(was only a tiny test - like a 'hello world')
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
B4X:
'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

As I said it was a very simple test program.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Or if you want a value back from lua
B4X:
    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)
 
Upvote 0

sseaand

Member
Licensed User
Longtime User
У меня Lua 3.0.1 работал нормально в программе, отличной от пользовательского интерфейса.
(это был всего лишь крошечный тест - типа «привет, мир»)
Problem with console output via print due to this error in 3.0.1
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
It's working fine here on java 21 in the console app.
 
Upvote 0

sseaand

Member
Licensed User
Longtime User
Or if you want a value back from lua
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
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
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
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
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)
 
Upvote 0
Top