lua-users home
lua-l archive

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


I either have a bug or something I dont understand.
It appears that every second lua_yield does nothing!.

The following code produces :

line 1  1
line 3  3
line 5  5
line 7  7
line 9  9
line 11 11
line 13 13
line 15 15
line 17 17
line 19 19
line 21 21
line 23 23
line 25 25


Code follows:

<test.lua>
for i=1,25 do
print(string.format("line %d",i),i)
end

<test.c>

static int l_print (lua_State *L) {
 int n = lua_gettop(L);  /* number of arguments */
 int i;
 lua_getglobal(L, "tostring");
 lua_pushnumber(L,3);
 for (i=1; i<=n; i++) {
   const char *s;
   if (i>1) {
     lua_pushliteral(L,"\t");
   }
   lua_pushvalue(L, n+1);  /* function to be called */
   lua_pushvalue(L, i);   /* value to print */
   lua_call(L, 1, 1);
   s = lua_tostring(L, -1);  /* get result */
   if (s == NULL)
     return luaL_error(L, LUA_QL("tostring") " must return a string to "
                          LUA_QL("print"));
 }
 lua_pushliteral(L,"\n");
 lua_concat(L,n*2);
 return lua_yield(L,2);
}

 <more code ...>


   do {
     status = lua_resume(L1, 0);
     if  (status == LUA_YIELD) {
       int nt = lua_gettop(L1) - 1;
       lua_remove(L1,1);
       switch (actcode) {
         default:
           fprintf(stderr, "actcode %d\n", actcode);
           break;
         case 2:
         case 3:
         {
           size_t lt=0;
           const char* s = lua_tolstring(L1,1,&lt);
           //if (s && lt)
           fwrite(s, (int)lt, 1, stderr);
           lua_remove(L1,1);
           status = lua_resume(L1, 0);
         }
         break;
       }
       if (status != LUA_YIELD) break;
     }
   } while (status == LUA_YIELD) ;


*The lua equivalent below works fine*

function print(...)
local n = select("#", ...)
local t = {}
for i = 1,n do
  if i > 1 then t[#t+1] = "\t" end
  t[#t+1] = select(i,...)
end
t[#t+1] = "\n"
coroutine.yield(1, table.concat(t))
end

f = loadfile("test.lua")

coro = coroutine.create(f)
repeat
 local st, n,s = coroutine.resume(coro)
 if (s) then io.stderr:write(tostring(s)) end
until coroutine.status(coro) ~= 'suspended'


(lua5.1)

David B.