seqlua
diff seqlua.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 | 2a43e8ffbced |
| children | 3362ec36cb09 |
line diff
1.1 --- a/seqlua.c Sun Aug 24 22:21:46 2014 +0200 1.2 +++ b/seqlua.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 static int seqlua_ipairsaux_raw(lua_State *L) { 1.10 lua_Integer i; 1.11 @@ -37,6 +38,20 @@ 1.12 if (luaL_getmetafield(L, 1, "__ipairs")) { 1.13 lua_pushvalue(L, 1); 1.14 lua_call(L, 1, 3); 1.15 + if (lua_type(L, -3) == LUA_TSTRING) { 1.16 + const char *method = lua_tostring(L, -3); 1.17 + if (!strcmp(method, "raw")) { 1.18 + lua_pushcfunction(L, seqlua_ipairsaux_raw); 1.19 + } else if (!strcmp(method, "index")) { 1.20 + lua_pushcfunction(L, seqlua_ipairsaux_index); 1.21 + } else if (!strcmp(method, "call")) { 1.22 + lua_pushcfunction(L, seqlua_ipairsaux_call); 1.23 + } else { 1.24 + luaL_error(L, "Unexpected string returned by __ipairs metamethod"); 1.25 + } 1.26 + lua_pushvalue(L, -3); 1.27 + lua_pushinteger(L, 0); 1.28 + } 1.29 } else { 1.30 int t = lua_type(L, 1); 1.31 if (t == LUA_TFUNCTION) {