https://github.com/martmists-gh/klua
[WIP] A Lua interpreter for Kotlin
https://github.com/martmists-gh/klua
interpreter kotlin lua
Last synced: 10 months ago
JSON representation
[WIP] A Lua interpreter for Kotlin
- Host: GitHub
- URL: https://github.com/martmists-gh/klua
- Owner: Martmists-GH
- License: bsd-4-clause
- Created: 2023-12-10T22:19:22.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T23:44:12.000Z (about 2 years ago)
- Last Synced: 2025-02-16T14:58:23.161Z (12 months ago)
- Topics: interpreter, kotlin, lua
- Language: Lua
- Homepage:
- Size: 440 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# KLua
KLua is a Lua interpreter written in Kotlin, based on Kotlinx.coroutines.
It currently only supports JVM, but should be easy to port once context receivers become available on other platforms.
## Features
- Based on Lua 5.4.6
- Coroutines are implemented natively using Kotlinx.coroutines
## Usage
Running snippets of Lua is simple:
```kotlin
val engine = LuaInterpreter()
engine.execute("print('Hello, world!')")
```
However, in some cases you may want to safeguard your application from malicious code, by removing e.g. io access.
```kotlin
val engine = LuaInterpreter()
engine.execute("print('Hello, world!')") { env: TTable ->
env["io"] = TNil // Remove io library from _ENV (= _G)
}
```
Registering custom types or methods is also fairly straightforward:
```kotlin
class Counter {
var value = 0
}
val engine = LuaInterpreter()
engine.execute("...") { env: TTable ->
env["my_function"] = TFunction { args: List ->
val action = args.argument(0, TNumber::class)
val value = action.value.toLong()
when (value) {
0L -> return_(TString("Hello, world!")) // Return a value
1L -> yield(TString("Hello, world!")) // Yield a value
else -> error("Invalid action") // Emit an error
}
}
env["some_counter"] = TUserdata(Counter()) // Methods can be added using the __index metamethod
}
```