seqlua

view seqlua.c @ 23:29792283522f

Removed iterator(...) function; ipairs doesn't accept iterator triplets anymore
author jbe
date Thu Aug 21 20:01:52 2014 +0200 (2014-08-21)
parents 12a7a8f5a77d
children 44880bcfc323
line source
1 #include <lua.h>
2 #include <lauxlib.h>
4 static int seqlua_ipairsaux_raw(lua_State *L) {
5 lua_Integer i;
6 luaL_checktype(L, 1, LUA_TTABLE);
7 i = luaL_checkinteger(L, 2) + 1;
8 lua_pushinteger(L, i);
9 lua_rawgeti(L, 1, i); // TODO: Lua 5.3 returns type
10 return lua_isnil(L, -1) ? 1 : 2;
11 }
13 static int seqlua_ipairsaux_meta(lua_State *L) {
14 lua_Integer i;
15 i = luaL_checkinteger(L, 2) + 1;
16 lua_pushinteger(L, i);
17 lua_pushinteger(L, i);
18 lua_gettable(L, 1); // TODO: Lua 5.3 returns type
19 return lua_isnil(L, -1) ? 1 : 2;
20 }
22 static int seqlua_ipairsaux_func(lua_State *L) {
23 lua_pushinteger(L, luaL_checkinteger(L, 2) + 1);
24 lua_insert(L, 1);
25 lua_settop(L, 2);
26 lua_call(L, 0, LUA_MULTRET);
27 if (lua_isnoneornil(L, 2)) {
28 lua_settop(L, 0);
29 lua_pushnil(L);
30 return 1;
31 } else {
32 return lua_gettop(L);
33 }
34 }
36 static int seqlua_ipairs(lua_State *L) {
37 luaL_checkany(L, 1); // provides better error message
38 if (
39 lua_type(L, 1) == LUA_TFUNCTION ||
40 (luaL_getmetafield(L, 1, "__call") && (lua_pop(L, 1), 1))
41 ) {
42 lua_pushcfunction(L, seqlua_ipairsaux_func);
43 } else if (luaL_getmetafield(L, 1, "__index")) {
44 lua_pushcfunction(L, seqlua_ipairsaux_meta);
45 } else {
46 luaL_checktype(L, 1, LUA_TTABLE);
47 lua_pushcfunction(L, seqlua_ipairsaux_raw);
48 }
49 lua_pushvalue(L, 1);
50 lua_pushinteger(L, 0);
51 return 3;
52 }
54 int luaopen_seqlua(lua_State *L) {
55 lua_pushcfunction(L, seqlua_ipairs);
56 lua_setglobal(L, "ipairs");
57 return 1;
58 }

Impressum / About Us