Files
llgo/c/lua/_demo/funccall-string/funccall.go

49 lines
1.0 KiB
Go
Raw Normal View History

2024-06-27 18:19:45 +08:00
package main
import (
_ "unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/lua"
)
func main() {
2024-07-13 22:57:01 +08:00
L := lua.Newstate()
2024-06-27 18:19:45 +08:00
defer L.Close()
2024-07-13 22:57:01 +08:00
L.Openlibs()
2024-06-27 18:19:45 +08:00
code := c.Str(
`function processStrings(a, b, c)
print('Received string a: ' .. a)
print('Received string b: ', b)
print('Received string c (formatted): ' .. c)
return a .. b .. c
end`)
2024-07-13 22:57:01 +08:00
if res := L.Dostring(code); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.Tostring(-1))
2024-06-27 18:19:45 +08:00
}
2024-07-13 22:57:01 +08:00
L.Getglobal(c.Str("processStrings"))
2024-06-27 18:19:45 +08:00
2024-07-13 22:57:01 +08:00
L.Pushstring(c.Str("Hello, World!"))
L.Pushlstring(c.Str(`Hello Lua In LLGO`), 17)
L.Pushfstring(c.Str(`Hello %s In %d`), c.Str("LLGO"), 2024)
2024-06-27 18:19:45 +08:00
2024-07-13 22:57:01 +08:00
if res := L.Pcall(3, 1, 0); res != lua.OK {
c.Printf(c.Str("error: %s\n"), L.Tostring(-1))
2024-06-27 18:19:45 +08:00
}
2024-07-13 22:57:01 +08:00
if res := L.Isstring(-1); res != 0 {
result := L.Tostring(-1)
2024-06-27 18:19:45 +08:00
c.Printf(c.Str("result: %s\n"), result)
}
}
/* Expected output:
Received string a: Hello, World!
Received string b: Hello Lua In LLGO
Received string c (formatted): Hello LLGO In 2024
result: Hello, World!Hello Lua In LLGOHello LLGO In 2024
*/