aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-06-04 11:22:21 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-06-04 11:22:21 -0300
commit14edd364c3abcb758e74c68a2bdd4ddaeefdae2a (patch)
tree9350100ad9b962cae6e6406b4d7462e1443bc137
parent514d94274853e6f0dfd6bb2ffa2e1fc64db926dd (diff)
downloadlua-14edd364c3abcb758e74c68a2bdd4ddaeefdae2a.tar.gz
lua-14edd364c3abcb758e74c68a2bdd4ddaeefdae2a.tar.bz2
lua-14edd364c3abcb758e74c68a2bdd4ddaeefdae2a.zip
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.
-rw-r--r--lbaselib.c15
-rw-r--r--manual/manual.of8
-rw-r--r--testes/all.lua22
-rw-r--r--testes/main.lua18
4 files changed, 43 insertions, 20 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 83f61e6d..4724e759 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -37,9 +37,20 @@ static int luaB_print (lua_State *L) {
37} 37}
38 38
39 39
40/*
41** Creates a warning with all given arguments.
42** Check first for errors; otherwise an error may interrupt
43** the composition of a warning, leaving it unfinished.
44*/
40static int luaB_warn (lua_State *L) { 45static int luaB_warn (lua_State *L) {
41 const char *msg = luaL_checkstring(L, 1); 46 int n = lua_gettop(L); /* number of arguments */
42 lua_warning(L, msg, lua_toboolean(L, 2)); 47 int i;
48 luaL_checkstring(L, 1); /* at least one argument */
49 for (i = 2; i <= n; i++)
50 luaL_checkstring(L, i); /* make sure all arguments are strings */
51 for (i = 1; i <= n; i++) /* compose warning */
52 lua_warning(L, lua_tostring(L, i), 1);
53 lua_warning(L, "", 0); /* close warning */
43 return 0; 54 return 0;
44} 55}
45 56
diff --git a/manual/manual.of b/manual/manual.of
index eb4e671d..4c9c20b2 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -6326,12 +6326,10 @@ The current value of this variable is @St{Lua 5.4}.
6326 6326
6327} 6327}
6328 6328
6329@LibEntry{warn (message [, tocont])| 6329@LibEntry{warn (msg1, @Cdots)|
6330 6330
6331Emits a warning with the given message. 6331Emits a warning with a message composed by the concatenation
6332A message in a call with @id{tocont} true should be 6332of all its arguments (which should be strings).
6333continued in another call to this function.
6334The default for @id{tocont} is false.
6335 6333
6336} 6334}
6337 6335
diff --git a/testes/all.lua b/testes/all.lua
index 8d727b6b..2e6fe038 100644
--- a/testes/all.lua
+++ b/testes/all.lua
@@ -5,8 +5,8 @@
5 5
6local version = "Lua 5.4" 6local version = "Lua 5.4"
7if _VERSION ~= version then 7if _VERSION ~= version then
8 warn(string.format( 8 warn("This test suite is for ", version,
9 "This test suite is for %s, not for %s\nExiting tests", version, _VERSION)) 9 ", not for ", _VERSION, "\nExiting tests")
10 return 10 return
11end 11end
12 12
@@ -190,16 +190,13 @@ assert(dofile('verybig.lua', true) == 10); collectgarbage()
190dofile('files.lua') 190dofile('files.lua')
191 191
192if #msgs > 0 then 192if #msgs > 0 then
193 warn("#tests not performed:", true) 193 local m = table.concat(msgs, "\n ")
194 for i=1,#msgs do 194 warn("#tests not performed:\n ", m, "\n")
195 warn("\n ", true); warn(msgs[i], true)
196 end
197 warn("\n")
198end 195end
199 196
200print("(there should be two warnings now)") 197print("(there should be two warnings now)")
201warn("#This is ", true); warn("an expected", true); warn(" warning") 198warn("#This is ", "an expected", " warning")
202warn("#This is", true); warn(" another one") 199warn("#This is", " another one")
203 200
204-- no test module should define 'debug' 201-- no test module should define 'debug'
205assert(debug == nil) 202assert(debug == nil)
@@ -216,9 +213,10 @@ _G.showmem = showmem
216 213
217end --) 214end --)
218 215
219local _G, showmem, print, format, clock, time, difftime, assert, open = 216local _G, showmem, print, format, clock, time, difftime,
217 assert, open, warn =
220 _G, showmem, print, string.format, os.clock, os.time, os.difftime, 218 _G, showmem, print, string.format, os.clock, os.time, os.difftime,
221 assert, io.open 219 assert, io.open, warn
222 220
223-- file with time of last performed test 221-- file with time of last performed test
224local fname = T and "time-debug.txt" or "time.txt" 222local fname = T and "time-debug.txt" or "time.txt"
@@ -262,7 +260,7 @@ if not usertests then
262 local diff = (clocktime - lasttime) / lasttime 260 local diff = (clocktime - lasttime) / lasttime
263 local tolerance = 0.05 -- 5% 261 local tolerance = 0.05 -- 5%
264 if (diff >= tolerance or diff <= -tolerance) then 262 if (diff >= tolerance or diff <= -tolerance) then
265 print(format("WARNING: time difference from previous test: %+.1f%%", 263 warn(format("#time difference from previous test: %+.1f%%",
266 diff * 100)) 264 diff * 100))
267 end 265 end
268 assert(open(fname, "w")):write(clocktime):close() 266 assert(open(fname, "w")):write(clocktime):close()
diff --git a/testes/main.lua b/testes/main.lua
index 47d84d4c..4c09645a 100644
--- a/testes/main.lua
+++ b/testes/main.lua
@@ -347,10 +347,26 @@ NoRun("syntax error", "lua -e a")
347NoRun("'-l' needs argument", "lua -l") 347NoRun("'-l' needs argument", "lua -l")
348 348
349 349
350if T then -- auxiliary library? 350if T then -- test library?
351 print("testing 'not enough memory' to create a state") 351 print("testing 'not enough memory' to create a state")
352 NoRun("not enough memory", "env MEMLIMIT=100 lua") 352 NoRun("not enough memory", "env MEMLIMIT=100 lua")
353
354 -- testing 'warn'
355 warn("@123", "456", "789")
356 assert(_WARN == "@123456789")
353end 357end
358
359do
360 -- 'warn' must get at least one argument
361 local st, msg = pcall(warn)
362 assert(string.find(msg, "string expected"))
363
364 -- 'warn' does not leave unfinished warning in case of errors
365 -- (message would appear in next warning)
366 st, msg = pcall(warn, "SHOULD NOT APPEAR", {})
367 assert(string.find(msg, "string expected"))
368end
369
354print('+') 370print('+')
355 371
356print('testing Ctrl C') 372print('testing Ctrl C')