lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello, everyone!

Please consider the following piece of code:

---------------------------------------------------------------
#include <stdio.h>
#include <lua.h>

int test(lua_State* L) {
        printf("test start\n");
        return lua_yield(L, 0);
}

int main() {
        lua_State* L = luaL_newstate();
        lua_State* T = lua_newthread(L);
        luaL_ref(L, LUA_REGISTRYINDEX);
        lua_pushcfunction(T, test);
        printf("1st resume: %d\n", lua_resume(T, 0));
        printf("2nd resume: %d\n", lua_resume(T, 0));
        lua_close(L);
        return 0;
}
---------------------------------------------------------------

In this example, I am trying to use a C-function as the main function for lua_resume() call. The
program crashes with segfault.

Probably, it is forbidden to use lua_resume() this way, however I didn't find any related
information neihter in the Lua 5.1 manual nor on the Lua users wiki and the whole Internet. Furthermore,
there seem to be no obvious system restrictions to do such calls internally, and it looks further unnecessary
to put additional checks into the final code to distinguish between C and non-C main functions
provided to lua_resume when the decision is made, for instance, in Lua code by an end-user.

Of course, if you put "return 0" to the end of the "test" function instead of "return lua_yield(..)"
(no yield) or wrap the call to test() into a Lua function like "function () test() end" and call
lua_resume() with it as the main function, the code works fine.

Sorry, if I've occasionally missed something substantional which leads to the error. I admire the
work the Lua team is doing and I wish this information could become useful for them.

Best regards,
Arseny Vakhrushev