seqlua

diff seqlualib.c @ 0:47f9b323d68c

Initial commit
author jbe
date Wed Aug 20 00:39:10 2014 +0200 (2014-08-20)
parents
children 7b05ca6ef925
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/seqlualib.c	Wed Aug 20 00:39:10 2014 +0200
     1.3 @@ -0,0 +1,92 @@
     1.4 +#include <lua.h>
     1.5 +#include <lauxlib.h>
     1.6 +#include "seqlualib.h"
     1.7 +
     1.8 +#define SEQLUA_ITERTYPE_FUNC 1
     1.9 +#define SEQLUA_ITERTYPE_META 2
    1.10 +#define SEQLUA_ITERTYPE_RAW  3
    1.11 +
    1.12 +void seqlua_iterinit(lua_State *L, seqlua_Iterator *iter, int idx) {
    1.13 +  luaL_checkany(L, idx);  // provides better error message
    1.14 +  iter->L = L;
    1.15 +  if (luaL_getmetafield(L, idx, "__call")) {
    1.16 +    if (lua_type(L, -1) != LUA_TFUNCTION) luaL_error(L, "__call is not a function");
    1.17 +    iter->itertype = SEQLUA_ITERTYPE_FUNC;
    1.18 +  } else if (lua_type(L, idx) == LUA_TFUNCTION) {
    1.19 +    lua_pushvalue(L, idx);
    1.20 +    iter->itertype = SEQLUA_ITERTYPE_FUNC;
    1.21 +  } else {
    1.22 +    if (luaL_getmetafield(L, idx, "__index")) {
    1.23 +      lua_pop(L, 1);
    1.24 +      iter->itertype = SEQLUA_ITERTYPE_META;
    1.25 +    } else {
    1.26 +      luaL_checktype(L, idx, LUA_TTABLE);
    1.27 +      iter->itertype = SEQLUA_ITERTYPE_RAW;
    1.28 +    }
    1.29 +    lua_pushvalue(L, idx);
    1.30 +  }
    1.31 +  iter->i = 0;
    1.32 +}
    1.33 +
    1.34 +int seqlua_iternext(seqlua_Iterator *iter) {
    1.35 +  lua_State *L = iter->L;
    1.36 +  lua_Integer i = ++iter->i;
    1.37 +  switch (iter->itertype) {
    1.38 +  case SEQLUA_ITERTYPE_FUNC:
    1.39 +    lua_pushvalue(L, -1);
    1.40 +    lua_call(L, 0, 1);
    1.41 +    break;
    1.42 +  case SEQLUA_ITERTYPE_META:
    1.43 +    lua_pushinteger(L, i);
    1.44 +    lua_gettable(L, -2);
    1.45 +    break;
    1.46 +  case SEQLUA_ITERTYPE_RAW:
    1.47 +    lua_rawgeti(L, -1, i);
    1.48 +  }
    1.49 +  if (lua_isnil(L, -1)) {
    1.50 +    lua_pop(L, 2);
    1.51 +    return 0;
    1.52 +  }
    1.53 +  return 1;
    1.54 +}
    1.55 +
    1.56 +static int seqlua_iterclosureaux_raw(lua_State *L) {
    1.57 +  lua_Integer i = lua_tointeger(L, lua_upvalueindex(2)) + 1;
    1.58 +  lua_rawgeti(L, lua_upvalueindex(1), i);
    1.59 +  if (lua_isnil(L, -1)) return 1;
    1.60 +  lua_pushinteger(L, i);
    1.61 +  lua_replace(L, lua_upvalueindex(2));
    1.62 +  return 1;
    1.63 +}
    1.64 +
    1.65 +static int seqlua_iterclosureaux_meta(lua_State *L) {
    1.66 +  lua_Integer i = lua_tointeger(L, lua_upvalueindex(2)) + 1;
    1.67 +  lua_pushinteger(L, i);
    1.68 +  lua_gettable(L, lua_upvalueindex(1));
    1.69 +  if (lua_isnil(L, -1)) return 1;
    1.70 +  lua_pushinteger(L, i);
    1.71 +  lua_replace(L, lua_upvalueindex(2));
    1.72 +  return 1;
    1.73 +}
    1.74 +
    1.75 +void seqlua_iterclosure(lua_State *L, int idx) {
    1.76 +  if (lua_type(L, idx) == LUA_TFUNCTION) {
    1.77 +    // do nothing
    1.78 +  } else if (luaL_getmetafield(L, idx, "__call")) {
    1.79 +    if (lua_type(L, -1) != LUA_TFUNCTION) luaL_error(L, "__call is not a function");
    1.80 +    lua_replace(L, idx);
    1.81 +  } else if (luaL_getmetafield(L, idx, "__index")) {
    1.82 +    lua_pop(L, 1);
    1.83 +    lua_pushvalue(L, idx);
    1.84 +    lua_pushinteger(L, 0);
    1.85 +    lua_pushcclosure(L, seqlua_iterclosureaux_raw, 2);
    1.86 +    lua_replace(L, idx);
    1.87 +  } else {
    1.88 +    luaL_checktype(L, idx, LUA_TTABLE);
    1.89 +    lua_pushvalue(L, idx);
    1.90 +    lua_pushinteger(L, 0);
    1.91 +    lua_pushcclosure(L, seqlua_iterclosureaux_meta, 2);
    1.92 +    lua_replace(L, idx);
    1.93 +  }
    1.94 +}
    1.95 +

Impressum / About Us