lua-users home
lua-l archive

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


I compile this source with the commands:

gcc -o trlua coroutine.c -I /usr/include/lua5.1/ -llua5.1
gcc -o trluajit coroutine.c -I /usr/include/lua5.1/ -lluajit5.1

Then I try to execute binaries:
bash$ ./trlua
YIELD
bash$ ./trluajit
ERRRUN
attempt to yield across metamethod/C-call boundary

Where did I do a mistake? Or is it a bug in a luajit?

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int main(void) {
  lua_State * L = luaL_newstate();
  lua_State * L1;
  luaL_openlibs(L);

  luaL_loadstring(L, "return function() coroutine.yield(1) end");
  lua_call(L, 0, 1);
  L1 = lua_newthread(L);
  lua_pushvalue(L, -2);
  lua_xmove(L, L1, 1);
  {
    int rescode = lua_resume(L1, 0);
    switch(rescode) {
      case 0:
        puts("OK");
        break;
      case LUA_YIELD:
        puts("YIELD");
        break;
      case LUA_ERRRUN:
        puts("ERRRUN");
        puts(lua_tostring(L1, -1));
        break;
      case LUA_ERRMEM:
        puts("ERRMEM");
        break;
      case LUA_ERRERR:
        puts("ERRERR");
        break;
    }
  }
  lua_close(L);
}