'lua_load' has an extra argument 'mode'

This commit is contained in:
Roberto Ierusalimschy
2011-11-29 13:55:08 -02:00
parent 8c62bde36f
commit 3617e04e97
7 changed files with 50 additions and 75 deletions

6
lapi.c
View File

@@ -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 ** Lua API
** See Copyright Notice in lua.h ** 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, LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
const char *chunkname) { const char *chunkname, const char *mode) {
ZIO z; ZIO z;
int status; int status;
lua_lock(L); lua_lock(L);
if (!chunkname) chunkname = "?"; if (!chunkname) chunkname = "?";
luaZ_init(L, &z, reader, data); 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? */ if (status == LUA_OK) { /* no errors? */
LClosure *f = clLvalue(L->top - 1); /* get newly created function */ LClosure *f = clLvalue(L->top - 1); /* get newly created function */
if (f->nupvalues == 1) { /* does it have one upvalue? */ if (f->nupvalues == 1) { /* does it have one upvalue? */

View File

@@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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) { static int skipBOM (LoadF *lf) {
const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
int c; int c;
@@ -651,23 +641,14 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
} }
if (skipcomment(&lf, &c)) /* read initial portion */ if (skipcomment(&lf, &c)) /* read initial portion */
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
if (c == LUA_SIGNATURE[0]) { /* binary file? */ if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
if ((status = checkmode(L, mode, "binary")) != LUA_OK) lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
goto closefile; if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
if (filename) { skipcomment(&lf, &c); /* re-read initial portion */
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 != EOF) if (c != EOF)
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
status = lua_load(L, getF, &lf, lua_tostring(L, -1)); status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
closefile:
readstatus = ferror(lf.f); readstatus = ferror(lf.f);
if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (filename) fclose(lf.f); /* close file (even in case of errors) */
if (readstatus) { 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, LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
const char *name) { const char *name, const char *mode) {
LoadS ls; LoadS ls;
ls.s = buff; ls.s = buff;
ls.size = size; ls.size = size;
return lua_load(L, getS, &ls, name); return lua_load(L, getS, &ls, name, mode);
} }

View File

@@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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) #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
const char *name); const char *name, const char *mode);
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
LUALIB_API lua_State *(luaL_newstate) (void); 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_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)
/* /*
** {====================================================== ** {======================================================

View File

@@ -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 ** Basic library
** See Copyright Notice in lua.h ** 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 ** reserved slot, above all arguments, to hold a copy of the returned
** string to avoid it being collected while parsed. 'load' has four ** 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) { static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
const char *s; const char *s;
loaddata *ld = (loaddata *)ud; (void)(ud); /* not used */
luaL_checkstack(L, 2, "too many nested functions"); luaL_checkstack(L, 2, "too many nested functions");
lua_pushvalue(L, 1); /* get function */ lua_pushvalue(L, 1); /* get function */
lua_call(L, 0, 1); /* call it */ 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; return NULL;
} }
else if ((s = lua_tostring(L, -1)) != 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 */ lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
return lua_tolstring(L, RESERVEDSLOT, size); 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"); const char *mode = luaL_optstring(L, 3, "bt");
if (s != NULL) { /* loading a string? */ if (s != NULL) { /* loading a string? */
const char *chunkname = luaL_optstring(L, 2, s); const char *chunkname = luaL_optstring(L, 2, s);
status = (checkrights(L, mode, s) != NULL) status = luaL_loadbufferx(L, s, l, chunkname, mode);
|| luaL_loadbuffer(L, s, l, chunkname);
} }
else { /* loading from a reader function */ else { /* loading from a reader function */
const char *chunkname = luaL_optstring(L, 2, "=(load)"); const char *chunkname = luaL_optstring(L, 2, "=(load)");
loaddata ld;
ld.mode = mode;
luaL_checktype(L, 1, LUA_TFUNCTION); luaL_checktype(L, 1, LUA_TFUNCTION);
lua_settop(L, RESERVEDSLOT); /* create reserved slot */ 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 */ if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */
lua_pushvalue(L, 4); /* environment for loaded function */ lua_pushvalue(L, 4); /* environment for loaded function */

29
ldo.c
View File

@@ -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 ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -611,18 +611,34 @@ struct SParser { /* data to `f_parser' */
ZIO *z; ZIO *z;
Mbuffer buff; /* dynamic structure used by the scanner */ Mbuffer buff; /* dynamic structure used by the scanner */
Dyndata dyd; /* dynamic structures used by the parser */ Dyndata dyd; /* dynamic structures used by the parser */
const char *mode;
const char *name; 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) { static void f_parser (lua_State *L, void *ud) {
int i; int i;
Proto *tf; Proto *tf;
Closure *cl; Closure *cl;
struct SParser *p = cast(struct SParser *, ud); struct SParser *p = cast(struct SParser *, ud);
int c = zgetc(p->z); /* read first character */ int c = zgetc(p->z); /* read first character */
tf = (c == LUA_SIGNATURE[0]) if (c == LUA_SIGNATURE[0]) {
? luaU_undump(L, p->z, &p->buff, p->name) checkmode(L, p->mode, "binary");
: luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); 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); setptvalue2s(L, L->top, tf);
incr_top(L); incr_top(L);
cl = luaF_newLclosure(L, tf); 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; struct SParser p;
int status; int status;
L->nny++; /* cannot yield during parsing */ 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.actvar.arr = NULL; p.dyd.actvar.size = 0;
p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
p.dyd.label.arr = NULL; p.dyd.label.size = 0; p.dyd.label.arr = NULL; p.dyd.label.size = 0;

5
ldo.h
View File

@@ -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 ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -26,7 +26,8 @@
/* type of protected functions, to be ran by `runprotected' */ /* type of protected functions, to be ran by `runprotected' */
typedef void (*Pfunc) (lua_State *L, void *ud); 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 void luaD_hook (lua_State *L, int event, int line);
LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults,

5
lua.h
View File

@@ -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 - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** 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) #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, 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); LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);