diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-09 13:16:06 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-09 13:16:06 -0200 |
commit | b1379936cf35787d3ef3aab82d1607a3e1562eef (patch) | |
tree | fe47cb5c35fddab945faf731f0bc175bf5431352 /ltm.c | |
parent | 4e0de3a43cc30a83334c272cb7575bf8412bfeae (diff) | |
download | lua-b1379936cf35787d3ef3aab82d1607a3e1562eef.tar.gz lua-b1379936cf35787d3ef3aab82d1607a3e1562eef.tar.bz2 lua-b1379936cf35787d3ef3aab82d1607a3e1562eef.zip |
vararg back to '...' (but with another implementation)
new implementation should have zero overhead for non-vararg functions
Diffstat (limited to 'ltm.c')
-rw-r--r-- | ltm.c | 55 |
1 files changed, 23 insertions, 32 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltm.c,v 2.58 2018/01/28 13:39:52 roberto Exp roberto $ | 2 | ** $Id: ltm.c,v 2.59 2018/02/07 15:18:04 roberto Exp roberto $ |
3 | ** Tag methods | 3 | ** Tag methods |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -216,41 +216,32 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, | |||
216 | } | 216 | } |
217 | 217 | ||
218 | 218 | ||
219 | void luaT_adjustvarargs (lua_State *L, int nfixparams, StkId base) { | 219 | void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci) { |
220 | int i; | 220 | int i; |
221 | Table *vtab; | 221 | int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ |
222 | TValue nname; | ||
223 | int actual = cast_int(L->top - base); /* number of arguments */ | ||
224 | int nextra = actual - nfixparams; /* number of extra arguments */ | 222 | int nextra = actual - nfixparams; /* number of extra arguments */ |
225 | vtab = luaH_new(L); /* create vararg table */ | 223 | ci->u.l.nextraargs = nextra; |
226 | sethvalue2s(L, L->top, vtab); /* anchor it for resizing */ | 224 | checkstackGC(L, nfixparams + 1); |
227 | L->top++; /* space ensured by caller */ | 225 | /* copy function and fixed parameters to the top of the stack */ |
228 | luaH_resize(L, vtab, nextra, 1); | 226 | for (i = 0; i <= nfixparams; i++) { |
229 | for (i = 0; i < nextra; i++) /* put extra arguments into vararg table */ | 227 | setobjs2s(L, L->top++, ci->func + i); |
230 | setobj2n(L, &vtab->array[i], s2v(L->top - nextra + i - 1)); | 228 | setnilvalue(s2v(ci->func + i)); /* erase original copy (for GC) */ |
231 | setsvalue(L, &nname, G(L)->nfield); /* get field 'n' */ | 229 | } |
232 | setivalue(luaH_set(L, vtab, &nname), nextra); /* store counter there */ | 230 | ci->func += actual + 1; |
233 | L->top -= nextra; /* remove extra elements from the stack */ | 231 | ci->top += actual + 1; |
234 | sethvalue2s(L, L->top - 1, vtab); /* move table to new top */ | ||
235 | luaC_checkGC(L); | ||
236 | } | 232 | } |
237 | 233 | ||
238 | 234 | ||
239 | void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) { | 235 | void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { |
240 | if (!ttistable(t)) | 236 | int i; |
241 | luaG_runerror(L, "'vararg' parameter is not a table"); | 237 | int nextra = ci->u.l.nextraargs; |
242 | else { | 238 | if (wanted < 0) { |
243 | int i; | 239 | wanted = nextra; /* get all extra arguments available */ |
244 | Table *h = hvalue(t); | 240 | checkstackp(L, nextra, where); /* ensure stack space */ |
245 | if (wanted < 0) { /* get all? */ | 241 | L->top = where + nextra; /* next instruction will need top */ |
246 | const TValue *ns = luaH_getstr(h, G(L)->nfield); | ||
247 | int n = (ttisinteger(ns)) ? cast_int(ivalue(ns)) : 0; | ||
248 | wanted = n; | ||
249 | checkstackp(L, n, where); | ||
250 | L->top = where + n; | ||
251 | } | ||
252 | for (i = 0; i < wanted; i++) /* get what is available */ | ||
253 | setobj2s(L, where + i, luaH_getint(h, i + 1)); | ||
254 | return; | ||
255 | } | 242 | } |
243 | for (i = 0; i < wanted && i < nextra; i++) | ||
244 | setobjs2s(L, where + i, ci->func - nextra + i); | ||
245 | for (; i < wanted; i++) /* complete required results with nil */ | ||
246 | setnilvalue(s2v(where + i)); | ||
256 | } | 247 | } |