seqlua
diff 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 diff
1.1 --- a/seqlua.c Thu Aug 21 13:04:45 2014 +0200 1.2 +++ b/seqlua.c Thu Aug 21 20:01:52 2014 +0200 1.3 @@ -33,41 +33,13 @@ 1.4 } 1.5 } 1.6 1.7 -static int seqlua_ipairsaux_triplet(lua_State *L) { 1.8 - lua_Integer i = lua_tointeger(L, lua_upvalueindex(4)) + 1; 1.9 - lua_settop(L, 0); 1.10 - lua_pushinteger(L, i); 1.11 - lua_replace(L, lua_upvalueindex(4)); 1.12 - lua_pushinteger(L, i); 1.13 - lua_pushvalue(L, lua_upvalueindex(1)); 1.14 - lua_pushvalue(L, lua_upvalueindex(2)); 1.15 - lua_pushvalue(L, lua_upvalueindex(3)); 1.16 - lua_call(L, 2, LUA_MULTRET); 1.17 - if (lua_isnoneornil(L, 2)) { 1.18 - lua_settop(L, 0); 1.19 - lua_pushnil(L); 1.20 - return 1; 1.21 - } else { 1.22 - lua_pushvalue(L, 2); 1.23 - lua_replace(L, lua_upvalueindex(3)); 1.24 - return lua_gettop(L); 1.25 - } 1.26 -} 1.27 - 1.28 static int seqlua_ipairs(lua_State *L) { 1.29 luaL_checkany(L, 1); // provides better error message 1.30 - if (lua_type(L, 1) == LUA_TFUNCTION) { 1.31 - seqlua_ipairs_function: 1.32 - if (!lua_isnoneornil(L, 2) || !lua_isnoneornil(L, 3)) { 1.33 - lua_settop(L, 3); 1.34 - lua_pushinteger(L, 0); 1.35 - lua_pushcclosure(L, seqlua_ipairsaux_triplet, 4); 1.36 - return 1; 1.37 - } 1.38 + if ( 1.39 + lua_type(L, 1) == LUA_TFUNCTION || 1.40 + (luaL_getmetafield(L, 1, "__call") && (lua_pop(L, 1), 1)) 1.41 + ) { 1.42 lua_pushcfunction(L, seqlua_ipairsaux_func); 1.43 - } else if (luaL_getmetafield(L, 1, "__call")) { 1.44 - lua_pop(L, 1); 1.45 - goto seqlua_ipairs_function; 1.46 } else if (luaL_getmetafield(L, 1, "__index")) { 1.47 lua_pushcfunction(L, seqlua_ipairsaux_meta); 1.48 } else { 1.49 @@ -79,70 +51,8 @@ 1.50 return 3; 1.51 } 1.52 1.53 -static int seqlua_iteratoraux_raw(lua_State *L) { 1.54 - lua_Integer i = lua_tointeger(L, lua_upvalueindex(2)) + 1; 1.55 - lua_rawgeti(L, lua_upvalueindex(1), i); 1.56 - if (lua_isnil(L, -1)) return 1; 1.57 - lua_pushinteger(L, i); 1.58 - lua_replace(L, lua_upvalueindex(2)); 1.59 - return 1; 1.60 -} 1.61 - 1.62 -static int seqlua_iteratoraux_meta(lua_State *L) { 1.63 - lua_Integer i = lua_tointeger(L, lua_upvalueindex(2)) + 1; 1.64 - lua_pushinteger(L, i); 1.65 - lua_gettable(L, lua_upvalueindex(1)); 1.66 - if (lua_isnil(L, -1)) return 1; 1.67 - lua_pushinteger(L, i); 1.68 - lua_replace(L, lua_upvalueindex(2)); 1.69 - return 1; 1.70 -} 1.71 - 1.72 -static int seqlua_iteratoraux_triplet(lua_State *L) { 1.73 - lua_settop(L, 0); 1.74 - lua_pushvalue(L, lua_upvalueindex(1)); 1.75 - lua_pushvalue(L, lua_upvalueindex(2)); 1.76 - lua_pushvalue(L, lua_upvalueindex(3)); 1.77 - lua_call(L, 2, LUA_MULTRET); 1.78 - if (lua_isnoneornil(L, 1)) { 1.79 - lua_settop(L, 1); 1.80 - return 1; 1.81 - } 1.82 - lua_pushvalue(L, 1); 1.83 - lua_replace(L, lua_upvalueindex(3)); 1.84 - return lua_gettop(L); 1.85 -} 1.86 - 1.87 -int seqlua_iterator(lua_State *L) { 1.88 - luaL_checkany(L, 1); // provides better error message 1.89 - lua_settop(L, 3); 1.90 - if (lua_type(L, 1) == LUA_TFUNCTION) { 1.91 - seqlua_iterator_function: 1.92 - if (lua_isnil(L, 2) && lua_isnil(L, 3)) { 1.93 - lua_settop(L, 1); 1.94 - } else { 1.95 - lua_pushcclosure(L, seqlua_iteratoraux_triplet, 3); 1.96 - } 1.97 - } else if (luaL_getmetafield(L, 1, "__call")) { 1.98 - lua_pop(L, 1); 1.99 - goto seqlua_iterator_function; 1.100 - } else if (luaL_getmetafield(L, 1, "__index")) { 1.101 - lua_pushvalue(L, 1); 1.102 - lua_pushinteger(L, 0); 1.103 - lua_pushcclosure(L, seqlua_iteratoraux_meta, 2); 1.104 - } else { 1.105 - luaL_checktype(L, 1, LUA_TTABLE); 1.106 - lua_pushvalue(L, 1); 1.107 - lua_pushinteger(L, 0); 1.108 - lua_pushcclosure(L, seqlua_iteratoraux_raw, 2); 1.109 - } 1.110 - return 1; 1.111 -} 1.112 - 1.113 int luaopen_seqlua(lua_State *L) { 1.114 lua_pushcfunction(L, seqlua_ipairs); 1.115 lua_setglobal(L, "ipairs"); 1.116 - lua_pushcfunction(L, seqlua_iterator); 1.117 - lua_setglobal(L, "iterator"); 1.118 return 1; 1.119 }