Function 'warn' is vararg

Instead of a 'tocont' flag, the function 'warn' in Lua now receives all
message pieces as multiple arguments in a single call. Besides being
simpler to use, this implementation ensures that Lua code cannot create
unfinished warnings.
This commit is contained in:
Roberto Ierusalimschy
2019-06-04 11:22:21 -03:00
parent 514d942748
commit 14edd364c3
4 changed files with 43 additions and 20 deletions

View File

@@ -37,9 +37,20 @@ static int luaB_print (lua_State *L) {
}
/*
** Creates a warning with all given arguments.
** Check first for errors; otherwise an error may interrupt
** the composition of a warning, leaving it unfinished.
*/
static int luaB_warn (lua_State *L) {
const char *msg = luaL_checkstring(L, 1);
lua_warning(L, msg, lua_toboolean(L, 2));
int n = lua_gettop(L); /* number of arguments */
int i;
luaL_checkstring(L, 1); /* at least one argument */
for (i = 2; i <= n; i++)
luaL_checkstring(L, i); /* make sure all arguments are strings */
for (i = 1; i <= n; i++) /* compose warning */
lua_warning(L, lua_tostring(L, i), 1);
lua_warning(L, "", 0); /* close warning */
return 0;
}