History

Lua 5.1.3-rc3

Lua Team committed 18 years ago

Commit 17b8e881 parent aeb9dbd3 files · +65 −33Browse files

Unified · Split

3 files
  1. doc/manual.html +16 −3
  2. src/ldo.c +2 −2
  3. src/liolib.c +47 −28
doc/manual.html+16−3
@@ -33,7 +33,7 @@ Freely available under the terms of the · 32 unmodified lines
3333 <!-- ====================================================================== -->
3434 <p>
3535
36−<!-- $Id: manual.of,v 1.44 2008/01/18 16:41:50 roberto Exp $ -->
36+<!-- $Id: manual.of,v 1.45 2008/01/19 00:17:30 roberto Exp $ -->
3737
3838
3939
@@ -3108,7 +3108,7 @@ and 0&nbsp;otherwise. · 3068 unmodified lines
31083108
31093109
31103110 <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
3111−<span class="apii">[-0, +0, <em>m</em>]</span>
3111+<span class="apii">[-0, +0, <em>-</em>]</span>
31123112 <pre>int lua_isstring (lua_State *L, int index);</pre>
31133113
31143114 <p>
@@ -3582,6 +3582,19 @@ light userdata with the same C&nbsp;address. · 467 unmodified lines
35823582
35833583
35843584
3585+<hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
3586+<span class="apii">[-0, +1, <em>m</em>]</span>
3587+<pre>void lua_pushliteral (lua_State *L, const char *s);</pre>
3588+
3589+<p>
3590+This macro is equivalent to <a href="#lua_pushlstring"><code>lua_pushlstring</code></a>,
3591+but can be used only when <code>s</code> is a literal string.
3592+In these cases, it automatically provides the string length.
3593+
3594+
3595+
3596+
3597+
35853598 <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
35863599 <span class="apii">[-0, +1, <em>m</em>]</span>
35873600 <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. · 5153 unmodified lines
87418754 <HR>
87428755 <SMALL>
87438756 Last update:
8744−Fri Jan 18 14:50:20 BRST 2008
8757+Fri Jan 18 22:32:24 BRST 2008
87458758 </SMALL>
87468759 <!--
87478760 Last change: revised for Lua 5.1.3
src/ldo.c+2−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldo.c,v 2.38.1.2 2008/01/03 15:20:39 roberto Exp $
2+** $Id: ldo.c,v 2.38.1.3 2008/01/18 22:31:22 roberto Exp $
33 ** Stack and Call structure of Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -332,7 +332,7 @@ static StkId callrethooks (lua_State *L, StkId firstResult) { · 326 unmodified lines
332332 ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
333333 luaD_callhook(L, LUA_HOOKRET, -1);
334334 if (f_isLua(L->ci)) { /* Lua function? */
335− while (L->ci->tailcalls--) /* call hook for eventual tail calls */
335+ while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
336336 luaD_callhook(L, LUA_HOOKTAILRET, -1);
337337 }
338338 return restorestack(L, fr);
src/liolib.c+47−28
@@ -1,5 +1,5 @@
11 /*
2−** $Id: liolib.c,v 2.73.1.2 2008/01/02 13:56:00 roberto Exp $
2+** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $
33 ** Standard I/O (and system) library
44 ** See Copyright Notice in lua.h
55 */
@@ -51,7 +51,7 @@ static void fileerror (lua_State *L, int arg, const char *filename) { · 45 unmodified lines
5151 }
5252
5353
54−#define topfile(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
54+#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
5555
5656
5757 static int io_type (lua_State *L) {
@@ -70,7 +70,7 @@ static int io_type (lua_State *L) { · 12 unmodified lines
7070
7171
7272 static FILE *tofile (lua_State *L) {
73− FILE **f = topfile(L);
73+ FILE **f = tofilep(L);
7474 if (*f == NULL)
7575 luaL_error(L, "attempt to use a closed file");
7676 return *f;
@@ -93,24 +93,33 @@ static FILE **newfile (lua_State *L) { · 16 unmodified lines
9393
9494
9595 /*
96−** this function has a separated environment, which defines the
97−** correct __close for 'popen' files
96+** function to (not) close the standard files stdin, stdout, and stderr
97+*/
98+static int io_noclose (lua_State *L) {
99+ lua_pushnil(L);
100+ lua_pushliteral(L, "cannot close standard file");
101+ return 2;
102+}
103+
104+
105+/*
106+** function to close 'popen' files
98107 */
99108 static int io_pclose (lua_State *L) {
100− FILE **p = topfile(L);
109+ FILE **p = tofilep(L);
101110 int ok = lua_pclose(L, *p);
102111 *p = NULL;
103112 return pushresult(L, ok, NULL);
104113 }
105114
106115
116+/*
117+** function to close regular files
118+*/
107119 static int io_fclose (lua_State *L) {
108− FILE **p = topfile(L);
109− int ok = 0;
110− if (*p != stdin && *p != stdout && *p != stderr) {
111− ok = (fclose(*p) == 0);
112− *p = NULL;
113− }
120+ FILE **p = tofilep(L);
121+ int ok = (fclose(*p) == 0);
122+ *p = NULL;
114123 return pushresult(L, ok, NULL);
115124 }
116125
@@ -131,17 +140,18 @@ static int io_close (lua_State *L) { · 14 unmodified lines
131140
132141
133142 static int io_gc (lua_State *L) {
134− FILE *f = *topfile(L);
135− if (f != NULL) /* ignore closed files */
143+ FILE *f = *tofilep(L);
144+ /* ignore closed files */
145+ if (f != NULL)
136146 aux_close(L);
137147 return 0;
138148 }
139149
140150
141151 static int io_tostring (lua_State *L) {
142− FILE *f = *topfile(L);
152+ FILE *f = *tofilep(L);
143153 if (f == NULL)
144− lua_pushstring(L, "file (closed)");
154+ lua_pushliteral(L, "file (closed)");
145155 else
146156 lua_pushfstring(L, "file (%p)", f);
147157 return 1;
@@ -157,6 +167,10 @@ static int io_open (lua_State *L) { · 9 unmodified lines
157167 }
158168
159169
170+/*
171+** this function has a separated environment, which defines the
172+** correct __close for 'popen' files
173+*/
160174 static int io_popen (lua_State *L) {
161175 const char *filename = luaL_checkstring(L, 1);
162176 const char *mode = luaL_optstring(L, 2, "r");
@@ -282,7 +296,7 @@ static int read_line (lua_State *L, FILE *f) { · 119 unmodified lines
282296 char *p = luaL_prepbuffer(&b);
283297 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
284298 luaL_pushresult(&b); /* close buffer */
285− return (lua_strlen(L, -1) > 0); /* check whether read something */
299+ return (lua_objlen(L, -1) > 0); /* check whether read something */
286300 }
287301 l = strlen(p);
288302 if (l == 0 || p[l-1] != '\n')
@@ -310,7 +324,7 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { · 21 unmodified lines
310324 n -= nr; /* still have to read `n' chars */
311325 } while (n > 0 && nr == rlen); /* until end of count or eof */
312326 luaL_pushresult(&b); /* close buffer */
313− return (n == 0 || lua_strlen(L, -1) > 0);
327+ return (n == 0 || lua_objlen(L, -1) > 0);
314328 }
315329
316330
@@ -504,31 +518,36 @@ static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) { · 187 unmodified lines
504518 lua_pushvalue(L, -1);
505519 lua_rawseti(L, LUA_ENVIRONINDEX, k);
506520 }
507− lua_setfield(L, -2, fname);
521+ lua_pushvalue(L, -2); /* copy environment */
522+ lua_setfenv(L, -2); /* set it */
523+ lua_setfield(L, -3, fname);
524+}
525+
526+
527+static void newfenv (lua_State *L, lua_CFunction cls) {
528+ lua_createtable(L, 0, 1);
529+ lua_pushcfunction(L, cls);
530+ lua_setfield(L, -2, "__close");
508531 }
509532
510533
511534 LUALIB_API int luaopen_io (lua_State *L) {
512535 createmeta(L);
513536 /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
514− lua_createtable(L, 2, 1);
537+ newfenv(L, io_fclose);
515538 lua_replace(L, LUA_ENVIRONINDEX);
516539 /* open library */
517540 luaL_register(L, LUA_IOLIBNAME, iolib);
518541 /* create (and set) default files */
542+ newfenv(L, io_noclose); /* close function for default files */
519543 createstdfile(L, stdin, IO_INPUT, "stdin");
520544 createstdfile(L, stdout, IO_OUTPUT, "stdout");
521545 createstdfile(L, stderr, 0, "stderr");
522− /* create environment for 'popen' */
546+ lua_pop(L, 1); /* pop environment for default files */
523547 lua_getfield(L, -1, "popen");
524− lua_createtable(L, 0, 1);
525− lua_pushcfunction(L, io_pclose);
526− lua_setfield(L, -2, "__close");
527− lua_setfenv(L, -2);
548+ newfenv(L, io_pclose); /* create environment for 'popen' */
549+ lua_setfenv(L, -2); /* set fenv for 'popen' */
528550 lua_pop(L, 1); /* pop 'popen' */
529− /* set default close function */
530− lua_pushcfunction(L, io_fclose);
531− lua_setfield(L, LUA_ENVIRONINDEX, "__close");
532551 return 1;
533552 }
534553