create coroutine changes?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

create coroutine changes?

Xavier Wang
I have tested the Lua 5.4.0 lua_resume API: it seems to returns the
wrong value in "nresults" when the function succeeds returned[1].

The function returns 1 value, but nrets value is 3.

and I found it's just my fault because this change in 5.4.0 manual(in 4c32d930):

---
 To start a coroutine,
-you push onto the thread stack the main function plus any arguments;
+you push the main function plus any arguments
+onto the empty stack of the thread.
---

So, it means in Lua 5.4.0, the lua_resume must be called on an empty
stack now? Is there some reason for this (besides simplifying the
implementation of the coroutine resume)?

IMHO lua_resume just like a yieldable lua_pcall, right? but after Lua
5.4.0 we can not call lua_resume in the middle of the process: we must
clear the stack before call it, and the only notable issue is the
wrong value of nresults.

So, is there some chance to fix the nresults and allow lua_resume
calls on a non-empty stack? or is there any better reason for this
(maybe undocumented?) incompatible change?

P.S. There is a way to call lua_resume better: always use it in C
function called by "lua_call/lua_pcall", in this case, the lua_resume
always has an empty stack to use. Is this the recommended way to use
lua_resume? Maybe we could specify it clearly in the manual.

[1]: test code
#include <stdio.h>
#include <lauxlib.h>

static int func(lua_State *L) {
    lua_pushinteger(L, 1);
    return 1;
}

int main(void)
{
    lua_State *L = luaL_newstate();
    lua_State *L1 = lua_newthread(L);
    lua_pushinteger(L1, 1);
    lua_pushinteger(L1, 2);
    lua_pushcfunction(L1, func);
    lua_pushinteger(L1, 3);
    lua_pushinteger(L1, 4);
    {
        int nrets, status = lua_resume(L1, NULL, 2, &nrets);
        printf("status: %d (nrets=%d)\n", status, nrets);
        lua_pop(L1, nrets);
    }

    lua_close(L);
    return 0;
}




--
regards,
Xavier Wang.