diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-10 12:58:14 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-10 12:58:14 -0300 |
commit | 8ba4523cccf59093543cec988b07957193d55692 (patch) | |
tree | 761eb3efbdbe54372d8d5486c2469c337be1f8a8 | |
parent | 979ad95eb114d8d43f928b184f051ac0cfacedf7 (diff) | |
download | lua-8ba4523cccf59093543cec988b07957193d55692.tar.gz lua-8ba4523cccf59093543cec988b07957193d55692.tar.bz2 lua-8ba4523cccf59093543cec988b07957193d55692.zip |
'print' does not call 'tostring' to format its arguments
-rw-r--r-- | lbaselib.c | 16 | ||||
-rw-r--r-- | manual/manual.of | 13 | ||||
-rw-r--r-- | testes/calls.lua | 15 |
3 files changed, 16 insertions, 28 deletions
@@ -24,18 +24,12 @@ | |||
24 | static int luaB_print (lua_State *L) { | 24 | static 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)| |
6144 | Receives any number of arguments | 6144 | Receives any number of arguments |
6145 | and prints their values to @id{stdout}, | 6145 | and prints their values to @id{stdout}, |
6146 | using the @Lid{tostring} function to convert each argument to a string. | 6146 | converting each argument to a string |
6147 | @id{print} is not intended for formatted output, | 6147 | following the same rules of @Lid{tostring}. |
6148 | |||
6149 | The function @id{print} is not intended for formatted output, | ||
6148 | but only as a quick way to show a value, | 6150 | but only as a quick way to show a value, |
6149 | for instance for debugging. | 6151 | for instance for debugging. |
6150 | For complete control over the output, | 6152 | For 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{ |
8778 | The function @Lid{print} does not call @Lid{tostring} | ||
8779 | to format its arguments; | ||
8780 | instead, it has this functionality hardwired. | ||
8781 | You should use @id{__tostring} to modify how values are printed. | ||
8782 | } | ||
8783 | |||
8784 | @item{ | ||
8776 | The pseudo-random number generator used by the function @Lid{math.random} | 8785 | The pseudo-random number generator used by the function @Lid{math.random} |
8777 | now starts with a somewhat random seed. | 8786 | now starts with a somewhat random seed. |
8778 | Moreover, it uses a different algorithm. | 8787 | Moreover, 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') | |||
21 | assert(not pcall(type)) | 21 | assert(not pcall(type)) |
22 | 22 | ||
23 | 23 | ||
24 | do -- 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 | ||
36 | end | ||
37 | |||
38 | |||
39 | -- testing local-function recursion | 24 | -- testing local-function recursion |
40 | fact = false | 25 | fact = false |
41 | do | 26 | do |