lua-users home
lua-l archive

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


Luiz Henrique de Figueiredo wrote:
[...]
> Thanks to everyone for the reports.

This came up from a failing luabind test. This works in Lua 5.1:

  extern "C" {
  #include "lua.h"
  #include "lauxlib.h"
  #include "lualib.h"
  }

  #include <cassert>
  #include <iostream>

  int yielding(lua_State* L)
  {
      int n = lua_tonumber(L, 1);
      lua_pushnumber(L, n); // REMOVE THIS AND IT WORKS
      return lua_yield(L, 1);
  }

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

      lua_State* t = lua_newthread(L);

      lua_pushcclosure(L, &yielding, 0);
      lua_setglobal(L, "yielding");

      if (luaL_dostring(t,
          "function f()\n"
          "  assert(yielding(1) == 1)\n"
          "  assert(yielding(2) == 2)\n"
          "  return 3\n"
          "end"))
      {
          std::cout << lua_tostring(t, -1) << "\n";
          lua_close(L);
          return 1;
      }

      lua_getglobal(t, "f");

      assert(lua_resume(t, 0) == LUA_YIELD);
      assert(lua_gettop(t) == 1);
      assert(lua_tonumber(t, -1) == 1);
      lua_pop(t, 1);

      lua_pushnumber(t, 1);
      assert(lua_resume(t, 1) == LUA_YIELD);
      assert(lua_gettop(t) == 1);
      assert(lua_tonumber(t, -1) == 2);
      lua_pop(t, 1);

      lua_pushnumber(t, 2);
      assert(lua_resume(t, 1) == 0);
      assert(lua_gettop(t) == 1);
      assert(lua_tonumber(t, -1) == 3);
      lua_pop(t, 1);

      lua_close(L);
  }

lua_resume() seems to prepend the function calling yield to the results,
 so that for instance:

  yielding(1) == <function &yielding>, 1

Note that it only happens if the "yielding()" function above modifies
the stack before returning.

-- 
Daniel Wallin
BoostPro Computing
http://www.boostpro.com