Implement printing from lua

This commit is contained in:
2025-03-26 13:37:27 +01:00
parent 10c39b02a0
commit a9b6f7f984
3 changed files with 45 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ package processor
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
@@ -239,6 +240,8 @@ modified = false
if err := L.DoString(helperScript); err != nil {
return fmt.Errorf("error loading helper functions: %v", err)
}
L.SetGlobal("print", L.NewFunction(printToGo))
return nil
}
@@ -295,6 +298,20 @@ func BuildLuaScript(luaExpr string) string {
return fullScript
}
func printToGo(L *lua.LState) int {
// Get the number of arguments passed to the Lua print function
n := L.GetTop()
// Create a slice to hold the arguments
args := make([]interface{}, n)
for i := 1; i <= n; i++ {
args[i-1] = L.Get(i) // Get the argument from Lua stack
}
// Print the arguments to Go's stdout
log.Print("Lua: ")
log.Println(args...)
return 0 // No return values
}
// Max returns the maximum of two integers
func Max(a, b int) int {
if a > b {