Lua 5.3.6

This commit is contained in:
Kae 2025-01-05 15:16:15 +11:00
parent 41f0b9001f
commit 9bc12f5f97
58 changed files with 2574 additions and 1566 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp $ ** $Id: lauxlib.h,v 1.131.1.1 2017/04/19 17:20:42 roberto Exp $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -16,10 +16,18 @@
/* extra error code for 'luaL_load' */ /* extra error code for 'luaL_loadfilex' */
#define LUA_ERRFILE (LUA_ERRERR+1) #define LUA_ERRFILE (LUA_ERRERR+1)
/* key, in the registry, for table of loaded modules */
#define LUA_LOADED_TABLE "_LOADED"
/* key, in the registry, for table of preloaded loaders */
#define LUA_PRELOAD_TABLE "_PRELOAD"
typedef struct luaL_Reg { typedef struct luaL_Reg {
const char *name; const char *name;
lua_CFunction func; lua_CFunction func;
@ -65,7 +73,7 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
LUALIB_API int (luaL_execresult) (lua_State *L, int stat); LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
/* pre-defined references */ /* predefined references */
#define LUA_NOREF (-2) #define LUA_NOREF (-2)
#define LUA_REFNIL (-1) #define LUA_REFNIL (-1)

9
source/extern/lua.h vendored
View File

@ -1,5 +1,4 @@
/* /*
** $Id: lua.h,v 1.328 2015/06/03 13:03:38 roberto Exp $
** 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
@ -19,11 +18,11 @@
#define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MAJOR "5"
#define LUA_VERSION_MINOR "3" #define LUA_VERSION_MINOR "3"
#define LUA_VERSION_NUM 503 #define LUA_VERSION_NUM 503
#define LUA_VERSION_RELEASE "1" #define LUA_VERSION_RELEASE "6"
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2015 Lua.org, PUC-Rio" #define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2020 Lua.org, PUC-Rio"
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
@ -361,7 +360,7 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
#define lua_pushliteral(L, s) lua_pushstring(L, "" s) #define lua_pushliteral(L, s) lua_pushstring(L, "" s)
#define lua_pushglobaltable(L) \ #define lua_pushglobaltable(L) \
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS) ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS))
#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) #define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
@ -460,7 +459,7 @@ struct lua_Debug {
/****************************************************************************** /******************************************************************************
* Copyright (C) 1994-2015 Lua.org, PUC-Rio. * Copyright (C) 1994-2020 Lua.org, PUC-Rio.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.249 2015/04/06 12:23:48 roberto Exp $ ** $Id: lapi.c,v 2.259.1.2 2017/12/06 18:35:12 roberto Exp $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -121,11 +121,11 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
lua_lock(to); lua_lock(to);
api_checknelems(from, n); api_checknelems(from, n);
api_check(from, G(from) == G(to), "moving among independent states"); api_check(from, G(from) == G(to), "moving among independent states");
api_check(from, to->ci->top - to->top >= n, "not enough elements to move"); api_check(from, to->ci->top - to->top >= n, "stack overflow");
from->top -= n; from->top -= n;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
setobj2s(to, to->top, from->top + i); setobj2s(to, to->top, from->top + i);
api_incr_top(to); to->top++; /* stack already checked by previous 'api_check' */
} }
lua_unlock(to); lua_unlock(to);
} }
@ -378,9 +378,9 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
return NULL; return NULL;
} }
lua_lock(L); /* 'luaO_tostring' may create a new string */ lua_lock(L); /* 'luaO_tostring' may create a new string */
luaO_tostring(L, o);
luaC_checkGC(L); luaC_checkGC(L);
o = index2addr(L, idx); /* previous call may reallocate the stack */ o = index2addr(L, idx); /* previous call may reallocate the stack */
luaO_tostring(L, o);
lua_unlock(L); lua_unlock(L);
} }
if (len != NULL) if (len != NULL)
@ -471,13 +471,18 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
} }
/*
** Pushes on the stack a string with given length. Avoid using 's' when
** 'len' == 0 (as 's' can be NULL in that case), due to later use of
** 'memcmp' and 'memcpy'.
*/
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
TString *ts; TString *ts;
lua_lock(L); lua_lock(L);
luaC_checkGC(L); ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
ts = luaS_newlstr(L, s, len);
setsvalue2s(L, L->top, ts); setsvalue2s(L, L->top, ts);
api_incr_top(L); api_incr_top(L);
luaC_checkGC(L);
lua_unlock(L); lua_unlock(L);
return getstr(ts); return getstr(ts);
} }
@ -489,12 +494,12 @@ LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
setnilvalue(L->top); setnilvalue(L->top);
else { else {
TString *ts; TString *ts;
luaC_checkGC(L);
ts = luaS_new(L, s); ts = luaS_new(L, s);
setsvalue2s(L, L->top, ts); setsvalue2s(L, L->top, ts);
s = getstr(ts); /* internal copy's address */ s = getstr(ts); /* internal copy's address */
} }
api_incr_top(L); api_incr_top(L);
luaC_checkGC(L);
lua_unlock(L); lua_unlock(L);
return s; return s;
} }
@ -504,8 +509,8 @@ LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
va_list argp) { va_list argp) {
const char *ret; const char *ret;
lua_lock(L); lua_lock(L);
luaC_checkGC(L);
ret = luaO_pushvfstring(L, fmt, argp); ret = luaO_pushvfstring(L, fmt, argp);
luaC_checkGC(L);
lua_unlock(L); lua_unlock(L);
return ret; return ret;
} }
@ -515,10 +520,10 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
const char *ret; const char *ret;
va_list argp; va_list argp;
lua_lock(L); lua_lock(L);
luaC_checkGC(L);
va_start(argp, fmt); va_start(argp, fmt);
ret = luaO_pushvfstring(L, fmt, argp); ret = luaO_pushvfstring(L, fmt, argp);
va_end(argp); va_end(argp);
luaC_checkGC(L);
lua_unlock(L); lua_unlock(L);
return ret; return ret;
} }
@ -528,12 +533,12 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
lua_lock(L); lua_lock(L);
if (n == 0) { if (n == 0) {
setfvalue(L->top, fn); setfvalue(L->top, fn);
api_incr_top(L);
} }
else { else {
CClosure *cl; CClosure *cl;
api_checknelems(L, n); api_checknelems(L, n);
api_check(L, n <= MAXUPVAL, "upvalue index too large"); api_check(L, n <= MAXUPVAL, "upvalue index too large");
luaC_checkGC(L);
cl = luaF_newCclosure(L, n); cl = luaF_newCclosure(L, n);
cl->f = fn; cl->f = fn;
L->top -= n; L->top -= n;
@ -542,8 +547,9 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
/* does not need barrier because closure is white */ /* does not need barrier because closure is white */
} }
setclCvalue(L, L->top, cl); setclCvalue(L, L->top, cl);
api_incr_top(L);
luaC_checkGC(L);
} }
api_incr_top(L);
lua_unlock(L); lua_unlock(L);
} }
@ -579,19 +585,30 @@ LUA_API int lua_pushthread (lua_State *L) {
*/ */
LUA_API int lua_getglobal (lua_State *L, const char *name) { static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
Table *reg = hvalue(&G(L)->l_registry); const TValue *slot;
const TValue *gt; /* global table */ TString *str = luaS_new(L, k);
lua_lock(L); if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
gt = luaH_getint(reg, LUA_RIDX_GLOBALS); setobj2s(L, L->top, slot);
setsvalue2s(L, L->top, luaS_new(L, name)); api_incr_top(L);
api_incr_top(L); }
luaV_gettable(L, gt, L->top - 1, L->top - 1); else {
setsvalue2s(L, L->top, str);
api_incr_top(L);
luaV_finishget(L, t, L->top - 1, L->top - 1, slot);
}
lua_unlock(L); lua_unlock(L);
return ttnov(L->top - 1); return ttnov(L->top - 1);
} }
LUA_API int lua_getglobal (lua_State *L, const char *name) {
Table *reg = hvalue(&G(L)->l_registry);
lua_lock(L);
return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
}
LUA_API int lua_gettable (lua_State *L, int idx) { LUA_API int lua_gettable (lua_State *L, int idx) {
StkId t; StkId t;
lua_lock(L); lua_lock(L);
@ -603,24 +620,25 @@ LUA_API int lua_gettable (lua_State *L, int idx) {
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
StkId t;
lua_lock(L); lua_lock(L);
t = index2addr(L, idx); return auxgetstr(L, index2addr(L, idx), k);
setsvalue2s(L, L->top, luaS_new(L, k));
api_incr_top(L);
luaV_gettable(L, t, L->top - 1, L->top - 1);
lua_unlock(L);
return ttnov(L->top - 1);
} }
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
StkId t; StkId t;
const TValue *slot;
lua_lock(L); lua_lock(L);
t = index2addr(L, idx); t = index2addr(L, idx);
setivalue(L->top, n); if (luaV_fastget(L, t, n, slot, luaH_getint)) {
api_incr_top(L); setobj2s(L, L->top, slot);
luaV_gettable(L, t, L->top - 1, L->top - 1); api_incr_top(L);
}
else {
setivalue(L->top, n);
api_incr_top(L);
luaV_finishget(L, t, L->top - 1, L->top - 1, slot);
}
lua_unlock(L); lua_unlock(L);
return ttnov(L->top - 1); return ttnov(L->top - 1);
} }
@ -666,12 +684,12 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
Table *t; Table *t;
lua_lock(L); lua_lock(L);
luaC_checkGC(L);
t = luaH_new(L); t = luaH_new(L);
sethvalue(L, L->top, t); sethvalue(L, L->top, t);
api_incr_top(L); api_incr_top(L);
if (narray > 0 || nrec > 0) if (narray > 0 || nrec > 0)
luaH_resize(L, t, narray, nrec); luaH_resize(L, t, narray, nrec);
luaC_checkGC(L);
lua_unlock(L); lua_unlock(L);
} }
@ -719,18 +737,29 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) {
** set functions (stack -> Lua) ** set functions (stack -> Lua)
*/ */
/*
** t[k] = value at the top of the stack (where 'k' is a string)
*/
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
const TValue *slot;
TString *str = luaS_new(L, k);
api_checknelems(L, 1);
if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1))
L->top--; /* pop value */
else {
setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
api_incr_top(L);
luaV_finishset(L, t, L->top - 1, L->top - 2, slot);
L->top -= 2; /* pop value and key */
}
lua_unlock(L); /* lock done by caller */
}
LUA_API void lua_setglobal (lua_State *L, const char *name) { LUA_API void lua_setglobal (lua_State *L, const char *name) {
Table *reg = hvalue(&G(L)->l_registry); Table *reg = hvalue(&G(L)->l_registry);
const TValue *gt; /* global table */ lua_lock(L); /* unlock done in 'auxsetstr' */
lua_lock(L); auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
api_checknelems(L, 1);
gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
setsvalue2s(L, L->top, luaS_new(L, name));
api_incr_top(L);
luaV_settable(L, gt, L->top - 1, L->top - 2);
L->top -= 2; /* pop value and key */
lua_unlock(L);
} }
@ -746,42 +775,40 @@ LUA_API void lua_settable (lua_State *L, int idx) {
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
StkId t; lua_lock(L); /* unlock done in 'auxsetstr' */
lua_lock(L); auxsetstr(L, index2addr(L, idx), k);
api_checknelems(L, 1);
t = index2addr(L, idx);
setsvalue2s(L, L->top, luaS_new(L, k));
api_incr_top(L);
luaV_settable(L, t, L->top - 1, L->top - 2);
L->top -= 2; /* pop value and key */
lua_unlock(L);
} }
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
StkId t; StkId t;
const TValue *slot;
lua_lock(L); lua_lock(L);
api_checknelems(L, 1); api_checknelems(L, 1);
t = index2addr(L, idx); t = index2addr(L, idx);
setivalue(L->top, n); if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1))
api_incr_top(L); L->top--; /* pop value */
luaV_settable(L, t, L->top - 1, L->top - 2); else {
L->top -= 2; /* pop value and key */ setivalue(L->top, n);
api_incr_top(L);
luaV_finishset(L, t, L->top - 1, L->top - 2, slot);
L->top -= 2; /* pop value and key */
}
lua_unlock(L); lua_unlock(L);
} }
LUA_API void lua_rawset (lua_State *L, int idx) { LUA_API void lua_rawset (lua_State *L, int idx) {
StkId o; StkId o;
Table *t; TValue *slot;
lua_lock(L); lua_lock(L);
api_checknelems(L, 2); api_checknelems(L, 2);
o = index2addr(L, idx); o = index2addr(L, idx);
api_check(L, ttistable(o), "table expected"); api_check(L, ttistable(o), "table expected");
t = hvalue(o); slot = luaH_set(L, hvalue(o), L->top - 2);
setobj2t(L, luaH_set(L, t, L->top-2), L->top-1); setobj2t(L, slot, L->top - 1);
invalidateTMcache(t); invalidateTMcache(hvalue(o));
luaC_barrierback(L, t, L->top-1); luaC_barrierback(L, hvalue(o), L->top-1);
L->top -= 2; L->top -= 2;
lua_unlock(L); lua_unlock(L);
} }
@ -789,14 +816,12 @@ LUA_API void lua_rawset (lua_State *L, int idx) {
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
StkId o; StkId o;
Table *t;
lua_lock(L); lua_lock(L);
api_checknelems(L, 1); api_checknelems(L, 1);
o = index2addr(L, idx); o = index2addr(L, idx);
api_check(L, ttistable(o), "table expected"); api_check(L, ttistable(o), "table expected");
t = hvalue(o); luaH_setint(L, hvalue(o), n, L->top - 1);
luaH_setint(L, t, n, L->top - 1); luaC_barrierback(L, hvalue(o), L->top-1);
luaC_barrierback(L, t, L->top-1);
L->top--; L->top--;
lua_unlock(L); lua_unlock(L);
} }
@ -804,16 +829,15 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
StkId o; StkId o;
Table *t; TValue k, *slot;
TValue k;
lua_lock(L); lua_lock(L);
api_checknelems(L, 1); api_checknelems(L, 1);
o = index2addr(L, idx); o = index2addr(L, idx);
api_check(L, ttistable(o), "table expected"); api_check(L, ttistable(o), "table expected");
t = hvalue(o);
setpvalue(&k, cast(void *, p)); setpvalue(&k, cast(void *, p));
setobj2t(L, luaH_set(L, t, &k), L->top - 1); slot = luaH_set(L, hvalue(o), &k);
luaC_barrierback(L, t, L->top - 1); setobj2t(L, slot, L->top - 1);
luaC_barrierback(L, hvalue(o), L->top - 1);
L->top--; L->top--;
lua_unlock(L); lua_unlock(L);
} }
@ -895,10 +919,10 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
L->ci->u.c.k = k; /* save continuation */ L->ci->u.c.k = k; /* save continuation */
L->ci->u.c.ctx = ctx; /* save context */ L->ci->u.c.ctx = ctx; /* save context */
luaD_call(L, func, nresults, 1); /* do the call */ luaD_call(L, func, nresults); /* do the call */
} }
else /* no continuation or no yieldable */ else /* no continuation or no yieldable */
luaD_call(L, func, nresults, 0); /* just do the call */ luaD_callnoyield(L, func, nresults); /* just do the call */
adjustresults(L, nresults); adjustresults(L, nresults);
lua_unlock(L); lua_unlock(L);
} }
@ -916,7 +940,7 @@ struct CallS { /* data to 'f_call' */
static void f_call (lua_State *L, void *ud) { static void f_call (lua_State *L, void *ud) {
struct CallS *c = cast(struct CallS *, ud); struct CallS *c = cast(struct CallS *, ud);
luaD_call(L, c->func, c->nresults, 0); luaD_callnoyield(L, c->func, c->nresults);
} }
@ -954,7 +978,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
L->errfunc = func; L->errfunc = func;
setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
luaD_call(L, c.func, nresults, 1); /* do the call */ luaD_call(L, c.func, nresults); /* do the call */
ci->callstatus &= ~CIST_YPCALL; ci->callstatus &= ~CIST_YPCALL;
L->errfunc = ci->u.c.old_errfunc; L->errfunc = ci->u.c.old_errfunc;
status = LUA_OK; /* if it is here, there were no errors */ status = LUA_OK; /* if it is here, there were no errors */
@ -1043,7 +1067,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
} }
case LUA_GCSTEP: { case LUA_GCSTEP: {
l_mem debt = 1; /* =1 to signal that it did an actual step */ l_mem debt = 1; /* =1 to signal that it did an actual step */
int oldrunning = g->gcrunning; lu_byte oldrunning = g->gcrunning;
g->gcrunning = 1; /* allow GC to run */ g->gcrunning = 1; /* allow GC to run */
if (data == 0) { if (data == 0) {
luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
@ -1117,7 +1141,6 @@ LUA_API void lua_concat (lua_State *L, int n) {
lua_lock(L); lua_lock(L);
api_checknelems(L, n); api_checknelems(L, n);
if (n >= 2) { if (n >= 2) {
luaC_checkGC(L);
luaV_concat(L, n); luaV_concat(L, n);
} }
else if (n == 0) { /* push empty string */ else if (n == 0) { /* push empty string */
@ -1125,6 +1148,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
api_incr_top(L); api_incr_top(L);
} }
/* else n == 1; nothing to do */ /* else n == 1; nothing to do */
luaC_checkGC(L);
lua_unlock(L); lua_unlock(L);
} }
@ -1160,10 +1184,10 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
LUA_API void *lua_newuserdata (lua_State *L, size_t size) { LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
Udata *u; Udata *u;
lua_lock(L); lua_lock(L);
luaC_checkGC(L);
u = luaS_newudata(L, size); u = luaS_newudata(L, size);
setuvalue(L, L->top, u); setuvalue(L, L->top, u);
api_incr_top(L); api_incr_top(L);
luaC_checkGC(L);
lua_unlock(L); lua_unlock(L);
return getudatamem(u); return getudatamem(u);
} }
@ -1230,13 +1254,12 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
} }
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { static UpVal **getupvalref (lua_State *L, int fidx, int n) {
LClosure *f; LClosure *f;
StkId fi = index2addr(L, fidx); StkId fi = index2addr(L, fidx);
api_check(L, ttisLclosure(fi), "Lua function expected"); api_check(L, ttisLclosure(fi), "Lua function expected");
f = clLvalue(fi); f = clLvalue(fi);
api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
if (pf) *pf = f;
return &f->upvals[n - 1]; /* get its upvalue pointer */ return &f->upvals[n - 1]; /* get its upvalue pointer */
} }
@ -1245,7 +1268,7 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
StkId fi = index2addr(L, fidx); StkId fi = index2addr(L, fidx);
switch (ttype(fi)) { switch (ttype(fi)) {
case LUA_TLCL: { /* lua closure */ case LUA_TLCL: { /* lua closure */
return *getupvalref(L, fidx, n, NULL); return *getupvalref(L, fidx, n);
} }
case LUA_TCCL: { /* C closure */ case LUA_TCCL: { /* C closure */
CClosure *f = clCvalue(fi); CClosure *f = clCvalue(fi);
@ -1262,9 +1285,10 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
int fidx2, int n2) { int fidx2, int n2) {
LClosure *f1; UpVal **up1 = getupvalref(L, fidx1, n1);
UpVal **up1 = getupvalref(L, fidx1, n1, &f1); UpVal **up2 = getupvalref(L, fidx2, n2);
UpVal **up2 = getupvalref(L, fidx2, n2, NULL); if (*up1 == *up2)
return;
luaC_upvdeccount(L, *up1); luaC_upvdeccount(L, *up1);
*up1 = *up2; *up1 = *up2;
(*up1)->refcount++; (*up1)->refcount++;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 roberto Exp $ ** $Id: lapi.h,v 2.9.1.1 2017/04/19 17:20:42 roberto Exp $
** Auxiliary functions from Lua API ** Auxiliary functions from Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.280 2015/02/03 17:38:24 roberto Exp $ ** $Id: lauxlib.c,v 1.289.1.1 2017/04/19 17:20:42 roberto Exp $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -17,7 +17,8 @@
#include <string.h> #include <string.h>
/* This file uses only the official API of Lua. /*
** This file uses only the official API of Lua.
** Any function declared here could be written as an application function. ** Any function declared here could be written as an application function.
*/ */
@ -33,8 +34,8 @@
*/ */
#define LEVELS1 12 /* size of the first part of the stack */ #define LEVELS1 10 /* size of the first part of the stack */
#define LEVELS2 10 /* size of the second part of the stack */ #define LEVELS2 11 /* size of the second part of the stack */
@ -68,12 +69,11 @@ static int findfield (lua_State *L, int objidx, int level) {
/* /*
** Search for a name for a function in all loaded modules ** Search for a name for a function in all loaded modules
** (registry._LOADED).
*/ */
static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
int top = lua_gettop(L); int top = lua_gettop(L);
lua_getinfo(L, "f", ar); /* push function */ lua_getinfo(L, "f", ar); /* push function */
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
if (findfield(L, top + 1, 2)) { if (findfield(L, top + 1, 2)) {
const char *name = lua_tostring(L, -1); const char *name = lua_tostring(L, -1);
if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */ if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */
@ -107,7 +107,7 @@ static void pushfuncname (lua_State *L, lua_Debug *ar) {
} }
static int countlevels (lua_State *L) { static int lastlevel (lua_State *L) {
lua_Debug ar; lua_Debug ar;
int li = 1, le = 1; int li = 1, le = 1;
/* find an upper bound */ /* find an upper bound */
@ -126,14 +126,16 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
const char *msg, int level) { const char *msg, int level) {
lua_Debug ar; lua_Debug ar;
int top = lua_gettop(L); int top = lua_gettop(L);
int numlevels = countlevels(L1); int last = lastlevel(L1);
int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0; int n1 = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1;
if (msg) lua_pushfstring(L, "%s\n", msg); if (msg)
lua_pushfstring(L, "%s\n", msg);
luaL_checkstack(L, 10, NULL);
lua_pushliteral(L, "stack traceback:"); lua_pushliteral(L, "stack traceback:");
while (lua_getstack(L1, level++, &ar)) { while (lua_getstack(L1, level++, &ar)) {
if (level == mark) { /* too many levels? */ if (n1-- == 0) { /* too many levels? */
lua_pushliteral(L, "\n\t..."); /* add a '...' */ lua_pushliteral(L, "\n\t..."); /* add a '...' */
level = numlevels - LEVELS2; /* and skip to last ones */ level = last - LEVELS2 + 1; /* and skip to last ones */
} }
else { else {
lua_getinfo(L1, "Slnt", &ar); lua_getinfo(L1, "Slnt", &ar);
@ -196,6 +198,10 @@ static void tag_error (lua_State *L, int arg, int tag) {
} }
/*
** The use of 'lua_pushfstring' ensures this function does not
** need reserved stack space when called.
*/
LUALIB_API void luaL_where (lua_State *L, int level) { LUALIB_API void luaL_where (lua_State *L, int level) {
lua_Debug ar; lua_Debug ar;
if (lua_getstack(L, level, &ar)) { /* check function at level */ if (lua_getstack(L, level, &ar)) { /* check function at level */
@ -205,10 +211,15 @@ LUALIB_API void luaL_where (lua_State *L, int level) {
return; return;
} }
} }
lua_pushliteral(L, ""); /* else, no information available... */ lua_pushfstring(L, ""); /* else, no information available... */
} }
/*
** 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.)
*/
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
va_list argp; va_list argp;
va_start(argp, fmt); va_start(argp, fmt);
@ -289,7 +300,7 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */ if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */
return 0; /* leave previous value on top, but return 0 */ return 0; /* leave previous value on top, but return 0 */
lua_pop(L, 1); lua_pop(L, 1);
lua_newtable(L); /* create metatable */ lua_createtable(L, 0, 2); /* create metatable */
lua_pushstring(L, tname); lua_pushstring(L, tname);
lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ lua_setfield(L, -2, "__name"); /* metatable.__name = tname */
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
@ -347,10 +358,15 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
} }
/*
** Ensures the stack has at least 'space' extra slots, raising an error
** if it cannot fulfill the request. (The error handling needs a few
** extra slots to format the error message. In case of an error without
** this extra space, Lua will generate the same 'stack overflow' error,
** but without 'msg'.)
*/
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
/* keep some extra space to run error routines, if needed */ if (!lua_checkstack(L, space)) {
const int extra = LUA_MINSTACK;
if (!lua_checkstack(L, space + extra)) {
if (msg) if (msg)
luaL_error(L, "stack overflow (%s)", msg); luaL_error(L, "stack overflow (%s)", msg);
else else
@ -435,6 +451,47 @@ LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg,
** ======================================================= ** =======================================================
*/ */
/* userdata to box arbitrary data */
typedef struct UBox {
void *box;
size_t bsize;
} UBox;
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 (temp == NULL && newsize > 0) { /* allocation error? */
resizebox(L, idx, 0); /* free buffer */
luaL_error(L, "not enough memory for buffer allocation");
}
box->box = temp;
box->bsize = newsize;
return temp;
}
static int boxgc (lua_State *L) {
resizebox(L, 1, 0);
return 0;
}
static void *newbox (lua_State *L, size_t newsize) {
UBox *box = (UBox *)lua_newuserdata(L, sizeof(UBox));
box->box = NULL;
box->bsize = 0;
if (luaL_newmetatable(L, "LUABOX")) { /* creating metatable? */
lua_pushcfunction(L, boxgc);
lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */
}
lua_setmetatable(L, -2);
return resizebox(L, -1, newsize);
}
/* /*
** check whether buffer is using a userdata on the stack as a temporary ** check whether buffer is using a userdata on the stack as a temporary
** buffer ** buffer
@ -455,11 +512,12 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
if (newsize < B->n || newsize - B->n < sz) if (newsize < B->n || newsize - B->n < sz)
luaL_error(L, "buffer too large"); luaL_error(L, "buffer too large");
/* create larger buffer */ /* create larger buffer */
newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));
/* move content to new buffer */
memcpy(newbuff, B->b, B->n * sizeof(char));
if (buffonstack(B)) if (buffonstack(B))
lua_remove(L, -2); /* remove old buffer */ newbuff = (char *)resizebox(L, -1, newsize);
else { /* no buffer yet */
newbuff = (char *)newbox(L, newsize);
memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */
}
B->b = newbuff; B->b = newbuff;
B->size = newsize; B->size = newsize;
} }
@ -468,9 +526,11 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
char *b = luaL_prepbuffsize(B, l); if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */
memcpy(b, s, l * sizeof(char)); char *b = luaL_prepbuffsize(B, l);
luaL_addsize(B, l); memcpy(b, s, l * sizeof(char));
luaL_addsize(B, l);
}
} }
@ -482,8 +542,10 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
LUALIB_API void luaL_pushresult (luaL_Buffer *B) { LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
lua_State *L = B->L; lua_State *L = B->L;
lua_pushlstring(L, B->b, B->n); lua_pushlstring(L, B->b, B->n);
if (buffonstack(B)) if (buffonstack(B)) {
lua_remove(L, -2); /* remove old buffer */ resizebox(L, -2, 0); /* delete old buffer */
lua_remove(L, -2); /* remove its header from the stack */
}
} }
@ -605,7 +667,7 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
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"; /* UTF-8 BOM mark */
int c; int c;
lf->n = 0; lf->n = 0;
do { do {
@ -630,7 +692,7 @@ static int skipcomment (LoadF *lf, int *cp) {
if (c == '#') { /* first line is a comment (Unix exec. file)? */ if (c == '#') { /* first line is a comment (Unix exec. file)? */
do { /* skip first line */ do { /* skip first line */
c = getc(lf->f); c = getc(lf->f);
} while (c != EOF && c != '\n') ; } while (c != EOF && c != '\n');
*cp = getc(lf->f); /* skip end-of-line, if present */ *cp = getc(lf->f); /* skip end-of-line, if present */
return 1; /* there was a comment */ return 1; /* there was a comment */
} }
@ -746,13 +808,17 @@ 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) { LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
if (!lua_isstring(L, -1))
luaL_error(L, "'__tostring' must return a string");
}
else {
switch (lua_type(L, idx)) { switch (lua_type(L, idx)) {
case LUA_TNUMBER: { case LUA_TNUMBER: {
if (lua_isinteger(L, idx)) if (lua_isinteger(L, idx))
lua_pushfstring(L, "%I", lua_tointeger(L, idx)); lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx));
else else
lua_pushfstring(L, "%f", lua_tonumber(L, idx)); lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx));
break; break;
} }
case LUA_TSTRING: case LUA_TSTRING:
@ -764,10 +830,15 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
case LUA_TNIL: case LUA_TNIL:
lua_pushliteral(L, "nil"); lua_pushliteral(L, "nil");
break; break;
default: default: {
lua_pushfstring(L, "%s: %p", luaL_typename(L, idx), int tt = luaL_getmetafield(L, idx, "__name"); /* try name */
lua_topointer(L, idx)); const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) :
luaL_typename(L, idx);
lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx));
if (tt != LUA_TNIL)
lua_remove(L, -2); /* remove '__name' */
break; break;
}
} }
} }
return lua_tolstring(L, -1, len); return lua_tolstring(L, -1, len);
@ -819,23 +890,23 @@ static int libsize (const luaL_Reg *l) {
/* /*
** Find or create a module table with a given name. The function ** Find or create a module table with a given name. The function
** first looks at the _LOADED table and, if that fails, try a ** first looks at the LOADED table and, if that fails, try a
** global variable with that name. In any case, leaves on the stack ** global variable with that name. In any case, leaves on the stack
** the module table. ** the module table.
*/ */
LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
int sizehint) { int sizehint) {
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ luaL_findtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE, 1);
if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no _LOADED[modname]? */ if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no LOADED[modname]? */
lua_pop(L, 1); /* remove previous result */ lua_pop(L, 1); /* remove previous result */
/* try global variable (and create one if it does not exist) */ /* try global variable (and create one if it does not exist) */
lua_pushglobaltable(L); lua_pushglobaltable(L);
if (luaL_findtable(L, 0, modname, sizehint) != NULL) if (luaL_findtable(L, 0, modname, sizehint) != NULL)
luaL_error(L, "name conflict for module '%s'", modname); luaL_error(L, "name conflict for module '%s'", modname);
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */ lua_setfield(L, -3, modname); /* LOADED[modname] = new table */
} }
lua_remove(L, -2); /* remove _LOADED table */ lua_remove(L, -2); /* remove LOADED table */
} }
@ -899,17 +970,17 @@ LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
*/ */
LUALIB_API void luaL_requiref (lua_State *L, const char *modname, LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
lua_CFunction openf, int glb) { lua_CFunction openf, int glb) {
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_getfield(L, -1, modname); /* _LOADED[modname] */ lua_getfield(L, -1, modname); /* LOADED[modname] */
if (!lua_toboolean(L, -1)) { /* package not already loaded? */ if (!lua_toboolean(L, -1)) { /* package not already loaded? */
lua_pop(L, 1); /* remove field */ lua_pop(L, 1); /* remove field */
lua_pushcfunction(L, openf); lua_pushcfunction(L, openf);
lua_pushstring(L, modname); /* argument to open function */ lua_pushstring(L, modname); /* argument to open function */
lua_call(L, 1, 1); /* call 'openf' to open module */ lua_call(L, 1, 1); /* call 'openf' to open module */
lua_pushvalue(L, -1); /* make copy of module (call result) */ lua_pushvalue(L, -1); /* make copy of module (call result) */
lua_setfield(L, -3, modname); /* _LOADED[modname] = module */ lua_setfield(L, -3, modname); /* LOADED[modname] = module */
} }
lua_remove(L, -2); /* remove _LOADED table */ lua_remove(L, -2); /* remove LOADED table */
if (glb) { if (glb) {
lua_pushvalue(L, -1); /* copy of module */ lua_pushvalue(L, -1); /* copy of module */
lua_setglobal(L, modname); /* _G[modname] = module */ lua_setglobal(L, modname); /* _G[modname] = module */
@ -940,8 +1011,13 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
free(ptr); free(ptr);
return NULL; return NULL;
} }
else else { /* cannot fail when shrinking a block */
return realloc(ptr, nsize); void *newptr = realloc(ptr, nsize);
if (newptr == NULL && ptr != NULL && nsize <= osize)
return ptr; /* keep the original block */
else /* no fail or not shrinking */
return newptr; /* use the new block */
}
} }
@ -967,6 +1043,6 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
luaL_error(L, "multiple Lua VMs detected"); luaL_error(L, "multiple Lua VMs detected");
else if (*v != ver) else if (*v != ver)
luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
ver, *v); (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.310 2015/03/28 19:14:47 roberto Exp $ ** $Id: lbaselib.c,v 1.314.1.1 2017/04/19 17:39:34 roberto Exp $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -86,8 +86,8 @@ static int luaB_tonumber (lua_State *L) {
const char *s; const char *s;
lua_Integer n = 0; /* to avoid warnings */ lua_Integer n = 0; /* to avoid warnings */
lua_Integer base = luaL_checkinteger(L, 2); lua_Integer base = luaL_checkinteger(L, 2);
luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */ luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
s = luaL_checklstring(L, 1, &l); s = lua_tolstring(L, 1, &l);
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
if (b_str2int(s, (int)base, &n) == s + l) { if (b_str2int(s, (int)base, &n) == s + l) {
lua_pushinteger(L, n); lua_pushinteger(L, n);
@ -102,8 +102,8 @@ static int luaB_tonumber (lua_State *L) {
static int luaB_error (lua_State *L) { static int luaB_error (lua_State *L) {
int level = (int)luaL_optinteger(L, 2, 1); int level = (int)luaL_optinteger(L, 2, 1);
lua_settop(L, 1); lua_settop(L, 1);
if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ if (lua_type(L, 1) == LUA_TSTRING && level > 0) {
luaL_where(L, level); luaL_where(L, level); /* add extra information */
lua_pushvalue(L, 1); lua_pushvalue(L, 1);
lua_concat(L, 2); lua_concat(L, 2);
} }
@ -198,20 +198,18 @@ static int luaB_collectgarbage (lua_State *L) {
} }
/*
** This function has all type names as upvalues, to maximize performance.
*/
static int luaB_type (lua_State *L) { static int luaB_type (lua_State *L) {
luaL_checkany(L, 1); int t = lua_type(L, 1);
lua_pushvalue(L, lua_upvalueindex(lua_type(L, 1) + 1)); luaL_argcheck(L, t != LUA_TNONE, 1, "value expected");
lua_pushstring(L, lua_typename(L, t));
return 1; return 1;
} }
static int pairsmeta (lua_State *L, const char *method, int iszero, static int pairsmeta (lua_State *L, const char *method, int iszero,
lua_CFunction iter) { lua_CFunction iter) {
luaL_checkany(L, 1);
if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */ if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */
luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
lua_pushcfunction(L, iter); /* will return generator, */ lua_pushcfunction(L, iter); /* will return generator, */
lua_pushvalue(L, 1); /* state, */ lua_pushvalue(L, 1); /* state, */
if (iszero) lua_pushinteger(L, 0); /* and initial value */ if (iszero) lua_pushinteger(L, 0); /* and initial value */
@ -243,18 +241,7 @@ static int luaB_pairs (lua_State *L) {
/* /*
** Traversal function for 'ipairs' for raw tables ** Traversal function for 'ipairs'
*/
static int ipairsaux_raw (lua_State *L) {
lua_Integer i = luaL_checkinteger(L, 2) + 1;
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushinteger(L, i);
return (lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2;
}
/*
** Traversal function for 'ipairs' for tables with metamethods
*/ */
static int ipairsaux (lua_State *L) { static int ipairsaux (lua_State *L) {
lua_Integer i = luaL_checkinteger(L, 2) + 1; lua_Integer i = luaL_checkinteger(L, 2) + 1;
@ -264,18 +251,15 @@ static int ipairsaux (lua_State *L) {
/* /*
** This function will use either 'ipairsaux' or 'ipairsaux_raw' to ** 'ipairs' function. Returns 'ipairsaux', given "table", 0.
** traverse a table, depending on whether the table has metamethods ** (The given "table" may not be a table.)
** that can affect the traversal.
*/ */
static int luaB_ipairs (lua_State *L) { static int luaB_ipairs (lua_State *L) {
lua_CFunction iter = (luaL_getmetafield(L, 1, "__index") != LUA_TNIL)
? ipairsaux : ipairsaux_raw;
#if defined(LUA_COMPAT_IPAIRS) #if defined(LUA_COMPAT_IPAIRS)
return pairsmeta(L, "__ipairs", 1, iter); return pairsmeta(L, "__ipairs", 1, ipairsaux);
#else #else
luaL_checkany(L, 1); luaL_checkany(L, 1);
lua_pushcfunction(L, iter); /* iteration function */ lua_pushcfunction(L, ipairsaux); /* iteration function */
lua_pushvalue(L, 1); /* state */ lua_pushvalue(L, 1); /* state */
lua_pushinteger(L, 0); /* initial value */ lua_pushinteger(L, 0); /* initial value */
return 3; return 3;
@ -490,9 +474,9 @@ static const luaL_Reg base_funcs[] = {
{"setmetatable", luaB_setmetatable}, {"setmetatable", luaB_setmetatable},
{"tonumber", luaB_tonumber}, {"tonumber", luaB_tonumber},
{"tostring", luaB_tostring}, {"tostring", luaB_tostring},
{"type", luaB_type},
{"xpcall", luaB_xpcall}, {"xpcall", luaB_xpcall},
/* placeholders */ /* placeholders */
{"type", NULL},
{"_G", NULL}, {"_G", NULL},
{"_VERSION", NULL}, {"_VERSION", NULL},
{NULL, NULL} {NULL, NULL}
@ -500,7 +484,6 @@ static const luaL_Reg base_funcs[] = {
LUAMOD_API int luaopen_base (lua_State *L) { LUAMOD_API int luaopen_base (lua_State *L) {
int i;
/* open lib into global table */ /* open lib into global table */
lua_pushglobaltable(L); lua_pushglobaltable(L);
luaL_setfuncs(L, base_funcs, 0); luaL_setfuncs(L, base_funcs, 0);
@ -510,11 +493,6 @@ LUAMOD_API int luaopen_base (lua_State *L) {
/* set global _VERSION */ /* set global _VERSION */
lua_pushliteral(L, LUA_VERSION); lua_pushliteral(L, LUA_VERSION);
lua_setfield(L, -2, "_VERSION"); lua_setfield(L, -2, "_VERSION");
/* set function 'type' with proper upvalues */
for (i = 0; i < LUA_NUMTAGS; i++) /* push all type names as upvalues */
lua_pushstring(L, lua_typename(L, i));
lua_pushcclosure(L, luaB_type, LUA_NUMTAGS);
lua_setfield(L, -2, "type");
return 1; return 1;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbitlib.c,v 1.28 2014/11/02 19:19:04 roberto Exp $ ** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $
** Standard library for bitwise operations ** Standard library for bitwise operations
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -19,6 +19,10 @@
#if defined(LUA_COMPAT_BITLIB) /* { */ #if defined(LUA_COMPAT_BITLIB) /* { */
#define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
#define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i))
/* number of bits to consider in a number */ /* number of bits to consider in a number */
#if !defined(LUA_NBITS) #if !defined(LUA_NBITS)
#define LUA_NBITS 32 #define LUA_NBITS 32
@ -46,14 +50,14 @@ static lua_Unsigned andaux (lua_State *L) {
int i, n = lua_gettop(L); int i, n = lua_gettop(L);
lua_Unsigned r = ~(lua_Unsigned)0; lua_Unsigned r = ~(lua_Unsigned)0;
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
r &= luaL_checkunsigned(L, i); r &= checkunsigned(L, i);
return trim(r); return trim(r);
} }
static int b_and (lua_State *L) { static int b_and (lua_State *L) {
lua_Unsigned r = andaux(L); lua_Unsigned r = andaux(L);
lua_pushunsigned(L, r); pushunsigned(L, r);
return 1; return 1;
} }
@ -69,8 +73,8 @@ static int b_or (lua_State *L) {
int i, n = lua_gettop(L); int i, n = lua_gettop(L);
lua_Unsigned r = 0; lua_Unsigned r = 0;
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
r |= luaL_checkunsigned(L, i); r |= checkunsigned(L, i);
lua_pushunsigned(L, trim(r)); pushunsigned(L, trim(r));
return 1; return 1;
} }
@ -79,15 +83,15 @@ static int b_xor (lua_State *L) {
int i, n = lua_gettop(L); int i, n = lua_gettop(L);
lua_Unsigned r = 0; lua_Unsigned r = 0;
for (i = 1; i <= n; i++) for (i = 1; i <= n; i++)
r ^= luaL_checkunsigned(L, i); r ^= checkunsigned(L, i);
lua_pushunsigned(L, trim(r)); pushunsigned(L, trim(r));
return 1; return 1;
} }
static int b_not (lua_State *L) { static int b_not (lua_State *L) {
lua_Unsigned r = ~luaL_checkunsigned(L, 1); lua_Unsigned r = ~checkunsigned(L, 1);
lua_pushunsigned(L, trim(r)); pushunsigned(L, trim(r));
return 1; return 1;
} }
@ -104,23 +108,23 @@ static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) {
else r <<= i; else r <<= i;
r = trim(r); r = trim(r);
} }
lua_pushunsigned(L, r); pushunsigned(L, r);
return 1; return 1;
} }
static int b_lshift (lua_State *L) { static int b_lshift (lua_State *L) {
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2)); return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2));
} }
static int b_rshift (lua_State *L) { static int b_rshift (lua_State *L) {
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2)); return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2));
} }
static int b_arshift (lua_State *L) { static int b_arshift (lua_State *L) {
lua_Unsigned r = luaL_checkunsigned(L, 1); lua_Unsigned r = checkunsigned(L, 1);
lua_Integer i = luaL_checkinteger(L, 2); lua_Integer i = luaL_checkinteger(L, 2);
if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
return b_shift(L, r, -i); return b_shift(L, r, -i);
@ -128,19 +132,19 @@ static int b_arshift (lua_State *L) {
if (i >= LUA_NBITS) r = ALLONES; if (i >= LUA_NBITS) r = ALLONES;
else else
r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */
lua_pushunsigned(L, r); pushunsigned(L, r);
return 1; return 1;
} }
} }
static int b_rot (lua_State *L, lua_Integer d) { static int b_rot (lua_State *L, lua_Integer d) {
lua_Unsigned r = luaL_checkunsigned(L, 1); lua_Unsigned r = checkunsigned(L, 1);
int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ int i = d & (LUA_NBITS - 1); /* i = d % NBITS */
r = trim(r); r = trim(r);
if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
r = (r << i) | (r >> (LUA_NBITS - i)); r = (r << i) | (r >> (LUA_NBITS - i));
lua_pushunsigned(L, trim(r)); pushunsigned(L, trim(r));
return 1; return 1;
} }
@ -175,23 +179,22 @@ static int fieldargs (lua_State *L, int farg, int *width) {
static int b_extract (lua_State *L) { static int b_extract (lua_State *L) {
int w; int w;
lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); lua_Unsigned r = trim(checkunsigned(L, 1));
int f = fieldargs(L, 2, &w); int f = fieldargs(L, 2, &w);
r = (r >> f) & mask(w); r = (r >> f) & mask(w);
lua_pushunsigned(L, r); pushunsigned(L, r);
return 1; return 1;
} }
static int b_replace (lua_State *L) { static int b_replace (lua_State *L) {
int w; int w;
lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); lua_Unsigned r = trim(checkunsigned(L, 1));
lua_Unsigned v = luaL_checkunsigned(L, 2); lua_Unsigned v = trim(checkunsigned(L, 2));
int f = fieldargs(L, 3, &w); int f = fieldargs(L, 3, &w);
int m = mask(w); lua_Unsigned m = mask(w);
v &= m; /* erase bits outside given width */ r = (r & ~(m << f)) | ((v & m) << f);
r = (r & ~(m << f)) | (v << f); pushunsigned(L, r);
lua_pushunsigned(L, r);
return 1; return 1;
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.h,v 1.63 2013/12/30 20:47:58 roberto Exp $ ** $Id: lcode.h,v 1.64.1.1 2017/04/19 17:20:42 roberto Exp $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -40,7 +40,8 @@ typedef enum BinOpr {
typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
#define getcode(fs,e) ((fs)->f->code[(e)->u.info]) /* get (pointer to) instruction of given 'expdesc' */
#define getinstruction(fs,e) ((fs)->f->code[(e)->u.info])
#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcorolib.c,v 1.9 2014/11/02 19:19:04 roberto Exp $ ** $Id: lcorolib.c,v 1.10.1.1 2017/04/19 17:20:42 roberto Exp $
** Coroutine Library ** Coroutine Library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -75,7 +75,7 @@ static int luaB_auxwrap (lua_State *L) {
lua_State *co = lua_tothread(L, lua_upvalueindex(1)); lua_State *co = lua_tothread(L, lua_upvalueindex(1));
int r = auxresume(L, co, lua_gettop(L)); int r = auxresume(L, co, lua_gettop(L));
if (r < 0) { if (r < 0) {
if (lua_isstring(L, -1)) { /* error object is a string? */ if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */
luaL_where(L, 1); /* add extra info */ luaL_where(L, 1); /* add extra info */
lua_insert(L, -2); lua_insert(L, -2);
lua_concat(L, 2); lua_concat(L, 2);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $ ** $Id: lctype.c,v 1.12.1.1 2017/04/19 17:20:42 roberto Exp $
** 'ctype' functions for Lua ** 'ctype' functions for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $
** 'ctype' functions for Lua ** 'ctype' functions for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldblib.c,v 1.149 2015/02/19 17:06:21 roberto Exp $ ** $Id: ldblib.c,v 1.151.1.1 2017/04/19 17:20:42 roberto Exp $
** Interface from Lua to its debug API ** Interface from Lua to its debug API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -28,8 +28,8 @@ static const int HOOKKEY = 0;
/* /*
** If L1 != L, L1 can be in any state, and therefore there is no ** If L1 != L, L1 can be in any state, and therefore there are no
** garanties about its stack space; any push in L1 must be ** guarantees about its stack space; any push in L1 must be
** checked. ** checked.
*/ */
static void checkstack (lua_State *L, lua_State *L1, int n) { static void checkstack (lua_State *L, lua_State *L1, int n) {

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 2.115 2015/05/22 17:45:56 roberto Exp $ ** $Id: ldebug.c,v 2.121.1.2 2017/07/10 17:21:50 roberto Exp $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -38,7 +38,8 @@
#define ci_func(ci) (clLvalue((ci)->func)) #define ci_func(ci) (clLvalue((ci)->func))
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
const char **name);
static int currentpc (CallInfo *ci) { static int currentpc (CallInfo *ci) {
@ -69,7 +70,13 @@ static void swapextra (lua_State *L) {
/* /*
** this function can be called asynchronous (e.g. during a signal) ** This function can be called asynchronously (e.g. during a signal).
** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by
** 'resethookcount') are for debug only, and it is no problem if they
** get arbitrary values (causes at most one wrong hook call). 'hookmask'
** is an atomic value. We assume that pointers are atomic too (e.g., gcc
** ensures that for all platforms where it runs). Moreover, 'hook' is
** always checked before being called (see 'luaD_hook').
*/ */
LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
if (func == NULL || mask == 0) { /* turn off hooks? */ if (func == NULL || mask == 0) { /* turn off hooks? */
@ -126,10 +133,11 @@ static const char *upvalname (Proto *p, int uv) {
static const char *findvararg (CallInfo *ci, int n, StkId *pos) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
int nparams = clLvalue(ci->func)->p->numparams; int nparams = clLvalue(ci->func)->p->numparams;
if (n >= cast_int(ci->u.l.base - ci->func) - nparams) int nvararg = cast_int(ci->u.l.base - ci->func) - nparams;
if (n <= -nvararg)
return NULL; /* no such vararg */ return NULL; /* no such vararg */
else { else {
*pos = ci->func + nparams + n; *pos = ci->func + nparams - n;
return "(*vararg)"; /* generic name for any vararg */ return "(*vararg)"; /* generic name for any vararg */
} }
} }
@ -141,7 +149,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n,
StkId base; StkId base;
if (isLua(ci)) { if (isLua(ci)) {
if (n < 0) /* access to vararg values? */ if (n < 0) /* access to vararg values? */
return findvararg(ci, -n, pos); return findvararg(ci, n, pos);
else { else {
base = ci->u.l.base; base = ci->u.l.base;
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
@ -238,6 +246,20 @@ static void collectvalidlines (lua_State *L, Closure *f) {
} }
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
if (ci == NULL) /* no 'ci'? */
return NULL; /* no info */
else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */
*name = "__gc";
return "metamethod"; /* report it as such */
}
/* calling function is a known Lua function? */
else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
return funcnamefromcode(L, ci->previous, name);
else return NULL; /* no way to find a name */
}
static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
Closure *f, CallInfo *ci) { Closure *f, CallInfo *ci) {
int status = 1; int status = 1;
@ -268,11 +290,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
break; break;
} }
case 'n': { case 'n': {
/* calling function is a known Lua function? */ ar->namewhat = getfuncname(L, ci, &ar->name);
if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
ar->namewhat = getfuncname(L, ci->previous, &ar->name);
else
ar->namewhat = NULL;
if (ar->namewhat == NULL) { if (ar->namewhat == NULL) {
ar->namewhat = ""; /* not found */ ar->namewhat = ""; /* not found */
ar->name = NULL; ar->name = NULL;
@ -465,8 +483,15 @@ static const char *getobjname (Proto *p, int lastpc, int reg,
} }
static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { /*
TMS tm = (TMS)0; /* to avoid warnings */ ** Try to find a name for a function based on the code that called it.
** (Only works when function was called by a Lua function.)
** Returns what the name is (e.g., "for iterator", "method",
** "metamethod") and sets '*name' to point to the name.
*/
static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
const char **name) {
TMS tm = (TMS)0; /* (initial value avoids warnings) */
Proto *p = ci_func(ci)->p; /* calling function */ Proto *p = ci_func(ci)->p; /* calling function */
int pc = currentpc(ci); /* calling instruction index */ int pc = currentpc(ci); /* calling instruction index */
Instruction i = p->code[pc]; /* calling instruction */ Instruction i = p->code[pc]; /* calling instruction */
@ -476,13 +501,13 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
} }
switch (GET_OPCODE(i)) { switch (GET_OPCODE(i)) {
case OP_CALL: case OP_CALL:
case OP_TAILCALL: /* get function name */ case OP_TAILCALL:
return getobjname(p, pc, GETARG_A(i), name); return getobjname(p, pc, GETARG_A(i), name); /* get function name */
case OP_TFORCALL: { /* for iterator */ case OP_TFORCALL: { /* for iterator */
*name = "for iterator"; *name = "for iterator";
return "for iterator"; return "for iterator";
} }
/* all other instructions can call only through metamethods */ /* other instructions can do calls through metamethods */
case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: case OP_GETTABUP: case OP_GETTABLE:
tm = TM_INDEX; tm = TM_INDEX;
break; break;
@ -503,7 +528,8 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
case OP_EQ: tm = TM_EQ; break; case OP_EQ: tm = TM_EQ; break;
case OP_LT: tm = TM_LT; break; case OP_LT: tm = TM_LT; break;
case OP_LE: tm = TM_LE; break; case OP_LE: tm = TM_LE; break;
default: lua_assert(0); /* other instructions cannot call a function */ default:
return NULL; /* cannot find a reasonable name */
} }
*name = getstr(G(L)->tmname[tm]); *name = getstr(G(L)->tmname[tm]);
return "metamethod"; return "metamethod";
@ -558,7 +584,7 @@ static const char *varinfo (lua_State *L, const TValue *o) {
l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
const char *t = objtypename(o); const char *t = luaT_objtypename(L, o);
luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
} }
@ -590,9 +616,9 @@ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
const char *t1 = objtypename(p1); const char *t1 = luaT_objtypename(L, p1);
const char *t2 = objtypename(p2); const char *t2 = luaT_objtypename(L, p2);
if (t1 == t2) if (strcmp(t1, t2) == 0)
luaG_runerror(L, "attempt to compare two %s values", t1); luaG_runerror(L, "attempt to compare two %s values", t1);
else else
luaG_runerror(L, "attempt to compare %s with %s", t1, t2); luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
@ -618,7 +644,7 @@ l_noret luaG_errormsg (lua_State *L) {
setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top, L->top - 1); /* move argument */
setobjs2s(L, L->top - 1, errfunc); /* push function */ setobjs2s(L, L->top - 1, errfunc); /* push function */
L->top++; /* assume EXTRA_STACK */ L->top++; /* assume EXTRA_STACK */
luaD_call(L, L->top - 2, 1, 0); /* call it */ luaD_callnoyield(L, L->top - 2, 1); /* call it */
} }
luaD_throw(L, LUA_ERRRUN); luaD_throw(L, LUA_ERRRUN);
} }
@ -628,6 +654,7 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
const char *msg; const char *msg;
va_list argp; va_list argp;
luaC_checkGC(L); /* error message uses memory */
va_start(argp, fmt); va_start(argp, fmt);
msg = luaO_pushvfstring(L, fmt, argp); /* format message */ msg = luaO_pushvfstring(L, fmt, argp); /* format message */
va_end(argp); va_end(argp);
@ -640,9 +667,11 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
void luaG_traceexec (lua_State *L) { void luaG_traceexec (lua_State *L) {
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
lu_byte mask = L->hookmask; lu_byte mask = L->hookmask;
int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0); int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
if (counthook) if (counthook)
resethookcount(L); /* reset count */ resethookcount(L); /* reset count */
else if (!(mask & LUA_MASKLINE))
return; /* no line hook and count != 0; nothing to be done */
if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
return; /* do not call hook again (VM yielded, so it did not move) */ return; /* do not call hook again (VM yielded, so it did not move) */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp $ ** $Id: ldebug.h,v 2.14.1.1 2017/04/19 17:20:42 roberto Exp $
** Auxiliary functions from Debug Interface module ** Auxiliary functions from Debug Interface module
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 2.138 2015/05/22 17:48:19 roberto Exp $ ** $Id: ldo.c,v 2.157.1.1 2017/04/19 17:20:42 roberto Exp $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -150,6 +150,11 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
/* }====================================================== */ /* }====================================================== */
/*
** {==================================================================
** Stack reallocation
** ===================================================================
*/
static void correctstack (lua_State *L, TValue *oldstack) { static void correctstack (lua_State *L, TValue *oldstack) {
CallInfo *ci; CallInfo *ci;
UpVal *up; UpVal *up;
@ -206,9 +211,9 @@ static int stackinuse (lua_State *L) {
CallInfo *ci; CallInfo *ci;
StkId lim = L->top; StkId lim = L->top;
for (ci = L->ci; ci != NULL; ci = ci->previous) { for (ci = L->ci; ci != NULL; ci = ci->previous) {
lua_assert(ci->top <= L->stack_last);
if (lim < ci->top) lim = ci->top; if (lim < ci->top) lim = ci->top;
} }
lua_assert(lim <= L->stack_last);
return cast_int(lim - L->stack) + 1; /* part of stack in use */ return cast_int(lim - L->stack) + 1; /* part of stack in use */
} }
@ -216,22 +221,38 @@ static int stackinuse (lua_State *L) {
void luaD_shrinkstack (lua_State *L) { void luaD_shrinkstack (lua_State *L) {
int inuse = stackinuse(L); int inuse = stackinuse(L);
int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; if (goodsize > LUAI_MAXSTACK)
if (L->stacksize > LUAI_MAXSTACK) /* was handling stack overflow? */ goodsize = LUAI_MAXSTACK; /* respect stack limit */
if (L->stacksize > LUAI_MAXSTACK) /* had been handling stack overflow? */
luaE_freeCI(L); /* free all CIs (list grew because of an error) */ luaE_freeCI(L); /* free all CIs (list grew because of an error) */
else else
luaE_shrinkCI(L); /* shrink list */ luaE_shrinkCI(L); /* shrink list */
if (inuse > LUAI_MAXSTACK || /* still handling stack overflow? */ /* if thread is currently not handling a stack overflow and its
goodsize >= L->stacksize) /* would grow instead of shrink? */ good size is smaller than current size, shrink its stack */
condmovestack(L); /* don't change stack (change only for debugging) */ if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) &&
else goodsize < L->stacksize)
luaD_reallocstack(L, goodsize); /* shrink it */ luaD_reallocstack(L, goodsize);
else /* don't change stack */
condmovestack(L,{},{}); /* (change only for debugging) */
} }
void luaD_inctop (lua_State *L) {
luaD_checkstack(L, 1);
L->top++;
}
/* }================================================================== */
/*
** Call a hook for the given event. Make sure there is a hook to be
** called. (Both 'L->hook' and 'L->hookmask', which triggers this
** function, can be changed asynchronously by signals.)
*/
void luaD_hook (lua_State *L, int event, int line) { void luaD_hook (lua_State *L, int event, int line) {
lua_Hook hook = L->hook; lua_Hook hook = L->hook;
if (hook && L->allowhook) { if (hook && L->allowhook) { /* make sure there is a hook */
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
ptrdiff_t top = savestack(L, L->top); ptrdiff_t top = savestack(L, L->top);
ptrdiff_t ci_top = savestack(L, ci->top); ptrdiff_t ci_top = savestack(L, ci->top);
@ -273,15 +294,15 @@ static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
int i; int i;
int nfixargs = p->numparams; int nfixargs = p->numparams;
StkId base, fixed; StkId base, fixed;
lua_assert(actual >= nfixargs);
/* move fixed parameters to final position */ /* move fixed parameters to final position */
luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */
fixed = L->top - actual; /* first fixed argument */ fixed = L->top - actual; /* first fixed argument */
base = L->top; /* final position of first argument */ base = L->top; /* final position of first argument */
for (i=0; i<nfixargs; i++) { for (i = 0; i < nfixargs && i < actual; i++) {
setobjs2s(L, L->top++, fixed + i); setobjs2s(L, L->top++, fixed + i);
setnilvalue(fixed + i); setnilvalue(fixed + i); /* erase original copy (for GC) */
} }
for (; i < nfixargs; i++)
setnilvalue(L->top++); /* complete missing arguments */
return base; return base;
} }
@ -304,85 +325,57 @@ static void tryfuncTM (lua_State *L, StkId func) {
} }
#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
/* /*
** returns true if function has been executed (C function) ** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'.
** Handle most typical cases (zero results for commands, one result for
** expressions, multiple results for tail calls/single parameters)
** separated.
*/ */
int luaD_precall (lua_State *L, StkId func, int nresults) { static int moveresults (lua_State *L, const TValue *firstResult, StkId res,
lua_CFunction f; int nres, int wanted) {
CallInfo *ci; switch (wanted) { /* handle typical cases separately */
int n; /* number of arguments (Lua) or returns (C) */ case 0: break; /* nothing to move */
ptrdiff_t funcr = savestack(L, func); case 1: { /* one result needed */
switch (ttype(func)) { if (nres == 0) /* no results? */
case LUA_TLCF: /* light C function */ firstResult = luaO_nilobject; /* adjust with nil */
f = fvalue(func); setobjs2s(L, res, firstResult); /* move it to proper place */
goto Cfunc; break;
case LUA_TCCL: { /* C closure */
f = clCvalue(func)->f;
Cfunc:
luaC_checkGC(L); /* stack grow uses memory */
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
ci = next_ci(L); /* now 'enter' new function */
ci->nresults = nresults;
ci->func = restorestack(L, funcr);
ci->top = L->top + LUA_MINSTACK;
lua_assert(ci->top <= L->stack_last);
ci->callstatus = 0;
if (L->hookmask & LUA_MASKCALL)
luaD_hook(L, LUA_HOOKCALL, -1);
lua_unlock(L);
n = (*f)(L); /* do the actual call */
lua_lock(L);
api_checknelems(L, n);
luaD_poscall(L, L->top - n, n);
return 1;
} }
case LUA_TLCL: { /* Lua function: prepare its call */ case LUA_MULTRET: {
StkId base; int i;
Proto *p = clLvalue(func)->p; for (i = 0; i < nres; i++) /* move all results to correct place */
n = cast_int(L->top - func) - 1; /* number of real arguments */ setobjs2s(L, res + i, firstResult + i);
luaC_checkGC(L); /* stack grow uses memory */ L->top = res + nres;
luaD_checkstack(L, p->maxstacksize); return 0; /* wanted == LUA_MULTRET */
for (; n < p->numparams; n++)
setnilvalue(L->top++); /* complete missing arguments */
if (!p->is_vararg) {
func = restorestack(L, funcr);
base = func + 1;
}
else {
base = adjust_varargs(L, p, n);
func = restorestack(L, funcr); /* previous call can change stack */
}
ci = next_ci(L); /* now 'enter' new function */
ci->nresults = nresults;
ci->func = func;
ci->u.l.base = base;
ci->top = base + p->maxstacksize;
lua_assert(ci->top <= L->stack_last);
ci->u.l.savedpc = p->code; /* starting point */
ci->callstatus = CIST_LUA;
L->top = ci->top;
if (L->hookmask & LUA_MASKCALL)
callhook(L, ci);
return 0;
} }
default: { /* not a function */ default: {
luaD_checkstack(L, 1); /* ensure space for metamethod */ int i;
func = restorestack(L, funcr); /* previous call may change stack */ if (wanted <= nres) { /* enough results? */
tryfuncTM(L, func); /* try to get '__call' metamethod */ for (i = 0; i < wanted; i++) /* move wanted results to correct place */
return luaD_precall(L, func, nresults); /* now it must be a function */ setobjs2s(L, res + i, firstResult + i);
}
else { /* not enough results; use all of them plus nils */
for (i = 0; i < nres; i++) /* move all results to correct place */
setobjs2s(L, res + i, firstResult + i);
for (; i < wanted; i++) /* complete wanted number of results */
setnilvalue(res + i);
}
break;
} }
} }
L->top = res + wanted; /* top points after the last result */
return 1;
} }
int luaD_poscall (lua_State *L, StkId firstResult, int nres) { /*
** Finishes a function call: calls hook if necessary, removes CallInfo,
** moves current number of results to proper place; returns 0 iff call
** wanted multiple (variable number of) results.
*/
int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
StkId res; StkId res;
int wanted, i; int wanted = ci->nresults;
CallInfo *ci = L->ci;
if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
if (L->hookmask & LUA_MASKRET) { if (L->hookmask & LUA_MASKRET) {
ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */
@ -392,15 +385,104 @@ int luaD_poscall (lua_State *L, StkId firstResult, int nres) {
L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */
} }
res = ci->func; /* res == final position of 1st result */ res = ci->func; /* res == final position of 1st result */
wanted = ci->nresults;
L->ci = ci->previous; /* back to caller */ L->ci = ci->previous; /* back to caller */
/* move results to correct place */ /* move results to proper place */
for (i = wanted; i != 0 && nres-- > 0; i--) return moveresults(L, firstResult, res, nres, wanted);
setobjs2s(L, res++, firstResult++); }
while (i-- > 0)
setnilvalue(res++);
L->top = res;
return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
/* macro to check stack size, preserving 'p' */
#define checkstackp(L,n,p) \
luaD_checkstackaux(L, n, \
ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
luaC_checkGC(L), /* stack grow uses memory */ \
p = restorestack(L, t__)) /* 'pos' part: restore 'p' */
/*
** Prepares a function call: checks the stack, creates a new CallInfo
** entry, fills in the relevant information, calls hook if needed.
** If function is a C function, does the call, too. (Otherwise, leave
** the execution ('luaV_execute') to the caller, to allow stackless
** calls.) Returns true iff function has been executed (C function).
*/
int luaD_precall (lua_State *L, StkId func, int nresults) {
lua_CFunction f;
CallInfo *ci;
switch (ttype(func)) {
case LUA_TCCL: /* C closure */
f = clCvalue(func)->f;
goto Cfunc;
case LUA_TLCF: /* light C function */
f = fvalue(func);
Cfunc: {
int n; /* number of returns */
checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
ci = next_ci(L); /* now 'enter' new function */
ci->nresults = nresults;
ci->func = func;
ci->top = L->top + LUA_MINSTACK;
lua_assert(ci->top <= L->stack_last);
ci->callstatus = 0;
if (L->hookmask & LUA_MASKCALL)
luaD_hook(L, LUA_HOOKCALL, -1);
lua_unlock(L);
n = (*f)(L); /* do the actual call */
lua_lock(L);
api_checknelems(L, n);
luaD_poscall(L, ci, L->top - n, n);
return 1;
}
case LUA_TLCL: { /* Lua function: prepare its call */
StkId base;
Proto *p = clLvalue(func)->p;
int n = cast_int(L->top - func) - 1; /* number of real arguments */
int fsize = p->maxstacksize; /* frame size */
checkstackp(L, fsize, func);
if (p->is_vararg)
base = adjust_varargs(L, p, n);
else { /* non vararg function */
for (; n < p->numparams; n++)
setnilvalue(L->top++); /* complete missing arguments */
base = func + 1;
}
ci = next_ci(L); /* now 'enter' new function */
ci->nresults = nresults;
ci->func = func;
ci->u.l.base = base;
L->top = ci->top = base + fsize;
lua_assert(ci->top <= L->stack_last);
ci->u.l.savedpc = p->code; /* starting point */
ci->callstatus = CIST_LUA;
if (L->hookmask & LUA_MASKCALL)
callhook(L, ci);
return 0;
}
default: { /* not a function */
checkstackp(L, 1, func); /* ensure space for metamethod */
tryfuncTM(L, func); /* try to get '__call' metamethod */
return luaD_precall(L, func, nresults); /* now it must be a function */
}
}
}
/*
** Check appropriate error for stack overflow ("regular" overflow or
** overflow while handling stack overflow). If 'nCalls' is larger than
** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but
** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to
** allow overflow handling to work)
*/
static void stackerror (lua_State *L) {
if (L->nCcalls == LUAI_MAXCCALLS)
luaG_runerror(L, "C stack overflow");
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
} }
@ -410,21 +492,25 @@ int luaD_poscall (lua_State *L, StkId firstResult, int nres) {
** When returns, all the results are on the stack, starting at the original ** When returns, all the results are on the stack, starting at the original
** function position. ** function position.
*/ */
void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { void luaD_call (lua_State *L, StkId func, int nResults) {
if (++L->nCcalls >= LUAI_MAXCCALLS) { if (++L->nCcalls >= LUAI_MAXCCALLS)
if (L->nCcalls == LUAI_MAXCCALLS) stackerror(L);
luaG_runerror(L, "C stack overflow");
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
}
if (!allowyield) L->nny++;
if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
luaV_execute(L); /* call it */ luaV_execute(L); /* call it */
if (!allowyield) L->nny--;
L->nCcalls--; L->nCcalls--;
} }
/*
** Similar to 'luaD_call', but does not allow yields during the call
*/
void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
L->nny++;
luaD_call(L, func, nResults);
L->nny--;
}
/* /*
** Completes the execution of an interrupted C function, calling its ** Completes the execution of an interrupted C function, calling its
** continuation function. ** continuation function.
@ -437,19 +523,17 @@ static void finishCcall (lua_State *L, int status) {
/* error status can only happen in a protected call */ /* error status can only happen in a protected call */
lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD);
if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */
ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ ci->callstatus &= ~CIST_YPCALL; /* continuation is also inside it */
L->errfunc = ci->u.c.old_errfunc; L->errfunc = ci->u.c.old_errfunc; /* with the same error function */
} }
/* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already
handled */ handled */
adjustresults(L, ci->nresults); adjustresults(L, ci->nresults);
/* call continuation function */
lua_unlock(L); lua_unlock(L);
n = (*ci->u.c.k)(L, status, ci->u.c.ctx); n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */
lua_lock(L); lua_lock(L);
api_checknelems(L, n); api_checknelems(L, n);
/* finish 'luaD_precall' */ luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */
luaD_poscall(L, L->top - n, n);
} }
@ -512,15 +596,16 @@ static int recover (lua_State *L, int status) {
/* /*
** signal an error in the call to 'resume', not in the execution of the ** Signal an error in the call to 'lua_resume', not in the execution
** coroutine itself. (Such errors should not be handled by any coroutine ** of the coroutine itself. (Such errors should not be handled by any
** error handler and should not kill the coroutine.) ** coroutine error handler and should not kill the coroutine.)
*/ */
static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { static int resume_error (lua_State *L, const char *msg, int narg) {
L->top = firstArg; /* remove args from the stack */ L->top -= narg; /* remove args from the stack */
setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */
api_incr_top(L); api_incr_top(L);
luaD_throw(L, -1); /* jump back to 'lua_resume' */ lua_unlock(L);
return LUA_ERRRUN;
} }
@ -532,22 +617,15 @@ static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
** coroutine. ** coroutine.
*/ */
static void resume (lua_State *L, void *ud) { static void resume (lua_State *L, void *ud) {
int nCcalls = L->nCcalls;
int n = *(cast(int*, ud)); /* number of arguments */ int n = *(cast(int*, ud)); /* number of arguments */
StkId firstArg = L->top - n; /* first argument */ StkId firstArg = L->top - n; /* first argument */
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
if (nCcalls >= LUAI_MAXCCALLS) if (L->status == LUA_OK) { /* starting a coroutine? */
resume_error(L, "C stack overflow", firstArg);
if (L->status == LUA_OK) { /* may be starting a coroutine */
if (ci != &L->base_ci) /* not in base level? */
resume_error(L, "cannot resume non-suspended coroutine", firstArg);
/* coroutine is in base level; start running it */
if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */
luaV_execute(L); /* call it */ luaV_execute(L); /* call it */
} }
else if (L->status != LUA_YIELD)
resume_error(L, "cannot resume dead coroutine", firstArg);
else { /* resuming from previous yield */ else { /* resuming from previous yield */
lua_assert(L->status == LUA_YIELD);
L->status = LUA_OK; /* mark that it is running (again) */ L->status = LUA_OK; /* mark that it is running (again) */
ci->func = restorestack(L, ci->extra); ci->func = restorestack(L, ci->extra);
if (isLua(ci)) /* yielded inside a hook? */ if (isLua(ci)) /* yielded inside a hook? */
@ -560,20 +638,27 @@ static void resume (lua_State *L, void *ud) {
api_checknelems(L, n); api_checknelems(L, n);
firstArg = L->top - n; /* yield results come from continuation */ firstArg = L->top - n; /* yield results come from continuation */
} }
luaD_poscall(L, firstArg, n); /* finish 'luaD_precall' */ luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */
} }
unroll(L, NULL); /* run continuation */ unroll(L, NULL); /* run continuation */
} }
lua_assert(nCcalls == L->nCcalls);
} }
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
int status; int status;
int oldnny = L->nny; /* save "number of non-yieldable" calls */ unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */
lua_lock(L); lua_lock(L);
luai_userstateresume(L, nargs); if (L->status == LUA_OK) { /* may be starting a coroutine */
if (L->ci != &L->base_ci) /* not in base level? */
return resume_error(L, "cannot resume non-suspended coroutine", nargs);
}
else if (L->status != LUA_YIELD)
return resume_error(L, "cannot resume dead coroutine", nargs);
L->nCcalls = (from) ? from->nCcalls + 1 : 1; L->nCcalls = (from) ? from->nCcalls + 1 : 1;
if (L->nCcalls >= LUAI_MAXCCALLS)
return resume_error(L, "C stack overflow", nargs);
luai_userstateresume(L, nargs);
L->nny = 0; /* allow yields */ L->nny = 0; /* allow yields */
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
status = luaD_rawrunprotected(L, resume, &nargs); status = luaD_rawrunprotected(L, resume, &nargs);
@ -684,7 +769,7 @@ static void f_parser (lua_State *L, void *ud) {
int c = zgetc(p->z); /* read first character */ int c = zgetc(p->z); /* read first character */
if (c == LUA_SIGNATURE[0]) { if (c == LUA_SIGNATURE[0]) {
checkmode(L, p->mode, "binary"); checkmode(L, p->mode, "binary");
cl = luaU_undump(L, p->z, &p->buff, p->name); cl = luaU_undump(L, p->z, p->name);
} }
else { else {
checkmode(L, p->mode, "text"); checkmode(L, p->mode, "text");

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 2.22 2015/05/22 17:48:19 roberto Exp $ ** $Id: ldo.h,v 2.29.1.1 2017/04/19 17:20:42 roberto Exp $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,11 +13,21 @@
#include "lzio.h" #include "lzio.h"
#define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ /*
luaD_growstack(L, n); else condmovestack(L); ** Macro to check stack size and grow stack if needed. Parameters
** 'pre'/'pos' allow the macro to preserve a pointer into the
** stack across reallocations, doing the work only when needed.
** 'condmovestack' is used in heavy tests to force a stack reallocation
** at every check.
*/
#define luaD_checkstackaux(L,n,pre,pos) \
if (L->stack_last - L->top <= (n)) \
{ pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); }
/* In general, 'pre'/'pos' are empty (nothing to save) */
#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0)
#define incr_top(L) {L->top++; luaD_checkstack(L,0);}
#define savestack(L,p) ((char *)(p) - (char *)L->stack) #define savestack(L,p) ((char *)(p) - (char *)L->stack)
#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) #define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
@ -30,14 +40,16 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
const char *mode); 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);
int allowyield); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
ptrdiff_t oldtop, ptrdiff_t ef); ptrdiff_t oldtop, ptrdiff_t ef);
LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult, int nres); LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult,
int nres);
LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
LUAI_FUNC void luaD_growstack (lua_State *L, int n); LUAI_FUNC void luaD_growstack (lua_State *L, int n);
LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_shrinkstack (lua_State *L);
LUAI_FUNC void luaD_inctop (lua_State *L);
LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldump.c,v 2.36 2015/03/30 15:43:51 roberto Exp $ ** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $
** save precompiled Lua chunks ** save precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -38,7 +38,7 @@ typedef struct {
static void DumpBlock (const void *b, size_t size, DumpState *D) { static void DumpBlock (const void *b, size_t size, DumpState *D) {
if (D->status == 0) { if (D->status == 0 && size > 0) {
lua_unlock(D->L); lua_unlock(D->L);
D->status = (*D->writer)(D->L, b, size, D->data); D->status = (*D->writer)(D->L, b, size, D->data);
lua_lock(D->L); lua_lock(D->L);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ ** $Id: lfunc.c,v 2.45.1.1 2017/04/19 17:39:34 roberto Exp $
** Auxiliary functions to manipulate prototypes and closures ** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp $ ** $Id: lfunc.h,v 2.15.1.1 2017/04/19 17:39:34 roberto Exp $
** Auxiliary functions to manipulate prototypes and closures ** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 2.205 2015/03/25 13:42:19 roberto Exp $ ** $Id: lgc.c,v 2.215.1.2 2017/08/31 16:15:27 roberto Exp $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -114,8 +114,13 @@ static void reallymarkobject (global_State *g, GCObject *o);
/* /*
** if key is not marked, mark its entry as dead (therefore removing it ** If key is not marked, mark its entry as dead. This allows key to be
** from the table) ** collected, but keeps its entry in the table. A dead node is needed
** when Lua looks up for a key (it may be part of a chain) and when
** traversing a weak table (key might be removed from the table during
** traversal). Other places never manipulate dead keys, because its
** associated nil value is enough to signal that the entry is logically
** empty.
*/ */
static void removeentry (Node *n) { static void removeentry (Node *n) {
lua_assert(ttisnil(gval(n))); lua_assert(ttisnil(gval(n)));
@ -462,7 +467,7 @@ static lu_mem traversetable (global_State *g, Table *h) {
else /* not weak */ else /* not weak */
traversestrongtable(g, h); traversestrongtable(g, h);
return sizeof(Table) + sizeof(TValue) * h->sizearray + return sizeof(Table) + sizeof(TValue) * h->sizearray +
sizeof(Node) * cast(size_t, sizenode(h)); sizeof(Node) * cast(size_t, allocsizenode(h));
} }
@ -542,7 +547,8 @@ static lu_mem traversethread (global_State *g, lua_State *th) {
} }
else if (g->gckind != KGC_EMERGENCY) else if (g->gckind != KGC_EMERGENCY)
luaD_shrinkstack(th); /* do not change stack in emergency cycle */ luaD_shrinkstack(th); /* do not change stack in emergency cycle */
return (sizeof(lua_State) + sizeof(TValue) * th->stacksize); return (sizeof(lua_State) + sizeof(TValue) * th->stacksize +
sizeof(CallInfo) * th->nci);
} }
@ -637,8 +643,9 @@ static void clearkeys (global_State *g, GCObject *l, GCObject *f) {
for (n = gnode(h, 0); n < limit; n++) { for (n = gnode(h, 0); n < limit; n++) {
if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) { if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) {
setnilvalue(gval(n)); /* remove value ... */ setnilvalue(gval(n)); /* remove value ... */
removeentry(n); /* and remove entry from table */
} }
if (ttisnil(gval(n))) /* is entry empty? */
removeentry(n); /* remove entry from table */
} }
} }
} }
@ -748,14 +755,11 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
/* /*
** sweep a list until a live object (or end of list) ** sweep a list until a live object (or end of list)
*/ */
static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) { static GCObject **sweeptolive (lua_State *L, GCObject **p) {
GCObject **old = p; GCObject **old = p;
int i = 0;
do { do {
i++;
p = sweeplist(L, p, 1); p = sweeplist(L, p, 1);
} while (p == old); } while (p == old);
if (n) *n += i;
return p; return p;
} }
@ -769,12 +773,11 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p, int *n) {
*/ */
/* /*
** If possible, free concatenation buffer and shrink string table ** If possible, shrink string table
*/ */
static void checkSizes (lua_State *L, global_State *g) { static void checkSizes (lua_State *L, global_State *g) {
if (g->gckind != KGC_EMERGENCY) { if (g->gckind != KGC_EMERGENCY) {
l_mem olddebt = g->GCdebt; l_mem olddebt = g->GCdebt;
luaZ_freebuffer(L, &g->buff); /* free concatenation buffer */
if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ if (g->strt.nuse < g->strt.size / 4) /* string table too big? */
luaS_resize(L, g->strt.size / 2); /* shrink it a little */ luaS_resize(L, g->strt.size / 2); /* shrink it a little */
g->GCestimate += g->GCdebt - olddebt; /* update estimate */ g->GCestimate += g->GCdebt - olddebt; /* update estimate */
@ -797,7 +800,7 @@ static GCObject *udata2finalize (global_State *g) {
static void dothecall (lua_State *L, void *ud) { static void dothecall (lua_State *L, void *ud) {
UNUSED(ud); UNUSED(ud);
luaD_call(L, L->top - 2, 0, 0); luaD_callnoyield(L, L->top - 2, 0);
} }
@ -816,7 +819,9 @@ static void GCTM (lua_State *L, int propagateerrors) {
setobj2s(L, L->top, tm); /* push finalizer... */ setobj2s(L, L->top, tm); /* push finalizer... */
setobj2s(L, L->top + 1, &v); /* ... and its argument */ setobj2s(L, L->top + 1, &v); /* ... and its argument */
L->top += 2; /* and (next line) call the finalizer */ L->top += 2; /* and (next line) call the finalizer */
L->ci->callstatus |= CIST_FIN; /* will run a finalizer */
status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0);
L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */
L->allowhook = oldah; /* restore hooks */ L->allowhook = oldah; /* restore hooks */
g->gcrunning = running; /* restore state */ g->gcrunning = running; /* restore state */
if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ if (status != LUA_OK && propagateerrors) { /* error while running __gc? */
@ -851,10 +856,10 @@ static int runafewfinalizers (lua_State *L) {
/* /*
** call all pending finalizers ** call all pending finalizers
*/ */
static void callallpendingfinalizers (lua_State *L, int propagateerrors) { static void callallpendingfinalizers (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
while (g->tobefnz) while (g->tobefnz)
GCTM(L, propagateerrors); GCTM(L, 0);
} }
@ -904,7 +909,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
if (issweepphase(g)) { if (issweepphase(g)) {
makewhite(g, o); /* "sweep" object 'o' */ makewhite(g, o); /* "sweep" object 'o' */
if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */
g->sweepgc = sweeptolive(L, g->sweepgc, NULL); /* change 'sweepgc' */ g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */
} }
/* search for pointer pointing to 'o' */ /* search for pointer pointing to 'o' */
for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ }
@ -946,19 +951,16 @@ static void setpause (global_State *g) {
/* /*
** Enter first sweep phase. ** Enter first sweep phase.
** The call to 'sweeptolive' makes pointer point to an object inside ** The call to 'sweeplist' tries to make pointer point to an object
** the list (instead of to the header), so that the real sweep do not ** inside the list (instead of to the header), so that the real sweep do
** need to skip objects created between "now" and the start of the real ** not need to skip objects created between "now" and the start of the
** sweep. ** real sweep.
** Returns how many objects it swept.
*/ */
static int entersweep (lua_State *L) { static void entersweep (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
int n = 0;
g->gcstate = GCSswpallgc; g->gcstate = GCSswpallgc;
lua_assert(g->sweepgc == NULL); lua_assert(g->sweepgc == NULL);
g->sweepgc = sweeptolive(L, &g->allgc, &n); g->sweepgc = sweeplist(L, &g->allgc, 1);
return n;
} }
@ -966,7 +968,7 @@ void luaC_freeallobjects (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
separatetobefnz(g, 1); /* separate all objects with finalizers */ separatetobefnz(g, 1); /* separate all objects with finalizers */
lua_assert(g->finobj == NULL); lua_assert(g->finobj == NULL);
callallpendingfinalizers(L, 0); callallpendingfinalizers(L);
lua_assert(g->tobefnz == NULL); lua_assert(g->tobefnz == NULL);
g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */ g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */
g->gckind = KGC_NORMAL; g->gckind = KGC_NORMAL;
@ -1059,12 +1061,11 @@ static lu_mem singlestep (lua_State *L) {
} }
case GCSatomic: { case GCSatomic: {
lu_mem work; lu_mem work;
int sw;
propagateall(g); /* make sure gray list is empty */ propagateall(g); /* make sure gray list is empty */
work = atomic(L); /* work is what was traversed by 'atomic' */ work = atomic(L); /* work is what was traversed by 'atomic' */
sw = entersweep(L); entersweep(L);
g->GCestimate = gettotalbytes(g); /* first estimate */; g->GCestimate = gettotalbytes(g); /* first estimate */;
return work + sw * GCSWEEPCOST; return work;
} }
case GCSswpallgc: { /* sweep "regular" objects */ case GCSswpallgc: { /* sweep "regular" objects */
return sweepstep(L, g, GCSswpfinobj, &g->finobj); return sweepstep(L, g, GCSswpfinobj, &g->finobj);
@ -1114,9 +1115,12 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
static l_mem getdebt (global_State *g) { static l_mem getdebt (global_State *g) {
l_mem debt = g->GCdebt; l_mem debt = g->GCdebt;
int stepmul = g->gcstepmul; int stepmul = g->gcstepmul;
debt = (debt / STEPMULADJ) + 1; if (debt <= 0) return 0; /* minimal debt */
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; else {
return debt; debt = (debt / STEPMULADJ) + 1;
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
return debt;
}
} }
/* /*

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.h,v 2.86 2014/10/25 11:50:46 roberto Exp $ ** $Id: lgc.h,v 2.91.1.1 2017/04/19 17:39:34 roberto Exp $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -101,26 +101,35 @@
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
#define luaC_condGC(L,c) \ /*
{if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} ** Does one step of collection when debt becomes positive. 'pre'/'pos'
#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) ** allows some adjustments to be done only when needed. macro
** 'condchangemem' is used only for heavy tests (forcing a full
** GC cycle on every opportunity)
*/
#define luaC_condGC(L,pre,pos) \
{ if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
condchangemem(L,pre,pos); }
/* more often than not, 'pre'/'pos' are empty */
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
#define luaC_barrier(L,p,v) { \ #define luaC_barrier(L,p,v) ( \
if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
luaC_barrier_(L,obj2gco(p),gcvalue(v)); } luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
#define luaC_barrierback(L,p,v) { \ #define luaC_barrierback(L,p,v) ( \
if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
luaC_barrierback_(L,p); } luaC_barrierback_(L,p) : cast_void(0))
#define luaC_objbarrier(L,p,o) { \ #define luaC_objbarrier(L,p,o) ( \
if (isblack(p) && iswhite(o)) \ (isblack(p) && iswhite(o)) ? \
luaC_barrier_(L,obj2gco(p),obj2gco(o)); } luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
#define luaC_upvalbarrier(L,uv) \ #define luaC_upvalbarrier(L,uv) ( \
{ if (iscollectable((uv)->v) && !upisopen(uv)) \ (iscollectable((uv)->v) && !upisopen(uv)) ? \
luaC_upvalbarrier_(L,uv); } luaC_upvalbarrier_(L,uv) : cast_void(0))
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_freeallobjects (lua_State *L);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: linit.c,v 1.38 2015/01/05 13:48:33 roberto Exp $ ** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $
** Initialization of libraries for lua.c and other clients ** Initialization of libraries for lua.c and other clients
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -18,10 +18,10 @@
** open the library, which is already linked to the application. ** open the library, which is already linked to the application.
** For that, do the following code: ** For that, do the following code:
** **
** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
** lua_pushcfunction(L, luaopen_modname); ** lua_pushcfunction(L, luaopen_modname);
** lua_setfield(L, -2, modname); ** lua_setfield(L, -2, modname);
** lua_pop(L, 1); // remove _PRELOAD table ** lua_pop(L, 1); // remove PRELOAD table
*/ */
#include "lprefix.h" #include "lprefix.h"

View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 2.144 2015/04/03 18:41:57 roberto Exp $ ** $Id: liolib.c,v 2.151.1.1 2017/04/19 17:29:57 roberto Exp $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -23,18 +23,25 @@
#include "lualib.h" #include "lualib.h"
#if !defined(l_checkmode)
/* /*
** Check whether 'mode' matches '[rwa]%+?b?'.
** Change this macro to accept other modes for 'fopen' besides ** Change this macro to accept other modes for 'fopen' besides
** the standard ones. ** the standard ones.
*/ */
#define l_checkmode(mode) \ #if !defined(l_checkmode)
(*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
(*mode != '+' || ++mode) && /* skip if char is '+' */ \ /* accepted extensions to 'mode' in 'fopen' */
(*mode != 'b' || ++mode) && /* skip if char is 'b' */ \ #if !defined(L_MODEEXT)
(*mode == '\0')) #define L_MODEEXT "b"
#endif
/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
static int l_checkmode (const char *mode) {
return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL &&
(*mode != '+' || (++mode, 1)) && /* skip if char is '+' */
(strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */
}
#endif #endif
@ -176,7 +183,7 @@ static FILE *tofile (lua_State *L) {
/* /*
** When creating file handles, always creates a 'closed' file handle ** When creating file handles, always creates a 'closed' file handle
** before opening the actual file; so, if there is a memory error, the ** before opening the actual file; so, if there is a memory error, the
** file is not left opened. ** handle is in a consistent state.
*/ */
static LStream *newprefile (lua_State *L) { static LStream *newprefile (lua_State *L) {
LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
@ -199,11 +206,16 @@ static int aux_close (lua_State *L) {
} }
static int f_close (lua_State *L) {
tofile(L); /* make sure argument is an open stream */
return aux_close(L);
}
static int io_close (lua_State *L) { static int io_close (lua_State *L) {
if (lua_isnone(L, 1)) /* no argument? */ if (lua_isnone(L, 1)) /* no argument? */
lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
tofile(L); /* make sure argument is an open stream */ return f_close(L);
return aux_close(L);
} }
@ -265,6 +277,8 @@ static int io_popen (lua_State *L) {
const char *filename = luaL_checkstring(L, 1); const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r"); const char *mode = luaL_optstring(L, 2, "r");
LStream *p = newprefile(L); LStream *p = newprefile(L);
luaL_argcheck(L, ((mode[0] == 'r' || mode[0] == 'w') && mode[1] == '\0'),
2, "invalid mode");
p->f = l_popen(L, filename, mode); p->f = l_popen(L, filename, mode);
p->closef = &io_pclose; p->closef = &io_pclose;
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
@ -318,8 +332,15 @@ static int io_output (lua_State *L) {
static int io_readline (lua_State *L); static int io_readline (lua_State *L);
/*
** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit
** in the limit for upvalues of a closure)
*/
#define MAXARGLINE 250
static void aux_lines (lua_State *L, int toclose) { static void aux_lines (lua_State *L, int toclose) {
int n = lua_gettop(L) - 1; /* number of arguments to read */ int n = lua_gettop(L) - 1; /* number of arguments to read */
luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments");
lua_pushinteger(L, n); /* number of arguments to read */ lua_pushinteger(L, n); /* number of arguments to read */
lua_pushboolean(L, toclose); /* close/not close file when finished */ lua_pushboolean(L, toclose); /* close/not close file when finished */
lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
@ -362,14 +383,17 @@ static int io_lines (lua_State *L) {
/* maximum length of a numeral */ /* maximum length of a numeral */
#define MAXRN 200 #if !defined (L_MAXLENNUM)
#define L_MAXLENNUM 200
#endif
/* auxiliary structure used by 'read_number' */ /* auxiliary structure used by 'read_number' */
typedef struct { typedef struct {
FILE *f; /* file being read */ FILE *f; /* file being read */
int c; /* current character (look ahead) */ int c; /* current character (look ahead) */
int n; /* number of elements in buffer 'buff' */ int n; /* number of elements in buffer 'buff' */
char buff[MAXRN + 1]; /* +1 for ending '\0' */ char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */
} RN; } RN;
@ -377,7 +401,7 @@ typedef struct {
** Add current char to buffer (if not out of space) and read next one ** Add current char to buffer (if not out of space) and read next one
*/ */
static int nextc (RN *rn) { static int nextc (RN *rn) {
if (rn->n >= MAXRN) { /* buffer overflow? */ if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */
rn->buff[0] = '\0'; /* invalidate result */ rn->buff[0] = '\0'; /* invalidate result */
return 0; /* fail */ return 0; /* fail */
} }
@ -390,10 +414,10 @@ static int nextc (RN *rn) {
/* /*
** Accept current char if it is in 'set' (of size 1 or 2) ** Accept current char if it is in 'set' (of size 2)
*/ */
static int test2 (RN *rn, const char *set) { static int test2 (RN *rn, const char *set) {
if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0')) if (rn->c == set[0] || rn->c == set[1])
return nextc(rn); return nextc(rn);
else return 0; else return 0;
} }
@ -422,11 +446,11 @@ static int read_number (lua_State *L, FILE *f) {
char decp[2]; char decp[2];
rn.f = f; rn.n = 0; rn.f = f; rn.n = 0;
decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
decp[1] = '\0'; decp[1] = '.'; /* always accept a dot */
l_lockfile(rn.f); l_lockfile(rn.f);
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
test2(&rn, "-+"); /* optional signal */ test2(&rn, "-+"); /* optional signal */
if (test2(&rn, "0")) { if (test2(&rn, "00")) {
if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
else count = 1; /* count initial '0' as a valid digit */ else count = 1; /* count initial '0' as a valid digit */
} }
@ -462,7 +486,7 @@ static int read_line (lua_State *L, FILE *f, int chop) {
int c = '\0'; int c = '\0';
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
while (c != EOF && c != '\n') { /* repeat until end of line */ while (c != EOF && c != '\n') { /* repeat until end of line */
char *buff = luaL_prepbuffer(&b); /* pre-allocate buffer */ char *buff = luaL_prepbuffer(&b); /* preallocate buffer */
int i = 0; int i = 0;
l_lockfile(f); /* no memory errors can happen inside the lock */ l_lockfile(f); /* no memory errors can happen inside the lock */
while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
@ -483,7 +507,7 @@ static void read_all (lua_State *L, FILE *f) {
luaL_Buffer b; luaL_Buffer b;
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
do { /* read file in chunks of LUAL_BUFFERSIZE bytes */ do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
char *p = luaL_prepbuffsize(&b, LUAL_BUFFERSIZE); char *p = luaL_prepbuffer(&b);
nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f); nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
luaL_addsize(&b, nr); luaL_addsize(&b, nr);
} while (nr == LUAL_BUFFERSIZE); } while (nr == LUAL_BUFFERSIZE);
@ -602,8 +626,10 @@ static int g_write (lua_State *L, FILE *f, int arg) {
if (lua_type(L, arg) == LUA_TNUMBER) { if (lua_type(L, arg) == LUA_TNUMBER) {
/* optimization: could be done exactly as for strings */ /* optimization: could be done exactly as for strings */
int len = lua_isinteger(L, arg) int len = lua_isinteger(L, arg)
? fprintf(f, LUA_INTEGER_FMT, lua_tointeger(L, arg)) ? fprintf(f, LUA_INTEGER_FMT,
: fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)); (LUAI_UACINT)lua_tointeger(L, arg))
: fprintf(f, LUA_NUMBER_FMT,
(LUAI_UACNUMBER)lua_tonumber(L, arg));
status = status && (len > 0); status = status && (len > 0);
} }
else { else {
@ -693,7 +719,7 @@ static const luaL_Reg iolib[] = {
** methods for file handles ** methods for file handles
*/ */
static const luaL_Reg flib[] = { static const luaL_Reg flib[] = {
{"close", io_close}, {"close", f_close},
{"flush", f_flush}, {"flush", f_flush},
{"lines", f_lines}, {"lines", f_lines},
{"read", f_read}, {"read", f_read},

View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 2.93 2015/05/22 17:45:56 roberto Exp $ ** $Id: llex.c,v 2.96.1.1 2017/04/19 17:20:42 roberto Exp $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -162,7 +162,6 @@ static void inclinenumber (LexState *ls) {
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
int firstchar) { int firstchar) {
ls->t.token = 0; ls->t.token = 0;
ls->decpoint = '.';
ls->L = L; ls->L = L;
ls->current = firstchar; ls->current = firstchar;
ls->lookahead.token = TK_EOS; /* no look-ahead token */ ls->lookahead.token = TK_EOS; /* no look-ahead token */
@ -207,37 +206,6 @@ static int check_next2 (LexState *ls, const char *set) {
} }
/*
** change all characters 'from' in buffer to 'to'
*/
static void buffreplace (LexState *ls, char from, char to) {
if (from != to) {
size_t n = luaZ_bufflen(ls->buff);
char *p = luaZ_buffer(ls->buff);
while (n--)
if (p[n] == from) p[n] = to;
}
}
#define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
/*
** in case of format error, try to change decimal point separator to
** the one defined in the current locale and check again
*/
static void trydecpoint (LexState *ls, TValue *o) {
char old = ls->decpoint;
ls->decpoint = lua_getlocaledecpoint();
buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
if (!buff2num(ls->buff, o)) {
/* format error with correct decimal point: no more options */
buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
lexerror(ls, "malformed number", TK_FLT);
}
}
/* LUA_NUMBER */ /* LUA_NUMBER */
/* /*
** this function is quite liberal in what it accepts, as 'luaO_str2num' ** this function is quite liberal in what it accepts, as 'luaO_str2num'
@ -261,9 +229,8 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
else break; else break;
} }
save(ls, '\0'); save(ls, '\0');
buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */
if (!buff2num(ls->buff, &obj)) /* format error? */ lexerror(ls, "malformed number", TK_FLT);
trydecpoint(ls, &obj); /* try to update decimal point separator */
if (ttisinteger(&obj)) { if (ttisinteger(&obj)) {
seminfo->i = ivalue(&obj); seminfo->i = ivalue(&obj);
return TK_INT; return TK_INT;
@ -277,12 +244,12 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
/* /*
** skip a sequence '[=*[' or ']=*]'; if sequence is wellformed, return ** reads a sequence '[=*[' or ']=*]', leaving the last bracket.
** its number of '='s; otherwise, return a negative number (-1 iff there ** If sequence is well formed, return its number of '='s + 2; otherwise,
** are no '='s after initial bracket) ** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...').
*/ */
static int skip_sep (LexState *ls) { static size_t skip_sep (LexState *ls) {
int count = 0; size_t count = 0;
int s = ls->current; int s = ls->current;
lua_assert(s == '[' || s == ']'); lua_assert(s == '[' || s == ']');
save_and_next(ls); save_and_next(ls);
@ -290,11 +257,14 @@ static int skip_sep (LexState *ls) {
save_and_next(ls); save_and_next(ls);
count++; count++;
} }
return (ls->current == s) ? count : (-count) - 1; return (ls->current == s) ? count + 2
: (count == 0) ? 1
: 0;
} }
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
int line = ls->linenumber; /* initial line (for error message) */ int line = ls->linenumber; /* initial line (for error message) */
save_and_next(ls); /* skip 2nd '[' */ save_and_next(ls); /* skip 2nd '[' */
if (currIsNewline(ls)) /* string starts with a newline? */ if (currIsNewline(ls)) /* string starts with a newline? */
@ -328,8 +298,8 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
} }
} endloop: } endloop:
if (seminfo) if (seminfo)
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep,
luaZ_bufflen(ls->buff) - 2*(2 + sep)); luaZ_bufflen(ls->buff) - 2 * sep);
} }
@ -477,9 +447,9 @@ static int llex (LexState *ls, SemInfo *seminfo) {
/* else is a comment */ /* else is a comment */
next(ls); next(ls);
if (ls->current == '[') { /* long comment? */ if (ls->current == '[') { /* long comment? */
int sep = skip_sep(ls); size_t sep = skip_sep(ls);
luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */
if (sep >= 0) { if (sep >= 2) {
read_long_string(ls, NULL, sep); /* skip long comment */ read_long_string(ls, NULL, sep); /* skip long comment */
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
break; break;
@ -491,12 +461,12 @@ static int llex (LexState *ls, SemInfo *seminfo) {
break; break;
} }
case '[': { /* long string or simply '[' */ case '[': { /* long string or simply '[' */
int sep = skip_sep(ls); size_t sep = skip_sep(ls);
if (sep >= 0) { if (sep >= 2) {
read_long_string(ls, seminfo, sep); read_long_string(ls, seminfo, sep);
return TK_STRING; return TK_STRING;
} }
else if (sep != -1) /* '[=...' missing second bracket */ else if (sep == 0) /* '[=...' missing second bracket */
lexerror(ls, "invalid long string delimiter", TK_STRING); lexerror(ls, "invalid long string delimiter", TK_STRING);
return '['; return '[';
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.h,v 1.78 2014/10/29 15:38:24 roberto Exp $ ** $Id: llex.h,v 1.79.1.1 2017/04/19 17:20:42 roberto Exp $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -69,7 +69,6 @@ typedef struct LexState {
struct Dyndata *dyd; /* dynamic structures used by the parser */ struct Dyndata *dyd; /* dynamic structures used by the parser */
TString *source; /* current source name */ TString *source; /* current source name */
TString *envn; /* environment variable name */ TString *envn; /* environment variable name */
char decpoint; /* locale decimal point */
} LexState; } LexState;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: llimits.h,v 1.135 2015/06/09 14:21:00 roberto Exp $ ** $Id: llimits.h,v 1.141.1.1 2017/04/19 17:20:42 roberto Exp $
** Limits, basic types, and some other 'installation-dependent' definitions ** Limits, basic types, and some other 'installation-dependent' definitions
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -64,7 +64,13 @@ typedef unsigned char lu_byte;
#if defined(LUAI_USER_ALIGNMENT_T) #if defined(LUAI_USER_ALIGNMENT_T)
typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
#else #else
typedef union { double u; void *s; lua_Integer i; long l; } L_Umaxalign; typedef union {
lua_Number n;
double u;
void *s;
lua_Integer i;
long l;
} L_Umaxalign;
#endif #endif
@ -78,7 +84,7 @@ typedef LUAI_UACINT l_uacInt;
#if defined(lua_assert) #if defined(lua_assert)
#define check_exp(c,e) (lua_assert(c), (e)) #define check_exp(c,e) (lua_assert(c), (e))
/* to avoid problems with conditions too long */ /* to avoid problems with conditions too long */
#define lua_longassert(c) { if (!(c)) lua_assert(0); } #define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
#else #else
#define lua_assert(c) ((void)0) #define lua_assert(c) ((void)0)
#define check_exp(c,e) (e) #define check_exp(c,e) (e)
@ -184,10 +190,13 @@ typedef unsigned long Instruction;
/* /*
** Size of cache for strings in the API (better be a prime) ** Size of cache for strings in the API. 'N' is the number of
** sets (better be a prime) and "M" is the size of each set (M == 1
** makes a direct cache.)
*/ */
#if !defined(STRCACHE_SIZE) #if !defined(STRCACHE_N)
#define STRCACHE_SIZE 127 #define STRCACHE_N 53
#define STRCACHE_M 2
#endif #endif
@ -198,7 +207,7 @@ typedef unsigned long Instruction;
/* /*
** macros that are executed whenether program enters the Lua core ** macros that are executed whenever program enters the Lua core
** ('lua_lock') and leaves the core ('lua_unlock') ** ('lua_lock') and leaves the core ('lua_unlock')
*/ */
#if !defined(lua_lock) #if !defined(lua_lock)
@ -297,17 +306,18 @@ typedef unsigned long Instruction;
** macro to control inclusion of some hard tests on stack reallocation ** macro to control inclusion of some hard tests on stack reallocation
*/ */
#if !defined(HARDSTACKTESTS) #if !defined(HARDSTACKTESTS)
#define condmovestack(L) ((void)0) #define condmovestack(L,pre,pos) ((void)0)
#else #else
/* realloc stack keeping its size */ /* realloc stack keeping its size */
#define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) #define condmovestack(L,pre,pos) \
{ int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; }
#endif #endif
#if !defined(HARDMEMTESTS) #if !defined(HARDMEMTESTS)
#define condchangemem(L) condmovestack(L) #define condchangemem(L,pre,pos) ((void)0)
#else #else
#define condchangemem(L) \ #define condchangemem(L,pre,pos) \
((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
#endif #endif
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lmathlib.c,v 1.115 2015/03/12 14:04:04 roberto Exp $ ** $Id: lmathlib.c,v 1.119.1.1 2017/04/19 17:20:42 roberto Exp $
** Standard mathematical library ** Standard mathematical library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -39,7 +39,7 @@
static int math_abs (lua_State *L) { static int math_abs (lua_State *L) {
if (lua_isinteger(L, 1)) { if (lua_isinteger(L, 1)) {
lua_Integer n = lua_tointeger(L, 1); lua_Integer n = lua_tointeger(L, 1);
if (n < 0) n = (lua_Integer)(0u - n); if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
lua_pushinteger(L, n); lua_pushinteger(L, n);
} }
else else
@ -184,10 +184,13 @@ static int math_log (lua_State *L) {
else { else {
lua_Number base = luaL_checknumber(L, 2); lua_Number base = luaL_checknumber(L, 2);
#if !defined(LUA_USE_C89) #if !defined(LUA_USE_C89)
if (base == 2.0) res = l_mathop(log2)(x); else if (base == l_mathop(2.0))
res = l_mathop(log2)(x); else
#endif #endif
if (base == 10.0) res = l_mathop(log10)(x); if (base == l_mathop(10.0))
else res = l_mathop(log)(x)/l_mathop(log)(base); res = l_mathop(log10)(x);
else
res = l_mathop(log)(x)/l_mathop(log)(base);
} }
lua_pushnumber(L, res); lua_pushnumber(L, res);
return 1; return 1;
@ -273,7 +276,7 @@ static int math_random (lua_State *L) {
static int math_randomseed (lua_State *L) { static int math_randomseed (lua_State *L) {
l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1)); l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
(void)rand(); /* discard first value to avoid undesirable correlations */ (void)l_rand(); /* discard first value to avoid undesirable correlations */
return 0; return 0;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $ ** $Id: lmem.c,v 1.91.1.1 2017/04/19 17:20:42 roberto Exp $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $ ** $Id: lmem.h,v 1.43.1.1 2017/04/19 17:20:42 roberto Exp $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: loadlib.c,v 1.126 2015/02/16 13:14:33 roberto Exp $ ** $Id: loadlib.c,v 1.130.1.1 2017/04/19 17:20:42 roberto Exp $
** Dynamic library loader for Lua ** Dynamic library loader for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
** **
@ -25,40 +25,9 @@
/* /*
** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment
** variables that Lua check to set its paths.
*/
#if !defined(LUA_PATH_VAR)
#define LUA_PATH_VAR "LUA_PATH"
#endif
#if !defined(LUA_CPATH_VAR)
#define LUA_CPATH_VAR "LUA_CPATH"
#endif
#define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
#define LUA_PATHVARVERSION LUA_PATH_VAR LUA_PATHSUFFIX
#define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_PATHSUFFIX
/*
** LUA_PATH_SEP is the character that separates templates in a path.
** LUA_PATH_MARK is the string that marks the substitution points in a
** template.
** LUA_EXEC_DIR in a Windows path is replaced by the executable's
** directory.
** LUA_IGMARK is a mark to ignore all before it when building the ** LUA_IGMARK is a mark to ignore all before it when building the
** luaopen_ function name. ** luaopen_ function name.
*/ */
#if !defined (LUA_PATH_SEP)
#define LUA_PATH_SEP ";"
#endif
#if !defined (LUA_PATH_MARK)
#define LUA_PATH_MARK "?"
#endif
#if !defined (LUA_EXEC_DIR)
#define LUA_EXEC_DIR "!"
#endif
#if !defined (LUA_IGMARK) #if !defined (LUA_IGMARK)
#define LUA_IGMARK "-" #define LUA_IGMARK "-"
#endif #endif
@ -94,7 +63,8 @@ static const int CLIBS = 0;
#define LIB_FAIL "open" #define LIB_FAIL "open"
#define setprogdir(L) ((void)0)
#define setprogdir(L) ((void)0)
/* /*
@ -179,7 +149,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
#include <windows.h> #include <windows.h>
#undef setprogdir
/* /*
** optional flags for LoadLibraryEx ** optional flags for LoadLibraryEx
@ -189,21 +158,30 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
#endif #endif
#undef setprogdir
/*
** Replace in the path (on the top of the stack) any occurrence
** of LUA_EXEC_DIR with the executable's path.
*/
static void setprogdir (lua_State *L) { static void setprogdir (lua_State *L) {
char buff[MAX_PATH + 1]; char buff[MAX_PATH + 1];
char *lb; char *lb;
DWORD nsize = sizeof(buff)/sizeof(char); DWORD nsize = sizeof(buff)/sizeof(char);
DWORD n = GetModuleFileNameA(NULL, buff, nsize); DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */
if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
luaL_error(L, "unable to get ModuleFileName"); luaL_error(L, "unable to get ModuleFileName");
else { else {
*lb = '\0'; *lb = '\0'; /* cut name on the last '\\' to get the path */
luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
lua_remove(L, -2); /* remove original string */ lua_remove(L, -2); /* remove original string */
} }
} }
static void pusherror (lua_State *L) { static void pusherror (lua_State *L) {
int error = GetLastError(); int error = GetLastError();
char buffer[128]; char buffer[128];
@ -272,6 +250,67 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
#endif /* } */ #endif /* } */
/*
** {==================================================================
** Set Paths
** ===================================================================
*/
/*
** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment
** variables that Lua check to set its paths.
*/
#if !defined(LUA_PATH_VAR)
#define LUA_PATH_VAR "LUA_PATH"
#endif
#if !defined(LUA_CPATH_VAR)
#define LUA_CPATH_VAR "LUA_CPATH"
#endif
#define AUXMARK "\1" /* auxiliary mark */
/*
** return registry.LUA_NOENV as a boolean
*/
static int noenv (lua_State *L) {
int b;
lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
b = lua_toboolean(L, -1);
lua_pop(L, 1); /* remove value */
return b;
}
/*
** Set a path
*/
static void setpath (lua_State *L, const char *fieldname,
const char *envname,
const char *dft) {
const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
const char *path = getenv(nver); /* use versioned name */
if (path == NULL) /* no environment variable? */
path = getenv(envname); /* try unversioned name */
if (path == NULL || noenv(L)) /* no environment variable? */
lua_pushstring(L, dft); /* use default */
else {
/* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
LUA_PATH_SEP AUXMARK LUA_PATH_SEP);
luaL_gsub(L, path, AUXMARK, dft);
lua_remove(L, -2); /* remove result from 1st 'gsub' */
}
setprogdir(L);
lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
lua_pop(L, 1); /* pop versioned variable name */
}
/* }================================================================== */
/* /*
** return registry.CLIBS[path] ** return registry.CLIBS[path]
*/ */
@ -520,7 +559,7 @@ static int searcher_Croot (lua_State *L) {
static int searcher_preload (lua_State *L) { static int searcher_preload (lua_State *L) {
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */ if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */
lua_pushfstring(L, "\n\tno field package.preload['%s']", name); lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
return 1; return 1;
@ -557,9 +596,9 @@ static void findloader (lua_State *L, const char *name) {
static int ll_require (lua_State *L) { static int ll_require (lua_State *L) {
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
lua_settop(L, 1); /* _LOADED table will be at index 2 */ lua_settop(L, 1); /* LOADED table will be at index 2 */
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_getfield(L, 2, name); /* _LOADED[name] */ lua_getfield(L, 2, name); /* LOADED[name] */
if (lua_toboolean(L, -1)) /* is it there? */ if (lua_toboolean(L, -1)) /* is it there? */
return 1; /* package is already loaded */ return 1; /* package is already loaded */
/* else must load package */ /* else must load package */
@ -569,11 +608,11 @@ static int ll_require (lua_State *L) {
lua_insert(L, -2); /* name is 1st argument (before search data) */ lua_insert(L, -2); /* name is 1st argument (before search data) */
lua_call(L, 2, 1); /* run loader to load module */ lua_call(L, 2, 1); /* run loader to load module */
if (!lua_isnil(L, -1)) /* non-nil return? */ if (!lua_isnil(L, -1)) /* non-nil return? */
lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ lua_setfield(L, 2, name); /* LOADED[name] = returned value */
if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
lua_pushboolean(L, 1); /* use true as result */ lua_pushboolean(L, 1); /* use true as result */
lua_pushvalue(L, -1); /* extra copy to be returned */ lua_pushvalue(L, -1); /* extra copy to be returned */
lua_setfield(L, 2, name); /* _LOADED[name] = true */ lua_setfield(L, 2, name); /* LOADED[name] = true */
} }
return 1; return 1;
} }
@ -666,41 +705,6 @@ static int ll_seeall (lua_State *L) {
/* auxiliary mark (for internal use) */
#define AUXMARK "\1"
/*
** return registry.LUA_NOENV as a boolean
*/
static int noenv (lua_State *L) {
int b;
lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
b = lua_toboolean(L, -1);
lua_pop(L, 1); /* remove value */
return b;
}
static void setpath (lua_State *L, const char *fieldname, const char *envname1,
const char *envname2, const char *def) {
const char *path = getenv(envname1);
if (path == NULL) /* no environment variable? */
path = getenv(envname2); /* try alternative name */
if (path == NULL || noenv(L)) /* no environment variable? */
lua_pushstring(L, def); /* use default */
else {
/* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
LUA_PATH_SEP AUXMARK LUA_PATH_SEP);
luaL_gsub(L, path, AUXMARK, def);
lua_remove(L, -2);
}
setprogdir(L);
lua_setfield(L, -2, fieldname);
}
static const luaL_Reg pk_funcs[] = { static const luaL_Reg pk_funcs[] = {
{"loadlib", ll_loadlib}, {"loadlib", ll_loadlib},
{"searchpath", ll_searchpath}, {"searchpath", ll_searchpath},
@ -732,7 +736,7 @@ static void createsearcherstable (lua_State *L) {
int i; int i;
/* create 'searchers' table */ /* create 'searchers' table */
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
/* fill it with pre-defined searchers */ /* fill it with predefined searchers */
for (i=0; searchers[i] != NULL; i++) { for (i=0; searchers[i] != NULL; i++) {
lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
lua_pushcclosure(L, searchers[i], 1); lua_pushcclosure(L, searchers[i], 1);
@ -764,19 +768,18 @@ LUAMOD_API int luaopen_package (lua_State *L) {
createclibstable(L); createclibstable(L);
luaL_newlib(L, pk_funcs); /* create 'package' table */ luaL_newlib(L, pk_funcs); /* create 'package' table */
createsearcherstable(L); createsearcherstable(L);
/* set field 'path' */ /* set paths */
setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT); setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT);
/* set field 'cpath' */ setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT);
setpath(L, "cpath", LUA_CPATHVARVERSION, LUA_CPATH_VAR, LUA_CPATH_DEFAULT);
/* store config information */ /* store config information */
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
lua_setfield(L, -2, "config"); lua_setfield(L, -2, "config");
/* set field 'loaded' */ /* set field 'loaded' */
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_setfield(L, -2, "loaded"); lua_setfield(L, -2, "loaded");
/* set field 'preload' */ /* set field 'preload' */
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
lua_setfield(L, -2, "preload"); lua_setfield(L, -2, "preload");
lua_pushglobaltable(L); lua_pushglobaltable(L);
lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.104 2015/04/11 18:30:08 roberto Exp $ ** $Id: lobject.c,v 2.113.1.1 2017/04/19 17:29:57 roberto Exp $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -55,9 +55,7 @@ int luaO_int2fb (unsigned int x) {
/* converts back */ /* converts back */
int luaO_fb2int (int x) { int luaO_fb2int (int x) {
int e = (x >> 3) & 0x1f; return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1);
if (e == 0) return x;
else return ((x & 7) + 8) << (e - 1);
} }
@ -245,20 +243,59 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
/* }====================================================== */ /* }====================================================== */
static const char *l_str2d (const char *s, lua_Number *result) { /* maximum length of a numeral */
#if !defined (L_MAXLENNUM)
#define L_MAXLENNUM 200
#endif
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
char *endptr; char *endptr;
if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
return NULL; : lua_str2number(s, &endptr);
else if (strpbrk(s, "xX")) /* hex? */ if (endptr == s) return NULL; /* nothing recognized? */
*result = lua_strx2number(s, &endptr); while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
else return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */
*result = lua_str2number(s, &endptr);
if (endptr == s) return NULL; /* nothing recognized */
while (lisspace(cast_uchar(*endptr))) endptr++;
return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */
} }
/*
** Convert string 's' to a Lua number (put in 'result'). Return NULL
** on fail or the address of the ending '\0' on success.
** 'pmode' points to (and 'mode' contains) special things in the string:
** - 'x'/'X' means an hexadecimal numeral
** - 'n'/'N' means 'inf' or 'nan' (which should be rejected)
** - '.' just optimizes the search for the common case (nothing special)
** This function accepts both the current locale or a dot as the radix
** mark. If the conversion fails, it may mean number has a dot but
** locale accepts something else. In that case, the code copies 's'
** to a buffer (because 's' is read-only), changes the dot to the
** current locale radix mark, and tries to convert again.
*/
static const char *l_str2d (const char *s, lua_Number *result) {
const char *endptr;
const char *pmode = strpbrk(s, ".xXnN");
int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
if (mode == 'n') /* reject 'inf' and 'nan' */
return NULL;
endptr = l_str2dloc(s, result, mode); /* try to convert */
if (endptr == NULL) { /* failed? may be a different locale */
char buff[L_MAXLENNUM + 1];
const char *pdot = strchr(s, '.');
if (strlen(s) > L_MAXLENNUM || pdot == NULL)
return NULL; /* string too long or no dot; fail */
strcpy(buff, s); /* copy string to buffer */
buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
endptr = l_str2dloc(buff, result, mode); /* try again */
if (endptr != NULL)
endptr = s + (endptr - buff); /* make relative to 's' */
}
return endptr;
}
#define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
#define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
static const char *l_str2int (const char *s, lua_Integer *result) { static const char *l_str2int (const char *s, lua_Integer *result) {
lua_Unsigned a = 0; lua_Unsigned a = 0;
int empty = 1; int empty = 1;
@ -275,7 +312,10 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
} }
else { /* decimal */ else { /* decimal */
for (; lisdigit(cast_uchar(*s)); s++) { for (; lisdigit(cast_uchar(*s)); s++) {
a = a * 10 + *s - '0'; int d = *s - '0';
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
return NULL; /* do not accept it (as integer) */
a = a * 10 + d;
empty = 0; empty = 0;
} }
} }
@ -333,9 +373,9 @@ void luaO_tostring (lua_State *L, StkId obj) {
size_t len; size_t len;
lua_assert(ttisnumber(obj)); lua_assert(ttisnumber(obj));
if (ttisinteger(obj)) if (ttisinteger(obj))
len = lua_integer2str(buff, ivalue(obj)); len = lua_integer2str(buff, sizeof(buff), ivalue(obj));
else { else {
len = lua_number2str(buff, fltvalue(obj)); len = lua_number2str(buff, sizeof(buff), fltvalue(obj));
#if !defined(LUA_COMPAT_FLOATSTRING) #if !defined(LUA_COMPAT_FLOATSTRING)
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
buff[len++] = lua_getlocaledecpoint(); buff[len++] = lua_getlocaledecpoint();
@ -348,27 +388,29 @@ void luaO_tostring (lua_State *L, StkId obj) {
static void pushstr (lua_State *L, const char *str, size_t l) { static void pushstr (lua_State *L, const char *str, size_t l) {
setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
luaD_inctop(L);
} }
/* this function handles only '%d', '%c', '%f', '%p', and '%s' /*
conventional formats, plus Lua-specific '%I' and '%U' */ ** this function handles only '%d', '%c', '%f', '%p', and '%s'
conventional formats, plus Lua-specific '%I' and '%U'
*/
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
int n = 0; int n = 0;
for (;;) { for (;;) {
const char *e = strchr(fmt, '%'); const char *e = strchr(fmt, '%');
if (e == NULL) break; if (e == NULL) break;
luaD_checkstack(L, 2); /* fmt + item */
pushstr(L, fmt, e - fmt); pushstr(L, fmt, e - fmt);
switch (*(e+1)) { switch (*(e+1)) {
case 's': { case 's': { /* zero-terminated string */
const char *s = va_arg(argp, char *); const char *s = va_arg(argp, char *);
if (s == NULL) s = "(null)"; if (s == NULL) s = "(null)";
pushstr(L, s, strlen(s)); pushstr(L, s, strlen(s));
break; break;
} }
case 'c': { case 'c': { /* an 'int' as a character */
char buff = cast(char, va_arg(argp, int)); char buff = cast(char, va_arg(argp, int));
if (lisprint(cast_uchar(buff))) if (lisprint(cast_uchar(buff)))
pushstr(L, &buff, 1); pushstr(L, &buff, 1);
@ -376,28 +418,29 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
luaO_pushfstring(L, "<\\%d>", cast_uchar(buff)); luaO_pushfstring(L, "<\\%d>", cast_uchar(buff));
break; break;
} }
case 'd': { case 'd': { /* an 'int' */
setivalue(L->top++, va_arg(argp, int)); setivalue(L->top, va_arg(argp, int));
goto top2str;
}
case 'I': { /* a 'lua_Integer' */
setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt)));
goto top2str;
}
case 'f': { /* a 'lua_Number' */
setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
top2str: /* convert the top element to a string */
luaD_inctop(L);
luaO_tostring(L, L->top - 1); luaO_tostring(L, L->top - 1);
break; break;
} }
case 'I': { case 'p': { /* a pointer */
setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
luaO_tostring(L, L->top - 1);
break;
}
case 'f': {
setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
luaO_tostring(L, L->top - 1);
break;
}
case 'p': {
char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
int l = sprintf(buff, "%p", va_arg(argp, void *)); void *p = va_arg(argp, void *);
int l = lua_pointer2str(buff, sizeof(buff), p);
pushstr(L, buff, l); pushstr(L, buff, l);
break; break;
} }
case 'U': { case 'U': { /* an 'int' as a UTF-8 sequence */
char buff[UTF8BUFFSZ]; char buff[UTF8BUFFSZ];
int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long))); int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long)));
pushstr(L, buff + UTF8BUFFSZ - l, l); pushstr(L, buff + UTF8BUFFSZ - l, l);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 2.111 2015/06/09 14:21:42 roberto Exp $ ** $Id: lobject.h,v 2.117.1.1 2017/04/19 17:39:34 roberto Exp $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -19,8 +19,8 @@
/* /*
** Extra tags for non-values ** Extra tags for non-values
*/ */
#define LUA_TPROTO LUA_NUMTAGS #define LUA_TPROTO LUA_NUMTAGS /* function prototypes */
#define LUA_TDEADKEY (LUA_NUMTAGS+1) #define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */
/* /*
** number of all possible tags (including LUA_TNONE but excluding DEADKEY) ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
@ -88,22 +88,32 @@ struct GCObject {
/*
** Union of all Lua values
*/
typedef union Value Value;
/* /*
** Tagged Values. This is the basic representation of values in Lua, ** Tagged Values. This is the basic representation of values in Lua,
** an actual value plus a tag with its type. ** an actual value plus a tag with its type.
*/ */
/*
** Union of all Lua values
*/
typedef union Value {
GCObject *gc; /* collectable objects */
void *p; /* light userdata */
int b; /* booleans */
lua_CFunction f; /* light C functions */
lua_Integer i; /* integer numbers */
lua_Number n; /* float numbers */
} Value;
#define TValuefields Value value_; int tt_ #define TValuefields Value value_; int tt_
typedef struct lua_TValue TValue;
typedef struct lua_TValue {
TValuefields;
} TValue;
/* macro defining a nil value */ /* macro defining a nil value */
@ -177,9 +187,9 @@ typedef struct lua_TValue TValue;
/* Macros for internal tests */ /* Macros for internal tests */
#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) #define righttt(obj) (ttype(obj) == gcvalue(obj)->tt)
#define checkliveness(g,obj) \ #define checkliveness(L,obj) \
lua_longassert(!iscollectable(obj) || \ lua_longassert(!iscollectable(obj) || \
(righttt(obj) && !isdead(g,gcvalue(obj)))) (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))
/* Macros to set values */ /* Macros to set values */
@ -215,32 +225,32 @@ typedef struct lua_TValue TValue;
#define setsvalue(L,obj,x) \ #define setsvalue(L,obj,x) \
{ TValue *io = (obj); TString *x_ = (x); \ { TValue *io = (obj); TString *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
checkliveness(G(L),io); } checkliveness(L,io); }
#define setuvalue(L,obj,x) \ #define setuvalue(L,obj,x) \
{ TValue *io = (obj); Udata *x_ = (x); \ { TValue *io = (obj); Udata *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \
checkliveness(G(L),io); } checkliveness(L,io); }
#define setthvalue(L,obj,x) \ #define setthvalue(L,obj,x) \
{ TValue *io = (obj); lua_State *x_ = (x); \ { TValue *io = (obj); lua_State *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \
checkliveness(G(L),io); } checkliveness(L,io); }
#define setclLvalue(L,obj,x) \ #define setclLvalue(L,obj,x) \
{ TValue *io = (obj); LClosure *x_ = (x); \ { TValue *io = (obj); LClosure *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \
checkliveness(G(L),io); } checkliveness(L,io); }
#define setclCvalue(L,obj,x) \ #define setclCvalue(L,obj,x) \
{ TValue *io = (obj); CClosure *x_ = (x); \ { TValue *io = (obj); CClosure *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \
checkliveness(G(L),io); } checkliveness(L,io); }
#define sethvalue(L,obj,x) \ #define sethvalue(L,obj,x) \
{ TValue *io = (obj); Table *x_ = (x); \ { TValue *io = (obj); Table *x_ = (x); \
val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \
checkliveness(G(L),io); } checkliveness(L,io); }
#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY) #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
@ -248,7 +258,7 @@ typedef struct lua_TValue TValue;
#define setobj(L,obj1,obj2) \ #define setobj(L,obj1,obj2) \
{ TValue *io1=(obj1); *io1 = *(obj2); \ { TValue *io1=(obj1); *io1 = *(obj2); \
(void)L; checkliveness(G(L),io1); } (void)L; checkliveness(L,io1); }
/* /*
@ -264,12 +274,13 @@ typedef struct lua_TValue TValue;
#define setptvalue2s setptvalue #define setptvalue2s setptvalue
/* from table to same table */ /* from table to same table */
#define setobjt2t setobj #define setobjt2t setobj
/* to table */
#define setobj2t setobj
/* to new object */ /* to new object */
#define setobj2n setobj #define setobj2n setobj
#define setsvalue2n setsvalue #define setsvalue2n setsvalue
/* to table (define it as an expression to be used in macros) */
#define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1)))
@ -280,21 +291,6 @@ typedef struct lua_TValue TValue;
*/ */
union Value {
GCObject *gc; /* collectable objects */
void *p; /* light userdata */
int b; /* booleans */
lua_CFunction f; /* light C functions */
lua_Integer i; /* integer numbers */
lua_Number n; /* float numbers */
};
struct lua_TValue {
TValuefields;
};
typedef TValue *StkId; /* index to stack elements */ typedef TValue *StkId; /* index to stack elements */
@ -329,9 +325,9 @@ typedef union UTString {
** Get the actual string (array of bytes) from a 'TString'. ** Get the actual string (array of bytes) from a 'TString'.
** (Access to 'extra' ensures that value is really a 'TString'.) ** (Access to 'extra' ensures that value is really a 'TString'.)
*/ */
#define getaddrstr(ts) (cast(char *, (ts)) + sizeof(UTString))
#define getstr(ts) \ #define getstr(ts) \
check_exp(sizeof((ts)->extra), cast(const char*, getaddrstr(ts))) check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString))
/* get the actual string (array of bytes) from a Lua value */ /* get the actual string (array of bytes) from a Lua value */
#define svalue(o) getstr(tsvalue(o)) #define svalue(o) getstr(tsvalue(o))
@ -375,13 +371,13 @@ typedef union UUdata {
#define setuservalue(L,u,o) \ #define setuservalue(L,u,o) \
{ const TValue *io=(o); Udata *iu = (u); \ { const TValue *io=(o); Udata *iu = (u); \
iu->user_ = io->value_; iu->ttuv_ = rttype(io); \ iu->user_ = io->value_; iu->ttuv_ = rttype(io); \
checkliveness(G(L),io); } checkliveness(L,io); }
#define getuservalue(L,u,o) \ #define getuservalue(L,u,o) \
{ TValue *io=(o); const Udata *iu = (u); \ { TValue *io=(o); const Udata *iu = (u); \
io->value_ = iu->user_; settt_(io, iu->ttuv_); \ io->value_ = iu->user_; settt_(io, iu->ttuv_); \
checkliveness(G(L),io); } checkliveness(L,io); }
/* /*
@ -419,8 +415,8 @@ typedef struct Proto {
int sizelineinfo; int sizelineinfo;
int sizep; /* size of 'p' */ int sizep; /* size of 'p' */
int sizelocvars; int sizelocvars;
int linedefined; int linedefined; /* debug information */
int lastlinedefined; int lastlinedefined; /* debug information */
TValue *k; /* constants used by the function */ TValue *k; /* constants used by the function */
Instruction *code; /* opcodes */ Instruction *code; /* opcodes */
struct Proto **p; /* functions defined inside the function */ struct Proto **p; /* functions defined inside the function */
@ -489,7 +485,7 @@ typedef union TKey {
#define setnodekey(L,key,obj) \ #define setnodekey(L,key,obj) \
{ TKey *k_=(key); const TValue *io_=(obj); \ { TKey *k_=(key); const TValue *io_=(obj); \
k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \ k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \
(void)L; checkliveness(G(L),io_); } (void)L; checkliveness(L,io_); }
typedef struct Node { typedef struct Node {

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp $ ** $Id: lopcodes.c,v 1.55.1.1 2017/04/19 17:20:42 roberto Exp $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.148 2014/10/25 11:50:46 roberto Exp $ ** $Id: lopcodes.h,v 1.149.1.1 2017/04/19 17:20:42 roberto Exp $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -139,7 +139,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
/* gets the index of the constant */ /* gets the index of the constant */
#define INDEXK(r) ((int)(r) & ~BITRK) #define INDEXK(r) ((int)(r) & ~BITRK)
#if !defined(MAXINDEXRK) /* (for debugging only) */
#define MAXINDEXRK (BITRK - 1) #define MAXINDEXRK (BITRK - 1)
#endif
/* code a constant index as a RK value */ /* code a constant index as a RK value */
#define RKASK(x) ((x) | BITRK) #define RKASK(x) ((x) | BITRK)

View File

@ -1,5 +1,5 @@
/* /*
** $Id: loslib.c,v 1.57 2015/04/10 17:41:04 roberto Exp $ ** $Id: loslib.c,v 1.65.1.1 2017/04/19 17:29:57 roberto Exp $
** Standard Operating System library ** Standard Operating System library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -24,18 +24,29 @@
/* /*
** {================================================================== ** {==================================================================
** list of valid conversion specifiers for the 'strftime' function ** List of valid conversion specifiers for the 'strftime' function;
** options are grouped by length; group of length 2 start with '||'.
** =================================================================== ** ===================================================================
*/ */
#if !defined(LUA_STRFTIMEOPTIONS) /* { */ #if !defined(LUA_STRFTIMEOPTIONS) /* { */
#if defined(LUA_USE_C89) /* options for ANSI C 89 (only 1-char options) */
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } #define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%"
/* options for ISO C 99 and POSIX */
#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
"||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */
/* options for Windows */
#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
#if defined(LUA_USE_WINDOWS)
#define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN
#elif defined(LUA_USE_C89)
#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89
#else /* C99 specification */ #else /* C99 specification */
#define LUA_STRFTIMEOPTIONS \ #define LUA_STRFTIMEOPTIONS L_STRFTIMEC99
{ "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
"E", "cCxXyY", \
"O", "deHImMSuUVwWy" }
#endif #endif
#endif /* } */ #endif /* } */
@ -54,7 +65,12 @@
*/ */
#define l_timet lua_Integer #define l_timet lua_Integer
#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
#define l_checktime(L,a) ((time_t)luaL_checkinteger(L,a))
static time_t l_checktime (lua_State *L, int arg) {
lua_Integer t = luaL_checkinteger(L, arg);
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
return (time_t)t;
}
#endif /* } */ #endif /* } */
@ -190,6 +206,23 @@ static void setboolfield (lua_State *L, const char *key, int value) {
lua_setfield(L, -2, key); lua_setfield(L, -2, key);
} }
/*
** Set all fields from structure 'tm' in the table on top of the stack
*/
static void setallfields (lua_State *L, struct tm *stm) {
setfield(L, "sec", stm->tm_sec);
setfield(L, "min", stm->tm_min);
setfield(L, "hour", stm->tm_hour);
setfield(L, "day", stm->tm_mday);
setfield(L, "month", stm->tm_mon + 1);
setfield(L, "year", stm->tm_year + 1900);
setfield(L, "wday", stm->tm_wday + 1);
setfield(L, "yday", stm->tm_yday + 1);
setboolfield(L, "isdst", stm->tm_isdst);
}
static int getboolfield (lua_State *L, const char *key) { static int getboolfield (lua_State *L, const char *key) {
int res; int res;
res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
@ -198,36 +231,43 @@ static int getboolfield (lua_State *L, const char *key) {
} }
static int getfield (lua_State *L, const char *key, int d) { /* maximum value for date fields (to avoid arithmetic overflows with 'int') */
int res, isnum; #if !defined(L_MAXDATEFIELD)
lua_getfield(L, -1, key); #define L_MAXDATEFIELD (INT_MAX / 2)
res = (int)lua_tointegerx(L, -1, &isnum); #endif
if (!isnum) {
if (d < 0) static int getfield (lua_State *L, const char *key, int d, int delta) {
int isnum;
int t = lua_getfield(L, -1, key); /* get field and its type */
lua_Integer res = lua_tointegerx(L, -1, &isnum);
if (!isnum) { /* field is not an integer? */
if (t != LUA_TNIL) /* some other value? */
return luaL_error(L, "field '%s' is not an integer", key);
else if (d < 0) /* absent field; no default? */
return luaL_error(L, "field '%s' missing in date table", key); return luaL_error(L, "field '%s' missing in date table", key);
res = d; res = d;
} }
else {
if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD))
return luaL_error(L, "field '%s' is out-of-bound", key);
res -= delta;
}
lua_pop(L, 1); lua_pop(L, 1);
return res; return (int)res;
} }
static const char *checkoption (lua_State *L, const char *conv, char *buff) { static const char *checkoption (lua_State *L, const char *conv,
static const char *const options[] = LUA_STRFTIMEOPTIONS; ptrdiff_t convlen, char *buff) {
unsigned int i; const char *option = LUA_STRFTIMEOPTIONS;
for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) { int oplen = 1; /* length of options being checked */
if (*conv != '\0' && strchr(options[i], *conv) != NULL) { for (; *option != '\0' && oplen <= convlen; option += oplen) {
buff[1] = *conv; if (*option == '|') /* next block? */
if (*options[i + 1] == '\0') { /* one-char conversion specifier? */ oplen++; /* will check options with next length (+1) */
buff[2] = '\0'; /* end buffer */ else if (memcmp(conv, option, oplen) == 0) { /* match? */
return conv + 1; memcpy(buff, conv, oplen); /* copy valid option to buffer */
} buff[oplen] = '\0';
else if (*(conv + 1) != '\0' && return conv + oplen; /* return next item */
strchr(options[i + 1], *(conv + 1)) != NULL) {
buff[2] = *(conv + 1); /* valid two-char conversion specifier */
buff[3] = '\0'; /* end buffer */
return conv + 2;
}
} }
} }
luaL_argerror(L, 1, luaL_argerror(L, 1,
@ -236,9 +276,15 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) {
} }
/* maximum size for an individual 'strftime' item */
#define SIZETIMEFMT 250
static int os_date (lua_State *L) { static int os_date (lua_State *L) {
const char *s = luaL_optstring(L, 1, "%c"); size_t slen;
const char *s = luaL_optlstring(L, 1, "%c", &slen);
time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
const char *se = s + slen; /* 's' end */
struct tm tmr, *stm; struct tm tmr, *stm;
if (*s == '!') { /* UTC? */ if (*s == '!') { /* UTC? */
stm = l_gmtime(&t, &tmr); stm = l_gmtime(&t, &tmr);
@ -247,33 +293,27 @@ static int os_date (lua_State *L) {
else else
stm = l_localtime(&t, &tmr); stm = l_localtime(&t, &tmr);
if (stm == NULL) /* invalid date? */ if (stm == NULL) /* invalid date? */
lua_pushnil(L); return luaL_error(L,
else if (strcmp(s, "*t") == 0) { "time result cannot be represented in this installation");
if (strcmp(s, "*t") == 0) {
lua_createtable(L, 0, 9); /* 9 = number of fields */ lua_createtable(L, 0, 9); /* 9 = number of fields */
setfield(L, "sec", stm->tm_sec); setallfields(L, stm);
setfield(L, "min", stm->tm_min);
setfield(L, "hour", stm->tm_hour);
setfield(L, "day", stm->tm_mday);
setfield(L, "month", stm->tm_mon+1);
setfield(L, "year", stm->tm_year+1900);
setfield(L, "wday", stm->tm_wday+1);
setfield(L, "yday", stm->tm_yday+1);
setboolfield(L, "isdst", stm->tm_isdst);
} }
else { else {
char cc[4]; char cc[4]; /* buffer for individual conversion specifiers */
luaL_Buffer b; luaL_Buffer b;
cc[0] = '%'; cc[0] = '%';
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
while (*s) { while (s < se) {
if (*s != '%') /* no conversion specifier? */ if (*s != '%') /* not a conversion specifier? */
luaL_addchar(&b, *s++); luaL_addchar(&b, *s++);
else { else {
size_t reslen; size_t reslen;
char buff[200]; /* should be big enough for any conversion result */ char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
s = checkoption(L, s + 1, cc); s++; /* skip '%' */
reslen = strftime(buff, sizeof(buff), cc, stm); s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
luaL_addlstring(&b, buff, reslen); reslen = strftime(buff, SIZETIMEFMT, cc, stm);
luaL_addsize(&b, reslen);
} }
} }
luaL_pushresult(&b); luaL_pushresult(&b);
@ -290,21 +330,20 @@ static int os_time (lua_State *L) {
struct tm ts; struct tm ts;
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 1); /* make sure table is at the top */ lua_settop(L, 1); /* make sure table is at the top */
ts.tm_sec = getfield(L, "sec", 0); ts.tm_sec = getfield(L, "sec", 0, 0);
ts.tm_min = getfield(L, "min", 0); ts.tm_min = getfield(L, "min", 0, 0);
ts.tm_hour = getfield(L, "hour", 12); ts.tm_hour = getfield(L, "hour", 12, 0);
ts.tm_mday = getfield(L, "day", -1); ts.tm_mday = getfield(L, "day", -1, 0);
ts.tm_mon = getfield(L, "month", -1) - 1; ts.tm_mon = getfield(L, "month", -1, 1);
ts.tm_year = getfield(L, "year", -1) - 1900; ts.tm_year = getfield(L, "year", -1, 1900);
ts.tm_isdst = getboolfield(L, "isdst"); ts.tm_isdst = getboolfield(L, "isdst");
t = mktime(&ts); t = mktime(&ts);
setallfields(L, &ts); /* update fields with normalized values */
} }
if (t != (time_t)(l_timet)t) if (t != (time_t)(l_timet)t || t == (time_t)(-1))
luaL_error(L, "time result cannot be represented in this Lua installation"); return luaL_error(L,
else if (t == (time_t)(-1)) "time result cannot be represented in this installation");
lua_pushnil(L); l_pushtime(L, t);
else
l_pushtime(L, t);
return 1; return 1;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.147 2014/12/27 20:31:43 roberto Exp $ ** $Id: lparser.c,v 2.155.1.2 2017/04/29 18:11:40 roberto Exp $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -164,7 +164,8 @@ static int registerlocalvar (LexState *ls, TString *varname) {
int oldsize = f->sizelocvars; int oldsize = f->sizelocvars;
luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
LocVar, SHRT_MAX, "local variables"); LocVar, SHRT_MAX, "local variables");
while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; while (oldsize < f->sizelocvars)
f->locvars[oldsize++].varname = NULL;
f->locvars[fs->nlocvars].varname = varname; f->locvars[fs->nlocvars].varname = varname;
luaC_objbarrier(ls->L, f, varname); luaC_objbarrier(ls->L, f, varname);
return fs->nlocvars++; return fs->nlocvars++;
@ -230,7 +231,8 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues");
luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues,
Upvaldesc, MAXUPVAL, "upvalues"); Upvaldesc, MAXUPVAL, "upvalues");
while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; while (oldsize < f->sizeupvalues)
f->upvalues[oldsize++].name = NULL;
f->upvalues[fs->nups].instack = (v->k == VLOCAL); f->upvalues[fs->nups].instack = (v->k == VLOCAL);
f->upvalues[fs->nups].idx = cast_byte(v->u.info); f->upvalues[fs->nups].idx = cast_byte(v->u.info);
f->upvalues[fs->nups].name = name; f->upvalues[fs->nups].name = name;
@ -255,7 +257,8 @@ static int searchvar (FuncState *fs, TString *n) {
*/ */
static void markupval (FuncState *fs, int level) { static void markupval (FuncState *fs, int level) {
BlockCnt *bl = fs->bl; BlockCnt *bl = fs->bl;
while (bl->nactvar > level) bl = bl->previous; while (bl->nactvar > level)
bl = bl->previous;
bl->upval = 1; bl->upval = 1;
} }
@ -264,27 +267,26 @@ static void markupval (FuncState *fs, int level) {
Find variable with given name 'n'. If it is an upvalue, add this Find variable with given name 'n'. If it is an upvalue, add this
upvalue into all intermediate functions. upvalue into all intermediate functions.
*/ */
static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
if (fs == NULL) /* no more levels? */ if (fs == NULL) /* no more levels? */
return VVOID; /* default is global */ init_exp(var, VVOID, 0); /* default is global */
else { else {
int v = searchvar(fs, n); /* look up locals at current level */ int v = searchvar(fs, n); /* look up locals at current level */
if (v >= 0) { /* found? */ if (v >= 0) { /* found? */
init_exp(var, VLOCAL, v); /* variable is local */ init_exp(var, VLOCAL, v); /* variable is local */
if (!base) if (!base)
markupval(fs, v); /* local will be used as an upval */ markupval(fs, v); /* local will be used as an upval */
return VLOCAL;
} }
else { /* not found as local at current level; try upvalues */ else { /* not found as local at current level; try upvalues */
int idx = searchupvalue(fs, n); /* try existing upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */
if (idx < 0) { /* not found? */ if (idx < 0) { /* not found? */
if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */ singlevaraux(fs->prev, n, var, 0); /* try upper levels */
return VVOID; /* not found; is a global */ if (var->k == VVOID) /* not found? */
return; /* it is a global */
/* else was LOCAL or UPVAL */ /* else was LOCAL or UPVAL */
idx = newupvalue(fs, n, var); /* will be a new upvalue */ idx = newupvalue(fs, n, var); /* will be a new upvalue */
} }
init_exp(var, VUPVAL, idx); init_exp(var, VUPVAL, idx); /* new or old upvalue */
return VUPVAL;
} }
} }
} }
@ -293,10 +295,11 @@ static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
static void singlevar (LexState *ls, expdesc *var) { static void singlevar (LexState *ls, expdesc *var) {
TString *varname = str_checkname(ls); TString *varname = str_checkname(ls);
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */ singlevaraux(fs, varname, var, 1);
if (var->k == VVOID) { /* global name? */
expdesc key; expdesc key;
singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ singlevaraux(fs, ls->envn, var, 1); /* get environment variable */
lua_assert(var->k == VLOCAL || var->k == VUPVAL); lua_assert(var->k != VVOID); /* this one must exist */
codestring(ls, &key, varname); /* key is variable name */ codestring(ls, &key, varname); /* key is variable name */
luaK_indexed(fs, var, &key); /* env[varname] */ luaK_indexed(fs, var, &key); /* env[varname] */
} }
@ -320,6 +323,8 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
luaK_nil(fs, reg, extra); luaK_nil(fs, reg, extra);
} }
} }
if (nexps > nvars)
ls->fs->freereg -= nexps - nvars; /* remove extra values */
} }
@ -499,7 +504,8 @@ static Proto *addprototype (LexState *ls) {
if (fs->np >= f->sizep) { if (fs->np >= f->sizep) {
int oldsize = f->sizep; int oldsize = f->sizep;
luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions"); luaM_growvector(L, f->p, fs->np, f->sizep, Proto *, MAXARG_Bx, "functions");
while (oldsize < f->sizep) f->p[oldsize++] = NULL; while (oldsize < f->sizep)
f->p[oldsize++] = NULL;
} }
f->p[fs->np++] = clp = luaF_newproto(L); f->p[fs->np++] = clp = luaF_newproto(L);
luaC_objbarrier(L, f, clp); luaC_objbarrier(L, f, clp);
@ -538,6 +544,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
fs->bl = NULL; fs->bl = NULL;
f = fs->f; f = fs->f;
f->source = ls->source; f->source = ls->source;
luaC_objbarrier(ls->L, f, f->source);
f->maxstacksize = 2; /* registers 0/1 are always valid */ f->maxstacksize = 2; /* registers 0/1 are always valid */
enterblock(fs, bl, 0); enterblock(fs, bl, 0);
} }
@ -760,7 +767,7 @@ static void parlist (LexState *ls) {
} }
case TK_DOTS: { /* param -> '...' */ case TK_DOTS: { /* param -> '...' */
luaX_next(ls); luaX_next(ls);
f->is_vararg = 1; f->is_vararg = 1; /* declared vararg */
break; break;
} }
default: luaX_syntaxerror(ls, "<name> or '...' expected"); default: luaX_syntaxerror(ls, "<name> or '...' expected");
@ -1155,11 +1162,8 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
int nexps; int nexps;
checknext(ls, '='); checknext(ls, '=');
nexps = explist(ls, &e); nexps = explist(ls, &e);
if (nexps != nvars) { if (nexps != nvars)
adjust_assign(ls, nvars, nexps, &e); adjust_assign(ls, nvars, nexps, &e);
if (nexps > nvars)
ls->fs->freereg -= nexps - nvars; /* remove extra values */
}
else { else {
luaK_setoneret(ls->fs, &e); /* close last expression */ luaK_setoneret(ls->fs, &e); /* close last expression */
luaK_storevar(ls->fs, &lh->v, &e); luaK_storevar(ls->fs, &lh->v, &e);
@ -1225,7 +1229,7 @@ static void labelstat (LexState *ls, TString *label, int line) {
checkrepeated(fs, ll, label); /* check for repeated labels */ checkrepeated(fs, ll, label); /* check for repeated labels */
checknext(ls, TK_DBCOLON); /* skip double colon */ checknext(ls, TK_DBCOLON); /* skip double colon */
/* create new entry for this label */ /* create new entry for this label */
l = newlabelentry(ls, ll, label, line, fs->pc); l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs));
skipnoopstat(ls); /* skip other no-op statements */ skipnoopstat(ls); /* skip other no-op statements */
if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */
/* assume that locals are already out of scope */ /* assume that locals are already out of scope */
@ -1389,7 +1393,7 @@ static void test_then_block (LexState *ls, int *escapelist) {
luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */
enterblock(fs, &bl, 0); /* must enter block before 'goto' */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */
gotostat(ls, v.t); /* handle goto/break */ gotostat(ls, v.t); /* handle goto/break */
skipnoopstat(ls); /* skip other no-op statements */ while (testnext(ls, ';')) {} /* skip colons */
if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ if (block_follow(ls, 0)) { /* 'goto' is the entire block? */
leaveblock(fs); leaveblock(fs);
return; /* and that is it */ return; /* and that is it */
@ -1493,7 +1497,7 @@ static void exprstat (LexState *ls) {
} }
else { /* stat -> func */ else { /* stat -> func */
check_condition(ls, v.v.k == VCALL, "syntax error"); check_condition(ls, v.v.k == VCALL, "syntax error");
SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ SETARG_C(getinstruction(fs, &v.v), 1); /* call statement uses no results */
} }
} }
@ -1510,8 +1514,8 @@ static void retstat (LexState *ls) {
if (hasmultret(e.k)) { if (hasmultret(e.k)) {
luaK_setmultret(fs, &e); luaK_setmultret(fs, &e);
if (e.k == VCALL && nret == 1) { /* tail call? */ if (e.k == VCALL && nret == 1) { /* tail call? */
SET_OPCODE(getcode(fs,&e), OP_TAILCALL); SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL);
lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar);
} }
first = fs->nactvar; first = fs->nactvar;
nret = LUA_MULTRET; /* return all values */ nret = LUA_MULTRET; /* return all values */
@ -1610,9 +1614,10 @@ static void mainfunc (LexState *ls, FuncState *fs) {
BlockCnt bl; BlockCnt bl;
expdesc v; expdesc v;
open_func(ls, fs, &bl); open_func(ls, fs, &bl);
fs->f->is_vararg = 1; /* main function is always vararg */ fs->f->is_vararg = 1; /* main function is always declared vararg */
init_exp(&v, VLOCAL, 0); /* create and... */ init_exp(&v, VLOCAL, 0); /* create and... */
newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */
luaC_objbarrier(ls->L, fs->f, ls->envn);
luaX_next(ls); /* read first token */ luaX_next(ls); /* read first token */
statlist(ls); /* parse main body */ statlist(ls); /* parse main body */
check(ls, TK_EOS); check(ls, TK_EOS);
@ -1626,11 +1631,12 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
FuncState funcstate; FuncState funcstate;
LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */
setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */ setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
incr_top(L); luaD_inctop(L);
lexstate.h = luaH_new(L); /* create table for scanner */ lexstate.h = luaH_new(L); /* create table for scanner */
sethvalue(L, L->top, lexstate.h); /* anchor it */ sethvalue(L, L->top, lexstate.h); /* anchor it */
incr_top(L); luaD_inctop(L);
funcstate.f = cl->p = luaF_newproto(L); funcstate.f = cl->p = luaF_newproto(L);
luaC_objbarrier(L, cl, cl->p);
funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ lua_assert(iswhite(funcstate.f)); /* do not need barrier here */
lexstate.buff = buff; lexstate.buff = buff;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.h,v 1.74 2014/10/25 11:50:46 roberto Exp $ ** $Id: lparser.h,v 1.76.1.1 2017/04/19 17:20:42 roberto Exp $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,25 +13,38 @@
/* /*
** Expression descriptor ** Expression and variable descriptor.
** Code generation for variables and expressions can be delayed to allow
** optimizations; An 'expdesc' structure describes a potentially-delayed
** variable/expression. It has a description of its "main" value plus a
** list of conditional jumps that can also produce its value (generated
** by short-circuit operators 'and'/'or').
*/ */
/* kinds of variables/expressions */
typedef enum { typedef enum {
VVOID, /* no value */ VVOID, /* when 'expdesc' describes the last expression a list,
VNIL, this kind means an empty list (so, no expression) */
VTRUE, VNIL, /* constant nil */
VFALSE, VTRUE, /* constant true */
VK, /* info = index of constant in 'k' */ VFALSE, /* constant false */
VKFLT, /* nval = numerical float value */ VK, /* constant in 'k'; info = index of constant in 'k' */
VKINT, /* nval = numerical integer value */ VKFLT, /* floating constant; nval = numerical float value */
VNONRELOC, /* info = result register */ VKINT, /* integer constant; nval = numerical integer value */
VLOCAL, /* info = local register */ VNONRELOC, /* expression has its value in a fixed register;
VUPVAL, /* info = index of upvalue in 'upvalues' */ info = result register */
VINDEXED, /* t = table register/upvalue; idx = index R/K */ VLOCAL, /* local variable; info = local register */
VJMP, /* info = instruction pc */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
VRELOCABLE, /* info = instruction pc */ VINDEXED, /* indexed variable;
VCALL, /* info = instruction pc */ ind.vt = whether 't' is register or upvalue;
VVARARG /* info = instruction pc */ ind.t = table register or upvalue;
ind.idx = key's R/K index */
VJMP, /* expression is a test/comparison;
info = pc of corresponding jump instruction */
VRELOCABLE, /* expression can put result in any register;
info = instruction pc */
VCALL, /* expression is a function call; info = instruction pc */
VVARARG /* vararg expression; info = instruction pc */
} expkind; } expkind;
@ -41,14 +54,14 @@ typedef enum {
typedef struct expdesc { typedef struct expdesc {
expkind k; expkind k;
union { union {
lua_Integer ival; /* for VKINT */
lua_Number nval; /* for VKFLT */
int info; /* for generic use */
struct { /* for indexed variables (VINDEXED) */ struct { /* for indexed variables (VINDEXED) */
short idx; /* index (R/K) */ short idx; /* index (R/K) */
lu_byte t; /* table (register or upvalue) */ lu_byte t; /* table (register or upvalue) */
lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */
} ind; } ind;
int info; /* for generic use */
lua_Number nval; /* for VKFLT */
lua_Integer ival; /* for VKINT */
} u; } u;
int t; /* patch list of 'exit when true' */ int t; /* patch list of 'exit when true' */
int f; /* patch list of 'exit when false' */ int f; /* patch list of 'exit when false' */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $ ** $Id: lprefix.h,v 1.2.1.1 2017/04/19 17:20:42 roberto Exp $
** Definitions for Lua code that must come before any other header file ** Definitions for Lua code that must come before any other header file
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 2.128 2015/03/04 13:31:21 roberto Exp $ ** $Id: lstate.c,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -76,7 +76,7 @@ typedef struct LG {
*/ */
#define addbuff(b,p,e) \ #define addbuff(b,p,e) \
{ size_t t = cast(size_t, e); \ { size_t t = cast(size_t, e); \
memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
static unsigned int makeseed (lua_State *L) { static unsigned int makeseed (lua_State *L) {
char buff[4 * sizeof(size_t)]; char buff[4 * sizeof(size_t)];
@ -93,10 +93,14 @@ static unsigned int makeseed (lua_State *L) {
/* /*
** set GCdebt to a new value keeping the value (totalbytes + GCdebt) ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
** invariant ** invariant (and avoiding underflows in 'totalbytes')
*/ */
void luaE_setdebt (global_State *g, l_mem debt) { void luaE_setdebt (global_State *g, l_mem debt) {
g->totalbytes -= (debt - g->GCdebt); l_mem tb = gettotalbytes(g);
lua_assert(tb > 0);
if (debt < tb - MAX_LMEM)
debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
g->totalbytes = tb - debt;
g->GCdebt = debt; g->GCdebt = debt;
} }
@ -107,6 +111,7 @@ CallInfo *luaE_extendCI (lua_State *L) {
L->ci->next = ci; L->ci->next = ci;
ci->previous = L->ci; ci->previous = L->ci;
ci->next = NULL; ci->next = NULL;
L->nci++;
return ci; return ci;
} }
@ -121,6 +126,7 @@ void luaE_freeCI (lua_State *L) {
while ((ci = next) != NULL) { while ((ci = next) != NULL) {
next = ci->next; next = ci->next;
luaM_free(L, ci); luaM_free(L, ci);
L->nci--;
} }
} }
@ -130,13 +136,14 @@ void luaE_freeCI (lua_State *L) {
*/ */
void luaE_shrinkCI (lua_State *L) { void luaE_shrinkCI (lua_State *L) {
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
while (ci->next != NULL) { /* while there is 'next' */ CallInfo *next2; /* next's next */
CallInfo *next2 = ci->next->next; /* next's next */ /* while there are two nexts */
if (next2 == NULL) break; while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
luaM_free(L, ci->next); /* remove next */ luaM_free(L, ci->next); /* free next */
L->nci--;
ci->next = next2; /* remove 'next' from the list */ ci->next = next2; /* remove 'next' from the list */
next2->previous = ci; next2->previous = ci;
ci = next2; ci = next2; /* keep next's next */
} }
} }
@ -166,6 +173,7 @@ static void freestack (lua_State *L) {
return; /* stack not completely built yet */ return; /* stack not completely built yet */
L->ci = &L->base_ci; /* free the entire 'ci' list */ L->ci = &L->base_ci; /* free the entire 'ci' list */
luaE_freeCI(L); luaE_freeCI(L);
lua_assert(L->nci == 0);
luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
} }
@ -214,6 +222,7 @@ static void preinit_thread (lua_State *L, global_State *g) {
G(L) = g; G(L) = g;
L->stack = NULL; L->stack = NULL;
L->ci = NULL; L->ci = NULL;
L->nci = 0;
L->stacksize = 0; L->stacksize = 0;
L->twups = L; /* thread has no upvalues */ L->twups = L; /* thread has no upvalues */
L->errorJmp = NULL; L->errorJmp = NULL;
@ -237,7 +246,6 @@ static void close_state (lua_State *L) {
if (g->version) /* closing a fully built state? */ if (g->version) /* closing a fully built state? */
luai_userstateclose(L); luai_userstateclose(L);
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
luaZ_freebuffer(L, &g->buff);
freestack(L); freestack(L);
lua_assert(gettotalbytes(g) == sizeof(LG)); lua_assert(gettotalbytes(g) == sizeof(LG));
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
@ -306,7 +314,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g->strt.size = g->strt.nuse = 0; g->strt.size = g->strt.nuse = 0;
g->strt.hash = NULL; g->strt.hash = NULL;
setnilvalue(&g->l_registry); setnilvalue(&g->l_registry);
luaZ_initbuffer(L, &g->buff);
g->panic = NULL; g->panic = NULL;
g->version = NULL; g->version = NULL;
g->gcstate = GCSpause; g->gcstate = GCSpause;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 2.122 2015/06/01 16:34:37 roberto Exp $ ** $Id: lstate.h,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -26,6 +26,24 @@
** 'tobefnz': all objects ready to be finalized; ** 'tobefnz': all objects ready to be finalized;
** 'fixedgc': all objects that are not to be collected (currently ** 'fixedgc': all objects that are not to be collected (currently
** only small strings, such as reserved words). ** only small strings, such as reserved words).
**
** Moreover, there is another set of lists that control gray objects.
** These lists are linked by fields 'gclist'. (All objects that
** can become gray have such a field. The field is not the same
** in all objects, but it always has this name.) Any gray object
** must belong to one of these lists, and all objects in these lists
** must be gray:
**
** 'gray': regular gray objects, still waiting to be visited.
** 'grayagain': objects that must be revisited at the atomic phase.
** That includes
** - black objects got in a write barrier;
** - all kinds of weak tables during propagation phase;
** - all threads.
** 'weak': tables with weak values to be cleared;
** 'ephemeron': ephemeron tables with white->white entries;
** 'allweak': tables with weak keys and/or weak values to be cleared.
** The last three lists are used only during the atomic phase.
*/ */
@ -33,6 +51,15 @@
struct lua_longjmp; /* defined in ldo.c */ struct lua_longjmp; /* defined in ldo.c */
/*
** Atomic type (relative to signals) to better ensure that 'lua_sethook'
** is thread safe
*/
#if !defined(l_signalT)
#include <signal.h>
#define l_signalT sig_atomic_t
#endif
/* extra stack space to handle TM calls and some other extras */ /* extra stack space to handle TM calls and some other extras */
#define EXTRA_STACK 5 #define EXTRA_STACK 5
@ -79,7 +106,7 @@ typedef struct CallInfo {
} u; } u;
ptrdiff_t extra; ptrdiff_t extra;
short nresults; /* expected number of results from this function */ short nresults; /* expected number of results from this function */
lu_byte callstatus; unsigned short callstatus;
} CallInfo; } CallInfo;
@ -89,12 +116,13 @@ typedef struct CallInfo {
#define CIST_OAH (1<<0) /* original value of 'allowhook' */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */
#define CIST_LUA (1<<1) /* call is running a Lua function */ #define CIST_LUA (1<<1) /* call is running a Lua function */
#define CIST_HOOKED (1<<2) /* call is running a debug hook */ #define CIST_HOOKED (1<<2) /* call is running a debug hook */
#define CIST_REENTRY (1<<3) /* call is running on same invocation of #define CIST_FRESH (1<<3) /* call is running on a fresh invocation
luaV_execute of previous call */ of luaV_execute */
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
#define CIST_TAIL (1<<5) /* call was tail called */ #define CIST_TAIL (1<<5) /* call was tail called */
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
#define CIST_LEQ (1<<7) /* using __lt for __le */ #define CIST_LEQ (1<<7) /* using __lt for __le */
#define CIST_FIN (1<<8) /* call is running a finalizer */
#define isLua(ci) ((ci)->callstatus & CIST_LUA) #define isLua(ci) ((ci)->callstatus & CIST_LUA)
@ -109,7 +137,7 @@ typedef struct CallInfo {
typedef struct global_State { typedef struct global_State {
lua_Alloc frealloc; /* function to reallocate memory */ lua_Alloc frealloc; /* function to reallocate memory */
void *ud; /* auxiliary data to 'frealloc' */ void *ud; /* auxiliary data to 'frealloc' */
lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
lu_mem GCmemtrav; /* memory traversed by the GC */ lu_mem GCmemtrav; /* memory traversed by the GC */
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
@ -131,7 +159,6 @@ typedef struct global_State {
GCObject *tobefnz; /* list of userdata to be GC */ GCObject *tobefnz; /* list of userdata to be GC */
GCObject *fixedgc; /* list of objects not to be collected */ GCObject *fixedgc; /* list of objects not to be collected */
struct lua_State *twups; /* list of threads with open upvalues */ struct lua_State *twups; /* list of threads with open upvalues */
Mbuffer buff; /* temporary buffer for string concatenation */
unsigned int gcfinnum; /* number of finalizers to call in each GC step */ unsigned int gcfinnum; /* number of finalizers to call in each GC step */
int gcpause; /* size of pause between successive GCs */ int gcpause; /* size of pause between successive GCs */
int gcstepmul; /* GC 'granularity' */ int gcstepmul; /* GC 'granularity' */
@ -141,7 +168,7 @@ typedef struct global_State {
TString *memerrmsg; /* memory-error message */ TString *memerrmsg; /* memory-error message */
TString *tmname[TM_N]; /* array with tag-method names */ TString *tmname[TM_N]; /* array with tag-method names */
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
TString *strcache[STRCACHE_SIZE][1]; /* cache for strings in API */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
} global_State; } global_State;
@ -150,6 +177,7 @@ typedef struct global_State {
*/ */
struct lua_State { struct lua_State {
CommonHeader; CommonHeader;
unsigned short nci; /* number of items in 'ci' list */
lu_byte status; lu_byte status;
StkId top; /* first free slot in the stack */ StkId top; /* first free slot in the stack */
global_State *l_G; global_State *l_G;
@ -162,14 +190,14 @@ struct lua_State {
struct lua_State *twups; /* list of threads with open upvalues */ struct lua_State *twups; /* list of threads with open upvalues */
struct lua_longjmp *errorJmp; /* current error recover point */ struct lua_longjmp *errorJmp; /* current error recover point */
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
lua_Hook hook; volatile lua_Hook hook;
ptrdiff_t errfunc; /* current error handling function (stack index) */ ptrdiff_t errfunc; /* current error handling function (stack index) */
int stacksize; int stacksize;
int basehookcount; int basehookcount;
int hookcount; int hookcount;
unsigned short nny; /* number of non-yieldable calls in stack */ unsigned short nny; /* number of non-yieldable calls in stack */
unsigned short nCcalls; /* number of nested C calls */ unsigned short nCcalls; /* number of nested C calls */
lu_byte hookmask; l_signalT hookmask;
lu_byte allowhook; lu_byte allowhook;
}; };
@ -212,7 +240,7 @@ union GCUnion {
/* actual number of total bytes allocated */ /* actual number of total bytes allocated */
#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) #define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.c,v 2.49 2015/06/01 16:34:37 roberto Exp $ ** $Id: lstring.c,v 2.56.1.1 2017/04/19 17:20:42 roberto Exp $
** String table (keeps all strings handled by Lua) ** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -48,14 +48,23 @@ int luaS_eqlngstr (TString *a, TString *b) {
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
unsigned int h = seed ^ cast(unsigned int, l); unsigned int h = seed ^ cast(unsigned int, l);
size_t l1;
size_t step = (l >> LUAI_HASHLIMIT) + 1; size_t step = (l >> LUAI_HASHLIMIT) + 1;
for (l1 = l; l1 >= step; l1 -= step) for (; l >= step; l -= step)
h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
return h; return h;
} }
unsigned int luaS_hashlongstr (TString *ts) {
lua_assert(ts->tt == LUA_TLNGSTR);
if (ts->extra == 0) { /* no hash? */
ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash);
ts->extra = 1; /* now it has its hash */
}
return ts->hash;
}
/* /*
** resizes the string table ** resizes the string table
*/ */
@ -92,11 +101,12 @@ void luaS_resize (lua_State *L, int newsize) {
** a non-collectable string.) ** a non-collectable string.)
*/ */
void luaS_clearcache (global_State *g) { void luaS_clearcache (global_State *g) {
int i; int i, j;
for (i = 0; i < STRCACHE_SIZE; i++) { for (i = 0; i < STRCACHE_N; i++)
if (iswhite(g->strcache[i][0])) /* will entry be collected? */ for (j = 0; j < STRCACHE_M; j++) {
g->strcache[i][0] = g->memerrmsg; /* replace it with something fixed */ if (iswhite(g->strcache[i][j])) /* will entry be collected? */
} g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
}
} }
@ -105,13 +115,14 @@ void luaS_clearcache (global_State *g) {
*/ */
void luaS_init (lua_State *L) { void luaS_init (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
int i; int i, j;
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
/* pre-create memory-error message */ /* pre-create memory-error message */
g->memerrmsg = luaS_newliteral(L, MEMERRMSG); g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
for (i = 0; i < STRCACHE_SIZE; i++) /* fill cache with valid strings */ for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
g->strcache[i][0] = g->memerrmsg; for (j = 0; j < STRCACHE_M; j++)
g->strcache[i][j] = g->memerrmsg;
} }
@ -119,8 +130,7 @@ void luaS_init (lua_State *L) {
/* /*
** creates a new string object ** creates a new string object
*/ */
static TString *createstrobj (lua_State *L, const char *str, size_t l, static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
int tag, unsigned int h) {
TString *ts; TString *ts;
GCObject *o; GCObject *o;
size_t totalsize; /* total size of TString object */ size_t totalsize; /* total size of TString object */
@ -129,8 +139,14 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l,
ts = gco2ts(o); ts = gco2ts(o);
ts->hash = h; ts->hash = h;
ts->extra = 0; ts->extra = 0;
memcpy(getaddrstr(ts), str, l * sizeof(char)); getstr(ts)[l] = '\0'; /* ending 0 */
getaddrstr(ts)[l] = '\0'; /* ending 0 */ return ts;
}
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
ts->u.lnglen = l;
return ts; return ts;
} }
@ -153,6 +169,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
global_State *g = G(L); global_State *g = G(L);
unsigned int h = luaS_hash(str, l, g->seed); unsigned int h = luaS_hash(str, l, g->seed);
TString **list = &g->strt.hash[lmod(h, g->strt.size)]; TString **list = &g->strt.hash[lmod(h, g->strt.size)];
lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
for (ts = *list; ts != NULL; ts = ts->u.hnext) { for (ts = *list; ts != NULL; ts = ts->u.hnext) {
if (l == ts->shrlen && if (l == ts->shrlen &&
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
@ -166,7 +183,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
luaS_resize(L, g->strt.size * 2); luaS_resize(L, g->strt.size * 2);
list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
} }
ts = createstrobj(L, str, l, LUA_TSHRSTR, h); ts = createstrobj(L, l, LUA_TSHRSTR, h);
memcpy(getstr(ts), str, l * sizeof(char));
ts->shrlen = cast_byte(l); ts->shrlen = cast_byte(l);
ts->u.hnext = *list; ts->u.hnext = *list;
*list = ts; *list = ts;
@ -183,10 +201,10 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
return internshrstr(L, str, l); return internshrstr(L, str, l);
else { else {
TString *ts; TString *ts;
if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
luaM_toobig(L); luaM_toobig(L);
ts = createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); ts = luaS_createlngstrobj(L, l);
ts->u.lnglen = l; memcpy(getstr(ts), str, l * sizeof(char));
return ts; return ts;
} }
} }
@ -199,15 +217,19 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
** check hits. ** check hits.
*/ */
TString *luaS_new (lua_State *L, const char *str) { TString *luaS_new (lua_State *L, const char *str) {
unsigned int i = point2uint(str) % STRCACHE_SIZE; /* hash */ unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
int j;
TString **p = G(L)->strcache[i]; TString **p = G(L)->strcache[i];
if (strcmp(str, getstr(p[0])) == 0) /* hit? */ for (j = 0; j < STRCACHE_M; j++) {
return p[0]; /* that it is */ if (strcmp(str, getstr(p[j])) == 0) /* hit? */
else { /* normal route */ return p[j]; /* that is it */
TString *s = luaS_newlstr(L, str, strlen(str));
p[0] = s;
return s;
} }
/* normal route */
for (j = STRCACHE_M - 1; j > 0; j--)
p[j] = p[j - 1]; /* move out last element */
/* new element is first in the list */
p[0] = luaS_newlstr(L, str, strlen(str));
return p[0];
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.h,v 1.59 2015/03/25 13:42:19 roberto Exp $ ** $Id: lstring.h,v 1.61.1.1 2017/04/19 17:20:42 roberto Exp $
** String table (keep all strings handled by Lua) ** String table (keep all strings handled by Lua)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -34,6 +34,7 @@
LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts);
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
LUAI_FUNC void luaS_resize (lua_State *L, int newsize); LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
LUAI_FUNC void luaS_clearcache (global_State *g); LUAI_FUNC void luaS_clearcache (global_State *g);
@ -42,6 +43,7 @@ LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s);
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstrlib.c,v 1.229 2015/05/20 17:39:23 roberto Exp $ ** $Id: lstrlib.c,v 1.254.1.1 2017/04/19 17:29:57 roberto Exp $
** Standard library for string operations and pattern-matching ** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,6 +13,7 @@
#include <ctype.h> #include <ctype.h>
#include <float.h> #include <float.h>
#include <limits.h> #include <limits.h>
#include <locale.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -26,7 +27,8 @@
/* /*
** maximum number of captures that a pattern can do during ** maximum number of captures that a pattern can do during
** pattern-matching. This limit is arbitrary. ** pattern-matching. This limit is arbitrary, but must fit in
** an unsigned char.
*/ */
#if !defined(LUA_MAXCAPTURES) #if !defined(LUA_MAXCAPTURES)
#define LUA_MAXCAPTURES 32 #define LUA_MAXCAPTURES 32
@ -41,8 +43,10 @@
** Some sizes are better limited to fit in 'int', but must also fit in ** Some sizes are better limited to fit in 'int', but must also fit in
** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
*/ */
#define MAX_SIZET ((size_t)(~(size_t)0))
#define MAXSIZE \ #define MAXSIZE \
(sizeof(size_t) < sizeof(int) ? (~(size_t)0) : (size_t)(INT_MAX)) (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
@ -208,12 +212,12 @@ static int str_dump (lua_State *L) {
typedef struct MatchState { typedef struct MatchState {
int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
const char *src_init; /* init of source string */ const char *src_init; /* init of source string */
const char *src_end; /* end ('\0') of source string */ const char *src_end; /* end ('\0') of source string */
const char *p_end; /* end ('\0') of pattern */ const char *p_end; /* end ('\0') of pattern */
lua_State *L; lua_State *L;
int level; /* total number of captures (finished or unfinished) */ int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
unsigned char level; /* total number of captures (finished or unfinished) */
struct { struct {
const char *init; const char *init;
ptrdiff_t len; ptrdiff_t len;
@ -584,6 +588,22 @@ static int nospecials (const char *p, size_t l) {
} }
static void prepstate (MatchState *ms, lua_State *L,
const char *s, size_t ls, const char *p, size_t lp) {
ms->L = L;
ms->matchdepth = MAXCCALLS;
ms->src_init = s;
ms->src_end = s + ls;
ms->p_end = p + lp;
}
static void reprepstate (MatchState *ms) {
ms->level = 0;
lua_assert(ms->matchdepth == MAXCCALLS);
}
static int str_find_aux (lua_State *L, int find) { static int str_find_aux (lua_State *L, int find) {
size_t ls, lp; size_t ls, lp;
const char *s = luaL_checklstring(L, 1, &ls); const char *s = luaL_checklstring(L, 1, &ls);
@ -611,15 +631,10 @@ static int str_find_aux (lua_State *L, int find) {
if (anchor) { if (anchor) {
p++; lp--; /* skip anchor character */ p++; lp--; /* skip anchor character */
} }
ms.L = L; prepstate(&ms, L, s, ls, p, lp);
ms.matchdepth = MAXCCALLS;
ms.src_init = s;
ms.src_end = s + ls;
ms.p_end = p + lp;
do { do {
const char *res; const char *res;
ms.level = 0; reprepstate(&ms);
lua_assert(ms.matchdepth == MAXCCALLS);
if ((res=match(&ms, s1, p)) != NULL) { if ((res=match(&ms, s1, p)) != NULL) {
if (find) { if (find) {
lua_pushinteger(L, (s1 - s) + 1); /* start */ lua_pushinteger(L, (s1 - s) + 1); /* start */
@ -646,29 +661,25 @@ static int str_match (lua_State *L) {
} }
/* state for 'gmatch' */
typedef struct GMatchState {
const char *src; /* current position */
const char *p; /* pattern */
const char *lastmatch; /* end of last match */
MatchState ms; /* match state */
} GMatchState;
static int gmatch_aux (lua_State *L) { static int gmatch_aux (lua_State *L) {
MatchState ms; GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3));
size_t ls, lp;
const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
const char *src; const char *src;
ms.L = L; gm->ms.L = L;
ms.matchdepth = MAXCCALLS; for (src = gm->src; src <= gm->ms.src_end; src++) {
ms.src_init = s;
ms.src_end = s+ls;
ms.p_end = p + lp;
for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
src <= ms.src_end;
src++) {
const char *e; const char *e;
ms.level = 0; reprepstate(&gm->ms);
lua_assert(ms.matchdepth == MAXCCALLS); if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) {
if ((e = match(&ms, src, p)) != NULL) { gm->src = gm->lastmatch = e;
lua_Integer newstart = e-s; return push_captures(&gm->ms, src, e);
if (e == src) newstart++; /* empty match? go at least one position */
lua_pushinteger(L, newstart);
lua_replace(L, lua_upvalueindex(3));
return push_captures(&ms, src, e);
} }
} }
return 0; /* not found */ return 0; /* not found */
@ -676,10 +687,14 @@ static int gmatch_aux (lua_State *L) {
static int gmatch (lua_State *L) { static int gmatch (lua_State *L) {
luaL_checkstring(L, 1); size_t ls, lp;
luaL_checkstring(L, 2); const char *s = luaL_checklstring(L, 1, &ls);
lua_settop(L, 2); const char *p = luaL_checklstring(L, 2, &lp);
lua_pushinteger(L, 0); GMatchState *gm;
lua_settop(L, 2); /* keep them on closure to avoid being collected */
gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState));
prepstate(&gm->ms, L, s, ls, p, lp);
gm->src = s; gm->p = p; gm->lastmatch = NULL;
lua_pushcclosure(L, gmatch_aux, 3); lua_pushcclosure(L, gmatch_aux, 3);
return 1; return 1;
} }
@ -746,12 +761,13 @@ static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
static int str_gsub (lua_State *L) { static int str_gsub (lua_State *L) {
size_t srcl, lp; size_t srcl, lp;
const char *src = luaL_checklstring(L, 1, &srcl); const char *src = luaL_checklstring(L, 1, &srcl); /* subject */
const char *p = luaL_checklstring(L, 2, &lp); const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
int tr = lua_type(L, 3); const char *lastmatch = NULL; /* end of last match */
lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); int tr = lua_type(L, 3); /* replacement type */
lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */
int anchor = (*p == '^'); int anchor = (*p == '^');
lua_Integer n = 0; lua_Integer n = 0; /* replacement count */
MatchState ms; MatchState ms;
luaL_Buffer b; luaL_Buffer b;
luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
@ -761,25 +777,18 @@ static int str_gsub (lua_State *L) {
if (anchor) { if (anchor) {
p++; lp--; /* skip anchor character */ p++; lp--; /* skip anchor character */
} }
ms.L = L; prepstate(&ms, L, src, srcl, p, lp);
ms.matchdepth = MAXCCALLS;
ms.src_init = src;
ms.src_end = src+srcl;
ms.p_end = p + lp;
while (n < max_s) { while (n < max_s) {
const char *e; const char *e;
ms.level = 0; reprepstate(&ms); /* (re)prepare state for new match */
lua_assert(ms.matchdepth == MAXCCALLS); if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */
e = match(&ms, src, p);
if (e) {
n++; n++;
add_value(&ms, &b, src, e, tr); add_value(&ms, &b, src, e, tr); /* add replacement to buffer */
src = lastmatch = e;
} }
if (e && e>src) /* non empty match? */ else if (src < ms.src_end) /* otherwise, skip one character */
src = e; /* skip it */
else if (src < ms.src_end)
luaL_addchar(&b, *src++); luaL_addchar(&b, *src++);
else break; else break; /* end of subject */
if (anchor) break; if (anchor) break;
} }
luaL_addlstring(&b, src, ms.src_end-src); luaL_addlstring(&b, src, ms.src_end-src);
@ -804,7 +813,6 @@ static int str_gsub (lua_State *L) {
** Hexadecimal floating-point formatter ** Hexadecimal floating-point formatter
*/ */
#include <locale.h>
#include <math.h> #include <math.h>
#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
@ -830,13 +838,13 @@ static lua_Number adddigit (char *buff, int n, lua_Number x) {
} }
static int num2straux (char *buff, lua_Number x) { static int num2straux (char *buff, int sz, lua_Number x) {
if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */ /* if 'inf' or 'NaN', format it like '%g' */
return sprintf(buff, LUA_NUMBER_FMT, x); /* equal to '%g' */ if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL)
return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x);
else if (x == 0) { /* can be -0... */ else if (x == 0) { /* can be -0... */
sprintf(buff, LUA_NUMBER_FMT, x); /* create "0" or "-0" followed by exponent */
strcat(buff, "x0p+0"); /* reuses '0/-0' from 'sprintf'... */ return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x);
return strlen(buff);
} }
else { else {
int e; int e;
@ -855,22 +863,23 @@ static int num2straux (char *buff, lua_Number x) {
m = adddigit(buff, n++, m * 16); m = adddigit(buff, n++, m * 16);
} while (m > 0); } while (m > 0);
} }
n += sprintf(buff + n, "p%+d", e); /* add exponent */ n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */
lua_assert(n < sz);
return n; return n;
} }
} }
static int lua_number2strx (lua_State *L, char *buff, const char *fmt, static int lua_number2strx (lua_State *L, char *buff, int sz,
lua_Number x) { const char *fmt, lua_Number x) {
int n = num2straux(buff, x); int n = num2straux(buff, sz, x);
if (fmt[SIZELENMOD] == 'A') { if (fmt[SIZELENMOD] == 'A') {
int i; int i;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
buff[i] = toupper(uchar(buff[i])); buff[i] = toupper(uchar(buff[i]));
} }
else if (fmt[SIZELENMOD] != 'a') else if (fmt[SIZELENMOD] != 'a')
luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
return n; return n;
} }
@ -879,10 +888,12 @@ static int lua_number2strx (lua_State *L, char *buff, const char *fmt,
/* /*
** Maximum size of each formatted item. This maximum size is produced ** Maximum size of each formatted item. This maximum size is produced
** by format('%.99f', minfloat), and is equal to 99 + 2 ('-' and '.') + ** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.',
** number of decimal digits to represent minfloat. ** and '\0') + number of decimal digits to represent maxfloat (which
** is maximum exponent + 1). (99+3+1 then rounded to 120 for "extra
** expenses", such as locale-dependent stuff)
*/ */
#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) #define MAX_ITEM (120 + l_mathlim(MAX_10_EXP))
/* valid flags in a format specification */ /* valid flags in a format specification */
@ -894,21 +905,19 @@ static int lua_number2strx (lua_State *L, char *buff, const char *fmt,
#define MAX_FORMAT 32 #define MAX_FORMAT 32
static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
size_t l;
const char *s = luaL_checklstring(L, arg, &l);
luaL_addchar(b, '"'); luaL_addchar(b, '"');
while (l--) { while (len--) {
if (*s == '"' || *s == '\\' || *s == '\n') { if (*s == '"' || *s == '\\' || *s == '\n') {
luaL_addchar(b, '\\'); luaL_addchar(b, '\\');
luaL_addchar(b, *s); luaL_addchar(b, *s);
} }
else if (*s == '\0' || iscntrl(uchar(*s))) { else if (iscntrl(uchar(*s))) {
char buff[10]; char buff[10];
if (!isdigit(uchar(*(s+1)))) if (!isdigit(uchar(*(s+1))))
sprintf(buff, "\\%d", (int)uchar(*s)); l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s));
else else
sprintf(buff, "\\%03d", (int)uchar(*s)); l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s));
luaL_addstring(b, buff); luaL_addstring(b, buff);
} }
else else
@ -918,6 +927,57 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
luaL_addchar(b, '"'); luaL_addchar(b, '"');
} }
/*
** Ensures the 'buff' string uses a dot as the radix character.
*/
static void checkdp (char *buff, int nb) {
if (memchr(buff, '.', nb) == NULL) { /* no dot? */
char point = lua_getlocaledecpoint(); /* try locale point */
char *ppoint = (char *)memchr(buff, point, nb);
if (ppoint) *ppoint = '.'; /* change it to a dot */
}
}
static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
switch (lua_type(L, arg)) {
case LUA_TSTRING: {
size_t len;
const char *s = lua_tolstring(L, arg, &len);
addquoted(b, s, len);
break;
}
case LUA_TNUMBER: {
char *buff = luaL_prepbuffsize(b, MAX_ITEM);
int nb;
if (!lua_isinteger(L, arg)) { /* float? */
lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */
nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n);
checkdp(buff, nb); /* ensure it uses a dot */
}
else { /* integers */
lua_Integer n = lua_tointeger(L, arg);
const char *format = (n == LUA_MININTEGER) /* corner case? */
? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */
: LUA_INTEGER_FMT; /* else use default format */
nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n);
}
luaL_addsize(b, nb);
break;
}
case LUA_TNIL: case LUA_TBOOLEAN: {
luaL_tolstring(L, arg, NULL);
luaL_addvalue(b);
break;
}
default: {
luaL_argerror(L, arg, "value has no literal form");
}
}
}
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
const char *p = strfrmt; const char *p = strfrmt;
while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
@ -975,41 +1035,47 @@ static int str_format (lua_State *L) {
strfrmt = scanformat(L, strfrmt, form); strfrmt = scanformat(L, strfrmt, form);
switch (*strfrmt++) { switch (*strfrmt++) {
case 'c': { case 'c': {
nb = sprintf(buff, form, (int)luaL_checkinteger(L, arg)); nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg));
break; break;
} }
case 'd': case 'i': case 'd': case 'i':
case 'o': case 'u': case 'x': case 'X': { case 'o': case 'u': case 'x': case 'X': {
lua_Integer n = luaL_checkinteger(L, arg); lua_Integer n = luaL_checkinteger(L, arg);
addlenmod(form, LUA_INTEGER_FRMLEN); addlenmod(form, LUA_INTEGER_FRMLEN);
nb = sprintf(buff, form, n); nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACINT)n);
break; break;
} }
case 'a': case 'A': case 'a': case 'A':
addlenmod(form, LUA_NUMBER_FRMLEN); addlenmod(form, LUA_NUMBER_FRMLEN);
nb = lua_number2strx(L, buff, form, luaL_checknumber(L, arg)); nb = lua_number2strx(L, buff, MAX_ITEM, form,
luaL_checknumber(L, arg));
break; break;
case 'e': case 'E': case 'f': case 'e': case 'E': case 'f':
case 'g': case 'G': { case 'g': case 'G': {
lua_Number n = luaL_checknumber(L, arg);
addlenmod(form, LUA_NUMBER_FRMLEN); addlenmod(form, LUA_NUMBER_FRMLEN);
nb = sprintf(buff, form, luaL_checknumber(L, arg)); nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n);
break; break;
} }
case 'q': { case 'q': {
addquoted(L, &b, arg); addliteral(L, &b, arg);
break; break;
} }
case 's': { case 's': {
size_t l; size_t l;
const char *s = luaL_tolstring(L, arg, &l); const char *s = luaL_tolstring(L, arg, &l);
if (!strchr(form, '.') && l >= 100) { if (form[2] == '\0') /* no modifiers? */
/* no precision and string is too long to be formatted; luaL_addvalue(&b); /* keep entire string */
keep original string */
luaL_addvalue(&b);
}
else { else {
nb = sprintf(buff, form, s); luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ if (!strchr(form, '.') && l >= 100) {
/* no precision and string is too long to be formatted */
luaL_addvalue(&b); /* keep entire string */
}
else { /* format the string into 'buff' */
nb = l_sprintf(buff, MAX_ITEM, form, s);
lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
}
} }
break; break;
} }
@ -1018,6 +1084,7 @@ static int str_format (lua_State *L) {
*(strfrmt - 1)); *(strfrmt - 1));
} }
} }
lua_assert(nb < MAX_ITEM);
luaL_addsize(&b, nb); luaL_addsize(&b, nb);
} }
} }
@ -1036,8 +1103,8 @@ static int str_format (lua_State *L) {
/* value used for padding */ /* value used for padding */
#if !defined(LUA_PACKPADBYTE) #if !defined(LUAL_PACKPADBYTE)
#define LUA_PACKPADBYTE 0x00 #define LUAL_PACKPADBYTE 0x00
#endif #endif
/* maximum size for the binary representation of an integer */ /* maximum size for the binary representation of an integer */
@ -1132,8 +1199,8 @@ static int getnum (const char **fmt, int df) {
static int getnumlimit (Header *h, const char **fmt, int df) { static int getnumlimit (Header *h, const char **fmt, int df) {
int sz = getnum(fmt, df); int sz = getnum(fmt, df);
if (sz > MAXINTSIZE || sz <= 0) if (sz > MAXINTSIZE || sz <= 0)
luaL_error(h->L, "integral size (%d) out of limits [1,%d]", return luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
sz, MAXINTSIZE); sz, MAXINTSIZE);
return sz; return sz;
} }
@ -1274,7 +1341,7 @@ static int str_pack (lua_State *L) {
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
totalsize += ntoalign + size; totalsize += ntoalign + size;
while (ntoalign-- > 0) while (ntoalign-- > 0)
luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */ luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */
arg++; arg++;
switch (opt) { switch (opt) {
case Kint: { /* signed integers */ case Kint: { /* signed integers */
@ -1309,8 +1376,11 @@ static int str_pack (lua_State *L) {
case Kchar: { /* fixed-size string */ case Kchar: { /* fixed-size string */
size_t len; size_t len;
const char *s = luaL_checklstring(L, arg, &len); const char *s = luaL_checklstring(L, arg, &len);
luaL_argcheck(L, len == (size_t)size, arg, "wrong length"); luaL_argcheck(L, len <= (size_t)size, arg,
luaL_addlstring(&b, s, size); "string longer than given size");
luaL_addlstring(&b, s, len); /* add string */
while (len++ < (size_t)size) /* pad extra space */
luaL_addchar(&b, LUAL_PACKPADBYTE);
break; break;
} }
case Kstring: { /* strings with length count */ case Kstring: { /* strings with length count */
@ -1333,7 +1403,7 @@ static int str_pack (lua_State *L) {
totalsize += len + 1; totalsize += len + 1;
break; break;
} }
case Kpadding: luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */ case Kpadding: luaL_addchar(&b, LUAL_PACKPADBYTE); /* FALLTHROUGH */
case Kpaddalign: case Knop: case Kpaddalign: case Knop:
arg--; /* undo increment */ arg--; /* undo increment */
break; break;
@ -1360,7 +1430,7 @@ static int str_packsize (lua_State *L) {
case Kstring: /* strings with length count */ case Kstring: /* strings with length count */
case Kzstr: /* zero-terminated string */ case Kzstr: /* zero-terminated string */
luaL_argerror(L, 1, "variable-length format"); luaL_argerror(L, 1, "variable-length format");
break; /* call never return, but to avoid warnings: *//* FALLTHROUGH */
default: break; default: break;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.111 2015/06/09 14:21:13 roberto Exp $ ** $Id: ltable.c,v 2.118.1.4 2018/06/08 16:22:51 roberto Exp $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -74,8 +74,6 @@
#define dummynode (&dummynode_) #define dummynode (&dummynode_)
#define isdummy(n) ((n) == dummynode)
static const Node dummynode_ = { static const Node dummynode_ = {
{NILCONSTANT}, /* value */ {NILCONSTANT}, /* value */
{{NILCONSTANT, 0}} /* key */ {{NILCONSTANT, 0}} /* key */
@ -85,7 +83,7 @@ static const Node dummynode_ = {
/* /*
** Hash for floating-point numbers. ** Hash for floating-point numbers.
** The main computation should be just ** The main computation should be just
** n = frepx(n, &i); return (n * INT_MAX) + i ** n = frexp(n, &i); return (n * INT_MAX) + i
** but there are some numerical subtleties. ** but there are some numerical subtleties.
** In a two-complement representation, INT_MAX does not has an exact ** In a two-complement representation, INT_MAX does not has an exact
** representation as a float, but INT_MIN does; because the absolute ** representation as a float, but INT_MIN does; because the absolute
@ -101,7 +99,7 @@ static int l_hashfloat (lua_Number n) {
lua_Integer ni; lua_Integer ni;
n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */ if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == HUGE_VAL); lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
return 0; return 0;
} }
else { /* normal case */ else { /* normal case */
@ -124,14 +122,8 @@ static Node *mainposition (const Table *t, const TValue *key) {
return hashmod(t, l_hashfloat(fltvalue(key))); return hashmod(t, l_hashfloat(fltvalue(key)));
case LUA_TSHRSTR: case LUA_TSHRSTR:
return hashstr(t, tsvalue(key)); return hashstr(t, tsvalue(key));
case LUA_TLNGSTR: { case LUA_TLNGSTR:
TString *s = tsvalue(key); return hashpow2(t, luaS_hashlongstr(tsvalue(key)));
if (s->extra == 0) { /* no hash? */
s->hash = luaS_hash(getstr(s), s->u.lnglen, s->hash);
s->extra = 1; /* now it has its hash */
}
return hashstr(t, tsvalue(key));
}
case LUA_TBOOLEAN: case LUA_TBOOLEAN:
return hashboolean(t, bvalue(key)); return hashboolean(t, bvalue(key));
case LUA_TLIGHTUSERDATA: case LUA_TLIGHTUSERDATA:
@ -139,6 +131,7 @@ static Node *mainposition (const Table *t, const TValue *key) {
case LUA_TLCF: case LUA_TLCF:
return hashpointer(t, fvalue(key)); return hashpointer(t, fvalue(key));
default: default:
lua_assert(!ttisdeadkey(key));
return hashpointer(t, gcvalue(key)); return hashpointer(t, gcvalue(key));
} }
} }
@ -230,7 +223,9 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
unsigned int na = 0; /* number of elements to go to array part */ unsigned int na = 0; /* number of elements to go to array part */
unsigned int optimal = 0; /* optimal size for array part */ unsigned int optimal = 0; /* optimal size for array part */
/* loop while keys can fill more than half of total size */ /* loop while keys can fill more than half of total size */
for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { for (i = 0, twotoi = 1;
twotoi > 0 && *pna > twotoi / 2;
i++, twotoi *= 2) {
if (nums[i] > 0) { if (nums[i] > 0) {
a += nums[i]; a += nums[i];
if (a > twotoi/2) { /* more than half elements present? */ if (a > twotoi/2) { /* more than half elements present? */
@ -313,14 +308,14 @@ static void setarrayvector (lua_State *L, Table *t, unsigned int size) {
static void setnodevector (lua_State *L, Table *t, unsigned int size) { static void setnodevector (lua_State *L, Table *t, unsigned int size) {
int lsize;
if (size == 0) { /* no elements to hash part? */ if (size == 0) { /* no elements to hash part? */
t->node = cast(Node *, dummynode); /* use common 'dummynode' */ t->node = cast(Node *, dummynode); /* use common 'dummynode' */
lsize = 0; t->lsizenode = 0;
t->lastfree = NULL; /* signal that it is using dummy node */
} }
else { else {
int i; int i;
lsize = luaO_ceillog2(size); int lsize = luaO_ceillog2(size);
if (lsize > MAXHBITS) if (lsize > MAXHBITS)
luaG_runerror(L, "table overflow"); luaG_runerror(L, "table overflow");
size = twoto(lsize); size = twoto(lsize);
@ -331,9 +326,21 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) {
setnilvalue(wgkey(n)); setnilvalue(wgkey(n));
setnilvalue(gval(n)); setnilvalue(gval(n));
} }
t->lsizenode = cast_byte(lsize);
t->lastfree = gnode(t, size); /* all positions are free */
} }
t->lsizenode = cast_byte(lsize); }
t->lastfree = gnode(t, size); /* all positions are free */
typedef struct {
Table *t;
unsigned int nhsize;
} AuxsetnodeT;
static void auxsetnode (lua_State *L, void *ud) {
AuxsetnodeT *asn = cast(AuxsetnodeT *, ud);
setnodevector(L, asn->t, asn->nhsize);
} }
@ -341,13 +348,18 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
unsigned int nhsize) { unsigned int nhsize) {
unsigned int i; unsigned int i;
int j; int j;
AuxsetnodeT asn;
unsigned int oldasize = t->sizearray; unsigned int oldasize = t->sizearray;
int oldhsize = t->lsizenode; int oldhsize = allocsizenode(t);
Node *nold = t->node; /* save old hash ... */ Node *nold = t->node; /* save old hash ... */
if (nasize > oldasize) /* array part must grow? */ if (nasize > oldasize) /* array part must grow? */
setarrayvector(L, t, nasize); setarrayvector(L, t, nasize);
/* create new hash part with appropriate size */ /* create new hash part with appropriate size */
setnodevector(L, t, nhsize); asn.t = t; asn.nhsize = nhsize;
if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) { /* mem. error? */
setarrayvector(L, t, oldasize); /* array back to its original size */
luaD_throw(L, LUA_ERRMEM); /* rethrow memory error */
}
if (nasize < oldasize) { /* array part must shrink? */ if (nasize < oldasize) { /* array part must shrink? */
t->sizearray = nasize; t->sizearray = nasize;
/* re-insert elements from vanishing slice */ /* re-insert elements from vanishing slice */
@ -359,7 +371,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
luaM_reallocvector(L, t->array, oldasize, nasize, TValue); luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
} }
/* re-insert elements from hash part */ /* re-insert elements from hash part */
for (j = twoto(oldhsize) - 1; j >= 0; j--) { for (j = oldhsize - 1; j >= 0; j--) {
Node *old = nold + j; Node *old = nold + j;
if (!ttisnil(gval(old))) { if (!ttisnil(gval(old))) {
/* doesn't need barrier/invalidate cache, as entry was /* doesn't need barrier/invalidate cache, as entry was
@ -367,13 +379,13 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
} }
} }
if (!isdummy(nold)) if (oldhsize > 0) /* not the dummy node? */
luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old hash */ luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */
} }
void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
int nsize = isdummy(t->node) ? 0 : sizenode(t); int nsize = allocsizenode(t);
luaH_resize(L, t, nasize, nsize); luaH_resize(L, t, nasize, nsize);
} }
@ -419,7 +431,7 @@ Table *luaH_new (lua_State *L) {
void luaH_free (lua_State *L, Table *t) { void luaH_free (lua_State *L, Table *t) {
if (!isdummy(t->node)) if (!isdummy(t))
luaM_freearray(L, t->node, cast(size_t, sizenode(t))); luaM_freearray(L, t->node, cast(size_t, sizenode(t)));
luaM_freearray(L, t->array, t->sizearray); luaM_freearray(L, t->array, t->sizearray);
luaM_free(L, t); luaM_free(L, t);
@ -427,10 +439,12 @@ void luaH_free (lua_State *L, Table *t) {
static Node *getfreepos (Table *t) { static Node *getfreepos (Table *t) {
while (t->lastfree > t->node) { if (!isdummy(t)) {
t->lastfree--; while (t->lastfree > t->node) {
if (ttisnil(gkey(t->lastfree))) t->lastfree--;
return t->lastfree; if (ttisnil(gkey(t->lastfree)))
return t->lastfree;
}
} }
return NULL; /* could not find a free place */ return NULL; /* could not find a free place */
} }
@ -450,7 +464,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
if (ttisnil(key)) luaG_runerror(L, "table index is nil"); if (ttisnil(key)) luaG_runerror(L, "table index is nil");
else if (ttisfloat(key)) { else if (ttisfloat(key)) {
lua_Integer k; lua_Integer k;
if (luaV_tointeger(key, &k, 0)) { /* index is int? */ if (luaV_tointeger(key, &k, 0)) { /* does index fit in an integer? */
setivalue(&aux, k); setivalue(&aux, k);
key = &aux; /* insert it as an integer */ key = &aux; /* insert it as an integer */
} }
@ -458,15 +472,15 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
luaG_runerror(L, "table index is NaN"); luaG_runerror(L, "table index is NaN");
} }
mp = mainposition(t, key); mp = mainposition(t, key);
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ if (!ttisnil(gval(mp)) || isdummy(t)) { /* main position is taken? */
Node *othern; Node *othern;
Node *f = getfreepos(t); /* get a free place */ Node *f = getfreepos(t); /* get a free place */
if (f == NULL) { /* cannot find a free place? */ if (f == NULL) { /* cannot find a free place? */
rehash(L, t, key); /* grow table */ rehash(L, t, key); /* grow table */
/* whatever called 'newkey' takes care of TM cache and GC barrier */ /* whatever called 'newkey' takes care of TM cache */
return luaH_set(L, t, key); /* insert key into grown table */ return luaH_set(L, t, key); /* insert key into grown table */
} }
lua_assert(!isdummy(f)); lua_assert(!isdummy(t));
othern = mainposition(t, gkey(mp)); othern = mainposition(t, gkey(mp));
if (othern != mp) { /* is colliding node out of its main position? */ if (othern != mp) { /* is colliding node out of its main position? */
/* yes; move colliding node into free position */ /* yes; move colliding node into free position */
@ -501,7 +515,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
*/ */
const TValue *luaH_getint (Table *t, lua_Integer key) { const TValue *luaH_getint (Table *t, lua_Integer key) {
/* (1 <= key && key <= t->sizearray) */ /* (1 <= key && key <= t->sizearray) */
if (l_castS2U(key - 1) < t->sizearray) if (l_castS2U(key) - 1 < t->sizearray)
return &t->array[key - 1]; return &t->array[key - 1];
else { else {
Node *n = hashint(t, key); Node *n = hashint(t, key);
@ -513,7 +527,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
if (nx == 0) break; if (nx == 0) break;
n += nx; n += nx;
} }
}; }
return luaO_nilobject; return luaO_nilobject;
} }
} }
@ -522,7 +536,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) {
/* /*
** search function for short strings ** search function for short strings
*/ */
const TValue *luaH_getstr (Table *t, TString *key) { const TValue *luaH_getshortstr (Table *t, TString *key) {
Node *n = hashstr(t, key); Node *n = hashstr(t, key);
lua_assert(key->tt == LUA_TSHRSTR); lua_assert(key->tt == LUA_TSHRSTR);
for (;;) { /* check whether 'key' is somewhere in the chain */ for (;;) { /* check whether 'key' is somewhere in the chain */
@ -531,11 +545,41 @@ const TValue *luaH_getstr (Table *t, TString *key) {
return gval(n); /* that's it */ return gval(n); /* that's it */
else { else {
int nx = gnext(n); int nx = gnext(n);
if (nx == 0) break; if (nx == 0)
return luaO_nilobject; /* not found */
n += nx; n += nx;
} }
}; }
return luaO_nilobject; }
/*
** "Generic" get version. (Not that generic: not valid for integers,
** which may be in array part, nor for floats with integral values.)
*/
static const TValue *getgeneric (Table *t, const TValue *key) {
Node *n = mainposition(t, key);
for (;;) { /* check whether 'key' is somewhere in the chain */
if (luaV_rawequalobj(gkey(n), key))
return gval(n); /* that's it */
else {
int nx = gnext(n);
if (nx == 0)
return luaO_nilobject; /* not found */
n += nx;
}
}
}
const TValue *luaH_getstr (Table *t, TString *key) {
if (key->tt == LUA_TSHRSTR)
return luaH_getshortstr(t, key);
else { /* for long strings, use generic case */
TValue ko;
setsvalue(cast(lua_State *, NULL), &ko, key);
return getgeneric(t, &ko);
}
} }
@ -544,7 +588,7 @@ const TValue *luaH_getstr (Table *t, TString *key) {
*/ */
const TValue *luaH_get (Table *t, const TValue *key) { const TValue *luaH_get (Table *t, const TValue *key) {
switch (ttype(key)) { switch (ttype(key)) {
case LUA_TSHRSTR: return luaH_getstr(t, tsvalue(key)); case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key));
case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
case LUA_TNIL: return luaO_nilobject; case LUA_TNIL: return luaO_nilobject;
case LUA_TNUMFLT: { case LUA_TNUMFLT: {
@ -553,19 +597,8 @@ const TValue *luaH_get (Table *t, const TValue *key) {
return luaH_getint(t, k); /* use specialized version */ return luaH_getint(t, k); /* use specialized version */
/* else... */ /* else... */
} /* FALLTHROUGH */ } /* FALLTHROUGH */
default: { default:
Node *n = mainposition(t, key); return getgeneric(t, key);
for (;;) { /* check whether 'key' is somewhere in the chain */
if (luaV_rawequalobj(gkey(n), key))
return gval(n); /* that's it */
else {
int nx = gnext(n);
if (nx == 0) break;
n += nx;
}
};
return luaO_nilobject;
}
} }
} }
@ -596,13 +629,13 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
} }
static int unbound_search (Table *t, unsigned int j) { static lua_Unsigned unbound_search (Table *t, lua_Unsigned j) {
unsigned int i = j; /* i is zero or a present index */ lua_Unsigned i = j; /* i is zero or a present index */
j++; j++;
/* find 'i' and 'j' such that i is present and j is not */ /* find 'i' and 'j' such that i is present and j is not */
while (!ttisnil(luaH_getint(t, j))) { while (!ttisnil(luaH_getint(t, j))) {
i = j; i = j;
if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ if (j > l_castS2U(LUA_MAXINTEGER) / 2) { /* overflow? */
/* table was built with bad purposes: resort to linear search */ /* table was built with bad purposes: resort to linear search */
i = 1; i = 1;
while (!ttisnil(luaH_getint(t, i))) i++; while (!ttisnil(luaH_getint(t, i))) i++;
@ -612,7 +645,7 @@ static int unbound_search (Table *t, unsigned int j) {
} }
/* now do a binary search between them */ /* now do a binary search between them */
while (j - i > 1) { while (j - i > 1) {
unsigned int m = (i+j)/2; lua_Unsigned m = (i+j)/2;
if (ttisnil(luaH_getint(t, m))) j = m; if (ttisnil(luaH_getint(t, m))) j = m;
else i = m; else i = m;
} }
@ -624,7 +657,7 @@ static int unbound_search (Table *t, unsigned int j) {
** Try to find a boundary in table 't'. A 'boundary' is an integer index ** Try to find a boundary in table 't'. A 'boundary' is an integer index
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
*/ */
int luaH_getn (Table *t) { lua_Unsigned luaH_getn (Table *t) {
unsigned int j = t->sizearray; unsigned int j = t->sizearray;
if (j > 0 && ttisnil(&t->array[j - 1])) { if (j > 0 && ttisnil(&t->array[j - 1])) {
/* there is a boundary in the array part: (binary) search for it */ /* there is a boundary in the array part: (binary) search for it */
@ -637,7 +670,7 @@ int luaH_getn (Table *t) {
return i; return i;
} }
/* else must find a boundary in hash part */ /* else must find a boundary in hash part */
else if (isdummy(t->node)) /* hash part is empty? */ else if (isdummy(t)) /* hash part is empty? */
return j; /* that is easy... */ return j; /* that is easy... */
else return unbound_search(t, j); else return unbound_search(t, j);
} }
@ -650,6 +683,6 @@ Node *luaH_mainposition (const Table *t, const TValue *key) {
return mainposition(t, key); return mainposition(t, key);
} }
int luaH_isdummy (Node *n) { return isdummy(n); } int luaH_isdummy (const Table *t) { return isdummy(t); }
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.h,v 2.20 2014/09/04 18:15:29 roberto Exp $ ** $Id: ltable.h,v 2.23.1.2 2018/05/24 19:39:05 roberto Exp $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -18,11 +18,23 @@
/* 'const' to avoid wrong writings that can mess up field 'next' */ /* 'const' to avoid wrong writings that can mess up field 'next' */
#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk))
/*
** writable version of 'gkey'; allows updates to individual fields,
** but not to the whole (which has incompatible type)
*/
#define wgkey(n) (&(n)->i_key.nk) #define wgkey(n) (&(n)->i_key.nk)
#define invalidateTMcache(t) ((t)->flags = 0) #define invalidateTMcache(t) ((t)->flags = 0)
/* true when 't' is using 'dummynode' as its hash part */
#define isdummy(t) ((t)->lastfree == NULL)
/* allocated size for hash nodes */
#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t))
/* returns the key, given the value of a table entry */ /* returns the key, given the value of a table entry */
#define keyfromval(v) \ #define keyfromval(v) \
(gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))
@ -31,6 +43,7 @@
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
TValue *value); TValue *value);
LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key);
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key);
@ -41,12 +54,12 @@ LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize);
LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC void luaH_free (lua_State *L, Table *t);
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
LUAI_FUNC int luaH_getn (Table *t); LUAI_FUNC lua_Unsigned luaH_getn (Table *t);
#if defined(LUA_DEBUG) #if defined(LUA_DEBUG)
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
LUAI_FUNC int luaH_isdummy (Node *n); LUAI_FUNC int luaH_isdummy (const Table *t);
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltablib.c,v 1.80 2015/01/13 16:27:29 roberto Exp $ ** $Id: ltablib.c,v 1.93.1.1 2017/04/19 17:20:42 roberto Exp $
** Library for Table Manipulation ** Library for Table Manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -12,6 +12,7 @@
#include <limits.h> #include <limits.h>
#include <stddef.h> #include <stddef.h>
#include <string.h>
#include "lua.h" #include "lua.h"
@ -19,40 +20,42 @@
#include "lualib.h" #include "lualib.h"
/* /*
** Structure with table-access functions ** Operations that an object must define to mimic a table
** (some functions only need some of them)
*/ */
typedef struct { #define TAB_R 1 /* read */
int (*geti) (lua_State *L, int idx, lua_Integer n); #define TAB_W 2 /* write */
void (*seti) (lua_State *L, int idx, lua_Integer n); #define TAB_L 4 /* length */
} TabA; #define TAB_RW (TAB_R | TAB_W) /* read/write */
/* #define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
** Check that 'arg' has a table and set access functions in 'ta' to raw
** or non-raw according to the presence of corresponding metamethods.
*/ static int checkfield (lua_State *L, const char *key, int n) {
static void checktab (lua_State *L, int arg, TabA *ta) { lua_pushstring(L, key);
ta->geti = NULL; ta->seti = NULL; return (lua_rawget(L, -n) != LUA_TNIL);
if (lua_getmetatable(L, arg)) {
lua_pushliteral(L, "__index"); /* 'index' metamethod */
if (lua_rawget(L, -2) != LUA_TNIL)
ta->geti = lua_geti;
lua_pushliteral(L, "__newindex"); /* 'newindex' metamethod */
if (lua_rawget(L, -3) != LUA_TNIL)
ta->seti = lua_seti;
lua_pop(L, 3); /* pop metatable plus both metamethods */
}
if (ta->geti == NULL || ta->seti == NULL) {
luaL_checktype(L, arg, LUA_TTABLE); /* must be table for raw methods */
if (ta->geti == NULL) ta->geti = lua_rawgeti;
if (ta->seti == NULL) ta->seti = lua_rawseti;
}
} }
#define aux_getn(L,n,ta) (checktab(L, n, ta), luaL_len(L, n)) /*
** Check that 'arg' either is a table or can behave like one (that is,
** has a metatable with the required metamethods)
*/
static void checktab (lua_State *L, int arg, int what) {
if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */
int n = 1; /* number of elements to pop */
if (lua_getmetatable(L, arg) && /* must have metatable */
(!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
(!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
(!(what & TAB_L) || checkfield(L, "__len", ++n))) {
lua_pop(L, n); /* pop metatable and tested metamethods */
}
else
luaL_checktype(L, arg, LUA_TTABLE); /* force an error */
}
}
#if defined(LUA_COMPAT_MAXN) #if defined(LUA_COMPAT_MAXN)
@ -74,8 +77,7 @@ static int maxn (lua_State *L) {
static int tinsert (lua_State *L) { static int tinsert (lua_State *L) {
TabA ta; lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
lua_Integer e = aux_getn(L, 1, &ta) + 1; /* first empty element */
lua_Integer pos; /* where to insert new element */ lua_Integer pos; /* where to insert new element */
switch (lua_gettop(L)) { switch (lua_gettop(L)) {
case 2: { /* called with only 2 arguments */ case 2: { /* called with only 2 arguments */
@ -87,8 +89,8 @@ static int tinsert (lua_State *L) {
pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
for (i = e; i > pos; i--) { /* move up elements */ for (i = e; i > pos; i--) { /* move up elements */
(*ta.geti)(L, 1, i - 1); lua_geti(L, 1, i - 1);
(*ta.seti)(L, 1, i); /* t[i] = t[i - 1] */ lua_seti(L, 1, i); /* t[i] = t[i - 1] */
} }
break; break;
} }
@ -96,67 +98,67 @@ static int tinsert (lua_State *L) {
return luaL_error(L, "wrong number of arguments to 'insert'"); return luaL_error(L, "wrong number of arguments to 'insert'");
} }
} }
(*ta.seti)(L, 1, pos); /* t[pos] = v */ lua_seti(L, 1, pos); /* t[pos] = v */
return 0; return 0;
} }
static int tremove (lua_State *L) { static int tremove (lua_State *L) {
TabA ta; lua_Integer size = aux_getn(L, 1, TAB_RW);
lua_Integer size = aux_getn(L, 1, &ta);
lua_Integer pos = luaL_optinteger(L, 2, size); lua_Integer pos = luaL_optinteger(L, 2, size);
if (pos != size) /* validate 'pos' if given */ if (pos != size) /* validate 'pos' if given */
luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
(*ta.geti)(L, 1, pos); /* result = t[pos] */ lua_geti(L, 1, pos); /* result = t[pos] */
for ( ; pos < size; pos++) { for ( ; pos < size; pos++) {
(*ta.geti)(L, 1, pos + 1); lua_geti(L, 1, pos + 1);
(*ta.seti)(L, 1, pos); /* t[pos] = t[pos + 1] */ lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
} }
lua_pushnil(L); lua_pushnil(L);
(*ta.seti)(L, 1, pos); /* t[pos] = nil */ lua_seti(L, 1, pos); /* t[pos] = nil */
return 1; return 1;
} }
/*
** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
** possible, copy in increasing order, which is better for rehashing.
** "possible" means destination after original range, or smaller
** than origin, or copying to another table.
*/
static int tmove (lua_State *L) { static int tmove (lua_State *L) {
TabA ta;
lua_Integer f = luaL_checkinteger(L, 2); lua_Integer f = luaL_checkinteger(L, 2);
lua_Integer e = luaL_checkinteger(L, 3); lua_Integer e = luaL_checkinteger(L, 3);
lua_Integer t = luaL_checkinteger(L, 4); lua_Integer t = luaL_checkinteger(L, 4);
int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
checktab(L, 1, TAB_R);
checktab(L, tt, TAB_W);
if (e >= f) { /* otherwise, nothing to move */ if (e >= f) { /* otherwise, nothing to move */
lua_Integer n, i; lua_Integer n, i;
ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL)
? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti)
: lua_geti;
ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL)
? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti)
: lua_seti;
luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3,
"too many elements to move"); "too many elements to move");
n = e - f + 1; /* number of elements to move */ n = e - f + 1; /* number of elements to move */
luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
"destination wrap around"); "destination wrap around");
if (t > f) { if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
for (i = n - 1; i >= 0; i--) { for (i = 0; i < n; i++) {
(*ta.geti)(L, 1, f + i); lua_geti(L, 1, f + i);
(*ta.seti)(L, tt, t + i); lua_seti(L, tt, t + i);
} }
} }
else { else {
for (i = 0; i < n; i++) { for (i = n - 1; i >= 0; i--) {
(*ta.geti)(L, 1, f + i); lua_geti(L, 1, f + i);
(*ta.seti)(L, tt, t + i); lua_seti(L, tt, t + i);
} }
} }
} }
lua_pushvalue(L, tt); /* return "to table" */ lua_pushvalue(L, tt); /* return destination table */
return 1; return 1;
} }
static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
(*ta->geti)(L, 1, i); lua_geti(L, 1, i);
if (!lua_isstring(L, -1)) if (!lua_isstring(L, -1))
luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", luaL_error(L, "invalid value (%s) at index %d in table for 'concat'",
luaL_typename(L, -1), i); luaL_typename(L, -1), i);
@ -165,21 +167,19 @@ static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) {
static int tconcat (lua_State *L) { static int tconcat (lua_State *L) {
TabA ta;
luaL_Buffer b; luaL_Buffer b;
lua_Integer last = aux_getn(L, 1, TAB_R);
size_t lsep; size_t lsep;
lua_Integer i, last;
const char *sep = luaL_optlstring(L, 2, "", &lsep); const char *sep = luaL_optlstring(L, 2, "", &lsep);
checktab(L, 1, &ta); lua_Integer i = luaL_optinteger(L, 3, 1);
i = luaL_optinteger(L, 3, 1); last = luaL_optinteger(L, 4, last);
last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1));
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
for (; i < last; i++) { for (; i < last; i++) {
addfield(L, &b, &ta, i); addfield(L, &b, i);
luaL_addlstring(&b, sep, lsep); luaL_addlstring(&b, sep, lsep);
} }
if (i == last) /* add last value (if interval was not empty) */ if (i == last) /* add last value (if interval was not empty) */
addfield(L, &b, &ta, i); addfield(L, &b, i);
luaL_pushresult(&b); luaL_pushresult(&b);
return 1; return 1;
} }
@ -197,7 +197,7 @@ static int pack (lua_State *L) {
lua_createtable(L, n, 1); /* create result table */ lua_createtable(L, n, 1); /* create result table */
lua_insert(L, 1); /* put it at index 1 */ lua_insert(L, 1); /* put it at index 1 */
for (i = n; i >= 1; i--) /* assign elements */ for (i = n; i >= 1; i--) /* assign elements */
lua_rawseti(L, 1, i); lua_seti(L, 1, i);
lua_pushinteger(L, n); lua_pushinteger(L, n);
lua_setfield(L, 1, "n"); /* t.n = number of elements */ lua_setfield(L, 1, "n"); /* t.n = number of elements */
return 1; /* return table */ return 1; /* return table */
@ -205,20 +205,17 @@ static int pack (lua_State *L) {
static int unpack (lua_State *L) { static int unpack (lua_State *L) {
TabA ta;
lua_Integer i, e;
lua_Unsigned n; lua_Unsigned n;
checktab(L, 1, &ta); lua_Integer i = luaL_optinteger(L, 2, 1);
i = luaL_optinteger(L, 2, 1); lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
if (i > e) return 0; /* empty range */ if (i > e) return 0; /* empty range */
n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))
return luaL_error(L, "too many results to unpack"); return luaL_error(L, "too many results to unpack");
do { /* must have at least one element */ for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
(*ta.geti)(L, 1, i); /* push arg[i..e] */ lua_geti(L, 1, i);
} while (i++ < e); }
lua_geti(L, 1, e); /* push last element */
return (int)n; return (int)n;
} }
@ -235,97 +232,191 @@ static int unpack (lua_State *L) {
*/ */
static void set2 (lua_State *L, TabA *ta, int i, int j) { /* type for array indices */
(*ta->seti)(L, 1, i); typedef unsigned int IdxT;
(*ta->seti)(L, 1, j);
/*
** Produce a "random" 'unsigned int' to randomize pivot choice. This
** macro is used only when 'sort' detects a big imbalance in the result
** of a partition. (If you don't want/need this "randomness", ~0 is a
** good choice.)
*/
#if !defined(l_randomizePivot) /* { */
#include <time.h>
/* size of 'e' measured in number of 'unsigned int's */
#define sof(e) (sizeof(e) / sizeof(unsigned int))
/*
** Use 'time' and 'clock' as sources of "randomness". Because we don't
** know the types 'clock_t' and 'time_t', we cannot cast them to
** anything without risking overflows. A safe way to use their values
** is to copy them to an array of a known type and use the array values.
*/
static unsigned int l_randomizePivot (void) {
clock_t c = clock();
time_t t = time(NULL);
unsigned int buff[sof(c) + sof(t)];
unsigned int i, rnd = 0;
memcpy(buff, &c, sof(c) * sizeof(unsigned int));
memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
for (i = 0; i < sof(buff); i++)
rnd += buff[i];
return rnd;
} }
#endif /* } */
/* arrays larger than 'RANLIMIT' may use randomized pivots */
#define RANLIMIT 100u
static void set2 (lua_State *L, IdxT i, IdxT j) {
lua_seti(L, 1, i);
lua_seti(L, 1, j);
}
/*
** Return true iff value at stack index 'a' is less than the value at
** index 'b' (according to the order of the sort).
*/
static int sort_comp (lua_State *L, int a, int b) { static int sort_comp (lua_State *L, int a, int b) {
if (!lua_isnil(L, 2)) { /* function? */ if (lua_isnil(L, 2)) /* no function? */
return lua_compare(L, a, b, LUA_OPLT); /* a < b */
else { /* function */
int res; int res;
lua_pushvalue(L, 2); lua_pushvalue(L, 2); /* push function */
lua_pushvalue(L, a-1); /* -1 to compensate function */ lua_pushvalue(L, a-1); /* -1 to compensate function */
lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
lua_call(L, 2, 1); lua_call(L, 2, 1); /* call function */
res = lua_toboolean(L, -1); res = lua_toboolean(L, -1); /* get result */
lua_pop(L, 1); lua_pop(L, 1); /* pop result */
return res; return res;
} }
else /* a < b? */
return lua_compare(L, a, b, LUA_OPLT);
} }
static void auxsort (lua_State *L, TabA *ta, int l, int u) {
while (l < u) { /* for tail recursion */ /*
int i, j; ** Does the partition: Pivot P is at the top of the stack.
/* sort elements a[l], a[(l+u)/2] and a[u] */ ** precondition: a[lo] <= P == a[up-1] <= a[up],
(*ta->geti)(L, 1, l); ** so it only needs to do the partition from lo + 1 to up - 2.
(*ta->geti)(L, 1, u); ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up]
if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ ** returns 'i'.
set2(L, ta, l, u); /* swap a[l] - a[u] */ */
static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
IdxT i = lo; /* will be incremented before first use */
IdxT j = up - 1; /* will be decremented before first use */
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
for (;;) {
/* next loop: repeat ++i while a[i] < P */
while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */
luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[i] */
}
/* after the loop, a[i] >= P and a[lo .. i - 1] < P */
/* next loop: repeat --j while P < a[j] */
while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
if (j < i) /* j < i but a[j] > P ?? */
luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[j] */
}
/* after the loop, a[j] <= P and a[j + 1 .. up] >= P */
if (j < i) { /* no elements out of place? */
/* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */
lua_pop(L, 1); /* pop a[j] */
/* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */
set2(L, up - 1, i);
return i;
}
/* otherwise, swap a[i] - a[j] to restore invariant and repeat */
set2(L, i, j);
}
}
/*
** Choose an element in the middle (2nd-3th quarters) of [lo,up]
** "randomized" by 'rnd'
*/
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
IdxT r4 = (up - lo) / 4; /* range/4 */
IdxT p = rnd % (r4 * 2) + (lo + r4);
lua_assert(lo + r4 <= p && p <= up - r4);
return p;
}
/*
** QuickSort algorithm (recursive function)
*/
static void auxsort (lua_State *L, IdxT lo, IdxT up,
unsigned int rnd) {
while (lo < up) { /* loop for tail recursion */
IdxT p; /* Pivot index */
IdxT n; /* to be used later */
/* sort elements 'lo', 'p', and 'up' */
lua_geti(L, 1, lo);
lua_geti(L, 1, up);
if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
set2(L, lo, up); /* swap a[lo] - a[up] */
else else
lua_pop(L, 2); lua_pop(L, 2); /* remove both values */
if (u-l == 1) break; /* only 2 elements */ if (up - lo == 1) /* only 2 elements? */
i = (l+u)/2; return; /* already sorted */
(*ta->geti)(L, 1, i); if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */
(*ta->geti)(L, 1, l); p = (lo + up)/2; /* middle element is a good pivot */
if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ else /* for larger intervals, it is worth a random pivot */
set2(L, ta, i, l); p = choosePivot(lo, up, rnd);
lua_geti(L, 1, p);
lua_geti(L, 1, lo);
if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
set2(L, p, lo); /* swap a[p] - a[lo] */
else { else {
lua_pop(L, 1); /* remove a[l] */ lua_pop(L, 1); /* remove a[lo] */
(*ta->geti)(L, 1, u); lua_geti(L, 1, up);
if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
set2(L, ta, i, u); set2(L, p, up); /* swap a[up] - a[p] */
else else
lua_pop(L, 2); lua_pop(L, 2);
} }
if (u-l == 2) break; /* only 3 elements */ if (up - lo == 2) /* only 3 elements? */
(*ta->geti)(L, 1, i); /* Pivot */ return; /* already sorted */
lua_pushvalue(L, -1); lua_geti(L, 1, p); /* get middle element (Pivot) */
(*ta->geti)(L, 1, u-1); lua_pushvalue(L, -1); /* push Pivot */
set2(L, ta, i, u-1); lua_geti(L, 1, up - 1); /* push a[up - 1] */
/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
i = l; j = u-1; p = partition(L, lo, up);
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
/* repeat ++i until a[i] >= P */ if (p - lo < up - p) { /* lower interval is smaller? */
while ((*ta->geti)(L, 1, ++i), sort_comp(L, -1, -2)) { auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */
if (i>=u) luaL_error(L, "invalid order function for sorting"); n = p - lo; /* size of smaller interval */
lua_pop(L, 1); /* remove a[i] */ lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */
}
/* repeat --j until a[j] <= P */
while ((*ta->geti)(L, 1, --j), sort_comp(L, -3, -1)) {
if (j<=l) luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[j] */
}
if (j<i) {
lua_pop(L, 3); /* pop pivot, a[i], a[j] */
break;
}
set2(L, ta, i, j);
}
(*ta->geti)(L, 1, u-1);
(*ta->geti)(L, 1, i);
set2(L, ta, u-1, i); /* swap pivot (a[u-1]) with a[i] */
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
/* adjust so that smaller half is in [j..i] and larger one in [l..u] */
if (i-l < u-i) {
j=l; i=i-1; l=i+2;
} }
else { else {
j=i+1; i=u; u=j-2; auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */
n = up - p; /* size of smaller interval */
up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
} }
auxsort(L, ta, j, i); /* call recursively the smaller one */ if ((up - lo) / 128 > n) /* partition too imbalanced? */
} /* repeat the routine for the larger one */ rnd = l_randomizePivot(); /* try a new randomization */
} /* tail call auxsort(L, lo, up, rnd) */
} }
static int sort (lua_State *L) { static int sort (lua_State *L) {
TabA ta; lua_Integer n = aux_getn(L, 1, TAB_RW);
int n = (int)aux_getn(L, 1, &ta); if (n > 1) { /* non-trivial interval? */
luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */ luaL_argcheck(L, n < INT_MAX, 1, "array too big");
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
luaL_checktype(L, 2, LUA_TFUNCTION); luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */
lua_settop(L, 2); /* make sure there are two arguments */ lua_settop(L, 2); /* make sure there are two arguments */
auxsort(L, &ta, 1, n); auxsort(L, 1, (IdxT)n, 0);
}
return 0; return 0;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.c,v 2.34 2015/03/30 15:42:27 roberto Exp $ ** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 roberto Exp $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -57,7 +57,7 @@ void luaT_init (lua_State *L) {
** tag methods ** tag methods
*/ */
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
const TValue *tm = luaH_getstr(events, ename); const TValue *tm = luaH_getshortstr(events, ename);
lua_assert(event <= TM_EQ); lua_assert(event <= TM_EQ);
if (ttisnil(tm)) { /* no tag method? */ if (ttisnil(tm)) { /* no tag method? */
events->flags |= cast_byte(1u<<event); /* cache this fact */ events->flags |= cast_byte(1u<<event); /* cache this fact */
@ -79,20 +79,41 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
default: default:
mt = G(L)->mt[ttnov(o)]; mt = G(L)->mt[ttnov(o)];
} }
return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
}
/*
** Return the name of the type of an object. For tables and userdata
** with metatable, use their '__name' metafield, if present.
*/
const char *luaT_objtypename (lua_State *L, const TValue *o) {
Table *mt;
if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
(ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
if (ttisstring(name)) /* is '__name' a string? */
return getstr(tsvalue(name)); /* use it as type name */
}
return ttypename(ttnov(o)); /* else use standard type name */
} }
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, TValue *p3, int hasres) { const TValue *p2, TValue *p3, int hasres) {
ptrdiff_t result = savestack(L, p3); ptrdiff_t result = savestack(L, p3);
setobj2s(L, L->top++, f); /* push function (assume EXTRA_STACK) */ StkId func = L->top;
setobj2s(L, L->top++, p1); /* 1st argument */ setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
setobj2s(L, L->top++, p2); /* 2nd argument */ setobj2s(L, func + 1, p1); /* 1st argument */
setobj2s(L, func + 2, p2); /* 2nd argument */
L->top += 3;
if (!hasres) /* no result? 'p3' is third argument */ if (!hasres) /* no result? 'p3' is third argument */
setobj2s(L, L->top++, p3); /* 3rd argument */ setobj2s(L, L->top++, p3); /* 3rd argument */
/* metamethod may yield only when called from Lua code */ /* metamethod may yield only when called from Lua code */
luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); if (isLua(L->ci))
luaD_call(L, func, hasres);
else
luaD_callnoyield(L, func, hasres);
if (hasres) { /* if has result, move it to its place */ if (hasres) { /* if has result, move it to its place */
p3 = restorestack(L, result); p3 = restorestack(L, result);
setobjs2s(L, p3, --L->top); setobjs2s(L, p3, --L->top);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 roberto Exp $ ** $Id: ltm.h,v 2.22.1.1 2017/04/19 17:20:42 roberto Exp $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -51,11 +51,12 @@ typedef enum {
#define fasttm(l,et,e) gfasttm(G(l), et, e) #define fasttm(l,et,e) gfasttm(G(l), et, e)
#define ttypename(x) luaT_typenames_[(x) + 1] #define ttypename(x) luaT_typenames_[(x) + 1]
#define objtypename(x) ttypename(ttnov(x))
LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS];
LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
TMS event); TMS event);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.c,v 2.41 2014/11/02 19:19:04 roberto Exp $ ** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -32,7 +32,6 @@
typedef struct { typedef struct {
lua_State *L; lua_State *L;
ZIO *Z; ZIO *Z;
Mbuffer *b;
const char *name; const char *name;
} LoadState; } LoadState;
@ -86,17 +85,28 @@ static lua_Integer LoadInteger (LoadState *S) {
} }
static TString *LoadString (LoadState *S) { static TString *LoadString (LoadState *S, Proto *p) {
lua_State *L = S->L;
size_t size = LoadByte(S); size_t size = LoadByte(S);
TString *ts;
if (size == 0xFF) if (size == 0xFF)
LoadVar(S, size); LoadVar(S, size);
if (size == 0) if (size == 0)
return NULL; return NULL;
else { else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
char *s = luaZ_openspace(S->L, S->b, --size); char buff[LUAI_MAXSHORTLEN];
LoadVector(S, s, size); LoadVector(S, buff, size);
return luaS_newlstr(S->L, s, size); ts = luaS_newlstr(L, buff, size);
} }
else { /* long string */
ts = luaS_createlngstrobj(L, size);
setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */
luaD_inctop(L);
LoadVector(S, getstr(ts), size); /* load directly in final place */
L->top--; /* pop string */
}
luaC_objbarrier(L, p, ts);
return ts;
} }
@ -136,7 +146,7 @@ static void LoadConstants (LoadState *S, Proto *f) {
break; break;
case LUA_TSHRSTR: case LUA_TSHRSTR:
case LUA_TLNGSTR: case LUA_TLNGSTR:
setsvalue2n(S->L, o, LoadString(S)); setsvalue2n(S->L, o, LoadString(S, f));
break; break;
default: default:
lua_assert(0); lua_assert(0);
@ -154,6 +164,7 @@ static void LoadProtos (LoadState *S, Proto *f) {
f->p[i] = NULL; f->p[i] = NULL;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
f->p[i] = luaF_newproto(S->L); f->p[i] = luaF_newproto(S->L);
luaC_objbarrier(S->L, f, f->p[i]);
LoadFunction(S, f->p[i], f->source); LoadFunction(S, f->p[i], f->source);
} }
} }
@ -185,18 +196,18 @@ static void LoadDebug (LoadState *S, Proto *f) {
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
f->locvars[i].varname = NULL; f->locvars[i].varname = NULL;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
f->locvars[i].varname = LoadString(S); f->locvars[i].varname = LoadString(S, f);
f->locvars[i].startpc = LoadInt(S); f->locvars[i].startpc = LoadInt(S);
f->locvars[i].endpc = LoadInt(S); f->locvars[i].endpc = LoadInt(S);
} }
n = LoadInt(S); n = LoadInt(S);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
f->upvalues[i].name = LoadString(S); f->upvalues[i].name = LoadString(S, f);
} }
static void LoadFunction (LoadState *S, Proto *f, TString *psource) { static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
f->source = LoadString(S); f->source = LoadString(S, f);
if (f->source == NULL) /* no source in dump? */ if (f->source == NULL) /* no source in dump? */
f->source = psource; /* reuse parent's source */ f->source = psource; /* reuse parent's source */
f->linedefined = LoadInt(S); f->linedefined = LoadInt(S);
@ -251,8 +262,7 @@ static void checkHeader (LoadState *S) {
/* /*
** load precompiled chunk ** load precompiled chunk
*/ */
LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
const char *name) {
LoadState S; LoadState S;
LClosure *cl; LClosure *cl;
if (*name == '@' || *name == '=') if (*name == '@' || *name == '=')
@ -263,12 +273,12 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
S.name = name; S.name = name;
S.L = L; S.L = L;
S.Z = Z; S.Z = Z;
S.b = buff;
checkHeader(&S); checkHeader(&S);
cl = luaF_newLclosure(L, LoadByte(&S)); cl = luaF_newLclosure(L, LoadByte(&S));
setclLvalue(L, L->top, cl); setclLvalue(L, L->top, cl);
incr_top(L); luaD_inctop(L);
cl->p = luaF_newproto(L); cl->p = luaF_newproto(L);
luaC_objbarrier(L, cl, cl->p);
LoadFunction(&S, cl->p, NULL); LoadFunction(&S, cl->p, NULL);
lua_assert(cl->nupvalues == cl->p->sizeupvalues); lua_assert(cl->nupvalues == cl->p->sizeupvalues);
luai_verifycode(L, buff, cl->p); luai_verifycode(L, buff, cl->p);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.h,v 1.44 2014/06/19 18:27:20 roberto Exp $ ** $Id: lundump.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -23,8 +23,7 @@
#define LUAC_FORMAT 0 /* this is the official format */ #define LUAC_FORMAT 0 /* this is the official format */
/* load one chunk; from lundump.c */ /* load one chunk; from lundump.c */
LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name);
const char* name);
/* dump one chunk; from ldump.c */ /* dump one chunk; from ldump.c */
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lutf8lib.c,v 1.15 2015/03/28 19:16:55 roberto Exp $ ** $Id: lutf8lib.c,v 1.16.1.1 2017/04/19 17:29:57 roberto Exp $
** Standard library for UTF-8 manipulation ** Standard library for UTF-8 manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -171,7 +171,7 @@ static int byteoffset (lua_State *L) {
} }
else { else {
if (iscont(s + posi)) if (iscont(s + posi))
luaL_error(L, "initial position is a continuation byte"); return luaL_error(L, "initial position is a continuation byte");
if (n < 0) { if (n < 0) {
while (n < 0 && posi > 0) { /* move back */ while (n < 0 && posi > 0) { /* move back */
do { /* find beginning of previous character */ do { /* find beginning of previous character */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.245 2015/06/09 15:53:35 roberto Exp $ ** $Id: lvm.c,v 2.268.1.1 2017/04/19 17:39:34 roberto Exp $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -153,75 +153,88 @@ static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
/* /*
** Main function for table access (invoking metamethods if needed). ** Finish the table access 'val = t[key]'.
** Compute 'val = t[key]' ** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to
** t[k] entry (which must be nil).
*/ */
void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
const TValue *slot) {
int loop; /* counter to avoid infinite loops */ int loop; /* counter to avoid infinite loops */
const TValue *tm; /* metamethod */
for (loop = 0; loop < MAXTAGLOOP; loop++) { for (loop = 0; loop < MAXTAGLOOP; loop++) {
const TValue *tm; if (slot == NULL) { /* 't' is not a table? */
if (ttistable(t)) { /* 't' is a table? */ lua_assert(!ttistable(t));
Table *h = hvalue(t); tm = luaT_gettmbyobj(L, t, TM_INDEX);
const TValue *res = luaH_get(h, key); /* do a primitive get */ if (ttisnil(tm))
if (!ttisnil(res) || /* result is not nil? */ luaG_typeerror(L, t, "index"); /* no metamethod */
(tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ /* else will try the metamethod */
setobj2s(L, val, res); /* result is the raw get */ }
else { /* 't' is a table */
lua_assert(ttisnil(slot));
tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */
if (tm == NULL) { /* no metamethod? */
setnilvalue(val); /* result is nil */
return; return;
} }
/* else will try metamethod */ /* else will try the metamethod */
} }
else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) if (ttisfunction(tm)) { /* is metamethod a function? */
luaG_typeerror(L, t, "index"); /* no metamethod */ luaT_callTM(L, tm, t, key, val, 1); /* call it */
if (ttisfunction(tm)) { /* metamethod is a function */
luaT_callTM(L, tm, t, key, val, 1);
return; return;
} }
t = tm; /* else repeat access over 'tm' */ t = tm; /* else try to access 'tm[key]' */
if (luaV_fastget(L,t,key,slot,luaH_get)) { /* fast track? */
setobj2s(L, val, slot); /* done */
return;
}
/* else repeat (tail call 'luaV_finishget') */
} }
luaG_runerror(L, "gettable chain too long; possible loop"); luaG_runerror(L, "'__index' chain too long; possible loop");
} }
/* /*
** Main function for table assignment (invoking metamethods if needed). ** Finish a table assignment 't[key] = val'.
** Compute 't[key] = val' ** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points
** to the entry 't[key]', or to 'luaO_nilobject' if there is no such
** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastset'
** would have done the job.)
*/ */
void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
StkId val, const TValue *slot) {
int loop; /* counter to avoid infinite loops */ int loop; /* counter to avoid infinite loops */
for (loop = 0; loop < MAXTAGLOOP; loop++) { for (loop = 0; loop < MAXTAGLOOP; loop++) {
const TValue *tm; const TValue *tm; /* '__newindex' metamethod */
if (ttistable(t)) { /* 't' is a table? */ if (slot != NULL) { /* is 't' a table? */
Table *h = hvalue(t); Table *h = hvalue(t); /* save 't' table */
TValue *oldval = cast(TValue *, luaH_get(h, key)); lua_assert(ttisnil(slot)); /* old value must be nil */
/* if previous value is not nil, there must be a previous entry tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */
in the table; a metamethod has no relevance */ if (tm == NULL) { /* no metamethod? */
if (!ttisnil(oldval) || if (slot == luaO_nilobject) /* no previous entry? */
/* previous value is nil; must check the metamethod */ slot = luaH_newkey(L, h, key); /* create one */
((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
/* no metamethod; is there a previous entry in the table? */
(oldval != luaO_nilobject ||
/* no previous entry; must create one. (The next test is
always true; we only need the assignment.) */
(oldval = luaH_newkey(L, h, key), 1)))) {
/* no metamethod and (now) there is an entry with given key */ /* no metamethod and (now) there is an entry with given key */
setobj2t(L, oldval, val); /* assign new value to that entry */ setobj2t(L, cast(TValue *, slot), val); /* set its new value */
invalidateTMcache(h); invalidateTMcache(h);
luaC_barrierback(L, h, val); luaC_barrierback(L, h, val);
return; return;
} }
/* else will try the metamethod */ /* else will try the metamethod */
} }
else /* not a table; check metamethod */ else { /* not a table; check metamethod */
if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
luaG_typeerror(L, t, "index"); luaG_typeerror(L, t, "index");
}
/* try the metamethod */ /* try the metamethod */
if (ttisfunction(tm)) { if (ttisfunction(tm)) {
luaT_callTM(L, tm, t, key, val, 0); luaT_callTM(L, tm, t, key, val, 0);
return; return;
} }
t = tm; /* else repeat assignment over 'tm' */ t = tm; /* else repeat assignment over 'tm' */
if (luaV_fastset(L, t, key, slot, luaH_get, val))
return; /* done */
/* else loop */
} }
luaG_runerror(L, "settable chain too long; possible loop"); luaG_runerror(L, "'__newindex' chain too long; possible loop");
} }
@ -443,6 +456,17 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
#define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
/* copy strings in stack from top - n up to top - 1 to buffer */
static void copy2buff (StkId top, int n, char *buff) {
size_t tl = 0; /* size already copied */
do {
size_t l = vslen(top - n); /* length of string being copied */
memcpy(buff + tl, svalue(top - n), l * sizeof(char));
tl += l;
} while (--n > 0);
}
/* /*
** Main operation for concatenation: concat 'total' values in the stack, ** Main operation for concatenation: concat 'total' values in the stack,
** from 'L->top - total' up to 'L->top - 1'. ** from 'L->top - total' up to 'L->top - 1'.
@ -462,24 +486,24 @@ void luaV_concat (lua_State *L, int total) {
else { else {
/* at least two non-empty string values; get as many as possible */ /* at least two non-empty string values; get as many as possible */
size_t tl = vslen(top - 1); size_t tl = vslen(top - 1);
char *buffer; TString *ts;
int i; /* collect total length and number of strings */
/* collect total length */ for (n = 1; n < total && tostring(L, top - n - 1); n++) {
for (i = 1; i < total && tostring(L, top-i-1); i++) { size_t l = vslen(top - n - 1);
size_t l = vslen(top - i - 1);
if (l >= (MAX_SIZE/sizeof(char)) - tl) if (l >= (MAX_SIZE/sizeof(char)) - tl)
luaG_runerror(L, "string length overflow"); luaG_runerror(L, "string length overflow");
tl += l; tl += l;
} }
buffer = luaZ_openspace(L, &G(L)->buff, tl); if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */
tl = 0; char buff[LUAI_MAXSHORTLEN];
n = i; copy2buff(top, n, buff); /* copy strings to buffer */
do { /* copy all strings to buffer */ ts = luaS_newlstr(L, buff, tl);
size_t l = vslen(top - i); }
memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); else { /* long string; copy strings directly to final result */
tl += l; ts = luaS_createlngstrobj(L, tl);
} while (--i > 0); copy2buff(top, n, getstr(ts));
setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); /* create result */ }
setsvalue2s(L, top - n, ts); /* create result */
} }
total -= n-1; /* got 'n' strings to create 1 new */ total -= n-1; /* got 'n' strings to create 1 new */
L->top -= n-1; /* popped 'n' strings and pushed one */ L->top -= n-1; /* popped 'n' strings and pushed one */
@ -700,27 +724,20 @@ void luaV_finishOp (lua_State *L) {
** some macros for common tasks in 'luaV_execute' ** some macros for common tasks in 'luaV_execute'
*/ */
#if !defined(luai_runtimecheck)
#define luai_runtimecheck(L, c) /* void */
#endif
#define RA(i) (base+GETARG_A(i)) #define RA(i) (base+GETARG_A(i))
/* to be used after possible stack reallocation */
#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
#define KBx(i) \
(k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
/* execute a jump instruction */ /* execute a jump instruction */
#define dojump(ci,i,e) \ #define dojump(ci,i,e) \
{ int a = GETARG_A(i); \ { int a = GETARG_A(i); \
if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \ if (a != 0) luaF_close(L, ci->u.l.base + a - 1); \
ci->u.l.savedpc += GETARG_sBx(i) + e; } ci->u.l.savedpc += GETARG_sBx(i) + e; }
/* for test instructions, execute the jump instruction that follows it */ /* for test instructions, execute the jump instruction that follows it */
@ -730,38 +747,58 @@ void luaV_finishOp (lua_State *L) {
#define Protect(x) { {x;}; base = ci->u.l.base; } #define Protect(x) { {x;}; base = ci->u.l.base; }
#define checkGC(L,c) \ #define checkGC(L,c) \
Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \ { luaC_condGC(L, L->top = (c), /* limit of live values */ \
luaC_step(L); \ Protect(L->top = ci->top)); /* restore top */ \
L->top = ci->top;}) /* restore top */ \ luai_threadyield(L); }
luai_threadyield(L); )
/* fetch an instruction and prepare its execution */
#define vmfetch() { \
i = *(ci->u.l.savedpc++); \
if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) \
Protect(luaG_traceexec(L)); \
ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \
lua_assert(base == ci->u.l.base); \
lua_assert(base <= L->top && L->top < L->stack + L->stacksize); \
}
#define vmdispatch(o) switch(o) #define vmdispatch(o) switch(o)
#define vmcase(l) case l: #define vmcase(l) case l:
#define vmbreak break #define vmbreak break
/*
** copy of 'luaV_gettable', but protecting the call to potential
** metamethod (which can reallocate the stack)
*/
#define gettableProtected(L,t,k,v) { const TValue *slot; \
if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \
else Protect(luaV_finishget(L,t,k,v,slot)); }
/* same for 'luaV_settable' */
#define settableProtected(L,t,k,v) { const TValue *slot; \
if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
Protect(luaV_finishset(L,t,k,v,slot)); }
void luaV_execute (lua_State *L) { void luaV_execute (lua_State *L) {
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
LClosure *cl; LClosure *cl;
TValue *k; TValue *k;
StkId base; StkId base;
ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */
newframe: /* reentry point when frame changes (call/return) */ newframe: /* reentry point when frame changes (call/return) */
lua_assert(ci == L->ci); lua_assert(ci == L->ci);
cl = clLvalue(ci->func); cl = clLvalue(ci->func); /* local reference to function's closure */
k = cl->p->k; k = cl->p->k; /* local reference to function's constant table */
base = ci->u.l.base; base = ci->u.l.base; /* local copy of function's base */
/* main loop of interpreter */ /* main loop of interpreter */
for (;;) { for (;;) {
Instruction i = *(ci->u.l.savedpc++); Instruction i;
StkId ra; StkId ra;
if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && vmfetch();
(--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
Protect(luaG_traceexec(L));
}
/* WARNING: several calls may realloc the stack and invalidate 'ra' */
ra = RA(i);
lua_assert(base == ci->u.l.base);
lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
vmdispatch (GET_OPCODE(i)) { vmdispatch (GET_OPCODE(i)) {
vmcase(OP_MOVE) { vmcase(OP_MOVE) {
setobjs2s(L, ra, RB(i)); setobjs2s(L, ra, RB(i));
@ -797,17 +834,22 @@ void luaV_execute (lua_State *L) {
vmbreak; vmbreak;
} }
vmcase(OP_GETTABUP) { vmcase(OP_GETTABUP) {
int b = GETARG_B(i); TValue *upval = cl->upvals[GETARG_B(i)]->v;
Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra)); TValue *rc = RKC(i);
gettableProtected(L, upval, rc, ra);
vmbreak; vmbreak;
} }
vmcase(OP_GETTABLE) { vmcase(OP_GETTABLE) {
Protect(luaV_gettable(L, RB(i), RKC(i), ra)); StkId rb = RB(i);
TValue *rc = RKC(i);
gettableProtected(L, rb, rc, ra);
vmbreak; vmbreak;
} }
vmcase(OP_SETTABUP) { vmcase(OP_SETTABUP) {
int a = GETARG_A(i); TValue *upval = cl->upvals[GETARG_A(i)]->v;
Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i))); TValue *rb = RKB(i);
TValue *rc = RKC(i);
settableProtected(L, upval, rb, rc);
vmbreak; vmbreak;
} }
vmcase(OP_SETUPVAL) { vmcase(OP_SETUPVAL) {
@ -817,7 +859,9 @@ void luaV_execute (lua_State *L) {
vmbreak; vmbreak;
} }
vmcase(OP_SETTABLE) { vmcase(OP_SETTABLE) {
Protect(luaV_settable(L, ra, RKB(i), RKC(i))); TValue *rb = RKB(i);
TValue *rc = RKC(i);
settableProtected(L, ra, rb, rc);
vmbreak; vmbreak;
} }
vmcase(OP_NEWTABLE) { vmcase(OP_NEWTABLE) {
@ -831,9 +875,15 @@ void luaV_execute (lua_State *L) {
vmbreak; vmbreak;
} }
vmcase(OP_SELF) { vmcase(OP_SELF) {
const TValue *aux;
StkId rb = RB(i); StkId rb = RB(i);
setobjs2s(L, ra+1, rb); TValue *rc = RKC(i);
Protect(luaV_gettable(L, rb, RKC(i), ra)); TString *key = tsvalue(rc); /* key must be a string */
setobjs2s(L, ra + 1, rb);
if (luaV_fastget(L, rb, key, aux, luaH_getstr)) {
setobj2s(L, ra, aux);
}
else Protect(luaV_finishget(L, rb, rc, ra, aux));
vmbreak; vmbreak;
} }
vmcase(OP_ADD) { vmcase(OP_ADD) {
@ -1020,7 +1070,7 @@ void luaV_execute (lua_State *L) {
StkId rb; StkId rb;
L->top = base + c + 1; /* mark the end of concat operands */ L->top = base + c + 1; /* mark the end of concat operands */
Protect(luaV_concat(L, c - b + 1)); Protect(luaV_concat(L, c - b + 1));
ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */ ra = RA(i); /* 'luaV_concat' may invoke TMs and move the stack */
rb = base + b; rb = base + b;
setobjs2s(L, ra, rb); setobjs2s(L, ra, rb);
checkGC(L, (ra >= rb ? ra + 1 : rb)); checkGC(L, (ra >= rb ? ra + 1 : rb));
@ -1035,7 +1085,7 @@ void luaV_execute (lua_State *L) {
TValue *rb = RKB(i); TValue *rb = RKB(i);
TValue *rc = RKC(i); TValue *rc = RKC(i);
Protect( Protect(
if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i)) if (luaV_equalobj(L, rb, rc) != GETARG_A(i))
ci->u.l.savedpc++; ci->u.l.savedpc++;
else else
donextjump(ci); donextjump(ci);
@ -1082,12 +1132,12 @@ void luaV_execute (lua_State *L) {
int nresults = GETARG_C(i) - 1; int nresults = GETARG_C(i) - 1;
if (b != 0) L->top = ra+b; /* else previous instruction set top */ if (b != 0) L->top = ra+b; /* else previous instruction set top */
if (luaD_precall(L, ra, nresults)) { /* C function? */ if (luaD_precall(L, ra, nresults)) { /* C function? */
if (nresults >= 0) L->top = ci->top; /* adjust results */ if (nresults >= 0)
base = ci->u.l.base; L->top = ci->top; /* adjust results */
Protect((void)0); /* update 'base' */
} }
else { /* Lua function */ else { /* Lua function */
ci = L->ci; ci = L->ci;
ci->callstatus |= CIST_REENTRY;
goto newframe; /* restart luaV_execute over new Lua function */ goto newframe; /* restart luaV_execute over new Lua function */
} }
vmbreak; vmbreak;
@ -1096,8 +1146,9 @@ void luaV_execute (lua_State *L) {
int b = GETARG_B(i); int b = GETARG_B(i);
if (b != 0) L->top = ra+b; /* else previous instruction set top */ if (b != 0) L->top = ra+b; /* else previous instruction set top */
lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */
base = ci->u.l.base; Protect((void)0); /* update 'base' */
}
else { else {
/* tail call: put called frame (n) in place of caller one (o) */ /* tail call: put called frame (n) in place of caller one (o) */
CallInfo *nci = L->ci; /* called frame */ CallInfo *nci = L->ci; /* called frame */
@ -1125,8 +1176,8 @@ void luaV_execute (lua_State *L) {
vmcase(OP_RETURN) { vmcase(OP_RETURN) {
int b = GETARG_B(i); int b = GETARG_B(i);
if (cl->p->sizep > 0) luaF_close(L, base); if (cl->p->sizep > 0) luaF_close(L, base);
b = luaD_poscall(L, ra, (b != 0 ? b - 1 : L->top - ra)); b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra)));
if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */ if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */
return; /* external invocation: return */ return; /* external invocation: return */
else { /* invocation via reentry: continue execution */ else { /* invocation via reentry: continue execution */
ci = L->ci; ci = L->ci;
@ -1139,7 +1190,7 @@ void luaV_execute (lua_State *L) {
vmcase(OP_FORLOOP) { vmcase(OP_FORLOOP) {
if (ttisinteger(ra)) { /* integer loop? */ if (ttisinteger(ra)) { /* integer loop? */
lua_Integer step = ivalue(ra + 2); lua_Integer step = ivalue(ra + 2);
lua_Integer idx = ivalue(ra) + step; /* increment index */ lua_Integer idx = intop(+, ivalue(ra), step); /* increment index */
lua_Integer limit = ivalue(ra + 1); lua_Integer limit = ivalue(ra + 1);
if ((0 < step) ? (idx <= limit) : (limit <= idx)) { if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
@ -1171,7 +1222,7 @@ void luaV_execute (lua_State *L) {
/* all values are integer */ /* all values are integer */
lua_Integer initv = (stopnow ? 0 : ivalue(init)); lua_Integer initv = (stopnow ? 0 : ivalue(init));
setivalue(plimit, ilimit); setivalue(plimit, ilimit);
setivalue(init, initv - ivalue(pstep)); setivalue(init, intop(-, initv, ivalue(pstep)));
} }
else { /* try making all values floats */ else { /* try making all values floats */
lua_Number ninit; lua_Number nlimit; lua_Number nstep; lua_Number ninit; lua_Number nlimit; lua_Number nstep;
@ -1194,7 +1245,7 @@ void luaV_execute (lua_State *L) {
setobjs2s(L, cb+1, ra+1); setobjs2s(L, cb+1, ra+1);
setobjs2s(L, cb, ra); setobjs2s(L, cb, ra);
L->top = cb + 3; /* func. + 2 args (state and index) */ L->top = cb + 3; /* func. + 2 args (state and index) */
Protect(luaD_call(L, cb, GETARG_C(i), 1)); Protect(luaD_call(L, cb, GETARG_C(i)));
L->top = ci->top; L->top = ci->top;
i = *(ci->u.l.savedpc++); /* go to next instruction */ i = *(ci->u.l.savedpc++); /* go to next instruction */
ra = RA(i); ra = RA(i);
@ -1219,11 +1270,10 @@ void luaV_execute (lua_State *L) {
lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
c = GETARG_Ax(*ci->u.l.savedpc++); c = GETARG_Ax(*ci->u.l.savedpc++);
} }
luai_runtimecheck(L, ttistable(ra));
h = hvalue(ra); h = hvalue(ra);
last = ((c-1)*LFIELDS_PER_FLUSH) + n; last = ((c-1)*LFIELDS_PER_FLUSH) + n;
if (last > h->sizearray) /* needs more space? */ if (last > h->sizearray) /* needs more space? */
luaH_resizearray(L, h, last); /* pre-allocate it at once */ luaH_resizearray(L, h, last); /* preallocate it at once */
for (; n > 0; n--) { for (; n > 0; n--) {
TValue *val = ra+n; TValue *val = ra+n;
luaH_setint(L, h, last--, val); luaH_setint(L, h, last--, val);
@ -1243,23 +1293,21 @@ void luaV_execute (lua_State *L) {
vmbreak; vmbreak;
} }
vmcase(OP_VARARG) { vmcase(OP_VARARG) {
int b = GETARG_B(i) - 1; int b = GETARG_B(i) - 1; /* required results */
int j; int j;
int n = cast_int(base - ci->func) - cl->p->numparams - 1; int n = cast_int(base - ci->func) - cl->p->numparams - 1;
if (n < 0) /* less arguments than parameters? */
n = 0; /* no vararg arguments */
if (b < 0) { /* B == 0? */ if (b < 0) { /* B == 0? */
b = n; /* get all var. arguments */ b = n; /* get all var. arguments */
Protect(luaD_checkstack(L, n)); Protect(luaD_checkstack(L, n));
ra = RA(i); /* previous call may change the stack */ ra = RA(i); /* previous call may change the stack */
L->top = ra + n; L->top = ra + n;
} }
for (j = 0; j < b; j++) { for (j = 0; j < b && j < n; j++)
if (j < n) { setobjs2s(L, ra + j, base - n + j);
setobjs2s(L, ra + j, base - n + j); for (; j < b; j++) /* complete required results with nil */
} setnilvalue(ra + j);
else {
setnilvalue(ra + j);
}
}
vmbreak; vmbreak;
} }
vmcase(OP_EXTRAARG) { vmcase(OP_EXTRAARG) {

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.h,v 2.35 2015/02/20 14:27:53 roberto Exp $ ** $Id: lvm.h,v 2.41.1.1 2017/04/19 17:20:42 roberto Exp $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -48,15 +48,60 @@
#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
/*
** fast track for 'gettable': if 't' is a table and 't[k]' is not nil,
** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise,
** return 0 (meaning it will have to check metamethod) with 'slot'
** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise).
** 'f' is the raw get function to use.
*/
#define luaV_fastget(L,t,k,slot,f) \
(!ttistable(t) \
? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
: (slot = f(hvalue(t), k), /* else, do raw access */ \
!ttisnil(slot))) /* result not nil? */
/*
** standard implementation for 'gettable'
*/
#define luaV_gettable(L,t,k,v) { const TValue *slot; \
if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \
else luaV_finishget(L,t,k,v,slot); }
/*
** Fast track for set table. If 't' is a table and 't[k]' is not nil,
** call GC barrier, do a raw 't[k]=v', and return true; otherwise,
** return false with 'slot' equal to NULL (if 't' is not a table) or
** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro
** returns true, there is no need to 'invalidateTMcache', because the
** call is not creating a new entry.
*/
#define luaV_fastset(L,t,k,slot,f,v) \
(!ttistable(t) \
? (slot = NULL, 0) \
: (slot = f(hvalue(t), k), \
ttisnil(slot) ? 0 \
: (luaC_barrierback(L, hvalue(t), v), \
setobj2t(L, cast(TValue *,slot), v), \
1)))
#define luaV_settable(L,t,k,v) { const TValue *slot; \
if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
luaV_finishset(L,t,k,v,slot); }
LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
StkId val); StkId val, const TValue *slot);
LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
StkId val); StkId val, const TValue *slot);
LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_finishOp (lua_State *L);
LUAI_FUNC void luaV_execute (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L);
LUAI_FUNC void luaV_concat (lua_State *L, int total); LUAI_FUNC void luaV_concat (lua_State *L, int total);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lzio.c,v 1.36 2014/11/02 19:19:04 roberto Exp $ ** $Id: lzio.c,v 1.37.1.1 2017/04/19 17:20:42 roberto Exp $
** Buffered streams ** Buffered streams
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -66,13 +66,3 @@ size_t luaZ_read (ZIO *z, void *b, size_t n) {
return 0; return 0;
} }
/* ------------------------------------------------------------------------ */
char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
if (n > buff->buffsize) {
if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
luaZ_resizebuffer(L, buff, n);
}
return buff->buffer;
}

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lzio.h,v 1.30 2014/12/19 17:26:14 roberto Exp $ ** $Id: lzio.h,v 1.31.1.1 2017/04/19 17:20:42 roberto Exp $
** Buffered streams ** Buffered streams
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -44,7 +44,6 @@ typedef struct Mbuffer {
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
void *data); void *data);
LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: luaconf.h,v 1.251 2015/05/20 17:39:23 roberto Exp $ ** $Id: luaconf.h,v 1.259.1.1 2017/04/19 17:29:57 roberto Exp $
** Configuration file for Lua ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -145,7 +145,7 @@
#if !defined(LUA_FLOAT_TYPE) #if !defined(LUA_FLOAT_TYPE)
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
#endif /* } */ #endif
/* }================================================================== */ /* }================================================================== */
@ -158,6 +158,18 @@
** =================================================================== ** ===================================================================
*/ */
/*
** LUA_PATH_SEP is the character that separates templates in a path.
** LUA_PATH_MARK is the string that marks the substitution points in a
** template.
** LUA_EXEC_DIR in a Windows path is replaced by the executable's
** directory.
*/
#define LUA_PATH_SEP ";"
#define LUA_PATH_MARK "?"
#define LUA_EXEC_DIR "!"
/* /*
@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
** Lua libraries. ** Lua libraries.
@ -404,7 +416,7 @@
/* /*
@@ LUA_NUMBER is the floating-point type used by Lua. @@ LUA_NUMBER is the floating-point type used by Lua.
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' @@ LUAI_UACNUMBER is the result of a 'default argument promotion'
@@ over a floating number. @@ over a floating number.
@@ l_mathlim(x) corrects limit name 'x' to the proper float type @@ l_mathlim(x) corrects limit name 'x' to the proper float type
** by prefixing it with one of FLT/DBL/LDBL. ** by prefixing it with one of FLT/DBL/LDBL.
@ -412,9 +424,34 @@
@@ LUA_NUMBER_FMT is the format for writing floats. @@ LUA_NUMBER_FMT is the format for writing floats.
@@ lua_number2str converts a float to a string. @@ lua_number2str converts a float to a string.
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations. @@ l_mathop allows the addition of an 'l' or 'f' to all math operations.
@@ l_floor takes the floor of a float.
@@ lua_str2number converts a decimal numeric string to a number. @@ lua_str2number converts a decimal numeric string to a number.
*/ */
/* The following definitions are good for most cases here */
#define l_floor(x) (l_mathop(floor)(x))
#define lua_number2str(s,sz,n) \
l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n))
/*
@@ lua_numbertointeger converts a float number to an integer, or
** returns 0 if float is not within the range of a lua_Integer.
** (The range comparisons are tricky because of rounding. The tests
** here assume a two-complement representation, where MININTEGER always
** has an exact representation as a float; MAXINTEGER may not have one,
** and therefore its conversion to float may have an ill-defined value.)
*/
#define lua_numbertointeger(n,p) \
((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
(*(p) = (LUA_INTEGER)(n), 1))
/* now the variable definitions */
#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ #if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */
#define LUA_NUMBER float #define LUA_NUMBER float
@ -468,32 +505,13 @@
#endif /* } */ #endif /* } */
#define l_floor(x) (l_mathop(floor)(x))
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
/*
@@ lua_numbertointeger converts a float number to an integer, or
** returns 0 if float is not within the range of a lua_Integer.
** (The range comparisons are tricky because of rounding. The tests
** here assume a two-complement representation, where MININTEGER always
** has an exact representation as a float; MAXINTEGER may not have one,
** and therefore its conversion to float may have an ill-defined value.)
*/
#define lua_numbertointeger(n,p) \
((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
(*(p) = (LUA_INTEGER)(n), 1))
/* /*
@@ LUA_INTEGER is the integer type used by Lua. @@ LUA_INTEGER is the integer type used by Lua.
** **
@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
** **
@@ LUAI_UACINT is the result of an 'usual argument conversion' @@ LUAI_UACINT is the result of a 'default argument promotion'
@@ over a lUA_INTEGER. @@ over a lUA_INTEGER.
@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
@@ LUA_INTEGER_FMT is the format for writing integers. @@ LUA_INTEGER_FMT is the format for writing integers.
@ -506,10 +524,12 @@
/* The following definitions are good for most cases here */ /* The following definitions are good for most cases here */
#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" #define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n))
#define LUAI_UACINT LUA_INTEGER #define LUAI_UACINT LUA_INTEGER
#define lua_integer2str(s,sz,n) \
l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n))
/* /*
** use LUAI_UACINT here to avoid problems with promotions (which ** use LUAI_UACINT here to avoid problems with promotions (which
** can turn a comparison between unsigneds into a signed comparison) ** can turn a comparison between unsigneds into a signed comparison)
@ -537,6 +557,7 @@
#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ #elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */
/* use presence of macro LLONG_MAX as proxy for C99 compliance */
#if defined(LLONG_MAX) /* { */ #if defined(LLONG_MAX) /* { */
/* use ISO C99 stuff */ /* use ISO C99 stuff */
@ -577,6 +598,17 @@
** =================================================================== ** ===================================================================
*/ */
/*
@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89.
** (All uses in Lua have only one format item.)
*/
#if !defined(LUA_USE_C89)
#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i)
#else
#define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i))
#endif
/* /*
@@ lua_strx2number converts an hexadecimal numeric string to a number. @@ lua_strx2number converts an hexadecimal numeric string to a number.
** In C99, 'strtod' does that conversion. Otherwise, you can ** In C99, 'strtod' does that conversion. Otherwise, you can
@ -584,10 +616,17 @@
** implementation. ** implementation.
*/ */
#if !defined(LUA_USE_C89) #if !defined(LUA_USE_C89)
#define lua_strx2number(s,p) lua_str2number(s,p) #define lua_strx2number(s,p) lua_str2number(s,p)
#endif #endif
/*
@@ lua_pointer2str converts a pointer to a readable string in a
** non-specified way.
*/
#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p)
/* /*
@@ lua_number2strx converts a float to an hexadecimal numeric string. @@ lua_number2strx converts a float to an hexadecimal numeric string.
** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that.
@ -595,7 +634,8 @@
** provide its own implementation. ** provide its own implementation.
*/ */
#if !defined(LUA_USE_C89) #if !defined(LUA_USE_C89)
#define lua_number2strx(L,b,f,n) sprintf(b,f,n) #define lua_number2strx(L,b,sz,f,n) \
((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n)))
#endif #endif
@ -711,11 +751,11 @@
/* /*
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
** CHANGE it if it uses too much C-stack space. (For long double, ** CHANGE it if it uses too much C-stack space. (For long double,
** 'string.format("%.99f", 1e4932)' needs ~5030 bytes, so a ** 'string.format("%.99f", -1e4932)' needs 5034 bytes, so a
** smaller buffer would force a memory allocation for each call to ** smaller buffer would force a memory allocation for each call to
** 'string.format'.) ** 'string.format'.)
*/ */
#if defined(LUA_FLOAT_LONGDOUBLE) #if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE
#define LUAL_BUFFERSIZE 8192 #define LUAL_BUFFERSIZE 8192
#else #else
#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) #define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer)))

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 roberto Exp $ ** $Id: lualib.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $
** Lua standard libraries ** Lua standard libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,6 +11,9 @@
#include "lua.h" #include "lua.h"
/* version suffix for environment variable names */
#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
LUAMOD_API int (luaopen_base) (lua_State *L); LUAMOD_API int (luaopen_base) (lua_State *L);