@@ -484,18 +484,65 @@ llisten(lua_State *L) {
484
484
return 1 ;
485
485
}
486
486
487
+ static size_t
488
+ count_size (lua_State * L , int index ) {
489
+ size_t tlen = 0 ;
490
+ int i ;
491
+ for (i = 1 ;lua_geti (L , index , i ) != LUA_TNIL ; ++ i ) {
492
+ size_t len ;
493
+ luaL_checklstring (L , -1 , & len );
494
+ tlen += len ;
495
+ lua_pop (L ,1 );
496
+ }
497
+ lua_pop (L ,1 );
498
+ return tlen ;
499
+ }
500
+
501
+ static void
502
+ concat_table (lua_State * L , int index , void * buffer , size_t tlen ) {
503
+ char * ptr = buffer ;
504
+ int i ;
505
+ for (i = 1 ;lua_geti (L , index , i ) != LUA_TNIL ; ++ i ) {
506
+ size_t len ;
507
+ const char * str = lua_tolstring (L , -1 , & len );
508
+ if (str == NULL || tlen < len ) {
509
+ break ;
510
+ }
511
+ memcpy (ptr , str , len );
512
+ ptr += len ;
513
+ tlen -= len ;
514
+ lua_pop (L ,1 );
515
+ }
516
+ if (tlen != 0 ) {
517
+ skynet_free (buffer );
518
+ luaL_error (L , "Invalid strings table" );
519
+ }
520
+ lua_pop (L ,1 );
521
+ }
522
+
487
523
static void *
488
524
get_buffer (lua_State * L , int index , int * sz ) {
489
525
void * buffer ;
490
- if (lua_isuserdata (L ,index )) {
526
+ switch (lua_type (L , index )) {
527
+ const char * str ;
528
+ size_t len ;
529
+ case LUA_TUSERDATA :
491
530
buffer = lua_touserdata (L ,index );
492
531
* sz = luaL_checkinteger (L ,index + 1 );
493
- } else {
494
- size_t len = 0 ;
495
- const char * str = luaL_checklstring (L , index , & len );
532
+ break ;
533
+ case LUA_TTABLE :
534
+ // concat the table as a string
535
+ len = count_size (L , index );
536
+ buffer = skynet_malloc (len );
537
+ concat_table (L , index , buffer , len );
538
+ * sz = (int )len ;
539
+ break ;
540
+ default :
541
+ str = luaL_checklstring (L , index , & len );
496
542
buffer = skynet_malloc (len );
497
543
memcpy (buffer , str , len );
498
544
* sz = (int )len ;
545
+ break ;
499
546
}
500
547
return buffer ;
501
548
}
0 commit comments