llgo/c/lua:table & coroutine
This commit is contained in:
60
c/lua/_demo/coroutine/coroutine.go
Normal file
60
c/lua/_demo/coroutine/coroutine.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/lua"
|
||||
)
|
||||
|
||||
func coroutineFunc(L *lua.State) {
|
||||
L.LoadString(c.Str(`
|
||||
function coro_func()
|
||||
for i = 1, 5 do
|
||||
coroutine.yield(i)
|
||||
end
|
||||
end
|
||||
`))
|
||||
L.PCall(0, 0, 0)
|
||||
L.GetGlobal(c.Str("coro_func"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
L.OpenLibs()
|
||||
|
||||
coroutineFunc(L) // Load and get the coroutine function
|
||||
|
||||
co := L.NewThread() // Create a new coroutine/thread
|
||||
L.PushValue(-2) // Move the function to the top of the stack
|
||||
L.XMove(co, 1) // Move the function to the new coroutine
|
||||
|
||||
var nres c.Int
|
||||
var status c.Int
|
||||
|
||||
c.Printf(c.Str("Resuming coroutine...\n"))
|
||||
// Resume coroutine and handle yields
|
||||
for {
|
||||
status = co.Resume(nil, 0, &nres)
|
||||
c.Printf(c.Str("Resuming coroutine %d...\n"), status)
|
||||
if status == lua.YIELD {
|
||||
yieldValue := co.ToInteger(-1)
|
||||
c.Printf(c.Str("Yield value: %d\n"), yieldValue)
|
||||
co.Pop(1) // Clean up the stack
|
||||
|
||||
// Check if the coroutine is yieldable
|
||||
if co.IsYieldable() != 0 {
|
||||
c.Printf(c.Str("Coroutine is yieldable.\n"))
|
||||
} else {
|
||||
c.Printf(c.Str("Coroutine is not yieldable.\n"))
|
||||
}
|
||||
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Check the final status of the coroutine
|
||||
finalStatus := co.Status()
|
||||
c.Printf(c.Str("Final status of coroutine: %d\n"), finalStatus)
|
||||
}
|
||||
@@ -12,7 +12,7 @@ func main() {
|
||||
defer L.Close()
|
||||
|
||||
L.OpenLibs()
|
||||
if res := L.Dostring(c.Str("function combineParams(num, str) return 'Result: ' .. str .. ' ' .. num end")); res != lua.OK {
|
||||
if res := L.DoString(c.Str("function combineParams(num, str) return 'Result: ' .. str .. ' ' .. num end")); res != lua.OK {
|
||||
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
|
||||
}
|
||||
L.GetGlobal(c.Str("combineParams"))
|
||||
|
||||
@@ -20,7 +20,7 @@ func main() {
|
||||
return a .. b .. c
|
||||
end`)
|
||||
|
||||
if res := L.Dostring(code); res != lua.OK {
|
||||
if res := L.DoString(code); res != lua.OK {
|
||||
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ func main() {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
L.OpenLibs()
|
||||
if res := L.Dostring(c.Str("print('hello world')")); res != lua.OK {
|
||||
if res := L.DoString(c.Str("print('hello world')")); res != lua.OK {
|
||||
println("error")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/lua"
|
||||
)
|
||||
|
||||
func main() {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
L.OpenLibs()
|
||||
// TODO(zzy): fix push interger got stuck
|
||||
code := c.Str(`function combineParams(x)
|
||||
return x * 2
|
||||
end`)
|
||||
if res := L.Dostring(code); res != lua.OK {
|
||||
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
|
||||
}
|
||||
L.GetGlobal(c.Str("combineParams"))
|
||||
c.Printf(c.Str("stack lens:%d\n"), L.GetTop()) // stack lens:1
|
||||
|
||||
L.PushInteger(lua.Integer(42))
|
||||
pushed := L.ToInteger(-1)
|
||||
|
||||
c.Printf(c.Str("pushed: %lld\n"), pushed) // pushed: 0
|
||||
c.Printf(c.Str("stack lens:%d\n"), L.GetTop()) // stack lens:1
|
||||
|
||||
// L.PushNumber(42)
|
||||
// pushed := L.ToNumber(-1)
|
||||
// c.Printf(c.Str("pushed: %f\n"), pushed)
|
||||
|
||||
if res := L.PCall(1, 1, 0); res != lua.OK {
|
||||
c.Printf(c.Str("error: %s\n"), L.ToString(-1))
|
||||
}
|
||||
if res := L.IsNumber(-1); res != 0 {
|
||||
result := L.ToInteger(-1)
|
||||
c.Printf(c.Str("result %f"), result)
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,8 @@ import (
|
||||
)
|
||||
|
||||
// printStack prints the current stack of the given Lua state.
|
||||
func printStack(L *lua.Lua_State, stateName *c.Char) {
|
||||
func printStack(L *lua.State, stateName *c.Char) {
|
||||
top := L.GetTop()
|
||||
// c.Printf(c.Str("%s stack (top=%d): "), c.GoStringData("sdasd"), top)
|
||||
c.Printf(c.Str("%s stack (top=%d):"), stateName, top)
|
||||
for i := 1; i <= int(top); i++ {
|
||||
c.Printf(c.Str("%s "), L.ToString(c.Int(i)))
|
||||
@@ -73,7 +72,7 @@ func main() {
|
||||
defer L1.Close()
|
||||
|
||||
// Move two elements to the new state
|
||||
L.Xmove(L1, c.Int(2))
|
||||
L.XMove(L1, c.Int(2))
|
||||
c.Printf(c.Str("\nAfter moving two elements to L1:\n"))
|
||||
printStack(L, c.Str("L1"))
|
||||
printStack(L1, c.Str("L2"))
|
||||
|
||||
51
c/lua/_demo/table/table.go
Normal file
51
c/lua/_demo/table/table.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/lua"
|
||||
)
|
||||
|
||||
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)
|
||||
L.Pop(1)
|
||||
}
|
||||
L.Pop(1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
L.OpenLibs()
|
||||
|
||||
L.NewTable()
|
||||
|
||||
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("John Doe"))
|
||||
L.SetField(-2, c.Str("fullname"))
|
||||
|
||||
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))
|
||||
c.Printf(c.Str("Age: %d\n"), age)
|
||||
L.Pop(1)
|
||||
|
||||
c.Printf(c.Str("All entries in the table:\n"))
|
||||
printTable(L)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user