seqlua

annotate 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
rev   line source
jbe@0 1 #include <lua.h>
jbe@0 2 #include <lauxlib.h>
jbe@0 3 #include "seqlualib.h"
jbe@0 4 #include <stdio.h>
jbe@0 5
jbe@0 6 static int seqlua_c_example_printcsv(lua_State *L) {
jbe@0 7 seqlua_Iterator iter;
jbe@0 8 seqlua_iterloop(L, &iter, 1) {
jbe@0 9 if (seqlua_itercount(&iter) > 1) fputs(",", stdout);
jbe@0 10 fputs(luaL_tolstring(L, -1, NULL), stdout);
jbe@8 11 lua_pop(L, 1); // pop value that luaL_tolstring pushed onto stack
jbe@0 12 }
jbe@0 13 fputs("\n", stdout);
jbe@0 14 return 0;
jbe@0 15 }
jbe@0 16
jbe@8 17 static int seqlua_c_example_printthree(lua_State *L) {
jbe@8 18 int i;
jbe@8 19 seqlua_iterclosure(L, 1);
jbe@8 20 for (i=0; i<3; i++) {
jbe@8 21 lua_pushvalue(L, 1);
jbe@8 22 lua_call(L, 0, 1);
jbe@8 23 fputs(luaL_tolstring(L, -1, NULL), stdout);
jbe@8 24 lua_pop(L, 1); // pop value that luaL_tolstring pushed onto stack
jbe@8 25 fputs("\n", stdout);
jbe@8 26 }
jbe@8 27 return 0;
jbe@8 28 }
jbe@8 29
jbe@0 30 int luaopen_seqlua_c_example(lua_State *L) {
jbe@0 31 lua_pushcfunction(L, seqlua_c_example_printcsv);
jbe@0 32 lua_setglobal(L, "printcsv");
jbe@8 33 lua_pushcfunction(L, seqlua_c_example_printthree);
jbe@8 34 lua_setglobal(L, "printthree");
jbe@0 35 return 0;
jbe@0 36 }

Impressum / About Us