[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Exit one Lua state from a (C) API function
- From: Viacheslav Usov <via.usov@...>
- Date: Mon, 1 Mar 2021 23:50:07 +0100
On Mon, Mar 1, 2021 at 7:55 PM bel <bel2125@gmail.com> wrote:
[A]
> int my_pcall(lua_State *L)
> {
> lua_pcall(L, lua_gettop(L)-1, 0, 0);
> UNREACHABLE
> return 0;
> }
[B]
> int num_from_lua(const char *code)
> {
> lua_State *L = luaL_newstate(); /* create state */
> luaL_openlibs(L);
> lua_pushcfunction(L, my_exit);
> lua_setglobal(L, "my_exit");
> lua_pushcfunction(L, my_pcallk);
> lua_setglobal(L, "my_pcallk");
>
> luaL_loadstring(L, code);
> int result = setjmp(JUMP_TO_EXIT);
> if (result) {
> return result;
> }
> int status = lua_pcall(L, 0, 1, 0);
> if (status) {
> printf("%s\n", lua_tostring(L, 1));
> } else {
> result = lua_tonumber(L, -1);
> }
> lua_close(L);
> return result;
> }
[C]
> a = num_from_lua(
> "co = coroutine.create("
> " function () "
> " my_pcall(coroutine.yield) "
> " end"
> "); "
> "coroutine.resume(co); "
> "print(coroutine.status(co)); "
> "return 32;"
> );
> check(a, 32);
Apparently you believe that [C] would call my_pcall() as defined in [A].
Observe, however, that [B] does not make the latter available to the
Lua state. So the coroutine in [B] simply fails when trying to call a
nil value, and my_pcall() is never called at all.
I would like to point out that for whatever strange reason you seem
determined to say that lua_pcall() or Lua's reference manual have a
bug, you should submit a bug report that demonstrates just this one
bug, not a program doing all kinds of things, which is so confusing
that you ended up confusing yourself.
Cheers,
V.