diff options
-rw-r--r-- | lapi.c | 6 | ||||
-rw-r--r-- | lauxlib.c | 37 | ||||
-rw-r--r-- | lauxlib.h | 8 | ||||
-rw-r--r-- | lbaselib.c | 35 | ||||
-rw-r--r-- | ldo.c | 29 | ||||
-rw-r--r-- | ldo.h | 5 | ||||
-rw-r--r-- | lua.h | 5 |
7 files changed, 50 insertions, 75 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.156 2011/10/31 17:48:51 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.157 2011/11/16 18:51:36 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -968,13 +968,13 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, | |||
968 | 968 | ||
969 | 969 | ||
970 | LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, | 970 | LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, |
971 | const char *chunkname) { | 971 | const char *chunkname, const char *mode) { |
972 | ZIO z; | 972 | ZIO z; |
973 | int status; | 973 | int status; |
974 | lua_lock(L); | 974 | lua_lock(L); |
975 | if (!chunkname) chunkname = "?"; | 975 | if (!chunkname) chunkname = "?"; |
976 | luaZ_init(L, &z, reader, data); | 976 | luaZ_init(L, &z, reader, data); |
977 | status = luaD_protectedparser(L, &z, chunkname); | 977 | status = luaD_protectedparser(L, &z, chunkname, mode); |
978 | if (status == LUA_OK) { /* no errors? */ | 978 | if (status == LUA_OK) { /* no errors? */ |
979 | LClosure *f = clLvalue(L->top - 1); /* get newly created function */ | 979 | LClosure *f = clLvalue(L->top - 1); /* get newly created function */ |
980 | if (f->nupvalues == 1) { /* does it have one upvalue? */ | 980 | if (f->nupvalues == 1) { /* does it have one upvalue? */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.235 2011/11/09 19:08:55 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.236 2011/11/14 17:10:24 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -592,16 +592,6 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { | |||
592 | } | 592 | } |
593 | 593 | ||
594 | 594 | ||
595 | static int checkmode (lua_State *L, const char *mode, const char *x) { | ||
596 | if (mode && strchr(mode, x[0]) == NULL) { | ||
597 | lua_pushfstring(L, | ||
598 | "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); | ||
599 | return LUA_ERRFILE; | ||
600 | } | ||
601 | else return LUA_OK; | ||
602 | } | ||
603 | |||
604 | |||
605 | static int skipBOM (LoadF *lf) { | 595 | static int skipBOM (LoadF *lf) { |
606 | const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ | 596 | const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ |
607 | int c; | 597 | int c; |
@@ -651,23 +641,14 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, | |||
651 | } | 641 | } |
652 | if (skipcomment(&lf, &c)) /* read initial portion */ | 642 | if (skipcomment(&lf, &c)) /* read initial portion */ |
653 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ | 643 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ |
654 | if (c == LUA_SIGNATURE[0]) { /* binary file? */ | 644 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
655 | if ((status = checkmode(L, mode, "binary")) != LUA_OK) | 645 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
656 | goto closefile; | 646 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); |
657 | if (filename) { | 647 | skipcomment(&lf, &c); /* re-read initial portion */ |
658 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ | ||
659 | if (lf.f == NULL) return errfile(L, "reopen", fnameindex); | ||
660 | skipcomment(&lf, &c); /* re-read initial portion */ | ||
661 | } | ||
662 | } | ||
663 | else { /* text file */ | ||
664 | if ((status = checkmode(L, mode, "text")) != LUA_OK) | ||
665 | goto closefile; | ||
666 | } | 648 | } |
667 | if (c != EOF) | 649 | if (c != EOF) |
668 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ | 650 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ |
669 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); | 651 | status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); |
670 | closefile: | ||
671 | readstatus = ferror(lf.f); | 652 | readstatus = ferror(lf.f); |
672 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ | 653 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
673 | if (readstatus) { | 654 | if (readstatus) { |
@@ -695,12 +676,12 @@ static const char *getS (lua_State *L, void *ud, size_t *size) { | |||
695 | } | 676 | } |
696 | 677 | ||
697 | 678 | ||
698 | LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, | 679 | LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, |
699 | const char *name) { | 680 | const char *name, const char *mode) { |
700 | LoadS ls; | 681 | LoadS ls; |
701 | ls.s = buff; | 682 | ls.s = buff; |
702 | ls.size = size; | 683 | ls.size = size; |
703 | return lua_load(L, getS, &ls, name); | 684 | return lua_load(L, getS, &ls, name, mode); |
704 | } | 685 | } |
705 | 686 | ||
706 | 687 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.h,v 1.118 2011/11/11 19:59:17 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.119 2011/11/14 17:10:24 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -77,8 +77,8 @@ LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, | |||
77 | 77 | ||
78 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) | 78 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) |
79 | 79 | ||
80 | LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, | 80 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, |
81 | const char *name); | 81 | const char *name, const char *mode); |
82 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); | 82 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); |
83 | 83 | ||
84 | LUALIB_API lua_State *(luaL_newstate) (void); | 84 | LUALIB_API lua_State *(luaL_newstate) (void); |
@@ -131,6 +131,8 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, | |||
131 | 131 | ||
132 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) | 132 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) |
133 | 133 | ||
134 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) | ||
135 | |||
134 | 136 | ||
135 | /* | 137 | /* |
136 | ** {====================================================== | 138 | ** {====================================================== |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.269 2011/11/14 17:10:24 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.270 2011/11/23 17:29:04 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -273,25 +273,6 @@ static int luaB_loadfile (lua_State *L) { | |||
273 | */ | 273 | */ |
274 | 274 | ||
275 | 275 | ||
276 | typedef struct { | ||
277 | const char *mode; | ||
278 | } loaddata; | ||
279 | |||
280 | |||
281 | /* | ||
282 | ** check whether a chunk (prefix in 's') satisfies given 'mode' | ||
283 | ** ('t' for text, 'b' for binary). Returns error message (also | ||
284 | ** pushed on the stack) in case of errors. | ||
285 | */ | ||
286 | static const char *checkrights (lua_State *L, const char *mode, const char *s) { | ||
287 | const char *x = (*s == LUA_SIGNATURE[0]) ? "binary" : "text"; | ||
288 | if (strchr(mode, x[0]) == NULL) | ||
289 | return lua_pushfstring(L, | ||
290 | "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); | ||
291 | else return NULL; | ||
292 | } | ||
293 | |||
294 | |||
295 | /* | 276 | /* |
296 | ** reserved slot, above all arguments, to hold a copy of the returned | 277 | ** reserved slot, above all arguments, to hold a copy of the returned |
297 | ** string to avoid it being collected while parsed. 'load' has four | 278 | ** 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) { | |||
308 | */ | 289 | */ |
309 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | 290 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { |
310 | const char *s; | 291 | const char *s; |
311 | loaddata *ld = (loaddata *)ud; | 292 | (void)(ud); /* not used */ |
312 | luaL_checkstack(L, 2, "too many nested functions"); | 293 | luaL_checkstack(L, 2, "too many nested functions"); |
313 | lua_pushvalue(L, 1); /* get function */ | 294 | lua_pushvalue(L, 1); /* get function */ |
314 | lua_call(L, 0, 1); /* call it */ | 295 | lua_call(L, 0, 1); /* call it */ |
@@ -317,11 +298,6 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | |||
317 | return NULL; | 298 | return NULL; |
318 | } | 299 | } |
319 | else if ((s = lua_tostring(L, -1)) != NULL) { | 300 | else if ((s = lua_tostring(L, -1)) != NULL) { |
320 | if (ld->mode != NULL) { /* first time? */ | ||
321 | s = checkrights(L, ld->mode, s); /* check mode */ | ||
322 | ld->mode = NULL; /* to avoid further checks */ | ||
323 | if (s) luaL_error(L, s); | ||
324 | } | ||
325 | lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ | 301 | lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ |
326 | return lua_tolstring(L, RESERVEDSLOT, size); | 302 | return lua_tolstring(L, RESERVEDSLOT, size); |
327 | } | 303 | } |
@@ -340,16 +316,13 @@ static int luaB_load (lua_State *L) { | |||
340 | const char *mode = luaL_optstring(L, 3, "bt"); | 316 | const char *mode = luaL_optstring(L, 3, "bt"); |
341 | if (s != NULL) { /* loading a string? */ | 317 | if (s != NULL) { /* loading a string? */ |
342 | const char *chunkname = luaL_optstring(L, 2, s); | 318 | const char *chunkname = luaL_optstring(L, 2, s); |
343 | status = (checkrights(L, mode, s) != NULL) | 319 | status = luaL_loadbufferx(L, s, l, chunkname, mode); |
344 | || luaL_loadbuffer(L, s, l, chunkname); | ||
345 | } | 320 | } |
346 | else { /* loading from a reader function */ | 321 | else { /* loading from a reader function */ |
347 | const char *chunkname = luaL_optstring(L, 2, "=(load)"); | 322 | const char *chunkname = luaL_optstring(L, 2, "=(load)"); |
348 | loaddata ld; | ||
349 | ld.mode = mode; | ||
350 | luaL_checktype(L, 1, LUA_TFUNCTION); | 323 | luaL_checktype(L, 1, LUA_TFUNCTION); |
351 | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ | 324 | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ |
352 | status = lua_load(L, generic_reader, &ld, chunkname); | 325 | status = lua_load(L, generic_reader, NULL, chunkname, mode); |
353 | } | 326 | } |
354 | if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ | 327 | if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ |
355 | lua_pushvalue(L, 4); /* environment for loaded function */ | 328 | lua_pushvalue(L, 4); /* environment for loaded function */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 2.100 2011/09/12 20:33:03 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 2.101 2011/10/07 20:45:19 roberto Exp roberto $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -611,18 +611,34 @@ struct SParser { /* data to `f_parser' */ | |||
611 | ZIO *z; | 611 | ZIO *z; |
612 | Mbuffer buff; /* dynamic structure used by the scanner */ | 612 | Mbuffer buff; /* dynamic structure used by the scanner */ |
613 | Dyndata dyd; /* dynamic structures used by the parser */ | 613 | Dyndata dyd; /* dynamic structures used by the parser */ |
614 | const char *mode; | ||
614 | const char *name; | 615 | const char *name; |
615 | }; | 616 | }; |
616 | 617 | ||
618 | |||
619 | static void checkmode (lua_State *L, const char *mode, const char *x) { | ||
620 | if (mode && strchr(mode, x[0]) == NULL) { | ||
621 | luaO_pushfstring(L, | ||
622 | "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); | ||
623 | luaD_throw(L, LUA_ERRSYNTAX); | ||
624 | } | ||
625 | } | ||
626 | |||
627 | |||
617 | static void f_parser (lua_State *L, void *ud) { | 628 | static void f_parser (lua_State *L, void *ud) { |
618 | int i; | 629 | int i; |
619 | Proto *tf; | 630 | Proto *tf; |
620 | Closure *cl; | 631 | Closure *cl; |
621 | struct SParser *p = cast(struct SParser *, ud); | 632 | struct SParser *p = cast(struct SParser *, ud); |
622 | int c = zgetc(p->z); /* read first character */ | 633 | int c = zgetc(p->z); /* read first character */ |
623 | tf = (c == LUA_SIGNATURE[0]) | 634 | if (c == LUA_SIGNATURE[0]) { |
624 | ? luaU_undump(L, p->z, &p->buff, p->name) | 635 | checkmode(L, p->mode, "binary"); |
625 | : luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); | 636 | tf = luaU_undump(L, p->z, &p->buff, p->name); |
637 | } | ||
638 | else { | ||
639 | checkmode(L, p->mode, "text"); | ||
640 | tf = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); | ||
641 | } | ||
626 | setptvalue2s(L, L->top, tf); | 642 | setptvalue2s(L, L->top, tf); |
627 | incr_top(L); | 643 | incr_top(L); |
628 | cl = luaF_newLclosure(L, tf); | 644 | cl = luaF_newLclosure(L, tf); |
@@ -632,11 +648,12 @@ static void f_parser (lua_State *L, void *ud) { | |||
632 | } | 648 | } |
633 | 649 | ||
634 | 650 | ||
635 | int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) { | 651 | int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, |
652 | const char *mode) { | ||
636 | struct SParser p; | 653 | struct SParser p; |
637 | int status; | 654 | int status; |
638 | L->nny++; /* cannot yield during parsing */ | 655 | L->nny++; /* cannot yield during parsing */ |
639 | p.z = z; p.name = name; | 656 | p.z = z; p.name = name; p.mode = mode; |
640 | p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; | 657 | p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; |
641 | p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; | 658 | p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; |
642 | p.dyd.label.arr = NULL; p.dyd.label.size = 0; | 659 | p.dyd.label.arr = NULL; p.dyd.label.size = 0; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.h,v 2.18 2009/12/17 12:28:57 roberto Exp roberto $ | 2 | ** $Id: ldo.h,v 2.19 2011/10/07 20:45:19 roberto Exp roberto $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -26,7 +26,8 @@ | |||
26 | /* type of protected functions, to be ran by `runprotected' */ | 26 | /* type of protected functions, to be ran by `runprotected' */ |
27 | typedef void (*Pfunc) (lua_State *L, void *ud); | 27 | typedef void (*Pfunc) (lua_State *L, void *ud); |
28 | 28 | ||
29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, |
30 | const char *mode); | ||
30 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); | 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); |
31 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); | 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); |
32 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, | 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.h,v 1.280 2011/10/24 14:54:05 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.281 2011/10/24 16:53:05 roberto Exp roberto $ |
3 | ** Lua - A Scripting Language | 3 | ** Lua - A Scripting Language |
4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) | 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) |
5 | ** See Copyright Notice at the end of this file | 5 | ** 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, | |||
254 | #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) | 254 | #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) |
255 | 255 | ||
256 | LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, | 256 | LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, |
257 | const char *chunkname); | 257 | const char *chunkname, |
258 | const char *mode); | ||
258 | 259 | ||
259 | LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); | 260 | LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); |
260 | 261 | ||