Files
llgo/c/lua/_demo/table/table.go

101 lines
1.9 KiB
Go
Raw Normal View History

2024-06-30 19:35:45 +08:00
package main
import (
2024-09-16 22:16:01 +08:00
"unsafe"
2024-06-30 19:35:45 +08:00
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/lua"
)
func printTable(L *lua.State) {
2024-07-13 22:57:01 +08:00
L.Pushnil()
2024-06-30 19:35:45 +08:00
for L.Next(-2) != 0 {
2024-07-13 22:57:01 +08:00
value := L.Tostring(-1)
2024-09-16 22:16:01 +08:00
switch L.Type(-2) {
case lua.STRING:
key := L.Tostring(-2)
c.Printf(c.Str("%s - %s\n"), key, value)
case lua.NUMBER:
key := L.Tonumber(-2)
c.Printf(c.Str("[%.0f] - %s\n"), key, value)
case lua.LIGHTUSERDATA:
c.Printf(c.Str("[pointer] - %s\n"), value)
default:
c.Printf(c.Str("unknown key type %s %d\n"), L.Typename(-2), L.Type(-2))
}
2024-06-30 19:35:45 +08:00
L.Pop(1)
}
L.Pop(1)
}
func main() {
2024-07-13 22:57:01 +08:00
L := lua.Newstate()
2024-06-30 19:35:45 +08:00
defer L.Close()
2024-07-13 22:57:01 +08:00
L.Openlibs()
2024-06-30 19:35:45 +08:00
2024-07-13 22:57:01 +08:00
L.Newtable()
2024-06-30 19:35:45 +08:00
2024-09-16 22:16:01 +08:00
// set table name:John
2024-07-13 22:57:01 +08:00
L.Pushstring(c.Str("name"))
L.Pushstring(c.Str("John"))
L.Settable(-3)
2024-06-30 19:35:45 +08:00
2024-09-16 22:16:01 +08:00
// set table age:30
2024-07-13 22:57:01 +08:00
L.Pushstring(c.Str("age"))
L.Pushnumber(30)
L.Settable(-3)
2024-06-30 19:35:45 +08:00
2024-09-16 22:16:01 +08:00
// set table field fullname:John Doe
2024-07-13 22:57:01 +08:00
L.Pushstring(c.Str("John Doe"))
L.Setfield(-2, c.Str("fullname"))
2024-06-30 19:35:45 +08:00
2024-09-16 22:16:01 +08:00
// set index field
L.Pushinteger(123)
L.Seti(-2, c.Int(1))
// set pointer key field
pointerKey := c.AllocaCStr("pointer key")
L.Pushstring(c.Str("pointer value"))
L.Rawsetp(-2, unsafe.Pointer(pointerKey))
// get field by Getfield
2024-07-13 22:57:01 +08:00
L.Getfield(-1, c.Str("name"))
2024-09-16 22:16:01 +08:00
c.Printf(c.Str("name: %s\n"), L.Tostring(-1))
2024-06-30 19:35:45 +08:00
L.Pop(1)
2024-09-16 22:16:01 +08:00
// get field by Rawget
L.Pushstring(c.Str("fullname"))
L.Rawget(-2)
c.Printf(c.Str("fullname: %s\n"), L.Tostring(-1))
L.Pop(1)
// get field by Gettable
2024-07-13 22:57:01 +08:00
L.Pushstring(c.Str("age"))
L.Gettable(-2)
age := int(L.Tonumber(-1))
2024-06-30 19:35:45 +08:00
c.Printf(c.Str("Age: %d\n"), age)
L.Pop(1)
2024-09-16 22:16:01 +08:00
// get index field
L.Geti(-1, c.Int(1))
c.Printf(c.Str("Index[%d] value: %d\n"), 1, L.Tointeger(-1))
L.Pop(1)
2024-06-30 19:35:45 +08:00
c.Printf(c.Str("All entries in the table:\n"))
printTable(L)
}
2024-07-13 22:57:01 +08:00
/* Expected output:
2024-09-16 22:16:01 +08:00
name: John
fullname: John Doe
2024-07-13 22:57:01 +08:00
Age: 30
2024-09-16 22:16:01 +08:00
Index[1] value: 123
2024-07-13 22:57:01 +08:00
All entries in the table:
2024-09-16 22:16:01 +08:00
[1] - 123
2024-07-13 22:57:01 +08:00
name - John
2024-09-16 22:16:01 +08:00
[pointer] - pointer value
fullname - John Doe
age - 30.0
2024-07-13 22:57:01 +08:00
*/