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:
15
lbaselib.c
15
lbaselib.c
@@ -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) {
|
static int luaB_warn (lua_State *L) {
|
||||||
const char *msg = luaL_checkstring(L, 1);
|
int n = lua_gettop(L); /* number of arguments */
|
||||||
lua_warning(L, msg, lua_toboolean(L, 2));
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6326,12 +6326,10 @@ The current value of this variable is @St{Lua 5.4}.
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@LibEntry{warn (message [, tocont])|
|
@LibEntry{warn (msg1, @Cdots)|
|
||||||
|
|
||||||
Emits a warning with the given message.
|
Emits a warning with a message composed by the concatenation
|
||||||
A message in a call with @id{tocont} true should be
|
of all its arguments (which should be strings).
|
||||||
continued in another call to this function.
|
|
||||||
The default for @id{tocont} is false.
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
local version = "Lua 5.4"
|
local version = "Lua 5.4"
|
||||||
if _VERSION ~= version then
|
if _VERSION ~= version then
|
||||||
warn(string.format(
|
warn("This test suite is for ", version,
|
||||||
"This test suite is for %s, not for %s\nExiting tests", version, _VERSION))
|
", not for ", _VERSION, "\nExiting tests")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -190,16 +190,13 @@ assert(dofile('verybig.lua', true) == 10); collectgarbage()
|
|||||||
dofile('files.lua')
|
dofile('files.lua')
|
||||||
|
|
||||||
if #msgs > 0 then
|
if #msgs > 0 then
|
||||||
warn("#tests not performed:", true)
|
local m = table.concat(msgs, "\n ")
|
||||||
for i=1,#msgs do
|
warn("#tests not performed:\n ", m, "\n")
|
||||||
warn("\n ", true); warn(msgs[i], true)
|
|
||||||
end
|
|
||||||
warn("\n")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
print("(there should be two warnings now)")
|
print("(there should be two warnings now)")
|
||||||
warn("#This is ", true); warn("an expected", true); warn(" warning")
|
warn("#This is ", "an expected", " warning")
|
||||||
warn("#This is", true); warn(" another one")
|
warn("#This is", " another one")
|
||||||
|
|
||||||
-- no test module should define 'debug'
|
-- no test module should define 'debug'
|
||||||
assert(debug == nil)
|
assert(debug == nil)
|
||||||
@@ -216,9 +213,10 @@ _G.showmem = showmem
|
|||||||
|
|
||||||
end --)
|
end --)
|
||||||
|
|
||||||
local _G, showmem, print, format, clock, time, difftime, assert, open =
|
local _G, showmem, print, format, clock, time, difftime,
|
||||||
|
assert, open, warn =
|
||||||
_G, showmem, print, string.format, os.clock, os.time, os.difftime,
|
_G, showmem, print, string.format, os.clock, os.time, os.difftime,
|
||||||
assert, io.open
|
assert, io.open, warn
|
||||||
|
|
||||||
-- file with time of last performed test
|
-- file with time of last performed test
|
||||||
local fname = T and "time-debug.txt" or "time.txt"
|
local fname = T and "time-debug.txt" or "time.txt"
|
||||||
@@ -262,7 +260,7 @@ if not usertests then
|
|||||||
local diff = (clocktime - lasttime) / lasttime
|
local diff = (clocktime - lasttime) / lasttime
|
||||||
local tolerance = 0.05 -- 5%
|
local tolerance = 0.05 -- 5%
|
||||||
if (diff >= tolerance or diff <= -tolerance) then
|
if (diff >= tolerance or diff <= -tolerance) then
|
||||||
print(format("WARNING: time difference from previous test: %+.1f%%",
|
warn(format("#time difference from previous test: %+.1f%%",
|
||||||
diff * 100))
|
diff * 100))
|
||||||
end
|
end
|
||||||
assert(open(fname, "w")):write(clocktime):close()
|
assert(open(fname, "w")):write(clocktime):close()
|
||||||
|
|||||||
@@ -347,10 +347,26 @@ NoRun("syntax error", "lua -e a")
|
|||||||
NoRun("'-l' needs argument", "lua -l")
|
NoRun("'-l' needs argument", "lua -l")
|
||||||
|
|
||||||
|
|
||||||
if T then -- auxiliary library?
|
if T then -- test library?
|
||||||
print("testing 'not enough memory' to create a state")
|
print("testing 'not enough memory' to create a state")
|
||||||
NoRun("not enough memory", "env MEMLIMIT=100 lua")
|
NoRun("not enough memory", "env MEMLIMIT=100 lua")
|
||||||
|
|
||||||
|
-- testing 'warn'
|
||||||
|
warn("@123", "456", "789")
|
||||||
|
assert(_WARN == "@123456789")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
-- 'warn' must get at least one argument
|
||||||
|
local st, msg = pcall(warn)
|
||||||
|
assert(string.find(msg, "string expected"))
|
||||||
|
|
||||||
|
-- 'warn' does not leave unfinished warning in case of errors
|
||||||
|
-- (message would appear in next warning)
|
||||||
|
st, msg = pcall(warn, "SHOULD NOT APPEAR", {})
|
||||||
|
assert(string.find(msg, "string expected"))
|
||||||
|
end
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
print('testing Ctrl C')
|
print('testing Ctrl C')
|
||||||
|
|||||||
Reference in New Issue
Block a user