diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-09-09 12:42:30 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-09-09 12:42:30 -0300 |
commit | bda83e22c0ab3e6fffbf0a71e5e4a5869da8f6ab (patch) | |
tree | c97128993d2c24d70f345863c6a3392f4e683de3 /ltablib.c | |
parent | 364cdbdbdbc1b5e88b1795460a09ea7f4a73a86b (diff) | |
download | lua-bda83e22c0ab3e6fffbf0a71e5e4a5869da8f6ab.tar.gz lua-bda83e22c0ab3e6fffbf0a71e5e4a5869da8f6ab.tar.bz2 lua-bda83e22c0ab3e6fffbf0a71e5e4a5869da8f6ab.zip |
'tablib' does not try to use raw operations when possible: fast
track should make standard operations fast enough to forgo
raw accesses
Diffstat (limited to 'ltablib.c')
-rw-r--r-- | ltablib.c | 165 |
1 files changed, 78 insertions, 87 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.80 2015/01/13 16:27:29 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.81 2015/07/04 16:31:42 roberto Exp roberto $ |
3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -19,42 +19,44 @@ | |||
19 | #include "lualib.h" | 19 | #include "lualib.h" |
20 | 20 | ||
21 | 21 | ||
22 | |||
23 | /* | 22 | /* |
24 | ** Structure with table-access functions | 23 | ** Operations that an object must define to mimic a table |
24 | ** (some functions only need some of them) | ||
25 | */ | 25 | */ |
26 | typedef struct { | 26 | #define TAB_R 1 |
27 | int (*geti) (lua_State *L, int idx, lua_Integer n); | 27 | #define TAB_W 2 |
28 | void (*seti) (lua_State *L, int idx, lua_Integer n); | 28 | #define TAB_L 4 |
29 | } TabA; | 29 | #define TAB_RW (TAB_R | TAB_W) |
30 | |||
31 | |||
32 | #define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n)) | ||
33 | |||
34 | |||
35 | static int checkfield (lua_State *L, const char *key, int n) { | ||
36 | lua_pushstring(L, key); | ||
37 | return (lua_rawget(L, -n) != LUA_TNIL); | ||
38 | } | ||
30 | 39 | ||
31 | 40 | ||
32 | /* | 41 | /* |
33 | ** Check that 'arg' has a table and set access functions in 'ta' to raw | 42 | ** Check that 'arg' either is a table or can behave like one (that is, |
34 | ** or non-raw according to the presence of corresponding metamethods. | 43 | ** has a metatable with the required metamethods) |
35 | */ | 44 | */ |
36 | static void checktab (lua_State *L, int arg, TabA *ta) { | 45 | static void checktab (lua_State *L, int arg, int what) { |
37 | ta->geti = NULL; ta->seti = NULL; | 46 | if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ |
38 | if (lua_getmetatable(L, arg)) { | 47 | int n = 1; /* number of elements to pop */ |
39 | lua_pushliteral(L, "__index"); /* 'index' metamethod */ | 48 | if (lua_getmetatable(L, arg) && /* must have metatable */ |
40 | if (lua_rawget(L, -2) != LUA_TNIL) | 49 | (!(what & TAB_R) || checkfield(L, "__index", ++n)) && |
41 | ta->geti = lua_geti; | 50 | (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && |
42 | lua_pushliteral(L, "__newindex"); /* 'newindex' metamethod */ | 51 | (!(what & TAB_L) || checkfield(L, "__len", ++n))) { |
43 | if (lua_rawget(L, -3) != LUA_TNIL) | 52 | lua_pop(L, n); /* pop metatable and tested metamethods */ |
44 | ta->seti = lua_seti; | 53 | } |
45 | lua_pop(L, 3); /* pop metatable plus both metamethods */ | 54 | else |
46 | } | 55 | luaL_argerror(L, arg, "table expected"); /* force an error */ |
47 | if (ta->geti == NULL || ta->seti == NULL) { | ||
48 | luaL_checktype(L, arg, LUA_TTABLE); /* must be table for raw methods */ | ||
49 | if (ta->geti == NULL) ta->geti = lua_rawgeti; | ||
50 | if (ta->seti == NULL) ta->seti = lua_rawseti; | ||
51 | } | 56 | } |
52 | } | 57 | } |
53 | 58 | ||
54 | 59 | ||
55 | #define aux_getn(L,n,ta) (checktab(L, n, ta), luaL_len(L, n)) | ||
56 | |||
57 | |||
58 | #if defined(LUA_COMPAT_MAXN) | 60 | #if defined(LUA_COMPAT_MAXN) |
59 | static int maxn (lua_State *L) { | 61 | static int maxn (lua_State *L) { |
60 | lua_Number max = 0; | 62 | lua_Number max = 0; |
@@ -74,8 +76,7 @@ static int maxn (lua_State *L) { | |||
74 | 76 | ||
75 | 77 | ||
76 | static int tinsert (lua_State *L) { | 78 | static int tinsert (lua_State *L) { |
77 | TabA ta; | 79 | lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ |
78 | lua_Integer e = aux_getn(L, 1, &ta) + 1; /* first empty element */ | ||
79 | lua_Integer pos; /* where to insert new element */ | 80 | lua_Integer pos; /* where to insert new element */ |
80 | switch (lua_gettop(L)) { | 81 | switch (lua_gettop(L)) { |
81 | case 2: { /* called with only 2 arguments */ | 82 | case 2: { /* called with only 2 arguments */ |
@@ -87,8 +88,8 @@ static int tinsert (lua_State *L) { | |||
87 | pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ | 88 | pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ |
88 | luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); | 89 | luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); |
89 | for (i = e; i > pos; i--) { /* move up elements */ | 90 | for (i = e; i > pos; i--) { /* move up elements */ |
90 | (*ta.geti)(L, 1, i - 1); | 91 | lua_geti(L, 1, i - 1); |
91 | (*ta.seti)(L, 1, i); /* t[i] = t[i - 1] */ | 92 | lua_seti(L, 1, i); /* t[i] = t[i - 1] */ |
92 | } | 93 | } |
93 | break; | 94 | break; |
94 | } | 95 | } |
@@ -96,42 +97,36 @@ static int tinsert (lua_State *L) { | |||
96 | return luaL_error(L, "wrong number of arguments to 'insert'"); | 97 | return luaL_error(L, "wrong number of arguments to 'insert'"); |
97 | } | 98 | } |
98 | } | 99 | } |
99 | (*ta.seti)(L, 1, pos); /* t[pos] = v */ | 100 | lua_seti(L, 1, pos); /* t[pos] = v */ |
100 | return 0; | 101 | return 0; |
101 | } | 102 | } |
102 | 103 | ||
103 | 104 | ||
104 | static int tremove (lua_State *L) { | 105 | static int tremove (lua_State *L) { |
105 | TabA ta; | 106 | lua_Integer size = aux_getn(L, 1, TAB_RW); |
106 | lua_Integer size = aux_getn(L, 1, &ta); | ||
107 | lua_Integer pos = luaL_optinteger(L, 2, size); | 107 | lua_Integer pos = luaL_optinteger(L, 2, size); |
108 | if (pos != size) /* validate 'pos' if given */ | 108 | if (pos != size) /* validate 'pos' if given */ |
109 | luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); | 109 | luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); |
110 | (*ta.geti)(L, 1, pos); /* result = t[pos] */ | 110 | lua_geti(L, 1, pos); /* result = t[pos] */ |
111 | for ( ; pos < size; pos++) { | 111 | for ( ; pos < size; pos++) { |
112 | (*ta.geti)(L, 1, pos + 1); | 112 | lua_geti(L, 1, pos + 1); |
113 | (*ta.seti)(L, 1, pos); /* t[pos] = t[pos + 1] */ | 113 | lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ |
114 | } | 114 | } |
115 | lua_pushnil(L); | 115 | lua_pushnil(L); |
116 | (*ta.seti)(L, 1, pos); /* t[pos] = nil */ | 116 | lua_seti(L, 1, pos); /* t[pos] = nil */ |
117 | return 1; | 117 | return 1; |
118 | } | 118 | } |
119 | 119 | ||
120 | 120 | ||
121 | static int tmove (lua_State *L) { | 121 | static int tmove (lua_State *L) { |
122 | TabA ta; | ||
123 | lua_Integer f = luaL_checkinteger(L, 2); | 122 | lua_Integer f = luaL_checkinteger(L, 2); |
124 | lua_Integer e = luaL_checkinteger(L, 3); | 123 | lua_Integer e = luaL_checkinteger(L, 3); |
125 | lua_Integer t = luaL_checkinteger(L, 4); | 124 | lua_Integer t = luaL_checkinteger(L, 4); |
126 | int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ | 125 | int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ |
126 | checktab(L, 1, TAB_R); | ||
127 | checktab(L, tt, TAB_W); | ||
127 | if (e >= f) { /* otherwise, nothing to move */ | 128 | if (e >= f) { /* otherwise, nothing to move */ |
128 | lua_Integer n, i; | 129 | lua_Integer n, i; |
129 | ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL) | ||
130 | ? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti) | ||
131 | : lua_geti; | ||
132 | ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL) | ||
133 | ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti) | ||
134 | : lua_seti; | ||
135 | luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, | 130 | luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, |
136 | "too many elements to move"); | 131 | "too many elements to move"); |
137 | n = e - f + 1; /* number of elements to move */ | 132 | n = e - f + 1; /* number of elements to move */ |
@@ -139,14 +134,14 @@ static int tmove (lua_State *L) { | |||
139 | "destination wrap around"); | 134 | "destination wrap around"); |
140 | if (t > f) { | 135 | if (t > f) { |
141 | for (i = n - 1; i >= 0; i--) { | 136 | for (i = n - 1; i >= 0; i--) { |
142 | (*ta.geti)(L, 1, f + i); | 137 | lua_geti(L, 1, f + i); |
143 | (*ta.seti)(L, tt, t + i); | 138 | lua_seti(L, tt, t + i); |
144 | } | 139 | } |
145 | } | 140 | } |
146 | else { | 141 | else { |
147 | for (i = 0; i < n; i++) { | 142 | for (i = 0; i < n; i++) { |
148 | (*ta.geti)(L, 1, f + i); | 143 | lua_geti(L, 1, f + i); |
149 | (*ta.seti)(L, tt, t + i); | 144 | lua_seti(L, tt, t + i); |
150 | } | 145 | } |
151 | } | 146 | } |
152 | } | 147 | } |
@@ -155,8 +150,8 @@ static int tmove (lua_State *L) { | |||
155 | } | 150 | } |
156 | 151 | ||
157 | 152 | ||
158 | static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { | 153 | static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { |
159 | (*ta->geti)(L, 1, i); | 154 | lua_geti(L, 1, i); |
160 | if (!lua_isstring(L, -1)) | 155 | if (!lua_isstring(L, -1)) |
161 | luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", | 156 | luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", |
162 | luaL_typename(L, -1), i); | 157 | luaL_typename(L, -1), i); |
@@ -165,21 +160,20 @@ static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { | |||
165 | 160 | ||
166 | 161 | ||
167 | static int tconcat (lua_State *L) { | 162 | static int tconcat (lua_State *L) { |
168 | TabA ta; | ||
169 | luaL_Buffer b; | 163 | luaL_Buffer b; |
170 | size_t lsep; | 164 | size_t lsep; |
171 | lua_Integer i, last; | 165 | lua_Integer i; |
166 | lua_Integer last = aux_getn(L, 1, TAB_R); | ||
172 | const char *sep = luaL_optlstring(L, 2, "", &lsep); | 167 | const char *sep = luaL_optlstring(L, 2, "", &lsep); |
173 | checktab(L, 1, &ta); | ||
174 | i = luaL_optinteger(L, 3, 1); | 168 | i = luaL_optinteger(L, 3, 1); |
175 | last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1)); | 169 | last = luaL_opt(L, luaL_checkinteger, 4, last); |
176 | luaL_buffinit(L, &b); | 170 | luaL_buffinit(L, &b); |
177 | for (; i < last; i++) { | 171 | for (; i < last; i++) { |
178 | addfield(L, &b, &ta, i); | 172 | addfield(L, &b, i); |
179 | luaL_addlstring(&b, sep, lsep); | 173 | luaL_addlstring(&b, sep, lsep); |
180 | } | 174 | } |
181 | if (i == last) /* add last value (if interval was not empty) */ | 175 | if (i == last) /* add last value (if interval was not empty) */ |
182 | addfield(L, &b, &ta, i); | 176 | addfield(L, &b, i); |
183 | luaL_pushresult(&b); | 177 | luaL_pushresult(&b); |
184 | return 1; | 178 | return 1; |
185 | } | 179 | } |
@@ -197,7 +191,7 @@ static int pack (lua_State *L) { | |||
197 | lua_createtable(L, n, 1); /* create result table */ | 191 | lua_createtable(L, n, 1); /* create result table */ |
198 | lua_insert(L, 1); /* put it at index 1 */ | 192 | lua_insert(L, 1); /* put it at index 1 */ |
199 | for (i = n; i >= 1; i--) /* assign elements */ | 193 | for (i = n; i >= 1; i--) /* assign elements */ |
200 | lua_rawseti(L, 1, i); | 194 | lua_seti(L, 1, i); |
201 | lua_pushinteger(L, n); | 195 | lua_pushinteger(L, n); |
202 | lua_setfield(L, 1, "n"); /* t.n = number of elements */ | 196 | lua_setfield(L, 1, "n"); /* t.n = number of elements */ |
203 | return 1; /* return table */ | 197 | return 1; /* return table */ |
@@ -205,10 +199,8 @@ static int pack (lua_State *L) { | |||
205 | 199 | ||
206 | 200 | ||
207 | static int unpack (lua_State *L) { | 201 | static int unpack (lua_State *L) { |
208 | TabA ta; | ||
209 | lua_Integer i, e; | 202 | lua_Integer i, e; |
210 | lua_Unsigned n; | 203 | lua_Unsigned n; |
211 | checktab(L, 1, &ta); | ||
212 | i = luaL_optinteger(L, 2, 1); | 204 | i = luaL_optinteger(L, 2, 1); |
213 | e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); | 205 | e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); |
214 | if (i > e) return 0; /* empty range */ | 206 | if (i > e) return 0; /* empty range */ |
@@ -216,9 +208,9 @@ static int unpack (lua_State *L) { | |||
216 | if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) | 208 | if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) |
217 | return luaL_error(L, "too many results to unpack"); | 209 | return luaL_error(L, "too many results to unpack"); |
218 | for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ | 210 | for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ |
219 | (*ta.geti)(L, 1, i); | 211 | lua_geti(L, 1, i); |
220 | } | 212 | } |
221 | (*ta.geti)(L, 1, e); /* push last element */ | 213 | lua_geti(L, 1, e); /* push last element */ |
222 | return (int)n; | 214 | return (int)n; |
223 | } | 215 | } |
224 | 216 | ||
@@ -235,9 +227,9 @@ static int unpack (lua_State *L) { | |||
235 | */ | 227 | */ |
236 | 228 | ||
237 | 229 | ||
238 | static void set2 (lua_State *L, TabA *ta, int i, int j) { | 230 | static void set2 (lua_State *L, int i, int j) { |
239 | (*ta->seti)(L, 1, i); | 231 | lua_seti(L, 1, i); |
240 | (*ta->seti)(L, 1, j); | 232 | lua_seti(L, 1, j); |
241 | } | 233 | } |
242 | 234 | ||
243 | static int sort_comp (lua_State *L, int a, int b) { | 235 | static int sort_comp (lua_State *L, int a, int b) { |
@@ -255,45 +247,45 @@ static int sort_comp (lua_State *L, int a, int b) { | |||
255 | return lua_compare(L, a, b, LUA_OPLT); | 247 | return lua_compare(L, a, b, LUA_OPLT); |
256 | } | 248 | } |
257 | 249 | ||
258 | static void auxsort (lua_State *L, TabA *ta, int l, int u) { | 250 | static void auxsort (lua_State *L, int l, int u) { |
259 | while (l < u) { /* for tail recursion */ | 251 | while (l < u) { /* for tail recursion */ |
260 | int i, j; | 252 | int i, j; |
261 | /* sort elements a[l], a[(l+u)/2] and a[u] */ | 253 | /* sort elements a[l], a[(l+u)/2] and a[u] */ |
262 | (*ta->geti)(L, 1, l); | 254 | lua_geti(L, 1, l); |
263 | (*ta->geti)(L, 1, u); | 255 | lua_geti(L, 1, u); |
264 | if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ | 256 | if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ |
265 | set2(L, ta, l, u); /* swap a[l] - a[u] */ | 257 | set2(L, l, u); /* swap a[l] - a[u] */ |
266 | else | 258 | else |
267 | lua_pop(L, 2); | 259 | lua_pop(L, 2); |
268 | if (u-l == 1) break; /* only 2 elements */ | 260 | if (u-l == 1) break; /* only 2 elements */ |
269 | i = (l+u)/2; | 261 | i = (l+u)/2; |
270 | (*ta->geti)(L, 1, i); | 262 | lua_geti(L, 1, i); |
271 | (*ta->geti)(L, 1, l); | 263 | lua_geti(L, 1, l); |
272 | if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ | 264 | if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ |
273 | set2(L, ta, i, l); | 265 | set2(L, i, l); |
274 | else { | 266 | else { |
275 | lua_pop(L, 1); /* remove a[l] */ | 267 | lua_pop(L, 1); /* remove a[l] */ |
276 | (*ta->geti)(L, 1, u); | 268 | lua_geti(L, 1, u); |
277 | if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ | 269 | if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ |
278 | set2(L, ta, i, u); | 270 | set2(L, i, u); |
279 | else | 271 | else |
280 | lua_pop(L, 2); | 272 | lua_pop(L, 2); |
281 | } | 273 | } |
282 | if (u-l == 2) break; /* only 3 elements */ | 274 | if (u-l == 2) break; /* only 3 elements */ |
283 | (*ta->geti)(L, 1, i); /* Pivot */ | 275 | lua_geti(L, 1, i); /* Pivot */ |
284 | lua_pushvalue(L, -1); | 276 | lua_pushvalue(L, -1); |
285 | (*ta->geti)(L, 1, u-1); | 277 | lua_geti(L, 1, u-1); |
286 | set2(L, ta, i, u-1); | 278 | set2(L, i, u-1); |
287 | /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ | 279 | /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ |
288 | i = l; j = u-1; | 280 | i = l; j = u-1; |
289 | for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ | 281 | for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ |
290 | /* repeat ++i until a[i] >= P */ | 282 | /* repeat ++i until a[i] >= P */ |
291 | while ((*ta->geti)(L, 1, ++i), sort_comp(L, -1, -2)) { | 283 | while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { |
292 | if (i>=u) luaL_error(L, "invalid order function for sorting"); | 284 | if (i>=u) luaL_error(L, "invalid order function for sorting"); |
293 | lua_pop(L, 1); /* remove a[i] */ | 285 | lua_pop(L, 1); /* remove a[i] */ |
294 | } | 286 | } |
295 | /* repeat --j until a[j] <= P */ | 287 | /* repeat --j until a[j] <= P */ |
296 | while ((*ta->geti)(L, 1, --j), sort_comp(L, -3, -1)) { | 288 | while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { |
297 | if (j<=l) luaL_error(L, "invalid order function for sorting"); | 289 | if (j<=l) luaL_error(L, "invalid order function for sorting"); |
298 | lua_pop(L, 1); /* remove a[j] */ | 290 | lua_pop(L, 1); /* remove a[j] */ |
299 | } | 291 | } |
@@ -301,11 +293,11 @@ static void auxsort (lua_State *L, TabA *ta, int l, int u) { | |||
301 | lua_pop(L, 3); /* pop pivot, a[i], a[j] */ | 293 | lua_pop(L, 3); /* pop pivot, a[i], a[j] */ |
302 | break; | 294 | break; |
303 | } | 295 | } |
304 | set2(L, ta, i, j); | 296 | set2(L, i, j); |
305 | } | 297 | } |
306 | (*ta->geti)(L, 1, u-1); | 298 | lua_geti(L, 1, u-1); |
307 | (*ta->geti)(L, 1, i); | 299 | lua_geti(L, 1, i); |
308 | set2(L, ta, u-1, i); /* swap pivot (a[u-1]) with a[i] */ | 300 | set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */ |
309 | /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ | 301 | /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ |
310 | /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ | 302 | /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ |
311 | if (i-l < u-i) { | 303 | if (i-l < u-i) { |
@@ -314,18 +306,17 @@ static void auxsort (lua_State *L, TabA *ta, int l, int u) { | |||
314 | else { | 306 | else { |
315 | j=i+1; i=u; u=j-2; | 307 | j=i+1; i=u; u=j-2; |
316 | } | 308 | } |
317 | auxsort(L, ta, j, i); /* call recursively the smaller one */ | 309 | auxsort(L, j, i); /* call recursively the smaller one */ |
318 | } /* repeat the routine for the larger one */ | 310 | } /* repeat the routine for the larger one */ |
319 | } | 311 | } |
320 | 312 | ||
321 | static int sort (lua_State *L) { | 313 | static int sort (lua_State *L) { |
322 | TabA ta; | 314 | int n = (int)aux_getn(L, 1, TAB_RW); |
323 | int n = (int)aux_getn(L, 1, &ta); | ||
324 | luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */ | 315 | luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */ |
325 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ | 316 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ |
326 | luaL_checktype(L, 2, LUA_TFUNCTION); | 317 | luaL_checktype(L, 2, LUA_TFUNCTION); |
327 | lua_settop(L, 2); /* make sure there are two arguments */ | 318 | lua_settop(L, 2); /* make sure there are two arguments */ |
328 | auxsort(L, &ta, 1, n); | 319 | auxsort(L, 1, n); |
329 | return 0; | 320 | return 0; |
330 | } | 321 | } |
331 | 322 | ||