@@ -27,7 +27,7 @@ int li_lua_fixindex(lua_State *L, int ndx) {
2727 return ndx ;
2828}
2929
30- static int traceback (lua_State * L ) {
30+ static int traceback (lua_State * L ) {
3131 if (!lua_isstring (L , 1 )) /* 'message' not a string? */
3232 return 1 ; /* keep it intact */
3333 lua_getglobal (L , "debug" );
@@ -205,44 +205,70 @@ void li_lua_environment_restore_globals(lua_State *L) /* -1 */ {
205205 li_lua_set_globals (L ); /* -1 */
206206}
207207
208- GString * li_lua_print_get_string (lua_State * L , int from , int to ) {
209- int i , n = lua_gettop (L );
208+ /* resulting object on top of the stack might eighter be the lua string (owning the returned memory) or an error */
209+ static const char * li_lua_tolstring (lua_State * L , liServer * srv , liVRequest * vr , int idx , size_t * len ) { /* +1 */
210+ int errfunc ;
211+
212+ switch (lua_type (L , idx )) {
213+ case LUA_TBOOLEAN :
214+ lua_pushstring (L , (lua_toboolean (L , idx ) ? "true" : "false" ));
215+ break ;
216+ case LUA_TNIL :
217+ lua_pushlstring (L , CONST_STR_LEN ("nil" ));
218+ break ;
219+ case LUA_TSTRING :
220+ case LUA_TNUMBER :
221+ lua_pushvalue (L , idx );
222+ break ;
223+ default :
224+ idx = li_lua_fixindex (L , idx );
225+ lua_pushcfunction (L , traceback ); /* +1 */
226+ errfunc = lua_gettop (L );
227+ lua_getglobal (L , "tostring" ); /* +1 */
228+ lua_pushvalue (L , idx ); /* +1 object to convert to string */
229+
230+ if (lua_pcall (L , 1 , 1 , errfunc )) { /* -2 (func + args), +1 result */
231+ _VR_ERROR (srv , vr , "li_lua_tolstring failed: %s" , lua_tostring (L , -1 ));
232+
233+ if (len ) * len = 0 ;
234+ return NULL ;
235+ }
236+ lua_remove (L , errfunc ); /* -1 */
237+ }
238+
239+ return lua_tolstring (L , -1 , len );
240+ }
241+
242+ GString * li_lua_print_get_string (lua_State * L , liServer * srv , liVRequest * vr , int from , int to ) {
243+ int i ;
210244 GString * buf = g_string_sized_new (0 );
211245
212- lua_getglobal (L , "tostring" );
213246 for (i = from ; i <= to ; i ++ ) {
214247 const char * s ;
215248 size_t len ;
216249
217- lua_pushvalue (L , n + 1 );
218- lua_pushvalue (L , i );
219- if (0 != lua_pcall (L , 1 , 1 , 0 )) goto failed ;
220- s = lua_tolstring (L , -1 , & len );
221- lua_pop (L , 1 );
250+ if (NULL == (s = li_lua_tolstring (L , srv , vr , i , & len ))) { /* +1 */
251+ s = "<failed tostring>" ;
252+ len = 17 ;
253+ }
222254
223- if (NULL == s ) goto failed ;
224- if (0 == len ) continue ;
225- if (buf -> len > 0 ) {
226- g_string_append_c (buf , ' ' );
227- li_g_string_append_len ( buf , s , len );
228- } else {
229- li_g_string_append_len ( buf , s , len );
255+ if (len > 0 ) {
256+ if (buf -> len > 0 ) {
257+ g_string_append_c (buf , ' ' );
258+ li_g_string_append_len (buf , s , len );
259+ } else {
260+ li_g_string_append_len ( buf , s , len );
261+ }
230262 }
263+ lua_pop (L , 1 ); /* -1 */
231264 }
232- lua_pop (L , 1 );
233265 return buf ;
234-
235- failed :
236- g_string_free (buf , TRUE);
237- lua_pushliteral (L , "lua_print_get_string: Couldn't convert parameter to string" );
238- lua_error (L );
239- return NULL ; /* should be unreachable */
240266}
241267
242268static int li_lua_error (lua_State * L ) {
243269 liServer * srv = lua_touserdata (L , lua_upvalueindex (1 ));
244270 liWorker * wrk = lua_touserdata (L , lua_upvalueindex (2 ));
245- GString * buf = li_lua_print_get_string (L , 1 , lua_gettop (L ));
271+ GString * buf = li_lua_print_get_string (L , srv , NULL , 1 , lua_gettop (L ));
246272
247273 _ERROR (srv , wrk , NULL , "(lua): %s" , buf -> str );
248274
@@ -254,7 +280,7 @@ static int li_lua_error(lua_State *L) {
254280static int li_lua_warning (lua_State * L ) {
255281 liServer * srv = lua_touserdata (L , lua_upvalueindex (1 ));
256282 liWorker * wrk = lua_touserdata (L , lua_upvalueindex (2 ));
257- GString * buf = li_lua_print_get_string (L , 1 , lua_gettop (L ));
283+ GString * buf = li_lua_print_get_string (L , srv , NULL , 1 , lua_gettop (L ));
258284
259285 _WARNING (srv , wrk , NULL , "(lua): %s" , buf -> str );
260286
@@ -266,7 +292,7 @@ static int li_lua_warning(lua_State *L) {
266292static int li_lua_info (lua_State * L ) {
267293 liServer * srv = lua_touserdata (L , lua_upvalueindex (1 ));
268294 liWorker * wrk = lua_touserdata (L , lua_upvalueindex (2 ));
269- GString * buf = li_lua_print_get_string (L , 1 , lua_gettop (L ));
295+ GString * buf = li_lua_print_get_string (L , srv , NULL , 1 , lua_gettop (L ));
270296
271297 _INFO (srv , wrk , NULL , "(lua): %s" , buf -> str );
272298
@@ -278,7 +304,7 @@ static int li_lua_info(lua_State *L) {
278304static int li_lua_debug (lua_State * L ) {
279305 liServer * srv = lua_touserdata (L , lua_upvalueindex (1 ));
280306 liWorker * wrk = lua_touserdata (L , lua_upvalueindex (2 ));
281- GString * buf = li_lua_print_get_string (L , 1 , lua_gettop (L ));
307+ GString * buf = li_lua_print_get_string (L , srv , NULL , 1 , lua_gettop (L ));
282308
283309 _DEBUG (srv , wrk , NULL , "(lua): %s" , buf -> str );
284310
0 commit comments