llgo/c/lua:link style

This commit is contained in:
luoliwoshang
2024-07-13 22:57:01 +08:00
parent 9edeee4b3f
commit de4b5b70da
12 changed files with 225 additions and 183 deletions

View File

@@ -6,10 +6,10 @@ import (
)
func printTable(L *lua.State) {
L.PushNil()
L.Pushnil()
for L.Next(-2) != 0 {
key := L.ToString(-2)
value := L.ToString(-1)
key := L.Tostring(-2)
value := L.Tostring(-1)
c.Printf(c.Str("%s - %s\n"), key, value)
L.Pop(1)
}
@@ -17,31 +17,31 @@ func printTable(L *lua.State) {
}
func main() {
L := lua.NewState()
L := lua.Newstate()
defer L.Close()
L.OpenLibs()
L.Openlibs()
L.NewTable()
L.Newtable()
L.PushString(c.Str("name"))
L.PushString(c.Str("John"))
L.SetTable(-3)
L.Pushstring(c.Str("name"))
L.Pushstring(c.Str("John"))
L.Settable(-3)
L.PushString(c.Str("age"))
L.PushNumber(30)
L.SetTable(-3)
L.Pushstring(c.Str("age"))
L.Pushnumber(30)
L.Settable(-3)
L.PushString(c.Str("John Doe"))
L.SetField(-2, c.Str("fullname"))
L.Pushstring(c.Str("John Doe"))
L.Setfield(-2, c.Str("fullname"))
L.GetField(-1, c.Str("name"))
c.Printf(c.Str("%s\n"), L.ToString(-1))
L.Getfield(-1, c.Str("name"))
c.Printf(c.Str("%s\n"), L.Tostring(-1))
L.Pop(1)
L.PushString(c.Str("age"))
L.GetTable(-2)
age := int(L.ToNumber(-1))
L.Pushstring(c.Str("age"))
L.Gettable(-2)
age := int(L.Tonumber(-1))
c.Printf(c.Str("Age: %d\n"), age)
L.Pop(1)
@@ -49,3 +49,12 @@ func main() {
printTable(L)
}
/* Expected output:
John
Age: 30
All entries in the table:
age - 30.0
fullname - John Doe
name - John
*/