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

40 lines
771 B
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()
if res := L.Loadstring(c.Str("function doubleNumber(x) return x * 2 end")); 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.Pcall(0, 0, 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
L.Getglobal(c.Str("doubleNumber"))
L.Pushnumber(10)
2024-06-27 18:19:45 +08:00
2024-07-13 22:57:01 +08:00
if res := L.Pcall(1, 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.Isnumber(-1); res != 0 {
result := L.Tointeger(-1)
2024-06-27 18:19:45 +08:00
c.Printf(c.Str("result: %lld\n"), result)
} else {
2024-07-13 22:57:01 +08:00
c.Printf(c.Str("error: %s\n"), L.Tostring(-1))
2024-06-27 18:19:45 +08:00
}
}
/* Expected output:
result: 20
*/