Lines Matching refs:L

33 #define aux_getn(L,n,w)	(checktab(L, n, (w) | TAB_L), luaL_len(L, n))  argument
36 static int checkfield (lua_State *L, const char *key, int n) { in checkfield() argument
37 lua_pushstring(L, key); in checkfield()
38 return (lua_rawget(L, -n) != LUA_TNIL); in checkfield()
46 static void checktab (lua_State *L, int arg, int what) { in checktab() argument
47 if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ in checktab()
49 if (lua_getmetatable(L, arg) && /* must have metatable */ in checktab()
50 (!(what & TAB_R) || checkfield(L, "__index", ++n)) && in checktab()
51 (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && in checktab()
52 (!(what & TAB_L) || checkfield(L, "__len", ++n))) { in checktab()
53 lua_pop(L, n); /* pop metatable and tested metamethods */ in checktab()
56 luaL_checktype(L, arg, LUA_TTABLE); /* force an error */ in checktab()
61 static int tinsert (lua_State *L) { in tinsert() argument
62 lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ in tinsert()
64 switch (lua_gettop(L)) { in tinsert()
71 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ in tinsert()
73 luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2, in tinsert()
76 lua_geti(L, 1, i - 1); in tinsert()
77 lua_seti(L, 1, i); /* t[i] = t[i - 1] */ in tinsert()
82 return luaL_error(L, "wrong number of arguments to 'insert'"); in tinsert()
85 lua_seti(L, 1, pos); /* t[pos] = v */ in tinsert()
90 static int tremove (lua_State *L) { in tremove() argument
91 lua_Integer size = aux_getn(L, 1, TAB_RW); in tremove()
92 lua_Integer pos = luaL_optinteger(L, 2, size); in tremove()
95 luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1, in tremove()
97 lua_geti(L, 1, pos); /* result = t[pos] */ in tremove()
99 lua_geti(L, 1, pos + 1); in tremove()
100 lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ in tremove()
102 lua_pushnil(L); in tremove()
103 lua_seti(L, 1, pos); /* remove entry t[pos] */ in tremove()
114 static int tmove (lua_State *L) { in tmove() argument
115 lua_Integer f = luaL_checkinteger(L, 2); in tmove()
116 lua_Integer e = luaL_checkinteger(L, 3); in tmove()
117 lua_Integer t = luaL_checkinteger(L, 4); in tmove()
118 int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ in tmove()
119 checktab(L, 1, TAB_R); in tmove()
120 checktab(L, tt, TAB_W); in tmove()
123 luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, in tmove()
126 luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, in tmove()
128 if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { in tmove()
130 lua_geti(L, 1, f + i); in tmove()
131 lua_seti(L, tt, t + i); in tmove()
136 lua_geti(L, 1, f + i); in tmove()
137 lua_seti(L, tt, t + i); in tmove()
141 lua_pushvalue(L, tt); /* return destination table */ in tmove()
146 static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { in addfield() argument
147 lua_geti(L, 1, i); in addfield()
148 if (!lua_isstring(L, -1)) in addfield()
149 luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", in addfield()
150 luaL_typename(L, -1), i); in addfield()
155 static int tconcat (lua_State *L) { in tconcat() argument
157 lua_Integer last = aux_getn(L, 1, TAB_R); in tconcat()
159 const char *sep = luaL_optlstring(L, 2, "", &lsep); in tconcat()
160 lua_Integer i = luaL_optinteger(L, 3, 1); in tconcat()
161 last = luaL_optinteger(L, 4, last); in tconcat()
162 luaL_buffinit(L, &b); in tconcat()
164 addfield(L, &b, i); in tconcat()
168 addfield(L, &b, i); in tconcat()
180 static int tpack (lua_State *L) { in tpack() argument
182 int n = lua_gettop(L); /* number of elements to pack */ in tpack()
183 lua_createtable(L, n, 1); /* create result table */ in tpack()
184 lua_insert(L, 1); /* put it at index 1 */ in tpack()
186 lua_seti(L, 1, i); in tpack()
187 lua_pushinteger(L, n); in tpack()
188 lua_setfield(L, 1, "n"); /* t.n = number of elements */ in tpack()
193 static int tunpack (lua_State *L) { in tunpack() argument
195 lua_Integer i = luaL_optinteger(L, 2, 1); in tunpack()
196 lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); in tunpack()
199 if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) in tunpack()
200 return luaL_error(L, "too many results to unpack"); in tunpack()
202 lua_geti(L, 1, i); in tunpack()
204 lua_geti(L, 1, e); /* push last element */ in tunpack()
263 static void set2 (lua_State *L, IdxT i, IdxT j) { in set2() argument
264 lua_seti(L, 1, i); in set2()
265 lua_seti(L, 1, j); in set2()
273 static int sort_comp (lua_State *L, int a, int b) { in sort_comp() argument
274 if (lua_isnil(L, 2)) /* no function? */ in sort_comp()
275 return lua_compare(L, a, b, LUA_OPLT); /* a < b */ in sort_comp()
278 lua_pushvalue(L, 2); /* push function */ in sort_comp()
279 lua_pushvalue(L, a-1); /* -1 to compensate function */ in sort_comp()
280 lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ in sort_comp()
281 lua_call(L, 2, 1); /* call function */ in sort_comp()
282 res = lua_toboolean(L, -1); /* get result */ in sort_comp()
283 lua_pop(L, 1); /* pop result */ in sort_comp()
296 static IdxT partition (lua_State *L, IdxT lo, IdxT up) { in partition() argument
302 while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { in partition()
304 luaL_error(L, "invalid order function for sorting"); in partition()
305 lua_pop(L, 1); /* remove a[i] */ in partition()
309 while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { in partition()
311 luaL_error(L, "invalid order function for sorting"); in partition()
312 lua_pop(L, 1); /* remove a[j] */ in partition()
317 lua_pop(L, 1); /* pop a[j] */ in partition()
319 set2(L, up - 1, i); in partition()
323 set2(L, i, j); in partition()
343 static void auxsort (lua_State *L, IdxT lo, IdxT up, in auxsort() argument
349 lua_geti(L, 1, lo); in auxsort()
350 lua_geti(L, 1, up); in auxsort()
351 if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ in auxsort()
352 set2(L, lo, up); /* swap a[lo] - a[up] */ in auxsort()
354 lua_pop(L, 2); /* remove both values */ in auxsort()
361 lua_geti(L, 1, p); in auxsort()
362 lua_geti(L, 1, lo); in auxsort()
363 if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ in auxsort()
364 set2(L, p, lo); /* swap a[p] - a[lo] */ in auxsort()
366 lua_pop(L, 1); /* remove a[lo] */ in auxsort()
367 lua_geti(L, 1, up); in auxsort()
368 if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ in auxsort()
369 set2(L, p, up); /* swap a[up] - a[p] */ in auxsort()
371 lua_pop(L, 2); in auxsort()
375 lua_geti(L, 1, p); /* get middle element (Pivot) */ in auxsort()
376 lua_pushvalue(L, -1); /* push Pivot */ in auxsort()
377 lua_geti(L, 1, up - 1); /* push a[up - 1] */ in auxsort()
378 set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ in auxsort()
379 p = partition(L, lo, up); in auxsort()
382 auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */ in auxsort()
387 auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */ in auxsort()
397 static int sort (lua_State *L) { in sort() argument
398 lua_Integer n = aux_getn(L, 1, TAB_RW); in sort()
400 luaL_argcheck(L, n < INT_MAX, 1, "array too big"); in sort()
401 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ in sort()
402 luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ in sort()
403 lua_settop(L, 2); /* make sure there are two arguments */ in sort()
404 auxsort(L, 1, (IdxT)n, 0); in sort()
424 LUAMOD_API int luaopen_table (lua_State *L) { in luaopen_table() argument
425 luaL_newlib(L, tab_funcs); in luaopen_table()