Lines Matching refs:L

19 #define aux_getn(L,n)	(luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))  argument
24 static int maxn (lua_State *L) { in maxn() argument
26 luaL_checktype(L, 1, LUA_TTABLE); in maxn()
27 lua_pushnil(L); /* first key */ in maxn()
28 while (lua_next(L, 1)) { in maxn()
29 lua_pop(L, 1); /* remove value */ in maxn()
30 if (lua_type(L, -1) == LUA_TNUMBER) { in maxn()
31 lua_Number v = lua_tonumber(L, -1); in maxn()
35 lua_pushnumber(L, max); in maxn()
41 static int tinsert (lua_State *L) { in tinsert() argument
42 int e = aux_getn(L, 1) + 1; /* first empty element */ in tinsert()
44 switch (lua_gettop(L)) { in tinsert()
51 pos = luaL_checkint(L, 2); /* 2nd argument is the position */ in tinsert()
52 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); in tinsert()
54 lua_rawgeti(L, 1, i-1); in tinsert()
55 lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ in tinsert()
60 return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); in tinsert()
63 lua_rawseti(L, 1, pos); /* t[pos] = v */ in tinsert()
68 static int tremove (lua_State *L) { in tremove() argument
69 int size = aux_getn(L, 1); in tremove()
70 int pos = luaL_optint(L, 2, size); in tremove()
72 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); in tremove()
73 lua_rawgeti(L, 1, pos); /* result = t[pos] */ in tremove()
75 lua_rawgeti(L, 1, pos+1); in tremove()
76 lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ in tremove()
78 lua_pushnil(L); in tremove()
79 lua_rawseti(L, 1, pos); /* t[pos] = nil */ in tremove()
84 static void addfield (lua_State *L, luaL_Buffer *b, int i) { in addfield() argument
85 lua_rawgeti(L, 1, i); in addfield()
86 if (!lua_isstring(L, -1)) in addfield()
87 luaL_error(L, "invalid value (%s) at index %d in table for " in addfield()
88 LUA_QL("concat"), luaL_typename(L, -1), i); in addfield()
93 static int tconcat (lua_State *L) { in tconcat() argument
97 const char *sep = luaL_optlstring(L, 2, "", &lsep); in tconcat()
98 luaL_checktype(L, 1, LUA_TTABLE); in tconcat()
99 i = luaL_optint(L, 3, 1); in tconcat()
100 last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); in tconcat()
101 luaL_buffinit(L, &b); in tconcat()
103 addfield(L, &b, i); in tconcat()
107 addfield(L, &b, i); in tconcat()
119 static int pack (lua_State *L) { in pack() argument
120 int n = lua_gettop(L); /* number of elements to pack */ in pack()
121 lua_createtable(L, n, 1); /* create result table */ in pack()
122 lua_pushinteger(L, n); in pack()
123 lua_setfield(L, -2, "n"); /* t.n = number of elements */ in pack()
126 lua_pushvalue(L, 1); in pack()
127 lua_rawseti(L, -2, 1); /* insert first element */ in pack()
128 lua_replace(L, 1); /* move table into index 1 */ in pack()
130 lua_rawseti(L, 1, i); in pack()
136 static int unpack (lua_State *L) { in unpack() argument
139 luaL_checktype(L, 1, LUA_TTABLE); in unpack()
140 i = luaL_optint(L, 2, 1); in unpack()
141 e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); in unpack()
144 if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n)) in unpack()
145 return luaL_error(L, "too many results to unpack"); in unpack()
146 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ in unpack()
148 lua_rawgeti(L, 1, i); in unpack()
165 static void set2 (lua_State *L, int i, int j) { in set2() argument
166 lua_rawseti(L, 1, i); in set2()
167 lua_rawseti(L, 1, j); in set2()
170 static int sort_comp (lua_State *L, int a, int b) { in sort_comp() argument
171 if (!lua_isnil(L, 2)) { /* function? */ in sort_comp()
173 lua_pushvalue(L, 2); in sort_comp()
174 lua_pushvalue(L, a-1); /* -1 to compensate function */ in sort_comp()
175 lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ in sort_comp()
176 lua_call(L, 2, 1); in sort_comp()
177 res = lua_toboolean(L, -1); in sort_comp()
178 lua_pop(L, 1); in sort_comp()
182 return lua_compare(L, a, b, LUA_OPLT); in sort_comp()
185 static void auxsort (lua_State *L, int l, int u) { in auxsort() argument
189 lua_rawgeti(L, 1, l); in auxsort()
190 lua_rawgeti(L, 1, u); in auxsort()
191 if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ in auxsort()
192 set2(L, l, u); /* swap a[l] - a[u] */ in auxsort()
194 lua_pop(L, 2); in auxsort()
197 lua_rawgeti(L, 1, i); in auxsort()
198 lua_rawgeti(L, 1, l); in auxsort()
199 if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ in auxsort()
200 set2(L, i, l); in auxsort()
202 lua_pop(L, 1); /* remove a[l] */ in auxsort()
203 lua_rawgeti(L, 1, u); in auxsort()
204 if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ in auxsort()
205 set2(L, i, u); in auxsort()
207 lua_pop(L, 2); in auxsort()
210 lua_rawgeti(L, 1, i); /* Pivot */ in auxsort()
211 lua_pushvalue(L, -1); in auxsort()
212 lua_rawgeti(L, 1, u-1); in auxsort()
213 set2(L, i, u-1); in auxsort()
218 while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { in auxsort()
219 if (i>=u) luaL_error(L, "invalid order function for sorting"); in auxsort()
220 lua_pop(L, 1); /* remove a[i] */ in auxsort()
223 while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { in auxsort()
224 if (j<=l) luaL_error(L, "invalid order function for sorting"); in auxsort()
225 lua_pop(L, 1); /* remove a[j] */ in auxsort()
228 lua_pop(L, 3); /* pop pivot, a[i], a[j] */ in auxsort()
231 set2(L, i, j); in auxsort()
233 lua_rawgeti(L, 1, u-1); in auxsort()
234 lua_rawgeti(L, 1, i); in auxsort()
235 set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */ in auxsort()
244 auxsort(L, j, i); /* call recursively the smaller one */ in auxsort()
248 static int sort (lua_State *L) { in sort() argument
249 int n = aux_getn(L, 1); in sort()
250 luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ in sort()
251 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ in sort()
252 luaL_checktype(L, 2, LUA_TFUNCTION); in sort()
253 lua_settop(L, 2); /* make sure there is two arguments */ in sort()
254 auxsort(L, 1, n); in sort()
275 LUAMOD_API int luaopen_table (lua_State *L) { in luaopen_table() argument
276 luaL_newlib(L, tab_funcs); in luaopen_table()
279 lua_getfield(L, -1, "unpack"); in luaopen_table()
280 lua_setglobal(L, "unpack"); in luaopen_table()