aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-08-16 14:58:02 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-08-16 14:58:02 -0300
commitb96b0b5abbf40cbdbed7952bf35a5a27ddf75928 (patch)
tree5d9d5463cb7d3424833abab20dd87bce1f4b240f
parentca13be9af784b7288d3a07d9b5bccb329086e885 (diff)
downloadlua-b96b0b5abbf40cbdbed7952bf35a5a27ddf75928.tar.gz
lua-b96b0b5abbf40cbdbed7952bf35a5a27ddf75928.tar.bz2
lua-b96b0b5abbf40cbdbed7952bf35a5a27ddf75928.zip
Added macro 'luaL_pushfail'
The macro 'luaL_pushfail' documents all places in the standard libraries that return nil to signal some kind of failure. It is defined as 'lua_pushnil'. The manual also got a notation (@fail) to document those returns. The tests were changed to be agnostic regarding whether 'fail' is 'nil' or 'false'.
-rw-r--r--lauxlib.c6
-rw-r--r--lauxlib.h4
-rw-r--r--lbaselib.c6
-rw-r--r--ldblib.c14
-rw-r--r--liolib.c10
-rw-r--r--lmathlib.c4
-rw-r--r--loadlib.c8
-rw-r--r--lstrlib.c4
-rw-r--r--lutf8lib.c4
-rwxr-xr-xmanual/2html1
-rw-r--r--manual/manual.of87
-rw-r--r--testes/api.lua2
-rw-r--r--testes/db.lua6
-rw-r--r--testes/errors.lua10
-rw-r--r--testes/files.lua30
-rw-r--r--testes/literals.lua2
-rw-r--r--testes/math.lua100
-rw-r--r--testes/pm.lua22
-rw-r--r--testes/strings.lua6
-rw-r--r--testes/utf8.lua4
20 files changed, 176 insertions, 154 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 014e7052..5a040ac6 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -249,7 +249,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
249 return 1; 249 return 1;
250 } 250 }
251 else { 251 else {
252 lua_pushnil(L); 252 luaL_pushfail(L);
253 if (fname) 253 if (fname)
254 lua_pushfstring(L, "%s: %s", fname, strerror(en)); 254 lua_pushfstring(L, "%s: %s", fname, strerror(en));
255 else 255 else
@@ -291,10 +291,10 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) {
291 if (*what == 'e' && stat == 0) /* successful termination? */ 291 if (*what == 'e' && stat == 0) /* successful termination? */
292 lua_pushboolean(L, 1); 292 lua_pushboolean(L, 1);
293 else 293 else
294 lua_pushnil(L); 294 luaL_pushfail(L);
295 lua_pushstring(L, what); 295 lua_pushstring(L, what);
296 lua_pushinteger(L, stat); 296 lua_pushinteger(L, stat);
297 return 3; /* return true/nil,what,code */ 297 return 3; /* return true/fail,what,code */
298 } 298 }
299} 299}
300 300
diff --git a/lauxlib.h b/lauxlib.h
index f68f6af1..c50cdf4d 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -153,6 +153,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
153#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 153#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
154 154
155 155
156/* push the value used to represent failure/error */
157#define luaL_pushfail(L) lua_pushnil(L)
158
159
156/* 160/*
157** {====================================================== 161** {======================================================
158** Generic Buffer manipulation 162** Generic Buffer manipulation
diff --git a/lbaselib.c b/lbaselib.c
index c68e6d38..747fd45a 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -106,7 +106,7 @@ static int luaB_tonumber (lua_State *L) {
106 return 1; 106 return 1;
107 } /* else not a number */ 107 } /* else not a number */
108 } /* else not a number */ 108 } /* else not a number */
109 lua_pushnil(L); /* not a number */ 109 luaL_pushfail(L); /* not a number */
110 return 1; 110 return 1;
111} 111}
112 112
@@ -308,9 +308,9 @@ static int load_aux (lua_State *L, int status, int envidx) {
308 return 1; 308 return 1;
309 } 309 }
310 else { /* error (message is on top of the stack) */ 310 else { /* error (message is on top of the stack) */
311 lua_pushnil(L); 311 luaL_pushfail(L);
312 lua_insert(L, -2); /* put before error message */ 312 lua_insert(L, -2); /* put before error message */
313 return 2; /* return nil plus error message */ 313 return 2; /* return fail plus error message */
314 } 314 }
315} 315}
316 316
diff --git a/ldblib.c b/ldblib.c
index 64158395..9f7aad51 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -65,7 +65,7 @@ static int db_setmetatable (lua_State *L) {
65static int db_getuservalue (lua_State *L) { 65static int db_getuservalue (lua_State *L) {
66 int n = (int)luaL_optinteger(L, 2, 1); 66 int n = (int)luaL_optinteger(L, 2, 1);
67 if (lua_type(L, 1) != LUA_TUSERDATA) 67 if (lua_type(L, 1) != LUA_TUSERDATA)
68 lua_pushnil(L); 68 luaL_pushfail(L);
69 else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) { 69 else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
70 lua_pushboolean(L, 1); 70 lua_pushboolean(L, 1);
71 return 2; 71 return 2;
@@ -80,7 +80,7 @@ static int db_setuservalue (lua_State *L) {
80 luaL_checkany(L, 2); 80 luaL_checkany(L, 2);
81 lua_settop(L, 2); 81 lua_settop(L, 2);
82 if (!lua_setiuservalue(L, 1, n)) 82 if (!lua_setiuservalue(L, 1, n))
83 lua_pushnil(L); 83 luaL_pushfail(L);
84 return 1; 84 return 1;
85} 85}
86 86
@@ -159,7 +159,7 @@ static int db_getinfo (lua_State *L) {
159 } 159 }
160 else { /* stack level */ 160 else { /* stack level */
161 if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { 161 if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
162 lua_pushnil(L); /* level out of range */ 162 luaL_pushfail(L); /* level out of range */
163 return 1; 163 return 1;
164 } 164 }
165 } 165 }
@@ -223,7 +223,7 @@ static int db_getlocal (lua_State *L) {
223 return 2; 223 return 2;
224 } 224 }
225 else { 225 else {
226 lua_pushnil(L); /* no name (nor value) */ 226 luaL_pushfail(L); /* no name (nor value) */
227 return 1; 227 return 1;
228 } 228 }
229 } 229 }
@@ -389,8 +389,10 @@ static int db_gethook (lua_State *L) {
389 char buff[5]; 389 char buff[5];
390 int mask = lua_gethookmask(L1); 390 int mask = lua_gethookmask(L1);
391 lua_Hook hook = lua_gethook(L1); 391 lua_Hook hook = lua_gethook(L1);
392 if (hook == NULL) /* no hook? */ 392 if (hook == NULL) { /* no hook? */
393 lua_pushnil(L); 393 luaL_pushfail(L);
394 return 1;
395 }
394 else if (hook != hookf) /* external hook? */ 396 else if (hook != hookf) /* external hook? */
395 lua_pushliteral(L, "external hook"); 397 lua_pushliteral(L, "external hook");
396 else { /* hook table must exist */ 398 else { /* hook table must exist */
diff --git a/liolib.c b/liolib.c
index 56507d5e..d8b0a6f9 100644
--- a/liolib.c
+++ b/liolib.c
@@ -153,7 +153,7 @@ static int io_type (lua_State *L) {
153 luaL_checkany(L, 1); 153 luaL_checkany(L, 1);
154 p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); 154 p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
155 if (p == NULL) 155 if (p == NULL)
156 lua_pushnil(L); /* not a file */ 156 luaL_pushfail(L); /* not a file */
157 else if (isclosed(p)) 157 else if (isclosed(p))
158 lua_pushliteral(L, "closed file"); 158 lua_pushliteral(L, "closed file");
159 else 159 else
@@ -593,7 +593,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
593 return luaL_fileresult(L, 0, NULL); 593 return luaL_fileresult(L, 0, NULL);
594 if (!success) { 594 if (!success) {
595 lua_pop(L, 1); /* remove last result */ 595 lua_pop(L, 1); /* remove last result */
596 lua_pushnil(L); /* push nil instead */ 596 luaL_pushfail(L); /* push nil instead */
597 } 597 }
598 return n - first; 598 return n - first;
599} 599}
@@ -624,9 +624,9 @@ static int io_readline (lua_State *L) {
624 lua_pushvalue(L, lua_upvalueindex(3 + i)); 624 lua_pushvalue(L, lua_upvalueindex(3 + i));
625 n = g_read(L, p->f, 2); /* 'n' is number of results */ 625 n = g_read(L, p->f, 2); /* 'n' is number of results */
626 lua_assert(n > 0); /* should return at least a nil */ 626 lua_assert(n > 0); /* should return at least a nil */
627 if (!lua_isnil(L, -n)) /* read at least one value? */ 627 if (lua_toboolean(L, -n)) /* read at least one value? */
628 return n; /* return them */ 628 return n; /* return them */
629 else { /* first result is nil: EOF or error */ 629 else { /* first result is false: EOF or error */
630 if (n > 1) { /* is there error information? */ 630 if (n > 1) { /* is there error information? */
631 /* 2nd result is error message */ 631 /* 2nd result is error message */
632 return luaL_error(L, "%s", lua_tostring(L, -n + 1)); 632 return luaL_error(L, "%s", lua_tostring(L, -n + 1));
@@ -782,7 +782,7 @@ static void createmeta (lua_State *L) {
782static int io_noclose (lua_State *L) { 782static int io_noclose (lua_State *L) {
783 LStream *p = tolstream(L); 783 LStream *p = tolstream(L);
784 p->closef = &io_noclose; /* keep file opened */ 784 p->closef = &io_noclose; /* keep file opened */
785 lua_pushnil(L); 785 luaL_pushfail(L);
786 lua_pushliteral(L, "cannot close standard file"); 786 lua_pushliteral(L, "cannot close standard file");
787 return 2; 787 return 2;
788} 788}
diff --git a/lmathlib.c b/lmathlib.c
index 752647e7..f49eb318 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -77,7 +77,7 @@ static int math_toint (lua_State *L) {
77 lua_pushinteger(L, n); 77 lua_pushinteger(L, n);
78 else { 78 else {
79 luaL_checkany(L, 1); 79 luaL_checkany(L, 1);
80 lua_pushnil(L); /* value is not convertible to integer */ 80 luaL_pushfail(L); /* value is not convertible to integer */
81 } 81 }
82 return 1; 82 return 1;
83} 83}
@@ -235,7 +235,7 @@ static int math_type (lua_State *L) {
235 lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float"); 235 lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
236 else { 236 else {
237 luaL_checkany(L, 1); 237 luaL_checkany(L, 1);
238 lua_pushnil(L); 238 luaL_pushfail(L);
239 } 239 }
240 return 1; 240 return 1;
241} 241}
diff --git a/loadlib.c b/loadlib.c
index 9ef00278..d7a3fb23 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -408,10 +408,10 @@ static int ll_loadlib (lua_State *L) {
408 if (stat == 0) /* no errors? */ 408 if (stat == 0) /* no errors? */
409 return 1; /* return the loaded function */ 409 return 1; /* return the loaded function */
410 else { /* error; error message is on stack top */ 410 else { /* error; error message is on stack top */
411 lua_pushnil(L); 411 luaL_pushfail(L);
412 lua_insert(L, -2); 412 lua_insert(L, -2);
413 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); 413 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
414 return 3; /* return nil, error message, and where */ 414 return 3; /* return fail, error message, and where */
415 } 415 }
416} 416}
417 417
@@ -505,9 +505,9 @@ static int ll_searchpath (lua_State *L) {
505 luaL_optstring(L, 4, LUA_DIRSEP)); 505 luaL_optstring(L, 4, LUA_DIRSEP));
506 if (f != NULL) return 1; 506 if (f != NULL) return 1;
507 else { /* error message is on top of the stack */ 507 else { /* error message is on top of the stack */
508 lua_pushnil(L); 508 luaL_pushfail(L);
509 lua_insert(L, -2); 509 lua_insert(L, -2);
510 return 2; /* return nil + error message */ 510 return 2; /* return fail + error message */
511 } 511 }
512} 512}
513 513
diff --git a/lstrlib.c b/lstrlib.c
index 8c9e1a83..7f4a0184 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -744,7 +744,7 @@ static int str_find_aux (lua_State *L, int find) {
744 const char *p = luaL_checklstring(L, 2, &lp); 744 const char *p = luaL_checklstring(L, 2, &lp);
745 size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; 745 size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1;
746 if (init > ls) { /* start after string's end? */ 746 if (init > ls) { /* start after string's end? */
747 lua_pushnil(L); /* cannot find anything */ 747 luaL_pushfail(L); /* cannot find anything */
748 return 1; 748 return 1;
749 } 749 }
750 /* explicit request or no special characters? */ 750 /* explicit request or no special characters? */
@@ -779,7 +779,7 @@ static int str_find_aux (lua_State *L, int find) {
779 } 779 }
780 } while (s1++ < ms.src_end && !anchor); 780 } while (s1++ < ms.src_end && !anchor);
781 } 781 }
782 lua_pushnil(L); /* not found */ 782 luaL_pushfail(L); /* not found */
783 return 1; 783 return 1;
784} 784}
785 785
diff --git a/lutf8lib.c b/lutf8lib.c
index b4b787e7..e63a5a74 100644
--- a/lutf8lib.c
+++ b/lutf8lib.c
@@ -103,7 +103,7 @@ static int utflen (lua_State *L) {
103 while (posi <= posj) { 103 while (posi <= posj) {
104 const char *s1 = utf8_decode(s + posi, NULL, !lax); 104 const char *s1 = utf8_decode(s + posi, NULL, !lax);
105 if (s1 == NULL) { /* conversion error? */ 105 if (s1 == NULL) { /* conversion error? */
106 lua_pushnil(L); /* return nil ... */ 106 luaL_pushfail(L); /* return fail ... */
107 lua_pushinteger(L, posi + 1); /* ... and current position */ 107 lua_pushinteger(L, posi + 1); /* ... and current position */
108 return 2; 108 return 2;
109 } 109 }
@@ -216,7 +216,7 @@ static int byteoffset (lua_State *L) {
216 if (n == 0) /* did it find given character? */ 216 if (n == 0) /* did it find given character? */
217 lua_pushinteger(L, posi + 1); 217 lua_pushinteger(L, posi + 1);
218 else /* no such character */ 218 else /* no such character */
219 lua_pushnil(L); 219 luaL_pushfail(L);
220 return 1; 220 return 1;
221} 221}
222 222
diff --git a/manual/2html b/manual/2html
index 605c6e59..a300f8d4 100755
--- a/manual/2html
+++ b/manual/2html
@@ -324,6 +324,7 @@ N = function (s) return (string.gsub(s, " ", "&nbsp;")) end,
324NE = id, -- tag"foreignphrase", 324NE = id, -- tag"foreignphrase",
325num = id, 325num = id,
326["nil"] = fixed(Tag.b"nil"), 326["nil"] = fixed(Tag.b"nil"),
327fail = fixed(Tag.b"fail"),
327Open = fixed"{", 328Open = fixed"{",
328part = section("h1", true), 329part = section("h1", true),
329Pat = compose(verbfixed, prepos("'", "'")), 330Pat = compose(verbfixed, prepos("'", "'")),
diff --git a/manual/manual.of b/manual/manual.of
index bb6ae884..53073a54 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -4058,12 +4058,15 @@ Returns 0 if the userdata does not have that value.
4058 4058
4059} 4059}
4060 4060
4061@APIEntry{void lua_setmetatable (lua_State *L, int index);| 4061@APIEntry{int lua_setmetatable (lua_State *L, int index);|
4062@apii{1,0,-} 4062@apii{1,0,-}
4063 4063
4064Pops a table from the stack and 4064Pops a table from the stack and
4065sets it as the new metatable for the value at the given index. 4065sets it as the new metatable for the value at the given index.
4066 4066
4067(For historical reasons, this function returns an @id{int},
4068which now is always 1.)
4069
4067} 4070}
4068 4071
4069@APIEntry{void lua_settable (lua_State *L, int index);| 4072@APIEntry{void lua_settable (lua_State *L, int index);|
@@ -5782,7 +5785,7 @@ that will be called to close the stream
5782when the handle is closed or collected; 5785when the handle is closed or collected;
5783this function receives the file handle as its sole argument and 5786this function receives the file handle as its sole argument and
5784must return either @true, in case of success, 5787must return either @true, in case of success,
5785or @nil plus an error message, in case of error. 5788or a false value plus an error message, in case of error.
5786Once Lua calls this field, 5789Once Lua calls this field,
5787it changes the field value to @id{NULL} 5790it changes the field value to @id{NULL}
5788to signal that the handle is closed. 5791to signal that the handle is closed.
@@ -5904,6 +5907,14 @@ to its expected parameters.
5904For instance, a function documented as @T{foo(arg)} 5907For instance, a function documented as @T{foo(arg)}
5905should not be called without an argument. 5908should not be called without an argument.
5906 5909
5910The notation @fail means a return value representing
5911some kind of failure or the absence of a better value to return.
5912Currently, @fail is equal to @nil,
5913but that may change in future versions.
5914The recommendation is to test the success of these functions
5915with @T{(not status)}, instead of @T{(status == nil)}.
5916
5917
5907Currently, Lua has the following standard libraries: 5918Currently, Lua has the following standard libraries:
5908@itemize{ 5919@itemize{
5909 5920
@@ -6108,8 +6119,8 @@ with previous results.
6108A return of an empty string, @nil, or no value signals the end of the chunk. 6119A return of an empty string, @nil, or no value signals the end of the chunk.
6109 6120
6110If there are no syntactic errors, 6121If there are no syntactic errors,
6111returns the compiled chunk as a function; 6122@id{load} returns the compiled chunk as a function;
6112otherwise, returns @nil plus the error message. 6123otherwise, it returns @fail plus the error message.
6113 6124
6114When you load a main chunk, 6125When you load a main chunk,
6115the resulting function will always have exactly one upvalue, 6126the resulting function will always have exactly one upvalue,
@@ -6301,7 +6312,7 @@ When called with no @id{base},
6301If the argument is already a number or 6312If the argument is already a number or
6302a string convertible to a number, 6313a string convertible to a number,
6303then @id{tonumber} returns this number; 6314then @id{tonumber} returns this number;
6304otherwise, it returns @nil. 6315otherwise, it returns @fail.
6305 6316
6306The conversion of strings can result in integers or floats, 6317The conversion of strings can result in integers or floats,
6307according to the lexical conventions of Lua @see{lexical}. 6318according to the lexical conventions of Lua @see{lexical}.
@@ -6315,7 +6326,7 @@ In bases @N{above 10}, the letter @Char{A} (in either upper or lower case)
6315@N{represents 10}, @Char{B} @N{represents 11}, and so forth, 6326@N{represents 10}, @Char{B} @N{represents 11}, and so forth,
6316with @Char{Z} representing 35. 6327with @Char{Z} representing 35.
6317If the string @id{e} is not a valid numeral in the given base, 6328If the string @id{e} is not a valid numeral in the given base,
6318the function returns @nil. 6329the function returns @fail.
6319 6330
6320} 6331}
6321 6332
@@ -6762,7 +6773,7 @@ will try to open the files
6762 6773
6763Returns the resulting name of the first file that it can 6774Returns the resulting name of the first file that it can
6764open in read mode (after closing the file), 6775open in read mode (after closing the file),
6765or @nil plus an error message if none succeeds. 6776or @fail plus an error message if none succeeds.
6766(This error message lists all file names it tried to open.) 6777(This error message lists all file names it tried to open.)
6767 6778
6768} 6779}
@@ -6841,7 +6852,7 @@ Looks for the first match of
6841@id{pattern} @see{pm} in the string @id{s}. 6852@id{pattern} @see{pm} in the string @id{s}.
6842If it finds a match, then @id{find} returns the indices @N{of @T{s}} 6853If it finds a match, then @id{find} returns the indices @N{of @T{s}}
6843where this occurrence starts and ends; 6854where this occurrence starts and ends;
6844otherwise, it returns @nil. 6855otherwise, it returns @fail.
6845A third, optional numeric argument @id{init} specifies 6856A third, optional numeric argument @id{init} specifies
6846where to start the search; 6857where to start the search;
6847its default value @N{is 1} and can be negative. 6858its default value @N{is 1} and can be negative.
@@ -7034,7 +7045,7 @@ Looks for the first @emph{match} of
7034the @id{pattern} @see{pm} in the string @id{s}. 7045the @id{pattern} @see{pm} in the string @id{s}.
7035If it finds one, then @id{match} returns 7046If it finds one, then @id{match} returns
7036the captures from the pattern; 7047the captures from the pattern;
7037otherwise it returns @nil. 7048otherwise it returns @fail.
7038If @id{pattern} specifies no captures, 7049If @id{pattern} specifies no captures,
7039then the whole match is returned. 7050then the whole match is returned.
7040A third, optional numeric argument @id{init} specifies 7051A third, optional numeric argument @id{init} specifies
@@ -7499,7 +7510,7 @@ Returns the number of UTF-8 characters in string @id{s}
7499that start between positions @id{i} and @id{j} (both inclusive). 7510that start between positions @id{i} and @id{j} (both inclusive).
7500The default for @id{i} is @num{1} and for @id{j} is @num{-1}. 7511The default for @id{i} is @num{1} and for @id{j} is @num{-1}.
7501If it finds any invalid byte sequence, 7512If it finds any invalid byte sequence,
7502returns a false value plus the position of the first invalid byte. 7513returns @fail plus the position of the first invalid byte.
7503 7514
7504} 7515}
7505 7516
@@ -7515,7 +7526,7 @@ so that @T{utf8.offset(s, -n)} gets the offset of the
7515@id{n}-th character from the end of the string. 7526@id{n}-th character from the end of the string.
7516If the specified character is neither in the subject 7527If the specified character is neither in the subject
7517nor right after its end, 7528nor right after its end,
7518the function returns @nil. 7529the function returns @fail.
7519 7530
7520As a special case, 7531As a special case,
7521when @id{n} is 0 the function returns the start of the encoding 7532when @id{n} is 0 the function returns the start of the encoding
@@ -7850,7 +7861,7 @@ Returns the tangent of @id{x} (assumed to be in radians).
7850 7861
7851If the value @id{x} is convertible to an integer, 7862If the value @id{x} is convertible to an integer,
7852returns that integer. 7863returns that integer.
7853Otherwise, returns @nil. 7864Otherwise, returns @fail.
7854 7865
7855} 7866}
7856 7867
@@ -7858,7 +7869,7 @@ Otherwise, returns @nil.
7858 7869
7859Returns @St{integer} if @id{x} is an integer, 7870Returns @St{integer} if @id{x} is an integer,
7860@St{float} if it is a float, 7871@St{float} if it is a float,
7861or @nil if @id{x} is not a number. 7872or @fail if @id{x} is not a number.
7862 7873
7863} 7874}
7864 7875
@@ -7897,10 +7908,10 @@ three predefined file handles with their usual meanings from C:
7897The I/O library never closes these files. 7908The I/O library never closes these files.
7898 7909
7899Unless otherwise stated, 7910Unless otherwise stated,
7900all I/O functions return @nil on failure, 7911all I/O functions return @fail on failure,
7901plus an error message as a second result and 7912plus an error message as a second result and
7902a system-dependent error code as a third result, 7913a system-dependent error code as a third result,
7903and some value different from @nil on success. 7914and some non-false value on success.
7904On non-POSIX systems, 7915On non-POSIX systems,
7905the computation of the error message and error code 7916the computation of the error message and error code
7906in case of errors 7917in case of errors
@@ -8021,7 +8032,7 @@ and it is automatically removed when the program ends.
8021Checks whether @id{obj} is a valid file handle. 8032Checks whether @id{obj} is a valid file handle.
8022Returns the string @T{"file"} if @id{obj} is an open file handle, 8033Returns the string @T{"file"} if @id{obj} is an open file handle,
8023@T{"closed file"} if @id{obj} is a closed file handle, 8034@T{"closed file"} if @id{obj} is a closed file handle,
8024or @nil if @id{obj} is not a file handle. 8035or @fail if @id{obj} is not a file handle.
8025 8036
8026} 8037}
8027 8038
@@ -8075,7 +8086,7 @@ Reads the file @id{file},
8075according to the given formats, which specify what to read. 8086according to the given formats, which specify what to read.
8076For each format, 8087For each format,
8077the function returns a string or a number with the characters read, 8088the function returns a string or a number with the characters read,
8078or @nil if it cannot read data with the specified format. 8089or @fail if it cannot read data with the specified format.
8079(In this latter case, 8090(In this latter case,
8080the function does not read subsequent formats.) 8091the function does not read subsequent formats.)
8081When called without arguments, 8092When called without arguments,
@@ -8094,31 +8105,32 @@ is a valid prefix for a numeral;
8094if that prefix does not form a valid numeral 8105if that prefix does not form a valid numeral
8095(e.g., an empty string, @St{0x}, or @St{3.4e-}) 8106(e.g., an empty string, @St{0x}, or @St{3.4e-})
8096or it is too long (more than 200 characters), 8107or it is too long (more than 200 characters),
8097it is discarded and the format returns @nil. 8108it is discarded and the format returns @fail.
8098} 8109}
8099 8110
8100@item{@St{a}| 8111@item{@St{a}|
8101reads the whole file, starting at the current position. 8112reads the whole file, starting at the current position.
8102On end of file, it returns the empty string. 8113On end of file, it returns the empty string;
8114this format never fails.
8103} 8115}
8104 8116
8105@item{@St{l}| 8117@item{@St{l}|
8106reads the next line skipping the end of line, 8118reads the next line skipping the end of line,
8107returning @nil on end of file. 8119returning @fail on end of file.
8108This is the default format. 8120This is the default format.
8109} 8121}
8110 8122
8111@item{@St{L}| 8123@item{@St{L}|
8112reads the next line keeping the end-of-line character (if present), 8124reads the next line keeping the end-of-line character (if present),
8113returning @nil on end of file. 8125returning @fail on end of file.
8114} 8126}
8115 8127
8116@item{@emph{number}| 8128@item{@emph{number}|
8117reads a string with up to this number of bytes, 8129reads a string with up to this number of bytes,
8118returning @nil on end of file. 8130returning @fail on end of file.
8119If @id{number} is zero, 8131If @id{number} is zero,
8120it reads nothing and returns an empty string, 8132it reads nothing and returns an empty string,
8121or @nil on end of file. 8133or @fail on end of file.
8122} 8134}
8123 8135
8124} 8136}
@@ -8139,7 +8151,7 @@ specified by the string @id{whence}, as follows:
8139} 8151}
8140In case of success, @id{seek} returns the final file position, 8152In case of success, @id{seek} returns the final file position,
8141measured in bytes from the beginning of the file. 8153measured in bytes from the beginning of the file.
8142If @id{seek} fails, it returns @nil, 8154If @id{seek} fails, it returns @fail,
8143plus a string describing the error. 8155plus a string describing the error.
8144 8156
8145The default value for @id{whence} is @T{"cur"}, 8157The default value for @id{whence} is @T{"cur"},
@@ -8179,7 +8191,6 @@ Writes the value of each of its arguments to @id{file}.
8179The arguments must be strings or numbers. 8191The arguments must be strings or numbers.
8180 8192
8181In case of success, this function returns @id{file}. 8193In case of success, this function returns @id{file}.
8182Otherwise it returns @nil plus a string describing the error.
8183 8194
8184} 8195}
8185 8196
@@ -8251,7 +8262,7 @@ This function is equivalent to the @ANSI{system}.
8251It passes @id{command} to be executed by an operating system shell. 8262It passes @id{command} to be executed by an operating system shell.
8252Its first result is @true 8263Its first result is @true
8253if the command terminated successfully, 8264if the command terminated successfully,
8254or @nil otherwise. 8265or @fail otherwise.
8255After this first result 8266After this first result
8256the function returns a string plus a number, 8267the function returns a string plus a number,
8257as follows: 8268as follows:
@@ -8293,7 +8304,7 @@ closes the Lua state before exiting.
8293@LibEntry{os.getenv (varname)| 8304@LibEntry{os.getenv (varname)|
8294 8305
8295Returns the value of the process environment variable @id{varname}, 8306Returns the value of the process environment variable @id{varname},
8296or @nil if the variable is not defined. 8307or @fail if the variable is not defined.
8297 8308
8298} 8309}
8299 8310
@@ -8301,7 +8312,7 @@ or @nil if the variable is not defined.
8301 8312
8302Deletes the file (or empty directory, on @x{POSIX} systems) 8313Deletes the file (or empty directory, on @x{POSIX} systems)
8303with the given name. 8314with the given name.
8304If this function fails, it returns @nil, 8315If this function fails, it returns @fail
8305plus a string describing the error and the error code. 8316plus a string describing the error and the error code.
8306Otherwise, it returns true. 8317Otherwise, it returns true.
8307 8318
@@ -8310,7 +8321,7 @@ Otherwise, it returns true.
8310@LibEntry{os.rename (oldname, newname)| 8321@LibEntry{os.rename (oldname, newname)|
8311 8322
8312Renames the file or directory named @id{oldname} to @id{newname}. 8323Renames the file or directory named @id{oldname} to @id{newname}.
8313If this function fails, it returns @nil, 8324If this function fails, it returns @fail,
8314plus a string describing the error and the error code. 8325plus a string describing the error and the error code.
8315Otherwise, it returns true. 8326Otherwise, it returns true.
8316 8327
@@ -8325,7 +8336,7 @@ Sets the current locale of the program.
8325@T{"monetary"}, @T{"numeric"}, or @T{"time"}; 8336@T{"monetary"}, @T{"numeric"}, or @T{"time"};
8326the default category is @T{"all"}. 8337the default category is @T{"all"}.
8327The function returns the name of the new locale, 8338The function returns the name of the new locale,
8328or @nil if the request cannot be honored. 8339or @fail if the request cannot be honored.
8329 8340
8330If @id{locale} is the empty string, 8341If @id{locale} is the empty string,
8331the current locale is set to an implementation-defined native locale. 8342the current locale is set to an implementation-defined native locale.
@@ -8444,6 +8455,8 @@ the current hook function, the current hook mask,
8444and the current hook count, 8455and the current hook count,
8445as set by the @Lid{debug.sethook} function. 8456as set by the @Lid{debug.sethook} function.
8446 8457
8458Returns @fail if there is no active hook.
8459
8447} 8460}
8448 8461
8449@LibEntry{debug.getinfo ([thread,] f [, what])| 8462@LibEntry{debug.getinfo ([thread,] f [, what])|
@@ -8458,7 +8471,7 @@ of the given thread:
8458(except for tail calls, which do not count on the stack); 8471(except for tail calls, which do not count on the stack);
8459and so on. 8472and so on.
8460If @id{f} is a number greater than the number of active functions, 8473If @id{f} is a number greater than the number of active functions,
8461then @id{getinfo} returns @nil. 8474then @id{getinfo} returns @fail.
8462 8475
8463The returned table can contain all the fields returned by @Lid{lua_getinfo}, 8476The returned table can contain all the fields returned by @Lid{lua_getinfo},
8464with the string @id{what} describing which fields to fill in. 8477with the string @id{what} describing which fields to fill in.
@@ -8496,7 +8509,8 @@ Compile-time constants may not appear in this listing,
8496if they were optimized away by the compiler. 8509if they were optimized away by the compiler.
8497Negative indices refer to vararg arguments; 8510Negative indices refer to vararg arguments;
8498@num{-1} is the first vararg argument. 8511@num{-1} is the first vararg argument.
8499The function returns @nil if there is no variable with the given index, 8512The function returns @fail
8513if there is no variable with the given index,
8500and raises an error when called with a level out of range. 8514and raises an error when called with a level out of range.
8501(You can call @Lid{debug.getinfo} to check whether the level is valid.) 8515(You can call @Lid{debug.getinfo} to check whether the level is valid.)
8502 8516
@@ -8527,7 +8541,8 @@ Returns the registry table @see{registry}.
8527 8541
8528This function returns the name and the value of the upvalue 8542This function returns the name and the value of the upvalue
8529with index @id{up} of the function @id{f}. 8543with index @id{up} of the function @id{f}.
8530The function returns @nil if there is no upvalue with the given index. 8544The function returns @fail
8545if there is no upvalue with the given index.
8531 8546
8532(For Lua functions, 8547(For Lua functions,
8533upvalues are the external local variables that the function uses, 8548upvalues are the external local variables that the function uses,
@@ -8615,7 +8630,7 @@ and @N{level 1} is the hook function.)
8615 8630
8616This function assigns the value @id{value} to the local variable 8631This function assigns the value @id{value} to the local variable
8617with index @id{local} of the function at level @id{level} of the stack. 8632with index @id{local} of the function at level @id{level} of the stack.
8618The function returns @nil if there is no local 8633The function returns @fail if there is no local
8619variable with the given index, 8634variable with the given index,
8620and raises an error when called with a @id{level} out of range. 8635and raises an error when called with a @id{level} out of range.
8621(You can call @id{getinfo} to check whether the level is valid.) 8636(You can call @id{getinfo} to check whether the level is valid.)
@@ -8638,7 +8653,7 @@ Returns @id{value}.
8638 8653
8639This function assigns the value @id{value} to the upvalue 8654This function assigns the value @id{value} to the upvalue
8640with index @id{up} of the function @id{f}. 8655with index @id{up} of the function @id{f}.
8641The function returns @nil if there is no upvalue 8656The function returns @fail if there is no upvalue
8642with the given index. 8657with the given index.
8643Otherwise, it returns the name of the upvalue. 8658Otherwise, it returns the name of the upvalue.
8644 8659
@@ -8653,7 +8668,7 @@ the @id{n}-th user value associated to the given @id{udata}.
8653@id{udata} must be a full userdata. 8668@id{udata} must be a full userdata.
8654 8669
8655Returns @id{udata}, 8670Returns @id{udata},
8656or @nil if the userdata does not have that value. 8671or @fail if the userdata does not have that value.
8657 8672
8658} 8673}
8659 8674
diff --git a/testes/api.lua b/testes/api.lua
index 4f9d6717..b2680633 100644
--- a/testes/api.lua
+++ b/testes/api.lua
@@ -698,7 +698,7 @@ for k, v in ipairs(t) do
698 assert(v1 == v and p) 698 assert(v1 == v and p)
699end 699end
700 700
701assert(debug.getuservalue(4) == nil) 701assert(not debug.getuservalue(4))
702 702
703debug.setuservalue(b, function () return 10 end, 10) 703debug.setuservalue(b, function () return 10 end, 10)
704collectgarbage() -- function should not be collected 704collectgarbage() -- function should not be collected
diff --git a/testes/db.lua b/testes/db.lua
index a64a1130..c43243a6 100644
--- a/testes/db.lua
+++ b/testes/db.lua
@@ -351,12 +351,12 @@ assert(g(0,0) == 30)
351 351
352 352
353debug.sethook(nil); 353debug.sethook(nil);
354assert(debug.gethook() == nil) 354assert(not debug.gethook())
355 355
356 356
357-- minimal tests for setuservalue/getuservalue 357-- minimal tests for setuservalue/getuservalue
358do 358do
359 assert(debug.setuservalue(io.stdin, 10) == nil) 359 assert(not debug.setuservalue(io.stdin, 10))
360 local a, b = debug.getuservalue(io.stdin, 10) 360 local a, b = debug.getuservalue(io.stdin, 10)
361 assert(a == nil and not b) 361 assert(a == nil and not b)
362end 362end
@@ -414,7 +414,7 @@ end, "c")
414a:f(1,2,3,4,5) 414a:f(1,2,3,4,5)
415assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) 415assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
416assert(XX == 12) 416assert(XX == 12)
417assert(debug.gethook() == nil) 417assert(not debug.gethook())
418 418
419 419
420-- testing access to local variables in return hook (bug in 5.2) 420-- testing access to local variables in return hook (bug in 5.2)
diff --git a/testes/errors.lua b/testes/errors.lua
index 6e7b8004..f9623b1d 100644
--- a/testes/errors.lua
+++ b/testes/errors.lua
@@ -18,7 +18,7 @@ end
18 18
19local function doit (s) 19local function doit (s)
20 local f, msg = load(s) 20 local f, msg = load(s)
21 if f == nil then return msg end 21 if not f then return msg end
22 local cond, msg = pcall(f) 22 local cond, msg = pcall(f)
23 return (not cond) and msg 23 return (not cond) and msg
24end 24end
@@ -312,8 +312,8 @@ end
312 312
313local function lineerror (s, l) 313local function lineerror (s, l)
314 local err,msg = pcall(load(s)) 314 local err,msg = pcall(load(s))
315 local line = string.match(msg, ":(%d+):") 315 local line = tonumber(string.match(msg, ":(%d+):"))
316 assert(tonumber(line) == l) 316 assert(line == l or (not line and not l))
317end 317end
318 318
319lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2) 319lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
@@ -359,7 +359,7 @@ local p = [[
359g() 359g()
360]] 360]]
361X=3;lineerror((p), 3) 361X=3;lineerror((p), 3)
362X=0;lineerror((p), nil) 362X=0;lineerror((p), false)
363X=1;lineerror((p), 2) 363X=1;lineerror((p), 2)
364X=2;lineerror((p), 1) 364X=2;lineerror((p), 1)
365 365
@@ -510,7 +510,7 @@ checksyntax("a\1a = 1", "", "<\\1>", 1)
510checksyntax("\255a = 1", "", "<\\255>", 1) 510checksyntax("\255a = 1", "", "<\\255>", 1)
511 511
512doit('I = load("a=9+"); a=3') 512doit('I = load("a=9+"); a=3')
513assert(a==3 and I == nil) 513assert(a==3 and not I)
514print('+') 514print('+')
515 515
516lim = 1000 516lim = 1000
diff --git a/testes/files.lua b/testes/files.lua
index 585e5948..677c0dc2 100644
--- a/testes/files.lua
+++ b/testes/files.lua
@@ -184,7 +184,7 @@ three
184 local f <close> = assert(io.open(file, "r")) 184 local f <close> = assert(io.open(file, "r"))
185 -- second item failing 185 -- second item failing
186 l1, n1, n2, dummy = f:read("l", "n", "n", "l") 186 l1, n1, n2, dummy = f:read("l", "n", "n", "l")
187 assert(l1 == "a line" and n1 == nil) 187 assert(l1 == "a line" and not n1)
188end 188end
189assert(os.remove(file)) 189assert(os.remove(file))
190 190
@@ -228,7 +228,7 @@ assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n")
228assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e") 228assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e")
229 229
230do -- attempt to read too long number 230do -- attempt to read too long number
231 assert(f:read("n") == nil) -- fails 231 assert(not f:read("n")) -- fails
232 local s = f:read("L") -- read rest of line 232 local s = f:read("L") -- read rest of line
233 assert(string.find(s, "^00*\n$")) -- lots of 0's left 233 assert(string.find(s, "^00*\n$")) -- lots of 0's left
234end 234end
@@ -314,13 +314,13 @@ assert(io.read() == "fourth_line")
314assert(io.read() == "") -- empty line 314assert(io.read() == "") -- empty line
315assert(io.read('n') == 3450) 315assert(io.read('n') == 3450)
316assert(io.read(1) == '\n') 316assert(io.read(1) == '\n')
317assert(io.read(0) == nil) -- end of file 317assert(not io.read(0)) -- end of file
318assert(io.read(1) == nil) -- end of file 318assert(not io.read(1)) -- end of file
319assert(io.read(30000) == nil) -- end of file 319assert(not io.read(30000)) -- end of file
320assert(({io.read(1)})[2] == undef) 320assert(({io.read(1)})[2] == undef)
321assert(io.read() == nil) -- end of file 321assert(not io.read()) -- end of file
322assert(({io.read()})[2] == undef) 322assert(({io.read()})[2] == undef)
323assert(io.read('n') == nil) -- end of file 323assert(not io.read('n')) -- end of file
324assert(({io.read('n')})[2] == undef) 324assert(({io.read('n')})[2] == undef)
325assert(io.read('a') == '') -- end of file (OK for 'a') 325assert(io.read('a') == '') -- end of file (OK for 'a')
326assert(io.read('a') == '') -- end of file (OK for 'a') 326assert(io.read('a') == '') -- end of file (OK for 'a')
@@ -356,7 +356,7 @@ assert(io.read(string.len(t)) == t)
356assert(io.read(1) == ' ') 356assert(io.read(1) == ' ')
357assert(io.read(0)) 357assert(io.read(0))
358assert(io.read('a') == ';end of file\n') 358assert(io.read('a') == ';end of file\n')
359assert(io.read(0) == nil) 359assert(not io.read(0))
360assert(io.close(io.input())) 360assert(io.close(io.input()))
361 361
362 362
@@ -364,7 +364,7 @@ assert(io.close(io.input()))
364do 364do
365 local function ismsg (m) 365 local function ismsg (m)
366 -- error message is not a code number 366 -- error message is not a code number
367 return (type(m) == "string" and tonumber(m) == nil) 367 return (type(m) == "string" and not tonumber(m))
368 end 368 end
369 369
370 -- read 370 -- read
@@ -393,7 +393,7 @@ assert(io.read"L" == "\n")
393assert(io.read"L" == "\n") 393assert(io.read"L" == "\n")
394assert(io.read"L" == "line\n") 394assert(io.read"L" == "line\n")
395assert(io.read"L" == "other") 395assert(io.read"L" == "other")
396assert(io.read"L" == nil) 396assert(not io.read"L")
397io.input():close() 397io.input():close()
398 398
399local f = assert(io.open(file)) 399local f = assert(io.open(file))
@@ -462,7 +462,7 @@ end
462-- test for multipe arguments in 'lines' 462-- test for multipe arguments in 'lines'
463io.output(file); io.write"0123456789\n":close() 463io.output(file); io.write"0123456789\n":close()
464for a,b in io.lines(file, 1, 1) do 464for a,b in io.lines(file, 1, 1) do
465 if a == "\n" then assert(b == nil) 465 if a == "\n" then assert(not b)
466 else assert(tonumber(a) == tonumber(b) - 1) 466 else assert(tonumber(a) == tonumber(b) - 1)
467 end 467 end
468end 468end
@@ -473,13 +473,13 @@ end
473 473
474for a,b,c in io.lines(file, "a", 0, 1) do 474for a,b,c in io.lines(file, "a", 0, 1) do
475 if a == "" then break end 475 if a == "" then break end
476 assert(a == "0123456789\n" and b == nil and c == nil) 476 assert(a == "0123456789\n" and not b and not c)
477end 477end
478collectgarbage() -- to close file in previous iteration 478collectgarbage() -- to close file in previous iteration
479 479
480io.output(file); io.write"00\n10\n20\n30\n40\n":close() 480io.output(file); io.write"00\n10\n20\n30\n40\n":close()
481for a, b in io.lines(file, "n", "n") do 481for a, b in io.lines(file, "n", "n") do
482 if a == 40 then assert(b == nil) 482 if a == 40 then assert(not b)
483 else assert(a == b - 10) 483 else assert(a == b - 10)
484 end 484 end
485end 485end
@@ -654,7 +654,7 @@ and the rest of the file
654io.input(file) 654io.input(file)
655local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10) 655local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10)
656assert(io.close(io.input())) 656assert(io.close(io.input()))
657assert(_ == ' ' and __ == nil) 657assert(_ == ' ' and not __)
658assert(type(a) == 'number' and a==123.4 and b==-56e-2) 658assert(type(a) == 'number' and a==123.4 and b==-56e-2)
659assert(d=='second line' and e=='third line') 659assert(d=='second line' and e=='third line')
660assert(h==[[ 660assert(h==[[
@@ -706,7 +706,7 @@ if not _soft then
706 io.input():seek('set', 0) 706 io.input():seek('set', 0)
707 y = io.read() -- huge line 707 y = io.read() -- huge line
708 assert(x == y..'\n'..io.read()) 708 assert(x == y..'\n'..io.read())
709 assert(io.read() == nil) 709 assert(not io.read())
710 io.close(io.input()) 710 io.close(io.input())
711 assert(os.remove(file)) 711 assert(os.remove(file))
712 x = nil; y = nil 712 x = nil; y = nil
diff --git a/testes/literals.lua b/testes/literals.lua
index 27f9377d..e101eabf 100644
--- a/testes/literals.lua
+++ b/testes/literals.lua
@@ -281,7 +281,7 @@ if os.setlocale("pt_BR") or os.setlocale("ptb") then
281 281
282 assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1) 282 assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
283 283
284 assert(tonumber"inf" == nil and tonumber"NAN" == nil) 284 assert(not tonumber"inf" and not tonumber"NAN")
285 285
286 assert(assert(load(string.format("return %q", 4.51)))() == 4.51) 286 assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
287 287
diff --git a/testes/math.lua b/testes/math.lua
index bad43901..c7dc8285 100644
--- a/testes/math.lua
+++ b/testes/math.lua
@@ -39,7 +39,7 @@ do
39end 39end
40 40
41assert(math.type(0) == "integer" and math.type(0.0) == "float" 41assert(math.type(0) == "integer" and math.type(0.0) == "float"
42 and math.type("10") == nil) 42 and not math.type("10"))
43 43
44 44
45local function checkerror (msg, f, ...) 45local function checkerror (msg, f, ...)
@@ -381,17 +381,17 @@ assert(tonumber(1/0) == 1/0)
381 381
382-- 'tonumber' with strings 382-- 'tonumber' with strings
383assert(tonumber("0") == 0) 383assert(tonumber("0") == 0)
384assert(tonumber("") == nil) 384assert(not tonumber(""))
385assert(tonumber(" ") == nil) 385assert(not tonumber(" "))
386assert(tonumber("-") == nil) 386assert(not tonumber("-"))
387assert(tonumber(" -0x ") == nil) 387assert(not tonumber(" -0x "))
388assert(tonumber{} == nil) 388assert(not tonumber{})
389assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and 389assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and
390 tonumber'.01' == 0.01 and tonumber'-1.' == -1 and 390 tonumber'.01' == 0.01 and tonumber'-1.' == -1 and
391 tonumber'+1.' == 1) 391 tonumber'+1.' == 1)
392assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and 392assert(not tonumber'+ 0.01' and not tonumber'+.e1' and
393 tonumber'1e' == nil and tonumber'1.0e+' == nil and 393 not tonumber'1e' and not tonumber'1.0e+' and
394 tonumber'.' == nil) 394 not tonumber'.')
395assert(tonumber('-012') == -010-2) 395assert(tonumber('-012') == -010-2)
396assert(tonumber('-1.2e2') == - - -120) 396assert(tonumber('-1.2e2') == - - -120)
397 397
@@ -445,45 +445,45 @@ local function f (...)
445 end 445 end
446end 446end
447 447
448assert(f(tonumber('fFfa', 15)) == nil) 448assert(not f(tonumber('fFfa', 15)))
449assert(f(tonumber('099', 8)) == nil) 449assert(not f(tonumber('099', 8)))
450assert(f(tonumber('1\0', 2)) == nil) 450assert(not f(tonumber('1\0', 2)))
451assert(f(tonumber('', 8)) == nil) 451assert(not f(tonumber('', 8)))
452assert(f(tonumber(' ', 9)) == nil) 452assert(not f(tonumber(' ', 9)))
453assert(f(tonumber(' ', 9)) == nil) 453assert(not f(tonumber(' ', 9)))
454assert(f(tonumber('0xf', 10)) == nil) 454assert(not f(tonumber('0xf', 10)))
455 455
456assert(f(tonumber('inf')) == nil) 456assert(not f(tonumber('inf')))
457assert(f(tonumber(' INF ')) == nil) 457assert(not f(tonumber(' INF ')))
458assert(f(tonumber('Nan')) == nil) 458assert(not f(tonumber('Nan')))
459assert(f(tonumber('nan')) == nil) 459assert(not f(tonumber('nan')))
460 460
461assert(f(tonumber(' ')) == nil) 461assert(not f(tonumber(' ')))
462assert(f(tonumber('')) == nil) 462assert(not f(tonumber('')))
463assert(f(tonumber('1 a')) == nil) 463assert(not f(tonumber('1 a')))
464assert(f(tonumber('1 a', 2)) == nil) 464assert(not f(tonumber('1 a', 2)))
465assert(f(tonumber('1\0')) == nil) 465assert(not f(tonumber('1\0')))
466assert(f(tonumber('1 \0')) == nil) 466assert(not f(tonumber('1 \0')))
467assert(f(tonumber('1\0 ')) == nil) 467assert(not f(tonumber('1\0 ')))
468assert(f(tonumber('e1')) == nil) 468assert(not f(tonumber('e1')))
469assert(f(tonumber('e 1')) == nil) 469assert(not f(tonumber('e 1')))
470assert(f(tonumber(' 3.4.5 ')) == nil) 470assert(not f(tonumber(' 3.4.5 ')))
471 471
472 472
473-- testing 'tonumber' for invalid hexadecimal formats 473-- testing 'tonumber' for invalid hexadecimal formats
474 474
475assert(tonumber('0x') == nil) 475assert(not tonumber('0x'))
476assert(tonumber('x') == nil) 476assert(not tonumber('x'))
477assert(tonumber('x3') == nil) 477assert(not tonumber('x3'))
478assert(tonumber('0x3.3.3') == nil) -- two decimal points 478assert(not tonumber('0x3.3.3')) -- two decimal points
479assert(tonumber('00x2') == nil) 479assert(not tonumber('00x2'))
480assert(tonumber('0x 2') == nil) 480assert(not tonumber('0x 2'))
481assert(tonumber('0 x2') == nil) 481assert(not tonumber('0 x2'))
482assert(tonumber('23x') == nil) 482assert(not tonumber('23x'))
483assert(tonumber('- 0xaa') == nil) 483assert(not tonumber('- 0xaa'))
484assert(tonumber('-0xaaP ') == nil) -- no exponent 484assert(not tonumber('-0xaaP ')) -- no exponent
485assert(tonumber('0x0.51p') == nil) 485assert(not tonumber('0x0.51p'))
486assert(tonumber('0x5p+-2') == nil) 486assert(not tonumber('0x5p+-2'))
487 487
488 488
489-- testing hexadecimal numerals 489-- testing hexadecimal numerals
@@ -705,19 +705,19 @@ do -- testing floor & ceil
705 assert(eqT(math.tointeger(maxint), maxint)) 705 assert(eqT(math.tointeger(maxint), maxint))
706 assert(eqT(math.tointeger(maxint .. ""), maxint)) 706 assert(eqT(math.tointeger(maxint .. ""), maxint))
707 assert(eqT(math.tointeger(minint + 0.0), minint)) 707 assert(eqT(math.tointeger(minint + 0.0), minint))
708 assert(math.tointeger(0.0 - minint) == nil) 708 assert(not math.tointeger(0.0 - minint))
709 assert(math.tointeger(math.pi) == nil) 709 assert(not math.tointeger(math.pi))
710 assert(math.tointeger(-math.pi) == nil) 710 assert(not math.tointeger(-math.pi))
711 assert(math.floor(math.huge) == math.huge) 711 assert(math.floor(math.huge) == math.huge)
712 assert(math.ceil(math.huge) == math.huge) 712 assert(math.ceil(math.huge) == math.huge)
713 assert(math.tointeger(math.huge) == nil) 713 assert(not math.tointeger(math.huge))
714 assert(math.floor(-math.huge) == -math.huge) 714 assert(math.floor(-math.huge) == -math.huge)
715 assert(math.ceil(-math.huge) == -math.huge) 715 assert(math.ceil(-math.huge) == -math.huge)
716 assert(math.tointeger(-math.huge) == nil) 716 assert(not math.tointeger(-math.huge))
717 assert(math.tointeger("34.0") == 34) 717 assert(math.tointeger("34.0") == 34)
718 assert(math.tointeger("34.3") == nil) 718 assert(not math.tointeger("34.3"))
719 assert(math.tointeger({}) == nil) 719 assert(not math.tointeger({}))
720 assert(math.tointeger(0/0) == nil) -- NaN 720 assert(not math.tointeger(0/0)) -- NaN
721end 721end
722 722
723 723
diff --git a/testes/pm.lua b/testes/pm.lua
index 4d87fad2..94bb63ca 100644
--- a/testes/pm.lua
+++ b/testes/pm.lua
@@ -28,10 +28,10 @@ a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end
28assert(a == 9 and b == 11); 28assert(a == 9 and b == 11);
29a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position 29a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position
30assert(a == 11 and b == 11) 30assert(a == 11 and b == 11)
31assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending 31assert(not string.find('a\0a\0a\0a\0\0ab', 'b\0')) -- check ending
32assert(string.find('', '\0') == nil) 32assert(not string.find('', '\0'))
33assert(string.find('alo123alo', '12') == 4) 33assert(string.find('alo123alo', '12') == 4)
34assert(string.find('alo123alo', '^12') == nil) 34assert(not string.find('alo123alo', '^12'))
35 35
36assert(string.match("aaab", ".*b") == "aaab") 36assert(string.match("aaab", ".*b") == "aaab")
37assert(string.match("aaa", ".*a") == "aaa") 37assert(string.match("aaa", ".*a") == "aaa")
@@ -57,17 +57,17 @@ assert(f('aaa', 'ab*a') == 'aa')
57assert(f('aba', 'ab*a') == 'aba') 57assert(f('aba', 'ab*a') == 'aba')
58assert(f('aaab', 'a+') == 'aaa') 58assert(f('aaab', 'a+') == 'aaa')
59assert(f('aaa', '^.+$') == 'aaa') 59assert(f('aaa', '^.+$') == 'aaa')
60assert(f('aaa', 'b+') == nil) 60assert(not f('aaa', 'b+'))
61assert(f('aaa', 'ab+a') == nil) 61assert(not f('aaa', 'ab+a'))
62assert(f('aba', 'ab+a') == 'aba') 62assert(f('aba', 'ab+a') == 'aba')
63assert(f('a$a', '.$') == 'a') 63assert(f('a$a', '.$') == 'a')
64assert(f('a$a', '.%$') == 'a$') 64assert(f('a$a', '.%$') == 'a$')
65assert(f('a$a', '.$.') == 'a$a') 65assert(f('a$a', '.$.') == 'a$a')
66assert(f('a$a', '$$') == nil) 66assert(not f('a$a', '$$'))
67assert(f('a$b', 'a$') == nil) 67assert(not f('a$b', 'a$'))
68assert(f('a$a', '$') == '') 68assert(f('a$a', '$') == '')
69assert(f('', 'b*') == '') 69assert(f('', 'b*') == '')
70assert(f('aaa', 'bb*') == nil) 70assert(not f('aaa', 'bb*'))
71assert(f('aaab', 'a-') == '') 71assert(f('aaab', 'a-') == '')
72assert(f('aaa', '^.-$') == 'aaa') 72assert(f('aaa', '^.-$') == 'aaa')
73assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') 73assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab')
@@ -101,7 +101,7 @@ end
101assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o") 101assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o")
102assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3') 102assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3')
103assert(f1('=======', '^(=*)=%1$') == '=======') 103assert(f1('=======', '^(=*)=%1$') == '=======')
104assert(string.match('==========', '^([=]*)=%1$') == nil) 104assert(not string.match('==========', '^([=]*)=%1$'))
105 105
106local function range (i, j) 106local function range (i, j)
107 if i <= j then 107 if i <= j then
@@ -135,7 +135,7 @@ print('+');
135assert(string.match("alo xyzK", "(%w+)K") == "xyz") 135assert(string.match("alo xyzK", "(%w+)K") == "xyz")
136assert(string.match("254 K", "(%d*)K") == "") 136assert(string.match("254 K", "(%d*)K") == "")
137assert(string.match("alo ", "(%w*)$") == "") 137assert(string.match("alo ", "(%w*)$") == "")
138assert(string.match("alo ", "(%w+)$") == nil) 138assert(not string.match("alo ", "(%w+)$"))
139assert(string.find("(�lo)", "%(�") == 1) 139assert(string.find("(�lo)", "%(�") == 1)
140local a, b, c, d, e = string.match("�lo alo", "^(((.).).* (%w*))$") 140local a, b, c, d, e = string.match("�lo alo", "^(((.).).* (%w*))$")
141assert(a == '�lo alo' and b == '�l' and c == '�' and d == 'alo' and e == nil) 141assert(a == '�lo alo' and b == '�l' and c == '�' and d == 'alo' and e == nil)
@@ -209,7 +209,7 @@ assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4)
209 209
210 210
211function isbalanced (s) 211function isbalanced (s)
212 return string.find(string.gsub(s, "%b()", ""), "[()]") == nil 212 return not string.find(string.gsub(s, "%b()", ""), "[()]")
213end 213end
214 214
215assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a")) 215assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a"))
diff --git a/testes/strings.lua b/testes/strings.lua
index 2e0e160f..97875ec0 100644
--- a/testes/strings.lua
+++ b/testes/strings.lua
@@ -56,13 +56,13 @@ a,b = string.find("123456789", "345")
56assert(string.sub("123456789", a, b) == "345") 56assert(string.sub("123456789", a, b) == "345")
57assert(string.find("1234567890123456789", "345", 3) == 3) 57assert(string.find("1234567890123456789", "345", 3) == 3)
58assert(string.find("1234567890123456789", "345", 4) == 13) 58assert(string.find("1234567890123456789", "345", 4) == 13)
59assert(string.find("1234567890123456789", "346", 4) == nil) 59assert(not string.find("1234567890123456789", "346", 4))
60assert(string.find("1234567890123456789", ".45", -9) == 13) 60assert(string.find("1234567890123456789", ".45", -9) == 13)
61assert(string.find("abcdefg", "\0", 5, 1) == nil) 61assert(not string.find("abcdefg", "\0", 5, 1))
62assert(string.find("", "") == 1) 62assert(string.find("", "") == 1)
63assert(string.find("", "", 1) == 1) 63assert(string.find("", "", 1) == 1)
64assert(not string.find("", "", 2)) 64assert(not string.find("", "", 2))
65assert(string.find('', 'aaa', 1) == nil) 65assert(not string.find('', 'aaa', 1))
66assert(('alo(.)alo'):find('(.)', 1, 1) == 4) 66assert(('alo(.)alo'):find('(.)', 1, 1) == 4)
67 67
68assert(string.len("") == 0) 68assert(string.len("") == 0)
diff --git a/testes/utf8.lua b/testes/utf8.lua
index acbb181d..5954f6e8 100644
--- a/testes/utf8.lua
+++ b/testes/utf8.lua
@@ -30,8 +30,8 @@ local function checksyntax (s, t)
30 assert(assert(load(ts))() == s) 30 assert(assert(load(ts))() == s)
31end 31end
32 32
33assert(utf8.offset("alo", 5) == nil) 33assert(not utf8.offset("alo", 5))
34assert(utf8.offset("alo", -4) == nil) 34assert(not utf8.offset("alo", -4))
35 35
36-- 'check' makes several tests over the validity of string 's'. 36-- 'check' makes several tests over the validity of string 's'.
37-- 't' is the list of codepoints of 's'. 37-- 't' is the list of codepoints of 's'.