Prechádzať zdrojové kódy

'lua_load' has an extra argument 'mode'

Roberto Ierusalimschy 13 rokov pred
rodič
commit
3617e04e97
7 zmenil súbory, kde vykonal 50 pridanie a 75 odobranie
  1. 3 3
      lapi.c
  2. 9 28
      lauxlib.c
  3. 5 3
      lauxlib.h
  4. 4 31
      lbaselib.c
  5. 23 6
      ldo.c
  6. 3 2
      ldo.h
  7. 3 2
      lua.h

+ 3 - 3
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 2.156 2011/10/31 17:48:51 roberto Exp roberto $
+** $Id: lapi.c,v 2.157 2011/11/16 18:51:36 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -968,13 +968,13 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
 
 
 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
-                      const char *chunkname) {
+                      const char *chunkname, const char *mode) {
   ZIO z;
   int status;
   lua_lock(L);
   if (!chunkname) chunkname = "?";
   luaZ_init(L, &z, reader, data);
-  status = luaD_protectedparser(L, &z, chunkname);
+  status = luaD_protectedparser(L, &z, chunkname, mode);
   if (status == LUA_OK) {  /* no errors? */
     LClosure *f = clLvalue(L->top - 1);  /* get newly created function */
     if (f->nupvalues == 1) {  /* does it have one upvalue? */

+ 9 - 28
lauxlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lauxlib.c,v 1.235 2011/11/09 19:08:55 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.236 2011/11/14 17:10:24 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 */
@@ -592,16 +592,6 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
 }
 
 
-static int checkmode (lua_State *L, const char *mode, const char *x) {
-  if (mode && strchr(mode, x[0]) == NULL) {
-    lua_pushfstring(L,
-       "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
-    return LUA_ERRFILE;
-  }
-  else return LUA_OK;
-}
-
-
 static int skipBOM (LoadF *lf) {
   const char *p = "\xEF\xBB\xBF";  /* Utf8 BOM mark */
   int c;
@@ -651,23 +641,14 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
   }
   if (skipcomment(&lf, &c))  /* read initial portion */
     lf.buff[lf.n++] = '\n';  /* add line to correct line numbers */
-  if (c == LUA_SIGNATURE[0]) {  /* binary file? */
-    if ((status = checkmode(L, mode, "binary")) != LUA_OK)
-      goto closefile;
-    if (filename) {
-      lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
-      if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
-      skipcomment(&lf, &c);  /* re-read initial portion */
-    }
-  }
-  else {  /* text file */
-    if ((status = checkmode(L, mode, "text")) != LUA_OK)
-      goto closefile;
+  if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */
+    lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
+    if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
+    skipcomment(&lf, &c);  /* re-read initial portion */
   }
   if (c != EOF)
     lf.buff[lf.n++] = c;  /* 'c' is the first character of the stream */
-  status = lua_load(L, getF, &lf, lua_tostring(L, -1));
- closefile:
+  status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
   readstatus = ferror(lf.f);
   if (filename) fclose(lf.f);  /* close file (even in case of errors) */
   if (readstatus) {
@@ -695,12 +676,12 @@ static const char *getS (lua_State *L, void *ud, size_t *size) {
 }
 
 
-LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
-                                const char *name) {
+LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
+                                 const char *name, const char *mode) {
   LoadS ls;
   ls.s = buff;
   ls.size = size;
-  return lua_load(L, getS, &ls, name);
+  return lua_load(L, getS, &ls, name, mode);
 }
 
 

+ 5 - 3
lauxlib.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lauxlib.h,v 1.118 2011/11/11 19:59:17 roberto Exp roberto $
+** $Id: lauxlib.h,v 1.119 2011/11/14 17:10:24 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 */
@@ -77,8 +77,8 @@ LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename,
 
 #define luaL_loadfile(L,f)	luaL_loadfilex(L,f,NULL)
 
-LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
-                                  const char *name);
+LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
+                                   const char *name, const char *mode);
 LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
 
 LUALIB_API lua_State *(luaL_newstate) (void);
@@ -131,6 +131,8 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
 
 #define luaL_opt(L,f,n,d)	(lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
 
+#define luaL_loadbuffer(L,s,sz,n)	luaL_loadbufferx(L,s,sz,n,NULL)
+
 
 /*
 ** {======================================================

+ 4 - 31
lbaselib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbaselib.c,v 1.269 2011/11/14 17:10:24 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.270 2011/11/23 17:29:04 roberto Exp roberto $
 ** Basic library
 ** See Copyright Notice in lua.h
 */
@@ -273,25 +273,6 @@ static int luaB_loadfile (lua_State *L) {
 */
 
 
-typedef struct {
-  const char *mode;
-} loaddata;
-
-
-/*
-** check whether a chunk (prefix in 's') satisfies given 'mode'
-** ('t' for text, 'b' for binary). Returns error message (also
-** pushed on the stack) in case of errors.
-*/
-static const char *checkrights (lua_State *L, const char *mode, const char *s) {
-  const char *x = (*s == LUA_SIGNATURE[0]) ? "binary" : "text";
-  if (strchr(mode, x[0]) == NULL)
-    return lua_pushfstring(L,
-       "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
-  else return NULL;
-}
-
-
 /*
 ** reserved slot, above all arguments, to hold a copy of the returned
 ** string to avoid it being collected while parsed. 'load' has four
@@ -308,7 +289,7 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) {
 */
 static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
   const char *s;
-  loaddata *ld = (loaddata *)ud;
+  (void)(ud);  /* not used */
   luaL_checkstack(L, 2, "too many nested functions");
   lua_pushvalue(L, 1);  /* get function */
   lua_call(L, 0, 1);  /* call it */
@@ -317,11 +298,6 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
     return NULL;
   }
   else if ((s = lua_tostring(L, -1)) != NULL) {
-    if (ld->mode != NULL) {  /* first time? */
-      s = checkrights(L, ld->mode, s);  /* check mode */
-      ld->mode = NULL;  /* to avoid further checks */
-      if (s) luaL_error(L, s);
-    }
     lua_replace(L, RESERVEDSLOT);  /* save string in reserved slot */
     return lua_tolstring(L, RESERVEDSLOT, size);
   }
@@ -340,16 +316,13 @@ static int luaB_load (lua_State *L) {
   const char *mode = luaL_optstring(L, 3, "bt");
   if (s != NULL) {  /* loading a string? */
     const char *chunkname = luaL_optstring(L, 2, s);
-    status = (checkrights(L, mode, s) != NULL)
-           || luaL_loadbuffer(L, s, l, chunkname);
+    status = luaL_loadbufferx(L, s, l, chunkname, mode);
   }
   else {  /* loading from a reader function */
     const char *chunkname = luaL_optstring(L, 2, "=(load)");
-    loaddata ld;
-    ld.mode = mode;
     luaL_checktype(L, 1, LUA_TFUNCTION);
     lua_settop(L, RESERVEDSLOT);  /* create reserved slot */
-    status = lua_load(L, generic_reader, &ld, chunkname);
+    status = lua_load(L, generic_reader, NULL, chunkname, mode);
   }
   if (status == LUA_OK && top >= 4) {  /* is there an 'env' argument */
     lua_pushvalue(L, 4);  /* environment for loaded function */

+ 23 - 6
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 2.100 2011/09/12 20:33:03 roberto Exp roberto $
+** $Id: ldo.c,v 2.101 2011/10/07 20:45:19 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -611,18 +611,34 @@ struct SParser {  /* data to `f_parser' */
   ZIO *z;
   Mbuffer buff;  /* dynamic structure used by the scanner */
   Dyndata dyd;  /* dynamic structures used by the parser */
+  const char *mode;
   const char *name;
 };
 
+
+static void checkmode (lua_State *L, const char *mode, const char *x) {
+  if (mode && strchr(mode, x[0]) == NULL) {
+    luaO_pushfstring(L,
+       "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
+    luaD_throw(L, LUA_ERRSYNTAX);
+  }
+}
+
+
 static void f_parser (lua_State *L, void *ud) {
   int i;
   Proto *tf;
   Closure *cl;
   struct SParser *p = cast(struct SParser *, ud);
   int c = zgetc(p->z);  /* read first character */
-  tf = (c == LUA_SIGNATURE[0])
-           ? luaU_undump(L, p->z, &p->buff, p->name)
-           : luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
+  if (c == LUA_SIGNATURE[0]) {
+    checkmode(L, p->mode, "binary");
+    tf = luaU_undump(L, p->z, &p->buff, p->name);
+  }
+  else {
+    checkmode(L, p->mode, "text");
+    tf = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
+  }
   setptvalue2s(L, L->top, tf);
   incr_top(L);
   cl = luaF_newLclosure(L, tf);
@@ -632,11 +648,12 @@ static void f_parser (lua_State *L, void *ud) {
 }
 
 
-int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
+int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
+                                        const char *mode) {
   struct SParser p;
   int status;
   L->nny++;  /* cannot yield during parsing */
-  p.z = z; p.name = name;
+  p.z = z; p.name = name; p.mode = mode;
   p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
   p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
   p.dyd.label.arr = NULL; p.dyd.label.size = 0;

+ 3 - 2
ldo.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.h,v 2.18 2009/12/17 12:28:57 roberto Exp roberto $
+** $Id: ldo.h,v 2.19 2011/10/07 20:45:19 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -26,7 +26,8 @@
 /* type of protected functions, to be ran by `runprotected' */
 typedef void (*Pfunc) (lua_State *L, void *ud);
 
-LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name);
+LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
+                                                  const char *mode);
 LUAI_FUNC void luaD_hook (lua_State *L, int event, int line);
 LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
 LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults,

+ 3 - 2
lua.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.280 2011/10/24 14:54:05 roberto Exp roberto $
+** $Id: lua.h,v 1.281 2011/10/24 16:53:05 roberto Exp roberto $
 ** Lua - A Scripting Language
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** See Copyright Notice at the end of this file
@@ -254,7 +254,8 @@ LUA_API int   (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
 #define lua_pcall(L,n,r,f)	lua_pcallk(L, (n), (r), (f), 0, NULL)
 
 LUA_API int   (lua_load) (lua_State *L, lua_Reader reader, void *dt,
-                                        const char *chunkname);
+                                        const char *chunkname,
+                                        const char *mode);
 
 LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);