c/lua:coroutine
This commit is contained in:
42
c/lua/_demo/coroutine-cfunc/coroutine.go
Normal file
42
c/lua/_demo/coroutine-cfunc/coroutine.go
Normal 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
|
||||
*/
|
||||
@@ -36,8 +36,8 @@ func main() {
|
||||
// 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 {
|
||||
c.Printf(c.Str("Resuming coroutine %d...\n"), status)
|
||||
yieldValue := co.Tointeger(-1)
|
||||
c.Printf(c.Str("Yield value: %d\n"), yieldValue)
|
||||
co.Pop(1) // Clean up the stack
|
||||
@@ -58,3 +58,23 @@ func main() {
|
||||
finalStatus := co.Status()
|
||||
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
|
||||
*/
|
||||
|
||||
@@ -146,7 +146,6 @@ type KFunction func(L *State, status c.Int, ctx KContext) c.Int
|
||||
func (L *State) Close() {}
|
||||
|
||||
// State *(lua_newstate) (lua_Alloc f, void *ud);
|
||||
// State *(lua_newthread) (State *L);
|
||||
|
||||
// llgo:link (*State).Newthread C.lua_newthread
|
||||
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
|
||||
func (L *State) Isyieldable() c.Int { return 0 }
|
||||
|
||||
// TODO(zzy)
|
||||
// int (lua_yieldk) (State *L, int nresults, lua_KContext ctx, lua_KFunction k);
|
||||
// #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
|
||||
// llgo:link (*State).Yieldk C.lua_yieldk
|
||||
func (L *State) Yieldk(nresults c.Int, ctx KContext, k KFunction) c.Int { return 0 }
|
||||
func (L *State) Yield(nresults c.Int) c.Int { return L.Yieldk(nresults, nil, nil) }
|
||||
|
||||
// /*
|
||||
// ** Warning-related functions
|
||||
|
||||
Reference in New Issue
Block a user