- [NEW] Updatada la versió de Lua a 5.5.0
- [FIX] Afegits defines de Lua per a cada SO
This commit is contained in:
+193
-96
@@ -25,12 +25,7 @@
|
||||
#include "lua.h"
|
||||
|
||||
#include "lauxlib.h"
|
||||
|
||||
|
||||
#if !defined(MAX_SIZET)
|
||||
/* maximum value for size_t */
|
||||
#define MAX_SIZET ((size_t)(~(size_t)0))
|
||||
#endif
|
||||
#include "llimits.h"
|
||||
|
||||
|
||||
/*
|
||||
@@ -80,6 +75,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
||||
int top = lua_gettop(L);
|
||||
lua_getinfo(L, "f", ar); /* push function */
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
|
||||
luaL_checkstack(L, 6, "not enough stack"); /* slots for 'findfield' */
|
||||
if (findfield(L, top + 1, 2)) {
|
||||
const char *name = lua_tostring(L, -1);
|
||||
if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */
|
||||
@@ -98,14 +94,14 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
|
||||
|
||||
|
||||
static void pushfuncname (lua_State *L, lua_Debug *ar) {
|
||||
if (pushglobalfuncname(L, ar)) { /* try first a global name */
|
||||
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
|
||||
lua_remove(L, -2); /* remove name */
|
||||
}
|
||||
else if (*ar->namewhat != '\0') /* is there a name from code? */
|
||||
if (*ar->namewhat != '\0') /* is there a name from code? */
|
||||
lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */
|
||||
else if (*ar->what == 'm') /* main? */
|
||||
lua_pushliteral(L, "main chunk");
|
||||
else if (pushglobalfuncname(L, ar)) { /* try a global name */
|
||||
lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
|
||||
lua_remove(L, -2); /* remove name */
|
||||
}
|
||||
else if (*ar->what != 'C') /* for Lua functions, use <file:line> */
|
||||
lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
|
||||
else /* nothing left... */
|
||||
@@ -174,19 +170,27 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
|
||||
|
||||
LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
|
||||
lua_Debug ar;
|
||||
const char *argword;
|
||||
if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
|
||||
return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
|
||||
lua_getinfo(L, "n", &ar);
|
||||
if (strcmp(ar.namewhat, "method") == 0) {
|
||||
arg--; /* do not count 'self' */
|
||||
if (arg == 0) /* error is in the self argument itself? */
|
||||
return luaL_error(L, "calling '%s' on bad self (%s)",
|
||||
ar.name, extramsg);
|
||||
lua_getinfo(L, "nt", &ar);
|
||||
if (arg <= ar.extraargs) /* error in an extra argument? */
|
||||
argword = "extra argument";
|
||||
else {
|
||||
arg -= ar.extraargs; /* do not count extra arguments */
|
||||
if (strcmp(ar.namewhat, "method") == 0) { /* colon syntax? */
|
||||
arg--; /* do not count (extra) self argument */
|
||||
if (arg == 0) /* error in self argument? */
|
||||
return luaL_error(L, "calling '%s' on bad self (%s)",
|
||||
ar.name, extramsg);
|
||||
/* else go through; error in a regular argument */
|
||||
}
|
||||
argword = "argument";
|
||||
}
|
||||
if (ar.name == NULL)
|
||||
ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
|
||||
return luaL_error(L, "bad argument #%d to '%s' (%s)",
|
||||
arg, ar.name, extramsg);
|
||||
return luaL_error(L, "bad %s #%d to '%s' (%s)",
|
||||
argword, arg, ar.name, extramsg);
|
||||
}
|
||||
|
||||
|
||||
@@ -229,7 +233,7 @@ LUALIB_API void luaL_where (lua_State *L, int level) {
|
||||
/*
|
||||
** Again, the use of 'lua_pushvfstring' ensures this function does
|
||||
** not need reserved stack space when called. (At worst, it generates
|
||||
** an error with "stack overflow" instead of the given message.)
|
||||
** a memory error instead of the given message.)
|
||||
*/
|
||||
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
|
||||
va_list argp;
|
||||
@@ -249,11 +253,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
const char *msg;
|
||||
luaL_pushfail(L);
|
||||
msg = (en != 0) ? strerror(en) : "(no extra info)";
|
||||
if (fname)
|
||||
lua_pushfstring(L, "%s: %s", fname, strerror(en));
|
||||
lua_pushfstring(L, "%s: %s", fname, msg);
|
||||
else
|
||||
lua_pushstring(L, strerror(en));
|
||||
lua_pushstring(L, msg);
|
||||
lua_pushinteger(L, en);
|
||||
return 3;
|
||||
}
|
||||
@@ -470,18 +476,27 @@ typedef struct UBox {
|
||||
} UBox;
|
||||
|
||||
|
||||
/* Resize the buffer used by a box. Optimize for the common case of
|
||||
** resizing to the old size. (For instance, __gc will resize the box
|
||||
** to 0 even after it was closed. 'pushresult' may also resize it to a
|
||||
** final size that is equal to the one set when the buffer was created.)
|
||||
*/
|
||||
static void *resizebox (lua_State *L, int idx, size_t newsize) {
|
||||
void *ud;
|
||||
lua_Alloc allocf = lua_getallocf(L, &ud);
|
||||
UBox *box = (UBox *)lua_touserdata(L, idx);
|
||||
void *temp = allocf(ud, box->box, box->bsize, newsize);
|
||||
if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
|
||||
lua_pushliteral(L, "not enough memory");
|
||||
lua_error(L); /* raise a memory error */
|
||||
if (box->bsize == newsize) /* not changing size? */
|
||||
return box->box; /* keep the buffer */
|
||||
else {
|
||||
void *ud;
|
||||
lua_Alloc allocf = lua_getallocf(L, &ud);
|
||||
void *temp = allocf(ud, box->box, box->bsize, newsize);
|
||||
if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
|
||||
lua_pushliteral(L, "not enough memory");
|
||||
lua_error(L); /* raise a memory error */
|
||||
}
|
||||
box->box = temp;
|
||||
box->bsize = newsize;
|
||||
return temp;
|
||||
}
|
||||
box->box = temp;
|
||||
box->bsize = newsize;
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
@@ -526,14 +541,17 @@ static void newbox (lua_State *L) {
|
||||
|
||||
/*
|
||||
** Compute new size for buffer 'B', enough to accommodate extra 'sz'
|
||||
** bytes.
|
||||
** bytes plus one for a terminating zero.
|
||||
*/
|
||||
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
|
||||
size_t newsize = B->size * 2; /* double buffer size */
|
||||
if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
|
||||
return luaL_error(B->L, "buffer too large");
|
||||
if (newsize < B->n + sz) /* double is not big enough? */
|
||||
newsize = B->n + sz;
|
||||
size_t newsize = B->size;
|
||||
if (l_unlikely(sz >= MAX_SIZE - B->n))
|
||||
return cast_sizet(luaL_error(B->L, "resulting string too large"));
|
||||
/* else B->n + sz + 1 <= MAX_SIZE */
|
||||
if (newsize <= MAX_SIZE/3 * 2) /* no overflow? */
|
||||
newsize += (newsize >> 1); /* new size *= 1.5 */
|
||||
if (newsize < B->n + sz + 1) /* not big enough? */
|
||||
newsize = B->n + sz + 1;
|
||||
return newsize;
|
||||
}
|
||||
|
||||
@@ -593,9 +611,23 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
|
||||
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
|
||||
lua_State *L = B->L;
|
||||
checkbufferlevel(B, -1);
|
||||
lua_pushlstring(L, B->b, B->n);
|
||||
if (buffonstack(B))
|
||||
if (!buffonstack(B)) /* using static buffer? */
|
||||
lua_pushlstring(L, B->b, B->n); /* save result as regular string */
|
||||
else { /* reuse buffer already allocated */
|
||||
UBox *box = (UBox *)lua_touserdata(L, -1);
|
||||
void *ud;
|
||||
lua_Alloc allocf = lua_getallocf(L, &ud); /* function to free buffer */
|
||||
size_t len = B->n; /* final string length */
|
||||
char *s;
|
||||
resizebox(L, -1, len + 1); /* adjust box size to content size */
|
||||
s = (char*)box->box; /* final buffer address */
|
||||
s[len] = '\0'; /* add ending zero */
|
||||
/* clear box, as Lua will take control of the buffer */
|
||||
box->bsize = 0; box->box = NULL;
|
||||
lua_pushexternalstring(L, s, len, allocf, ud);
|
||||
lua_closeslot(L, -2); /* close the box */
|
||||
lua_gc(L, LUA_GCSTEP, len);
|
||||
}
|
||||
lua_remove(L, -2); /* remove box or placeholder from the stack */
|
||||
}
|
||||
|
||||
@@ -611,7 +643,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
|
||||
** box (if existent) is not on the top of the stack. So, instead of
|
||||
** calling 'luaL_addlstring', it replicates the code using -2 as the
|
||||
** last argument to 'prepbuffsize', signaling that the box is (or will
|
||||
** be) bellow the string being added to the buffer. (Box creation can
|
||||
** be) below the string being added to the buffer. (Box creation can
|
||||
** trigger an emergency GC, so we should not remove the string from the
|
||||
** stack before we have the space guaranteed.)
|
||||
*/
|
||||
@@ -649,13 +681,10 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
/* index of free-list header (after the predefined values) */
|
||||
#define freelist (LUA_RIDX_LAST + 1)
|
||||
|
||||
/*
|
||||
** The previously freed references form a linked list:
|
||||
** t[freelist] is the index of a first free index, or zero if list is
|
||||
** empty; t[t[freelist]] is the index of the second element; etc.
|
||||
** The previously freed references form a linked list: t[1] is the index
|
||||
** of a first free index, t[t[1]] is the index of the second element,
|
||||
** etc. A zero signals the end of the list.
|
||||
*/
|
||||
LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
int ref;
|
||||
@@ -664,19 +693,18 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
return LUA_REFNIL; /* 'nil' has a unique fixed reference */
|
||||
}
|
||||
t = lua_absindex(L, t);
|
||||
if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */
|
||||
if (lua_rawgeti(L, t, 1) == LUA_TNUMBER) /* already initialized? */
|
||||
ref = (int)lua_tointeger(L, -1); /* ref = t[1] */
|
||||
else { /* first access */
|
||||
lua_assert(!lua_toboolean(L, -1)); /* must be nil or false */
|
||||
ref = 0; /* list is empty */
|
||||
lua_pushinteger(L, 0); /* initialize as an empty list */
|
||||
lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */
|
||||
}
|
||||
else { /* already initialized */
|
||||
lua_assert(lua_isinteger(L, -1));
|
||||
ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
|
||||
lua_rawseti(L, t, 1); /* ref = t[1] = 0 */
|
||||
}
|
||||
lua_pop(L, 1); /* remove element from stack */
|
||||
if (ref != 0) { /* any free element? */
|
||||
lua_rawgeti(L, t, ref); /* remove it from list */
|
||||
lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
|
||||
lua_rawseti(L, t, 1); /* (t[1] = t[ref]) */
|
||||
}
|
||||
else /* no free elements */
|
||||
ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */
|
||||
@@ -688,11 +716,11 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
|
||||
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
||||
if (ref >= 0) {
|
||||
t = lua_absindex(L, t);
|
||||
lua_rawgeti(L, t, freelist);
|
||||
lua_rawgeti(L, t, 1);
|
||||
lua_assert(lua_isinteger(L, -1));
|
||||
lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
|
||||
lua_rawseti(L, t, ref); /* t[ref] = t[1] */
|
||||
lua_pushinteger(L, ref);
|
||||
lua_rawseti(L, t, freelist); /* t[freelist] = ref */
|
||||
lua_rawseti(L, t, 1); /* t[1] = ref */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -706,7 +734,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
|
||||
*/
|
||||
|
||||
typedef struct LoadF {
|
||||
int n; /* number of pre-read characters */
|
||||
unsigned n; /* number of pre-read characters */
|
||||
FILE *f; /* file being read */
|
||||
char buff[BUFSIZ]; /* area for reading file */
|
||||
} LoadF;
|
||||
@@ -714,7 +742,7 @@ typedef struct LoadF {
|
||||
|
||||
static const char *getF (lua_State *L, void *ud, size_t *size) {
|
||||
LoadF *lf = (LoadF *)ud;
|
||||
(void)L; /* not used */
|
||||
UNUSED(L);
|
||||
if (lf->n > 0) { /* are there pre-read characters to be read? */
|
||||
*size = lf->n; /* return them (chars already in buffer) */
|
||||
lf->n = 0; /* no more pre-read characters */
|
||||
@@ -731,25 +759,29 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
|
||||
|
||||
|
||||
static int errfile (lua_State *L, const char *what, int fnameindex) {
|
||||
const char *serr = strerror(errno);
|
||||
int err = errno;
|
||||
const char *filename = lua_tostring(L, fnameindex) + 1;
|
||||
lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
|
||||
if (err != 0)
|
||||
lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err));
|
||||
else
|
||||
lua_pushfstring(L, "cannot %s %s", what, filename);
|
||||
lua_remove(L, fnameindex);
|
||||
return LUA_ERRFILE;
|
||||
}
|
||||
|
||||
|
||||
static int skipBOM (LoadF *lf) {
|
||||
const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
|
||||
int c;
|
||||
lf->n = 0;
|
||||
do {
|
||||
c = getc(lf->f);
|
||||
if (c == EOF || c != *(const unsigned char *)p++) return c;
|
||||
lf->buff[lf->n++] = c; /* to be read by the parser */
|
||||
} while (*p != '\0');
|
||||
lf->n = 0; /* prefix matched; discard it */
|
||||
return getc(lf->f); /* return next character */
|
||||
/*
|
||||
** Skip an optional BOM at the start of a stream. If there is an
|
||||
** incomplete BOM (the first character is correct but the rest is
|
||||
** not), returns the first character anyway to force an error
|
||||
** (as no chunk can start with 0xEF).
|
||||
*/
|
||||
static int skipBOM (FILE *f) {
|
||||
int c = getc(f); /* read first character */
|
||||
if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */
|
||||
return getc(f); /* ignore BOM and return next char */
|
||||
else /* no (valid) BOM */
|
||||
return c; /* return first character */
|
||||
}
|
||||
|
||||
|
||||
@@ -760,13 +792,13 @@ static int skipBOM (LoadF *lf) {
|
||||
** first "valid" character of the file (after the optional BOM and
|
||||
** a first-line comment).
|
||||
*/
|
||||
static int skipcomment (LoadF *lf, int *cp) {
|
||||
int c = *cp = skipBOM(lf);
|
||||
static int skipcomment (FILE *f, int *cp) {
|
||||
int c = *cp = skipBOM(f);
|
||||
if (c == '#') { /* first line is a comment (Unix exec. file)? */
|
||||
do { /* skip first line */
|
||||
c = getc(lf->f);
|
||||
c = getc(f);
|
||||
} while (c != EOF && c != '\n');
|
||||
*cp = getc(lf->f); /* skip end-of-line, if present */
|
||||
*cp = getc(f); /* next character after comment, if present */
|
||||
return 1; /* there was a comment */
|
||||
}
|
||||
else return 0; /* no comment */
|
||||
@@ -785,20 +817,27 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
|
||||
}
|
||||
else {
|
||||
lua_pushfstring(L, "@%s", filename);
|
||||
errno = 0;
|
||||
lf.f = fopen(filename, "r");
|
||||
if (lf.f == NULL) return errfile(L, "open", fnameindex);
|
||||
}
|
||||
if (skipcomment(&lf, &c)) /* read initial portion */
|
||||
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
|
||||
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 */
|
||||
lf.n = 0;
|
||||
if (skipcomment(lf.f, &c)) /* read initial portion */
|
||||
lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */
|
||||
if (c == LUA_SIGNATURE[0]) { /* binary file? */
|
||||
lf.n = 0; /* remove possible newline */
|
||||
if (filename) { /* "real" file? */
|
||||
errno = 0;
|
||||
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
|
||||
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
|
||||
skipcomment(lf.f, &c); /* re-read initial portion */
|
||||
}
|
||||
}
|
||||
if (c != EOF)
|
||||
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
|
||||
lf.buff[lf.n++] = cast_char(c); /* 'c' is the first character */
|
||||
status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
|
||||
readstatus = ferror(lf.f);
|
||||
errno = 0; /* no useful error number until here */
|
||||
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
||||
if (readstatus) {
|
||||
lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
|
||||
@@ -817,7 +856,7 @@ typedef struct LoadS {
|
||||
|
||||
static const char *getS (lua_State *L, void *ud, size_t *size) {
|
||||
LoadS *ls = (LoadS *)ud;
|
||||
(void)L; /* not used */
|
||||
UNUSED(L);
|
||||
if (ls->size == 0) return NULL;
|
||||
*size = ls->size;
|
||||
ls->size = 0;
|
||||
@@ -881,6 +920,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
|
||||
|
||||
|
||||
LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
idx = lua_absindex(L,idx);
|
||||
if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
|
||||
if (!lua_isstring(L, -1))
|
||||
luaL_error(L, "'__tostring' must return a string");
|
||||
@@ -888,10 +928,9 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
else {
|
||||
switch (lua_type(L, idx)) {
|
||||
case LUA_TNUMBER: {
|
||||
if (lua_isinteger(L, idx))
|
||||
lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx));
|
||||
else
|
||||
lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx));
|
||||
char buff[LUA_N2SBUFFSZ];
|
||||
lua_numbertocstring(L, idx, buff);
|
||||
lua_pushstring(L, buff);
|
||||
break;
|
||||
}
|
||||
case LUA_TSTRING:
|
||||
@@ -926,7 +965,7 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
|
||||
LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
||||
luaL_checkstack(L, nup, "too many upvalues");
|
||||
for (; l->name != NULL; l++) { /* fill the table with given functions */
|
||||
if (l->func == NULL) /* place holder? */
|
||||
if (l->func == NULL) /* placeholder? */
|
||||
lua_pushboolean(L, 0);
|
||||
else {
|
||||
int i;
|
||||
@@ -989,7 +1028,7 @@ LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
|
||||
const char *wild;
|
||||
size_t l = strlen(p);
|
||||
while ((wild = strstr(s, p)) != NULL) {
|
||||
luaL_addlstring(b, s, wild - s); /* push prefix */
|
||||
luaL_addlstring(b, s, ct_diff2sz(wild - s)); /* push prefix */
|
||||
luaL_addstring(b, r); /* push replacement in place of pattern */
|
||||
s = wild + l; /* continue after 'p' */
|
||||
}
|
||||
@@ -1007,8 +1046,8 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
|
||||
}
|
||||
|
||||
|
||||
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
(void)ud; (void)osize; /* not used */
|
||||
void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
UNUSED(ud); UNUSED(osize);
|
||||
if (nsize == 0) {
|
||||
free(ptr);
|
||||
return NULL;
|
||||
@@ -1018,9 +1057,14 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Standard panic function just prints an error message. The test
|
||||
** with 'lua_type' avoids possible memory errors in 'lua_tostring'.
|
||||
*/
|
||||
static int panic (lua_State *L) {
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL) msg = "error object is not a string";
|
||||
const char *msg = (lua_type(L, -1) == LUA_TSTRING)
|
||||
? lua_tostring(L, -1)
|
||||
: "error object is not a string";
|
||||
lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
|
||||
msg);
|
||||
return 0; /* return to Lua to abort */
|
||||
@@ -1084,11 +1128,64 @@ static void warnfon (void *ud, const char *message, int tocont) {
|
||||
}
|
||||
|
||||
|
||||
LUALIB_API lua_State *luaL_newstate (void) {
|
||||
lua_State *L = lua_newstate(l_alloc, NULL);
|
||||
|
||||
/*
|
||||
** A function to compute an unsigned int with some level of
|
||||
** randomness. Rely on Address Space Layout Randomization (if present)
|
||||
** and the current time.
|
||||
*/
|
||||
#if !defined(luai_makeseed)
|
||||
|
||||
#include <time.h>
|
||||
|
||||
|
||||
/* Size for the buffer, in bytes */
|
||||
#define BUFSEEDB (sizeof(void*) + sizeof(time_t))
|
||||
|
||||
/* Size for the buffer in int's, rounded up */
|
||||
#define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int))
|
||||
|
||||
/*
|
||||
** Copy the contents of variable 'v' into the buffer pointed by 'b'.
|
||||
** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.)
|
||||
*/
|
||||
#define addbuff(b,v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v))
|
||||
|
||||
|
||||
static unsigned int luai_makeseed (void) {
|
||||
unsigned int buff[BUFSEED];
|
||||
unsigned int res;
|
||||
unsigned int i;
|
||||
time_t t = time(NULL);
|
||||
char *b = (char*)buff;
|
||||
addbuff(b, b); /* local variable's address */
|
||||
addbuff(b, t); /* time */
|
||||
/* fill (rare but possible) remain of the buffer with zeros */
|
||||
memset(b, 0, sizeof(buff) - BUFSEEDB);
|
||||
res = buff[0];
|
||||
for (i = 1; i < BUFSEED; i++)
|
||||
res ^= (res >> 3) + (res << 7) + buff[i];
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
|
||||
UNUSED(L);
|
||||
return luai_makeseed();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Use the name with parentheses so that headers can redefine it
|
||||
** as a macro.
|
||||
*/
|
||||
LUALIB_API lua_State *(luaL_newstate) (void) {
|
||||
lua_State *L = lua_newstate(luaL_alloc, NULL, luaL_makeseed(NULL));
|
||||
if (l_likely(L)) {
|
||||
lua_atpanic(L, &panic);
|
||||
lua_setwarnf(L, warnfoff, L); /* default is warnings off */
|
||||
lua_setwarnf(L, warnfon, L);
|
||||
}
|
||||
return L;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user