Project AR15.luagen is static source generation for luajava without using reflection
I have a plan to support maven but the priority is low for now
- Type
git submodule add https://github.com/dayo05/aris.luagen
to add this project as submodule - Inside global settings.gradle, append
includeBuild("aris.luagen")
on last - Add KSP gradle plugin inside plugins block of build.gradle:
id("com.google.devtools.ksp") version "2.0.0-1.0.21" // version of ksp can dependent on your kotlin version
- Inside dependencies block of build.gradle, append following code to import this project:
implementation("me.ddayo:aris.luagen")
ksp("me.ddayo:ap")
@LuaProvier("MyGenerated") // specify generated class name here. You can also set default name at ksp option
object MyFunctions {
@LuaFunction // this creates lua function as same name with kotlin side
fun function1() {
// do some stuff for function1
}
@LuaFunction(name = "custom_function") // this overwrites the name of function
fun function2() {
// do some stuff for custom_function
}
@LuaFunction("create_kotlin_class")
fun function3() = MyKotlinClass()
}
@LuaProvider("MyGenerated")
class MyKotlinClass: IStaticDecl by MyGenerated.MyKotlinClass_LuaGenerated {
/*
MyKotlinClass_LuaGenerated object which contains implementation of toLua() will be generated by KSP.
If this class is abstract, just adding @LuaProvider annotation is also ok
Using `by` keyword is equivalent with following code:
override fun toLua(lua: Lua) {
MyKotlinClass_LuaGenerated.toLua(lua)
}
Modification of toLua method is not recommended.
*/
@LuaFunction
fun myFUnction(param: Int) {
// do some stuff for myFUnction
}
}
class MyEngine: LuaEngine() {
init {
MyGenerated.initLua(lua)
}
}
class Main {
fun main() {
val engine = MyEngine()
engine.createTask("""
-- your code here
local a = create_kotlin_class()
a:myFUnction(1) -- invoke a.myFUnction with parameter 1
-- a.myFUnction(1) does not works because the function accepts self object as first argument
coroutine.yield() -- this exists from lua context
custom_function() -- this executed on second loop
""", "some_task")
engine.loop() // execute function
engine.loop() // resume lua code execution
}
}
Make sure you have added lua native library before running the code. This project does not ship the lua native
- You can see Test.kt for more examples :)
- Because this does not use reflection so it is fast after initialization
- You can hide some public functions on lua side
- Advanced task API works with lua coroutine
- This need extra initialization
- I hacked JNI side of luajava for some features so it can be unstable
I am tracking some issue on original luajava to make it stable as I can :)