c/lua:coroutine

This commit is contained in:
luoliwoshang
2024-07-28 20:15:11 +08:00
parent 35ba69a175
commit 072f606784
3 changed files with 66 additions and 5 deletions

View File

@@ -0,0 +1,42 @@
package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/lua"
)
func coroutineFunc(L *lua.State) c.Int {
c.Printf(c.Str("Coroutine started\n"))
L.Yield(0) // Pause the coroutine
c.Printf(c.Str("Coroutine resumed\n"))
return 0
}
func main() {
L := lua.Newstate()
defer L.Close()
L.Openlibs()
co := L.Newthread()
L.Pushcfunction(coroutineFunc)
L.Xmove(co, 1)
var nres c.Int
c.Printf(c.Str("Resuming coroutine for the first time\n"))
result := co.Resume(nil, 0, &nres)
if result == lua.YIELD {
c.Printf(c.Str("Coroutine yielded\n"))
result = co.Resume(nil, 0, &nres)
if result == 0 {
c.Printf(c.Str("Coroutine finished\n"))
}
}
}
/* Expected output:
Resuming coroutine for the first time
Coroutine started
Coroutine yielded
Coroutine finished
*/

View File

@@ -36,8 +36,8 @@ func main() {
// Resume coroutine and handle yields // Resume coroutine and handle yields
for { for {
status = co.Resume(nil, 0, &nres) status = co.Resume(nil, 0, &nres)
c.Printf(c.Str("Resuming coroutine %d...\n"), status)
if status == lua.YIELD { if status == lua.YIELD {
c.Printf(c.Str("Resuming coroutine %d...\n"), status)
yieldValue := co.Tointeger(-1) yieldValue := co.Tointeger(-1)
c.Printf(c.Str("Yield value: %d\n"), yieldValue) c.Printf(c.Str("Yield value: %d\n"), yieldValue)
co.Pop(1) // Clean up the stack co.Pop(1) // Clean up the stack
@@ -58,3 +58,23 @@ func main() {
finalStatus := co.Status() finalStatus := co.Status()
c.Printf(c.Str("Final status of coroutine: %d\n"), finalStatus) c.Printf(c.Str("Final status of coroutine: %d\n"), finalStatus)
} }
/* Expected output:
Resuming coroutine...
Resuming coroutine 1...
Yield value: 1
Coroutine is yieldable.
Resuming coroutine 1...
Yield value: 2
Coroutine is yieldable.
Resuming coroutine 1...
Yield value: 3
Coroutine is yieldable.
Resuming coroutine 1...
Yield value: 4
Coroutine is yieldable.
Resuming coroutine 1...
Yield value: 5
Coroutine is yieldable.
Final status of coroutine: 0
*/

View File

@@ -146,7 +146,6 @@ type KFunction func(L *State, status c.Int, ctx KContext) c.Int
func (L *State) Close() {} func (L *State) Close() {}
// State *(lua_newstate) (lua_Alloc f, void *ud); // State *(lua_newstate) (lua_Alloc f, void *ud);
// State *(lua_newthread) (State *L);
// llgo:link (*State).Newthread C.lua_newthread // llgo:link (*State).Newthread C.lua_newthread
func (L *State) Newthread() *State { return nil } func (L *State) Newthread() *State { return nil }
@@ -353,9 +352,9 @@ func (L *State) Status() c.Int { return 0 }
// llgo:link (*State).Isyieldable C.lua_isyieldable // llgo:link (*State).Isyieldable C.lua_isyieldable
func (L *State) Isyieldable() c.Int { return 0 } func (L *State) Isyieldable() c.Int { return 0 }
// TODO(zzy) // llgo:link (*State).Yieldk C.lua_yieldk
// int (lua_yieldk) (State *L, int nresults, lua_KContext ctx, lua_KFunction k); func (L *State) Yieldk(nresults c.Int, ctx KContext, k KFunction) c.Int { return 0 }
// #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) func (L *State) Yield(nresults c.Int) c.Int { return L.Yieldk(nresults, nil, nil) }
// /* // /*
// ** Warning-related functions // ** Warning-related functions