- 论坛徽章:
- 4
|
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- int n = 0;
- int func(lua_State *L) {
- printf("func_top=%d top=%s\n", lua_gettop(L), lua_tostring(L, -1));
- if (!n) {
- ++ n;
- return lua_yield(L, 1);
- } else {
- return 1;
- }
- }
- int main(int argc, char* const argv[]) {
- lua_State *L = luaL_newstate();
- /* init lua library */
- lua_pushcfunction(L, luaopen_base);
- if (lua_pcall(L, 0, 0, 0) != 0) {
- return 1;
- }
- lua_pushcfunction(L, luaopen_package);
- if (lua_pcall(L, 0, 0, 0 ) != 0) {
- return 2;
- }
-
- /* create the coroutine */
- lua_State *LL = lua_newthread(L);
- lua_pushcfunction(LL, func);
- lua_pushstring(LL, "hello world");
-
- /* first time resume */
- if (lua_resume(LL, 1) == LUA_YIELD) {
- printf("first_top=%d top_string=%s\n", lua_gettop(LL), lua_tostring(LL, -1));
- /* twice resume */
- if (lua_resume(LL, 1) == 0) {
- printf("second_top=%d top_string=%s\n", lua_gettop(LL), lua_tostring(LL, -1));
- }
- }
- lua_close(L);
- return 0;
- }
复制代码 func_top=1 top=hello world
first_top=1 top_string=hello world
Segmentation fault (core dumped)
#0 0x000000000040e1d5 in luaV_execute ()
(gdb) bt
#0 0x000000000040e1d5 in luaV_execute ()
#1 0x00000000004059ae in luaD_rawrunprotected ()
#2 0x000000000040666c in lua_resume ()
#3 0x0000000000402bbc in main () |
|