seqlua

diff seqlua_c_example.c @ 8:144f0bddee2b

Pass value as argument to __call metamethod
author jbe
date Wed Aug 20 04:24:08 2014 +0200 (2014-08-20)
parents 47f9b323d68c
children 29792283522f
line diff
     1.1 --- a/seqlua_c_example.c	Wed Aug 20 01:59:55 2014 +0200
     1.2 +++ b/seqlua_c_example.c	Wed Aug 20 04:24:08 2014 +0200
     1.3 @@ -8,14 +8,29 @@
     1.4    seqlua_iterloop(L, &iter, 1) {
     1.5      if (seqlua_itercount(&iter) > 1) fputs(",", stdout);
     1.6      fputs(luaL_tolstring(L, -1, NULL), stdout);
     1.7 -    lua_pop(L, 1);
     1.8 +    lua_pop(L, 1);  // pop value that luaL_tolstring pushed onto stack
     1.9    }
    1.10    fputs("\n", stdout);
    1.11    return 0;
    1.12  }
    1.13  
    1.14 +static int seqlua_c_example_printthree(lua_State *L) {
    1.15 +  int i;
    1.16 +  seqlua_iterclosure(L, 1);
    1.17 +  for (i=0; i<3; i++) {
    1.18 +    lua_pushvalue(L, 1);
    1.19 +    lua_call(L, 0, 1);
    1.20 +    fputs(luaL_tolstring(L, -1, NULL), stdout);
    1.21 +    lua_pop(L, 1);  // pop value that luaL_tolstring pushed onto stack
    1.22 +    fputs("\n", stdout);
    1.23 +  }
    1.24 +  return 0;
    1.25 +}
    1.26 +
    1.27  int luaopen_seqlua_c_example(lua_State *L) {
    1.28    lua_pushcfunction(L, seqlua_c_example_printcsv);
    1.29    lua_setglobal(L, "printcsv");
    1.30 +  lua_pushcfunction(L, seqlua_c_example_printthree);
    1.31 +  lua_setglobal(L, "printthree");
    1.32    return 0;
    1.33  }

Impressum / About Us