From f543ff65fc6a5c44cc939555f92fcf2aaa0a7e50 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sat, 6 May 2017 10:31:55 +0200 Subject: [PATCH] backport lua_len from 5.2 --- src/lj_api.c | 19 +++++++++++++++++++ src/lua.h | 1 + 2 files changed, 20 insertions(+) diff --git a/src/lj_api.c b/src/lj_api.c index 84508d7d..35698cc4 100644 --- a/src/lj_api.c +++ b/src/lj_api.c @@ -806,6 +806,25 @@ LUA_API void lua_concat(lua_State *L, int n) /* else n == 1: nothing to do. */ } +LUA_API void lua_len (lua_State *L, int idx) +{ + TValue *o = index2adr_stack(L, idx); + if (tvisstr(o)) { + setnumV(L->top, strV(o)->len); + } else if (tvistab(o) && (!LJ_52 || tvisnil(lj_meta_lookup(L, o, MM_len)))) { + setnumV(L->top, lj_tab_len(tabV(o))); + } else { + TValue *v; + L->top = lj_meta_len(L, o); + L->top += 2; + lj_vm_call(L, L->top-2, 1+1); + L->top -= 2+LJ_FR2; + v = L->top+1+LJ_FR2; + copyTV(L, L->top, v); + } + incr_top(L); +} + /* -- Object getters ------------------------------------------------------ */ LUA_API void lua_gettable(lua_State *L, int idx) diff --git a/src/lua.h b/src/lua.h index 2a548c5a..e6b74d0f 100644 --- a/src/lua.h +++ b/src/lua.h @@ -353,6 +353,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx); LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum); LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum); LUA_API size_t lua_rawlen (lua_State *L, int idx); +LUA_API void lua_len (lua_State *L, int idx); /* From Lua 5.3. */ LUA_API int lua_isyieldable (lua_State *L);