Skip to content

Commit 3c95752

Browse files
committed
templates: don't skip nils
1 parent 8ee8b16 commit 3c95752

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

http/lib.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,36 @@ tpl_term(int type, const char *str, size_t len, void *data)
9191
}
9292
}
9393

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+
94124
static int
95125
lbox_httpd_escape_html(struct lua_State *L)
96126
{
@@ -109,14 +139,7 @@ lbox_httpd_escape_html(struct lua_State *L)
109139
}
110140

111141
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);
120143
for (; *s; s++) {
121144
switch(*s) {
122145
case '&':

0 commit comments

Comments
 (0)