aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-04-10 12:58:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-04-10 12:58:14 -0300
commit8ba4523cccf59093543cec988b07957193d55692 (patch)
tree761eb3efbdbe54372d8d5486c2469c337be1f8a8
parent979ad95eb114d8d43f928b184f051ac0cfacedf7 (diff)
downloadlua-8ba4523cccf59093543cec988b07957193d55692.tar.gz
lua-8ba4523cccf59093543cec988b07957193d55692.tar.bz2
lua-8ba4523cccf59093543cec988b07957193d55692.zip
'print' does not call 'tostring' to format its arguments
-rw-r--r--lbaselib.c16
-rw-r--r--manual/manual.of13
-rw-r--r--testes/calls.lua15
3 files changed, 16 insertions, 28 deletions
diff --git a/lbaselib.c b/lbaselib.c
index d4b619a5..83f61e6d 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -24,18 +24,12 @@
24static int luaB_print (lua_State *L) { 24static int luaB_print (lua_State *L) {
25 int n = lua_gettop(L); /* number of arguments */ 25 int n = lua_gettop(L); /* number of arguments */
26 int i; 26 int i;
27 lua_getglobal(L, "tostring"); 27 for (i = 1; i <= n; i++) { /* for each argument */
28 for (i=1; i<=n; i++) {
29 const char *s;
30 size_t l; 28 size_t l;
31 lua_pushvalue(L, -1); /* function to be called */ 29 const char *s = luaL_tolstring(L, i, &l); /* convert it to string */
32 lua_pushvalue(L, i); /* value to print */ 30 if (i > 1) /* not the first element? */
33 lua_call(L, 1, 1); 31 lua_writestring("\t", 1); /* add a tab before it */
34 s = lua_tolstring(L, -1, &l); /* get result */ 32 lua_writestring(s, l); /* print it */
35 if (s == NULL)
36 return luaL_error(L, "'tostring' must return a string to 'print'");
37 if (i>1) lua_writestring("\t", 1);
38 lua_writestring(s, l);
39 lua_pop(L, 1); /* pop result */ 33 lua_pop(L, 1); /* pop result */
40 } 34 }
41 lua_writeline(); 35 lua_writeline();
diff --git a/manual/manual.of b/manual/manual.of
index 9f1ef631..6da2e494 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -6143,8 +6143,10 @@ In case of any error, @id{pcall} returns @false plus the error object.
6143@LibEntry{print (@Cdots)| 6143@LibEntry{print (@Cdots)|
6144Receives any number of arguments 6144Receives any number of arguments
6145and prints their values to @id{stdout}, 6145and prints their values to @id{stdout},
6146using the @Lid{tostring} function to convert each argument to a string. 6146converting each argument to a string
6147@id{print} is not intended for formatted output, 6147following the same rules of @Lid{tostring}.
6148
6149The function @id{print} is not intended for formatted output,
6148but only as a quick way to show a value, 6150but only as a quick way to show a value,
6149for instance for debugging. 6151for instance for debugging.
6150For complete control over the output, 6152For complete control over the output,
@@ -8773,6 +8775,13 @@ like any other error when calling a finalizer.)
8773@itemize{ 8775@itemize{
8774 8776
8775@item{ 8777@item{
8778The function @Lid{print} does not call @Lid{tostring}
8779to format its arguments;
8780instead, it has this functionality hardwired.
8781You should use @id{__tostring} to modify how values are printed.
8782}
8783
8784@item{
8776The pseudo-random number generator used by the function @Lid{math.random} 8785The pseudo-random number generator used by the function @Lid{math.random}
8777now starts with a somewhat random seed. 8786now starts with a somewhat random seed.
8778Moreover, it uses a different algorithm. 8787Moreover, it uses a different algorithm.
diff --git a/testes/calls.lua b/testes/calls.lua
index 941493b1..56a12ae6 100644
--- a/testes/calls.lua
+++ b/testes/calls.lua
@@ -21,21 +21,6 @@ assert(type(f) == 'function')
21assert(not pcall(type)) 21assert(not pcall(type))
22 22
23 23
24do -- test error in 'print' too...
25 local tostring = _ENV.tostring
26
27 _ENV.tostring = nil
28 local st, msg = pcall(print, 1)
29 assert(st == false and string.find(msg, "attempt to call a nil value"))
30
31 _ENV.tostring = function () return {} end
32 local st, msg = pcall(print, 1)
33 assert(st == false and string.find(msg, "must return a string"))
34
35 _ENV.tostring = tostring
36end
37
38
39-- testing local-function recursion 24-- testing local-function recursion
40fact = false 25fact = false
41do 26do