summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLua Team <team@lua.org>2008-01-19 12:00:00 +0000
committerrepogen <>2008-01-19 12:00:00 +0000
commit6e5d828a4035d80ba8e3e6f827094bf28919357b (patch)
tree13a5b1283950229ec148aa6f2e5fd8928d398924
parent5065c5cce2d4c35d65319fede6b8eddc410fb846 (diff)
downloadlua-github-6e5d828a4035d80ba8e3e6f827094bf28919357b.tar.gz
Lua 5.1.3-rc35.1.3-rc3
-rw-r--r--doc/manual.html19
-rw-r--r--src/ldo.c4
-rw-r--r--src/liolib.c75
3 files changed, 65 insertions, 33 deletions
diff --git a/doc/manual.html b/doc/manual.html
index 7c0fd2b8..b125c13d 100644
--- a/doc/manual.html
+++ b/doc/manual.html
@@ -33,7 +33,7 @@ Freely available under the terms of the
<!-- ====================================================================== -->
<p>
-<!-- $Id: manual.of,v 1.44 2008/01/18 16:41:50 roberto Exp $ -->
+<!-- $Id: manual.of,v 1.45 2008/01/19 00:17:30 roberto Exp $ -->
@@ -3108,7 +3108,7 @@ and 0&nbsp;otherwise.
<hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
-<span class="apii">[-0, +0, <em>m</em>]</span>
+<span class="apii">[-0, +0, <em>-</em>]</span>
<pre>int lua_isstring (lua_State *L, int index);</pre>
<p>
@@ -3582,6 +3582,19 @@ light userdata with the same C&nbsp;address.
+<hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
+<span class="apii">[-0, +1, <em>m</em>]</span>
+<pre>void lua_pushliteral (lua_State *L, const char *s);</pre>
+
+<p>
+This macro is equivalent to <a href="#lua_pushlstring"><code>lua_pushlstring</code></a>,
+but can be used only when <code>s</code> is a literal string.
+In these cases, it automatically provides the string length.
+
+
+
+
+
<hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
<span class="apii">[-0, +1, <em>m</em>]</span>
<pre>void lua_pushlstring (lua_State *L, const char *s, size_t len);</pre>
@@ -8741,7 +8754,7 @@ Here is the complete syntax of Lua in extended BNF.
<HR>
<SMALL>
Last update:
-Fri Jan 18 14:50:20 BRST 2008
+Fri Jan 18 22:32:24 BRST 2008
</SMALL>
<!--
Last change: revised for Lua 5.1.3
diff --git a/src/ldo.c b/src/ldo.c
index 7ddf633c..8de05f72 100644
--- a/src/ldo.c
+++ b/src/ldo.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldo.c,v 2.38.1.2 2008/01/03 15:20:39 roberto Exp $
+** $Id: ldo.c,v 2.38.1.3 2008/01/18 22:31:22 roberto Exp $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -332,7 +332,7 @@ static StkId callrethooks (lua_State *L, StkId firstResult) {
ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
luaD_callhook(L, LUA_HOOKRET, -1);
if (f_isLua(L->ci)) { /* Lua function? */
- while (L->ci->tailcalls--) /* call hook for eventual tail calls */
+ while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
luaD_callhook(L, LUA_HOOKTAILRET, -1);
}
return restorestack(L, fr);
diff --git a/src/liolib.c b/src/liolib.c
index e92984f1..e79ed1cb 100644
--- a/src/liolib.c
+++ b/src/liolib.c
@@ -1,5 +1,5 @@
/*
-** $Id: liolib.c,v 2.73.1.2 2008/01/02 13:56:00 roberto Exp $
+** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@@ -51,7 +51,7 @@ static void fileerror (lua_State *L, int arg, const char *filename) {
}
-#define topfile(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
+#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
static int io_type (lua_State *L) {
@@ -70,7 +70,7 @@ static int io_type (lua_State *L) {
static FILE *tofile (lua_State *L) {
- FILE **f = topfile(L);
+ FILE **f = tofilep(L);
if (*f == NULL)
luaL_error(L, "attempt to use a closed file");
return *f;
@@ -93,24 +93,33 @@ static FILE **newfile (lua_State *L) {
/*
-** this function has a separated environment, which defines the
-** correct __close for 'popen' files
+** function to (not) close the standard files stdin, stdout, and stderr
+*/
+static int io_noclose (lua_State *L) {
+ lua_pushnil(L);
+ lua_pushliteral(L, "cannot close standard file");
+ return 2;
+}
+
+
+/*
+** function to close 'popen' files
*/
static int io_pclose (lua_State *L) {
- FILE **p = topfile(L);
+ FILE **p = tofilep(L);
int ok = lua_pclose(L, *p);
*p = NULL;
return pushresult(L, ok, NULL);
}
+/*
+** function to close regular files
+*/
static int io_fclose (lua_State *L) {
- FILE **p = topfile(L);
- int ok = 0;
- if (*p != stdin && *p != stdout && *p != stderr) {
- ok = (fclose(*p) == 0);
- *p = NULL;
- }
+ FILE **p = tofilep(L);
+ int ok = (fclose(*p) == 0);
+ *p = NULL;
return pushresult(L, ok, NULL);
}
@@ -131,17 +140,18 @@ static int io_close (lua_State *L) {
static int io_gc (lua_State *L) {
- FILE *f = *topfile(L);
- if (f != NULL) /* ignore closed files */
+ FILE *f = *tofilep(L);
+ /* ignore closed files */
+ if (f != NULL)
aux_close(L);
return 0;
}
static int io_tostring (lua_State *L) {
- FILE *f = *topfile(L);
+ FILE *f = *tofilep(L);
if (f == NULL)
- lua_pushstring(L, "file (closed)");
+ lua_pushliteral(L, "file (closed)");
else
lua_pushfstring(L, "file (%p)", f);
return 1;
@@ -157,6 +167,10 @@ static int io_open (lua_State *L) {
}
+/*
+** this function has a separated environment, which defines the
+** correct __close for 'popen' files
+*/
static int io_popen (lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r");
@@ -282,7 +296,7 @@ static int read_line (lua_State *L, FILE *f) {
char *p = luaL_prepbuffer(&b);
if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
luaL_pushresult(&b); /* close buffer */
- return (lua_strlen(L, -1) > 0); /* check whether read something */
+ return (lua_objlen(L, -1) > 0); /* check whether read something */
}
l = strlen(p);
if (l == 0 || p[l-1] != '\n')
@@ -310,7 +324,7 @@ static int read_chars (lua_State *L, FILE *f, size_t n) {
n -= nr; /* still have to read `n' chars */
} while (n > 0 && nr == rlen); /* until end of count or eof */
luaL_pushresult(&b); /* close buffer */
- return (n == 0 || lua_strlen(L, -1) > 0);
+ return (n == 0 || lua_objlen(L, -1) > 0);
}
@@ -504,31 +518,36 @@ static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
lua_pushvalue(L, -1);
lua_rawseti(L, LUA_ENVIRONINDEX, k);
}
- lua_setfield(L, -2, fname);
+ lua_pushvalue(L, -2); /* copy environment */
+ lua_setfenv(L, -2); /* set it */
+ lua_setfield(L, -3, fname);
+}
+
+
+static void newfenv (lua_State *L, lua_CFunction cls) {
+ lua_createtable(L, 0, 1);
+ lua_pushcfunction(L, cls);
+ lua_setfield(L, -2, "__close");
}
LUALIB_API int luaopen_io (lua_State *L) {
createmeta(L);
/* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
- lua_createtable(L, 2, 1);
+ newfenv(L, io_fclose);
lua_replace(L, LUA_ENVIRONINDEX);
/* open library */
luaL_register(L, LUA_IOLIBNAME, iolib);
/* create (and set) default files */
+ newfenv(L, io_noclose); /* close function for default files */
createstdfile(L, stdin, IO_INPUT, "stdin");
createstdfile(L, stdout, IO_OUTPUT, "stdout");
createstdfile(L, stderr, 0, "stderr");
- /* create environment for 'popen' */
+ lua_pop(L, 1); /* pop environment for default files */
lua_getfield(L, -1, "popen");
- lua_createtable(L, 0, 1);
- lua_pushcfunction(L, io_pclose);
- lua_setfield(L, -2, "__close");
- lua_setfenv(L, -2);
+ newfenv(L, io_pclose); /* create environment for 'popen' */
+ lua_setfenv(L, -2); /* set fenv for 'popen' */
lua_pop(L, 1); /* pop 'popen' */
- /* set default close function */
- lua_pushcfunction(L, io_fclose);
- lua_setfield(L, LUA_ENVIRONINDEX, "__close");
return 1;
}