c/lua:table field operate

This commit is contained in:
luoliwoshang
2024-09-16 22:16:01 +08:00
parent e5a9af9a31
commit c6345279cc
2 changed files with 70 additions and 15 deletions

View File

@@ -1,6 +1,8 @@
package main
import (
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/lua"
)
@@ -8,9 +10,19 @@ import (
func printTable(L *lua.State) {
L.Pushnil()
for L.Next(-2) != 0 {
key := L.Tostring(-2)
value := L.Tostring(-1)
c.Printf(c.Str("%s - %s\n"), key, value)
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))
}
L.Pop(1)
}
L.Pop(1)
@@ -24,37 +36,65 @@ func main() {
L.Newtable()
// set table name:John
L.Pushstring(c.Str("name"))
L.Pushstring(c.Str("John"))
L.Settable(-3)
// set table age:30
L.Pushstring(c.Str("age"))
L.Pushnumber(30)
L.Settable(-3)
// set table field fullname:John Doe
L.Pushstring(c.Str("John Doe"))
L.Setfield(-2, c.Str("fullname"))
// 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
L.Getfield(-1, c.Str("name"))
c.Printf(c.Str("%s\n"), L.Tostring(-1))
c.Printf(c.Str("name: %s\n"), L.Tostring(-1))
L.Pop(1)
// 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
L.Pushstring(c.Str("age"))
L.Gettable(-2)
age := int(L.Tonumber(-1))
c.Printf(c.Str("Age: %d\n"), age)
L.Pop(1)
// 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)
c.Printf(c.Str("All entries in the table:\n"))
printTable(L)
}
/* Expected output:
John
name: John
fullname: John Doe
Age: 30
Index[1] value: 123
All entries in the table:
age - 30.0
fullname - John Doe
[1] - 123
name - John
[pointer] - pointer value
fullname - John Doe
age - 30.0
*/

View File

@@ -301,10 +301,17 @@ func (L *State) Gettable(idx c.Int) c.Int { return 0 }
// llgo:link (*State).Getfield C.lua_getfield
func (L *State) Getfield(idx c.Int, k *c.Char) c.Int { return 0 }
// LUA_API int (lua_geti) (State *L, int idx, lua_Integer n);
// LUA_API int (lua_rawget) (State *L, int idx);
// LUA_API int (lua_rawgeti) (State *L, int idx, lua_Integer n);
// LUA_API int (lua_rawgetp) (State *L, int idx, const void *p);
// llgo:link (*State).Geti C.lua_geti
func (L *State) Geti(idx c.Int, n Integer) c.Int { return 0 }
// llgo:link (*State).Rawget C.lua_rawget
func (L *State) Rawget(idx c.Int) c.Int { return 0 }
// llgo:link (*State).Rawgeti C.lua_rawgeti
func (L *State) Rawgeti(idx c.Int, n Integer) c.Int { return 0 }
// llgo:link (*State).Rawgetp C.lua_rawgetp
func (L *State) Rawgetp(idx c.Int, p c.Pointer) c.Int { return 0 }
// llgo:link (*State).Createtable C.lua_createtable
func (L *State) Createtable(narr c.Int, nrec c.Int) {}
@@ -330,10 +337,17 @@ func (L *State) Settable(idx c.Int) {}
// llgo:link (*State).Setfield C.lua_setfield
func (L *State) Setfield(idx c.Int, k *c.Char) {}
//void (lua_seti) (State *L, int idx, lua_Integer n);
//void (lua_rawset) (State *L, int idx);
//void (lua_rawseti) (State *L, int idx, lua_Integer n);
//void (lua_rawsetp) (State *L, int idx, const void *p);
// llgo:link (*State).Seti C.lua_seti
func (L *State) Seti(idx c.Int, n Integer) {}
// llgo:link (*State).Rawset C.lua_rawset
func (L *State) Rawset(idx c.Int) {}
// llgo:link (*State).Rawseti C.lua_rawseti
func (L *State) Rawseti(idx c.Int, n Integer) {}
// llgo:link (*State).Rawsetp C.lua_rawsetp
func (L *State) Rawsetp(idx c.Int, p c.Pointer) {}
// llgo:link (*State).Setmetatable C.lua_setmetatable
func (L *State) Setmetatable(objindex c.Int) c.Int { return 0 }
@@ -415,6 +429,7 @@ const (
/*
* miscellaneous functions
*/
// llgo:link (*State).Next C.lua_next
func (L *State) Next(idx c.Int) c.Int { return 0 }