@@ -91,6 +91,36 @@ tpl_term(int type, const char *str, size_t len, void *data)
91
91
}
92
92
}
93
93
94
+ /**
95
+ * This function exists because lua_tostring does not use
96
+ * __tostring metamethod, and this metamethod has to be used
97
+ * if we want to print Lua userdata correctly.
98
+ */
99
+ static const char *
100
+ luaT_tostring (lua_State * L , int idx )
101
+ {
102
+ if (!luaL_callmeta (L , idx , "__tostring" )) {
103
+ switch (lua_type (L , idx )) {
104
+ case LUA_TNUMBER :
105
+ case LUA_TSTRING :
106
+ return lua_tostring (L , idx );
107
+ case LUA_TBOOLEAN : {
108
+ int val = lua_toboolean (L , idx );
109
+ lua_pushstring (L , val ? "true" : "false" );
110
+ break ;
111
+ }
112
+ case LUA_TNIL :
113
+ lua_pushliteral (L , "nil" );
114
+ break ;
115
+ default :
116
+ lua_pushfstring (L , "%s: %p" , luaL_typename (L , idx ),
117
+ lua_topointer (L , idx ));
118
+ }
119
+ }
120
+
121
+ return lua_tostring (L , -1 );
122
+ }
123
+
94
124
static int
95
125
lbox_httpd_escape_html (struct lua_State * L )
96
126
{
@@ -109,14 +139,7 @@ lbox_httpd_escape_html(struct lua_State *L)
109
139
}
110
140
111
141
for (i = 1 ; i <= top ; i ++ ) {
112
- if (lua_isnil (L , i )) {
113
- luaL_addstring (& b , "nil" );
114
- continue ;
115
- }
116
- const char * s = lua_tostring (L , i );
117
- if (s == NULL ) {
118
- continue ;
119
- }
142
+ const char * s = luaT_tostring (L , i );
120
143
for (; * s ; s ++ ) {
121
144
switch (* s ) {
122
145
case '&' :
0 commit comments