seqlua
diff seqlualib.c @ 35:332216604f83
Support special return values of __ipairs metamethod (string as first return value)
author | jbe |
---|---|
date | Sun Aug 24 22:24:19 2014 +0200 (2014-08-24) |
parents | 3ff7cec8d3ce |
children | 3362ec36cb09 |
line diff
1.1 --- a/seqlualib.c Sun Aug 24 22:21:46 2014 +0200 1.2 +++ b/seqlualib.c Sun Aug 24 22:24:19 2014 +0200 1.3 @@ -1,6 +1,7 @@ 1.4 #include <lua.h> 1.5 #include <lauxlib.h> 1.6 #include "seqlualib.h" 1.7 +#include <string.h> 1.8 1.9 #define SEQLUA_ITERTYPE_IPAIRS 1 1.10 #define SEQLUA_ITERTYPE_CALL 2 1.11 @@ -11,7 +12,21 @@ 1.12 if (luaL_getmetafield(L, idx, "__ipairs")) { 1.13 lua_pushvalue(L, idx); 1.14 lua_call(L, 1, 3); 1.15 - iter->itertype = SEQLUA_ITERTYPE_IPAIRS; 1.16 + if (lua_type(L, -3) == LUA_TSTRING) { 1.17 + const char *method = lua_tostring(L, -3); 1.18 + if (!strcmp(method, "raw")) { 1.19 + iter->itertype = SEQLUA_ITERTYPE_RAW; 1.20 + } else if (!strcmp(method, "index")) { 1.21 + iter->itertype = SEQLUA_ITERTYPE_INDEX; 1.22 + } else if (!strcmp(method, "call")) { 1.23 + iter->itertype = SEQLUA_ITERTYPE_CALL; 1.24 + } else { 1.25 + luaL_error(L, "Unexpected string returned by __ipairs metamethod"); 1.26 + } 1.27 + iter->idx = lua_gettop(L) - 1; 1.28 + } else { 1.29 + iter->itertype = SEQLUA_ITERTYPE_IPAIRS; 1.30 + } 1.31 } else { 1.32 if (lua_type(L, idx) == LUA_TFUNCTION) { 1.33 iter->itertype = SEQLUA_ITERTYPE_CALL;