seqlua

diff seqlua.c @ 32:3ff7cec8d3ce

Do not respect __len metamethods
author jbe
date Sun Aug 24 00:00:43 2014 +0200 (2014-08-24)
parents 367fc70acc15
children 2a43e8ffbced
line diff
     1.1 --- a/seqlua.c	Sat Aug 23 23:35:10 2014 +0200
     1.2 +++ b/seqlua.c	Sun Aug 24 00:00:43 2014 +0200
     1.3 @@ -2,12 +2,21 @@
     1.4  #include <lauxlib.h>
     1.5  #include "seqlualib.h"
     1.6  
     1.7 +static int seqlua_ipairsaux_raw(lua_State *L) {
     1.8 +  lua_Integer i;
     1.9 +  luaL_checktype(L, 1, LUA_TTABLE);
    1.10 +  i = luaL_checkinteger(L, 2) + 1;
    1.11 +  lua_pushinteger(L, i);
    1.12 +  lua_rawgeti(L, 1, i);  // TODO: Lua 5.3 returns type
    1.13 +  return lua_isnil(L, -1) ? 1 : 2;
    1.14 +}
    1.15 +
    1.16  static int seqlua_ipairsaux_index(lua_State *L) {
    1.17    lua_Integer i = luaL_checkinteger(L, 2) + 1;
    1.18    lua_pushinteger(L, i);
    1.19    lua_pushinteger(L, i);
    1.20    lua_gettable(L, 1);  // TODO: Lua 5.3 returns type
    1.21 -  return lua_isnil(L, -1) && i > luaL_len(L, 1) ? 1 : 2;
    1.22 +  return lua_isnil(L, -1) ? 1 : 2;
    1.23  }
    1.24  
    1.25  static int seqlua_ipairsaux_call(lua_State *L) {
    1.26 @@ -32,10 +41,10 @@
    1.27      int t = lua_type(L, 1);
    1.28      if (t == LUA_TFUNCTION) {
    1.29        lua_pushcfunction(L, seqlua_ipairsaux_call);
    1.30 +    } else if (luaL_getmetafield(L, 1, "__index")) {
    1.31 +      lua_pushcfunction(L, seqlua_ipairsaux_index);
    1.32      } else {
    1.33 -      if (t != LUA_TTABLE && !luaL_getmetafield(L, 1, "__index")) {
    1.34 -        luaL_checktype(L, 1, LUA_TTABLE);
    1.35 -      }
    1.36 +      luaL_checktype(L, 1, LUA_TTABLE);
    1.37        lua_pushcfunction(L, seqlua_ipairsaux_index);
    1.38      }
    1.39      lua_pushvalue(L, 1);

Impressum / About Us