seqlua

view seqlua.c @ 17:12a7a8f5a77d

Bugfix: switched raw and meta helper functions
author jbe
date Wed Aug 20 06:40:46 2014 +0200 (2014-08-20)
parents 040a0ca089c1
children 29792283522f
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_ipairsaux_triplet(lua_State *L) {
37 lua_Integer i = lua_tointeger(L, lua_upvalueindex(4)) + 1;
38 lua_settop(L, 0);
39 lua_pushinteger(L, i);
40 lua_replace(L, lua_upvalueindex(4));
41 lua_pushinteger(L, i);
42 lua_pushvalue(L, lua_upvalueindex(1));
43 lua_pushvalue(L, lua_upvalueindex(2));
44 lua_pushvalue(L, lua_upvalueindex(3));
45 lua_call(L, 2, LUA_MULTRET);
46 if (lua_isnoneornil(L, 2)) {
47 lua_settop(L, 0);
48 lua_pushnil(L);
49 return 1;
50 } else {
51 lua_pushvalue(L, 2);
52 lua_replace(L, lua_upvalueindex(3));
53 return lua_gettop(L);
54 }
55 }
57 static int seqlua_ipairs(lua_State *L) {
58 luaL_checkany(L, 1); // provides better error message
59 if (lua_type(L, 1) == LUA_TFUNCTION) {
60 seqlua_ipairs_function:
61 if (!lua_isnoneornil(L, 2) || !lua_isnoneornil(L, 3)) {
62 lua_settop(L, 3);
63 lua_pushinteger(L, 0);
64 lua_pushcclosure(L, seqlua_ipairsaux_triplet, 4);
65 return 1;
66 }
67 lua_pushcfunction(L, seqlua_ipairsaux_func);
68 } else if (luaL_getmetafield(L, 1, "__call")) {
69 lua_pop(L, 1);
70 goto seqlua_ipairs_function;
71 } else if (luaL_getmetafield(L, 1, "__index")) {
72 lua_pushcfunction(L, seqlua_ipairsaux_meta);
73 } else {
74 luaL_checktype(L, 1, LUA_TTABLE);
75 lua_pushcfunction(L, seqlua_ipairsaux_raw);
76 }
77 lua_pushvalue(L, 1);
78 lua_pushinteger(L, 0);
79 return 3;
80 }
82 static int seqlua_iteratoraux_raw(lua_State *L) {
83 lua_Integer i = lua_tointeger(L, lua_upvalueindex(2)) + 1;
84 lua_rawgeti(L, lua_upvalueindex(1), i);
85 if (lua_isnil(L, -1)) return 1;
86 lua_pushinteger(L, i);
87 lua_replace(L, lua_upvalueindex(2));
88 return 1;
89 }
91 static int seqlua_iteratoraux_meta(lua_State *L) {
92 lua_Integer i = lua_tointeger(L, lua_upvalueindex(2)) + 1;
93 lua_pushinteger(L, i);
94 lua_gettable(L, lua_upvalueindex(1));
95 if (lua_isnil(L, -1)) return 1;
96 lua_pushinteger(L, i);
97 lua_replace(L, lua_upvalueindex(2));
98 return 1;
99 }
101 static int seqlua_iteratoraux_triplet(lua_State *L) {
102 lua_settop(L, 0);
103 lua_pushvalue(L, lua_upvalueindex(1));
104 lua_pushvalue(L, lua_upvalueindex(2));
105 lua_pushvalue(L, lua_upvalueindex(3));
106 lua_call(L, 2, LUA_MULTRET);
107 if (lua_isnoneornil(L, 1)) {
108 lua_settop(L, 1);
109 return 1;
110 }
111 lua_pushvalue(L, 1);
112 lua_replace(L, lua_upvalueindex(3));
113 return lua_gettop(L);
114 }
116 int seqlua_iterator(lua_State *L) {
117 luaL_checkany(L, 1); // provides better error message
118 lua_settop(L, 3);
119 if (lua_type(L, 1) == LUA_TFUNCTION) {
120 seqlua_iterator_function:
121 if (lua_isnil(L, 2) && lua_isnil(L, 3)) {
122 lua_settop(L, 1);
123 } else {
124 lua_pushcclosure(L, seqlua_iteratoraux_triplet, 3);
125 }
126 } else if (luaL_getmetafield(L, 1, "__call")) {
127 lua_pop(L, 1);
128 goto seqlua_iterator_function;
129 } else if (luaL_getmetafield(L, 1, "__index")) {
130 lua_pushvalue(L, 1);
131 lua_pushinteger(L, 0);
132 lua_pushcclosure(L, seqlua_iteratoraux_meta, 2);
133 } else {
134 luaL_checktype(L, 1, LUA_TTABLE);
135 lua_pushvalue(L, 1);
136 lua_pushinteger(L, 0);
137 lua_pushcclosure(L, seqlua_iteratoraux_raw, 2);
138 }
139 return 1;
140 }
142 int luaopen_seqlua(lua_State *L) {
143 lua_pushcfunction(L, seqlua_ipairs);
144 lua_setglobal(L, "ipairs");
145 lua_pushcfunction(L, seqlua_iterator);
146 lua_setglobal(L, "iterator");
147 return 1;
148 }

Impressum / About Us