@@ -110,7 +110,8 @@ ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)
110
110
111
111
case LUA_TTABLE :
112
112
113
- size += ngx_http_lua_calc_strlen_in_table (L , i , 0 /* strict */ );
113
+ size += ngx_http_lua_calc_strlen_in_table (L , i , i ,
114
+ 0 /* strict */ );
114
115
break ;
115
116
116
117
case LUA_TLIGHTUSERDATA :
@@ -187,7 +188,7 @@ ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)
187
188
break ;
188
189
189
190
case LUA_TTABLE :
190
- b -> last = ngx_http_lua_copy_str_in_table (L , b -> last );
191
+ b -> last = ngx_http_lua_copy_str_in_table (L , i , b -> last );
191
192
break ;
192
193
193
194
case LUA_TLIGHTUSERDATA :
@@ -242,7 +243,8 @@ ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)
242
243
243
244
244
245
size_t
245
- ngx_http_lua_calc_strlen_in_table (lua_State * L , int arg_i , unsigned strict )
246
+ ngx_http_lua_calc_strlen_in_table (lua_State * L , int index , int arg_i ,
247
+ unsigned strict )
246
248
{
247
249
double key ;
248
250
int max ;
@@ -252,11 +254,24 @@ ngx_http_lua_calc_strlen_in_table(lua_State *L, int arg_i, unsigned strict)
252
254
size_t len ;
253
255
const char * msg ;
254
256
257
+ if (index < 0 ) {
258
+ index = lua_gettop (L ) + index + 1 ;
259
+ }
260
+
261
+ dd ("table index: %d" , index );
262
+
255
263
max = 0 ;
256
264
257
265
lua_pushnil (L ); /* stack: table key */
258
- while (lua_next (L , -2 ) != 0 ) { /* stack: table key value */
259
- if (lua_type (L , -2 ) == LUA_TNUMBER && (key = lua_tonumber (L , -2 ))) {
266
+ while (lua_next (L , index ) != 0 ) { /* stack: table key value */
267
+ dd ("key type: %s" , luaL_typename (L , -2 ));
268
+
269
+ if (lua_type (L , -2 ) == LUA_TNUMBER ) {
270
+
271
+ key = lua_tonumber (L , -2 );
272
+
273
+ dd ("key value: %d" , (int ) key );
274
+
260
275
if (floor (key ) == key && key >= 1 ) {
261
276
if (key > max ) {
262
277
max = key ;
@@ -278,7 +293,7 @@ ngx_http_lua_calc_strlen_in_table(lua_State *L, int arg_i, unsigned strict)
278
293
size = 0 ;
279
294
280
295
for (i = 1 ; i <= max ; i ++ ) {
281
- lua_rawgeti (L , -1 , i ); /* stack: table value */
296
+ lua_rawgeti (L , index , i ); /* stack: table value */
282
297
type = lua_type (L , -1 );
283
298
284
299
switch (type ) {
@@ -315,7 +330,7 @@ ngx_http_lua_calc_strlen_in_table(lua_State *L, int arg_i, unsigned strict)
315
330
316
331
case LUA_TTABLE :
317
332
318
- size += ngx_http_lua_calc_strlen_in_table (L , arg_i , strict );
333
+ size += ngx_http_lua_calc_strlen_in_table (L , -1 , arg_i , strict );
319
334
break ;
320
335
321
336
case LUA_TLIGHTUSERDATA :
@@ -347,7 +362,7 @@ ngx_http_lua_calc_strlen_in_table(lua_State *L, int arg_i, unsigned strict)
347
362
348
363
349
364
u_char *
350
- ngx_http_lua_copy_str_in_table (lua_State * L , u_char * dst )
365
+ ngx_http_lua_copy_str_in_table (lua_State * L , int index , u_char * dst )
351
366
{
352
367
double key ;
353
368
int max ;
@@ -356,10 +371,14 @@ ngx_http_lua_copy_str_in_table(lua_State *L, u_char *dst)
356
371
size_t len ;
357
372
u_char * p ;
358
373
374
+ if (index < 0 ) {
375
+ index = lua_gettop (L ) + index + 1 ;
376
+ }
377
+
359
378
max = 0 ;
360
379
361
380
lua_pushnil (L ); /* stack: table key */
362
- while (lua_next (L , -2 ) != 0 ) { /* stack: table key value */
381
+ while (lua_next (L , index ) != 0 ) { /* stack: table key value */
363
382
key = lua_tonumber (L , -2 );
364
383
if (key > max ) {
365
384
max = key ;
@@ -369,7 +388,7 @@ ngx_http_lua_copy_str_in_table(lua_State *L, u_char *dst)
369
388
}
370
389
371
390
for (i = 1 ; i <= max ; i ++ ) {
372
- lua_rawgeti (L , -1 , i ); /* stack: table value */
391
+ lua_rawgeti (L , index , i ); /* stack: table value */
373
392
type = lua_type (L , -1 );
374
393
switch (type ) {
375
394
case LUA_TNUMBER :
@@ -402,7 +421,7 @@ ngx_http_lua_copy_str_in_table(lua_State *L, u_char *dst)
402
421
break ;
403
422
404
423
case LUA_TTABLE :
405
- dst = ngx_http_lua_copy_str_in_table (L , dst );
424
+ dst = ngx_http_lua_copy_str_in_table (L , -1 , dst );
406
425
break ;
407
426
408
427
case LUA_TLIGHTUSERDATA :
0 commit comments