diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-08-16 14:58:02 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-08-16 14:58:02 -0300 |
commit | b96b0b5abbf40cbdbed7952bf35a5a27ddf75928 (patch) | |
tree | 5d9d5463cb7d3424833abab20dd87bce1f4b240f /liolib.c | |
parent | ca13be9af784b7288d3a07d9b5bccb329086e885 (diff) | |
download | lua-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'.
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 10 |
1 files changed, 5 insertions, 5 deletions
@@ -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) { | |||
782 | static int io_noclose (lua_State *L) { | 782 | static 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 | } |