mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-31 17:52:27 +00:00
Bring Lua to 5.4.6. (#1214)
This essentially re-does the work of #875 on top of master. This is what I did to check that Cosmo's Lua extensions still worked: ``` $ build/bootstrap/make MODE=aarch64 o/aarch64/third_party/lua/lua $ ape o/aarch64/third_party/lua/lua >: 10 10 >: 010 8 >: 0b10 2 >: string.byte("\e") 27 >: "Hello, %s" % {"world"} Hello, world >: "*" * 3 *** ``` `luaL_traceback2` was used to show the stack trace with parameter values; it's used in `LuaCallWithTrace`, which is used in Redbean to run Lua code. You should be able to see the extended stack trace by running something like this: `redbean -e "function a(b)c()end a(2)"` (with "params" indicating the extended stack trace): ``` stack traceback: [string "function a(b)c()end a(2)"]:1: in function 'a', params: b = 2; [string "function a(b)c()end a(2)"]:1: in main chunk ``` @pkulchenko confirmed that I get the expected result with the updated code. This is what I did to check that Lua itself still worked: ``` $ cd third_party/lua/test/ $ ape ../../../o/aarch64/third_party/lua/lua all.lua ``` There's one test failure, in `files.lua`: ``` ***** FILE 'files.lua'***** testing i/o ../../../o/aarch64/third_party/lua/lua: files.lua:84: assertion failed! stack traceback: [C]: in function 'assert' files.lua:84: in main chunk (...tail calls...) all.lua:195: in main chunk [C]: in ? .>>> closing state <<< ``` That isn't a result of these changes; the same test is failing in master. The failure is here: ```lua if not _port then -- invalid seek local status, msg, code = io.stdin:seek("set", 1000) assert(not status and type(msg) == "string" and type(code) == "number") end ``` The test expects a seek to offset 1,000 on stdin to fail — but it doesn't. `status` ends up being the new offset rather than `nil`. If I comment out that one test, the remaining tests succeed.
This commit is contained in:
parent
3a599bfbe1
commit
0dbf01bf1d
90 changed files with 2741 additions and 1376 deletions
292
third_party/lua/lapi.c
vendored
292
third_party/lua/lapi.c
vendored
|
@ -3,7 +3,7 @@
|
|||
╚──────────────────────────────────────────────────────────────────────────────╝
|
||||
│ │
|
||||
│ Lua │
|
||||
│ Copyright © 2004-2021 Lua.org, PUC-Rio. │
|
||||
│ Copyright © 2004-2023 Lua.org, PUC-Rio. │
|
||||
│ │
|
||||
│ Permission is hereby granted, free of charge, to any person obtaining │
|
||||
│ a copy of this software and associated documentation files (the │
|
||||
|
@ -27,6 +27,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#define lapi_c
|
||||
#define LUA_CORE
|
||||
|
||||
#include "third_party/lua/lapi.h"
|
||||
#include "third_party/lua/ldebug.h"
|
||||
#include "third_party/lua/ldo.h"
|
||||
|
@ -66,45 +67,57 @@ const char lua_ident[] =
|
|||
#define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
|
||||
|
||||
|
||||
/*
|
||||
** Convert an acceptable index to a pointer to its respective value.
|
||||
** Non-valid indices return the special nil value 'G(L)->nilvalue'.
|
||||
*/
|
||||
static TValue *index2value (lua_State *L, int idx) {
|
||||
CallInfo *ci = L->ci;
|
||||
if (idx > 0) {
|
||||
StkId o = ci->func + idx;
|
||||
api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index");
|
||||
if (o >= L->top) return &G(L)->nilvalue;
|
||||
StkId o = ci->func.p + idx;
|
||||
api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
|
||||
if (o >= L->top.p) return &G(L)->nilvalue;
|
||||
else return s2v(o);
|
||||
}
|
||||
else if (!ispseudo(idx)) { /* negative index */
|
||||
api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
|
||||
return s2v(L->top + idx);
|
||||
api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
|
||||
"invalid index");
|
||||
return s2v(L->top.p + idx);
|
||||
}
|
||||
else if (idx == LUA_REGISTRYINDEX)
|
||||
return &G(L)->l_registry;
|
||||
else { /* upvalues */
|
||||
idx = LUA_REGISTRYINDEX - idx;
|
||||
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
|
||||
if (ttislcf(s2v(ci->func))) /* light C function? */
|
||||
return &G(L)->nilvalue; /* it has no upvalues */
|
||||
else {
|
||||
CClosure *func = clCvalue(s2v(ci->func));
|
||||
if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */
|
||||
CClosure *func = clCvalue(s2v(ci->func.p));
|
||||
return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
|
||||
: &G(L)->nilvalue;
|
||||
}
|
||||
else { /* light C function or Lua function (through a hook)?) */
|
||||
api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function");
|
||||
return &G(L)->nilvalue; /* no upvalues */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static StkId index2stack (lua_State *L, int idx) {
|
||||
|
||||
/*
|
||||
** Convert a valid actual index (not a pseudo-index) to its address.
|
||||
*/
|
||||
l_sinline StkId index2stack (lua_State *L, int idx) {
|
||||
CallInfo *ci = L->ci;
|
||||
if (idx > 0) {
|
||||
StkId o = ci->func + idx;
|
||||
api_check(L, o < L->top, "unacceptable index");
|
||||
StkId o = ci->func.p + idx;
|
||||
api_check(L, o < L->top.p, "invalid index");
|
||||
return o;
|
||||
}
|
||||
else { /* non-positive index */
|
||||
api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
|
||||
api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
|
||||
"invalid index");
|
||||
api_check(L, !ispseudo(idx), "invalid index");
|
||||
return L->top + idx;
|
||||
return L->top.p + idx;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,17 +128,12 @@ LUA_API int lua_checkstack (lua_State *L, int n) {
|
|||
lua_lock(L);
|
||||
ci = L->ci;
|
||||
api_check(L, n >= 0, "negative 'n'");
|
||||
if (L->stack_last - L->top > n) /* stack large enough? */
|
||||
if (L->stack_last.p - L->top.p > n) /* stack large enough? */
|
||||
res = 1; /* yes; check is OK */
|
||||
else { /* no; need to grow stack */
|
||||
int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
|
||||
if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
|
||||
res = 0; /* no */
|
||||
else /* try to grow stack */
|
||||
else /* need to grow stack */
|
||||
res = luaD_growstack(L, n, 0);
|
||||
}
|
||||
if (res && ci->top < L->top + n)
|
||||
ci->top = L->top + n; /* adjust frame top */
|
||||
if (res && ci->top.p < L->top.p + n)
|
||||
ci->top.p = L->top.p + n; /* adjust frame top */
|
||||
lua_unlock(L);
|
||||
return res;
|
||||
}
|
||||
|
@ -142,11 +150,11 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
|
|||
lua_lock(to);
|
||||
api_checknelems(from, n);
|
||||
api_check(from, G(from) == G(to), "moving among independent states");
|
||||
api_check(from, to->ci->top - to->top >= n, "stack overflow");
|
||||
from->top -= n;
|
||||
api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow");
|
||||
from->top.p -= n;
|
||||
for (i = 0; i < n; i++) {
|
||||
setobjs2s(to, to->top, from->top + i);
|
||||
to->top++; /* stack already checked by previous 'api_check' */
|
||||
setobjs2s(to, to->top.p, from->top.p + i);
|
||||
to->top.p++; /* stack already checked by previous 'api_check' */
|
||||
}
|
||||
lua_unlock(to);
|
||||
}
|
||||
|
@ -199,7 +207,7 @@ LUA_API lua_Number lua_version (lua_State *L) {
|
|||
LUA_API int lua_absindex (lua_State *L, int idx) {
|
||||
return (idx > 0 || ispseudo(idx))
|
||||
? idx
|
||||
: cast_int(L->top - L->ci->func) + idx;
|
||||
: cast_int(L->top.p - L->ci->func.p) + idx;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -210,7 +218,7 @@ LUA_API int lua_absindex (lua_State *L, int idx) {
|
|||
* particular, 0 means an empty stack.
|
||||
*/
|
||||
LUA_API int lua_gettop (lua_State *L) {
|
||||
return cast_int(L->top - (L->ci->func + 1));
|
||||
return cast_int(L->top.p - (L->ci->func.p + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -229,24 +237,24 @@ LUA_API void lua_settop (lua_State *L, int idx) {
|
|||
ptrdiff_t diff; /* difference for new top */
|
||||
lua_lock(L);
|
||||
ci = L->ci;
|
||||
func = ci->func;
|
||||
func = ci->func.p;
|
||||
if (idx >= 0) {
|
||||
api_check(L, idx <= ci->top - (func + 1), "new top too large");
|
||||
diff = ((func + 1) + idx) - L->top;
|
||||
api_check(L, idx <= ci->top.p - (func + 1), "new top too large");
|
||||
diff = ((func + 1) + idx) - L->top.p;
|
||||
for (; diff > 0; diff--)
|
||||
setnilvalue(s2v(L->top++)); /* clear new slots */
|
||||
setnilvalue(s2v(L->top.p++)); /* clear new slots */
|
||||
}
|
||||
else {
|
||||
api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
|
||||
api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
|
||||
diff = idx + 1; /* will "subtract" index (as it is negative) */
|
||||
}
|
||||
api_check(L, L->tbclist < L->top, "previous pop of an unclosed slot");
|
||||
newtop = L->top + diff;
|
||||
if (diff < 0 && L->tbclist >= newtop) {
|
||||
api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot");
|
||||
newtop = L->top.p + diff;
|
||||
if (diff < 0 && L->tbclist.p >= newtop) {
|
||||
lua_assert(hastocloseCfunc(ci->nresults));
|
||||
luaF_close(L, newtop, CLOSEKTOP, 0);
|
||||
newtop = luaF_close(L, newtop, CLOSEKTOP, 0);
|
||||
}
|
||||
L->top = newtop; /* correct top only after closing any upvalue */
|
||||
L->top.p = newtop; /* correct top only after closing any upvalue */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
@ -255,10 +263,9 @@ LUA_API void lua_closeslot (lua_State *L, int idx) {
|
|||
StkId level;
|
||||
lua_lock(L);
|
||||
level = index2stack(L, idx);
|
||||
api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist == level,
|
||||
api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level,
|
||||
"no variable to close at given level");
|
||||
luaF_close(L, level, CLOSEKTOP, 0);
|
||||
level = index2stack(L, idx); /* stack may be moved */
|
||||
level = luaF_close(L, level, CLOSEKTOP, 0);
|
||||
setnilvalue(s2v(level));
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
@ -270,7 +277,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) {
|
|||
** Note that we move(copy) only the value inside the stack.
|
||||
** (We do not move additional fields that may exist.)
|
||||
*/
|
||||
static void reverse (lua_State *L, StkId from, StkId to) {
|
||||
l_sinline void reverse (lua_State *L, StkId from, StkId to) {
|
||||
for (; from < to; from++, to--) {
|
||||
TValue temp;
|
||||
setobj(L, &temp, s2v(from));
|
||||
|
@ -287,7 +294,7 @@ static void reverse (lua_State *L, StkId from, StkId to) {
|
|||
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
|
||||
StkId p, t, m;
|
||||
lua_lock(L);
|
||||
t = L->top - 1; /* end of stack segment being rotated */
|
||||
t = L->top.p - 1; /* end of stack segment being rotated */
|
||||
p = index2stack(L, idx); /* start of segment */
|
||||
api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
|
||||
m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
|
||||
|
@ -306,7 +313,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
|
|||
api_check(L, isvalid(L, to), "invalid index");
|
||||
setobj(L, to, fr);
|
||||
if (isupvalue(toidx)) /* function upvalue? */
|
||||
luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr);
|
||||
luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr);
|
||||
/* LUA_REGISTRYINDEX does not need gc barrier
|
||||
(collector revisits it before finishing collection) */
|
||||
lua_unlock(L);
|
||||
|
@ -315,7 +322,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
|
|||
|
||||
LUA_API void lua_pushvalue (lua_State *L, int idx) {
|
||||
lua_lock(L);
|
||||
setobj2s(L, L->top, index2value(L, idx));
|
||||
setobj2s(L, L->top.p, index2value(L, idx));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
@ -384,12 +391,12 @@ LUA_API void lua_arith (lua_State *L, int op) {
|
|||
api_checknelems(L, 2); /* all other operations expect two operands */
|
||||
else { /* for unary operations, add fake 2nd operand */
|
||||
api_checknelems(L, 1);
|
||||
setobjs2s(L, L->top, L->top - 1);
|
||||
setobjs2s(L, L->top.p, L->top.p - 1);
|
||||
api_incr_top(L);
|
||||
}
|
||||
/* first operand at top - 2, second at top - 1; result go to top - 2 */
|
||||
luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2);
|
||||
L->top--; /* remove second operand */
|
||||
luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
|
||||
L->top.p--; /* remove second operand */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
@ -415,7 +422,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
|
|||
|
||||
|
||||
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
|
||||
size_t sz = luaO_str2num(s, s2v(L->top));
|
||||
size_t sz = luaO_str2num(s, s2v(L->top.p));
|
||||
if (sz != 0)
|
||||
api_incr_top(L);
|
||||
return sz;
|
||||
|
@ -490,7 +497,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
|
|||
}
|
||||
|
||||
|
||||
static void *touserdata (const TValue *o) {
|
||||
l_sinline void *touserdata (const TValue *o) {
|
||||
switch (ttype(o)) {
|
||||
case LUA_TUSERDATA: return getudatamem(uvalue(o));
|
||||
case LUA_TLIGHTUSERDATA: return pvalue(o);
|
||||
|
@ -572,7 +579,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) {
|
|||
*/
|
||||
LUA_API void lua_pushnil (lua_State *L) {
|
||||
lua_lock(L);
|
||||
setnilvalue(s2v(L->top));
|
||||
setnilvalue(s2v(L->top.p));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
@ -585,7 +592,7 @@ LUA_API void lua_pushnil (lua_State *L) {
|
|||
*/
|
||||
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
||||
lua_lock(L);
|
||||
setfltvalue(s2v(L->top), n);
|
||||
setfltvalue(s2v(L->top.p), n);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
@ -600,7 +607,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
|||
*/
|
||||
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
|
||||
lua_lock(L);
|
||||
setivalue(s2v(L->top), n);
|
||||
setivalue(s2v(L->top.p), n);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
@ -615,7 +622,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
|||
TString *ts;
|
||||
lua_lock(L);
|
||||
ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
|
||||
setsvalue2s(L, L->top, ts);
|
||||
setsvalue2s(L, L->top.p, ts);
|
||||
api_incr_top(L);
|
||||
luaC_checkGC(L);
|
||||
lua_unlock(L);
|
||||
|
@ -637,11 +644,11 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
|||
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
|
||||
lua_lock(L);
|
||||
if (s == NULL)
|
||||
setnilvalue(s2v(L->top));
|
||||
setnilvalue(s2v(L->top.p));
|
||||
else {
|
||||
TString *ts;
|
||||
ts = luaS_new(L, s);
|
||||
setsvalue2s(L, L->top, ts);
|
||||
setsvalue2s(L, L->top.p, ts);
|
||||
s = getstr(ts); /* internal copy's address */
|
||||
}
|
||||
api_incr_top(L);
|
||||
|
@ -729,7 +736,7 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
|
|||
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
lua_lock(L);
|
||||
if (n == 0) {
|
||||
setfvalue(s2v(L->top), fn);
|
||||
setfvalue(s2v(L->top.p), fn);
|
||||
api_incr_top(L);
|
||||
}
|
||||
else {
|
||||
|
@ -738,13 +745,13 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
|||
api_check(L, n <= MAXUPVAL, "upvalue index too large");
|
||||
cl = luaF_newCclosure(L, n);
|
||||
cl->f = fn;
|
||||
L->top -= n;
|
||||
L->top.p -= n;
|
||||
while (n--) {
|
||||
setobj2n(L, &cl->upvalue[n], s2v(L->top + n));
|
||||
setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n));
|
||||
/* does not need barrier because closure is white */
|
||||
lua_assert(iswhite(cl));
|
||||
}
|
||||
setclCvalue(L, s2v(L->top), cl);
|
||||
setclCvalue(L, s2v(L->top.p), cl);
|
||||
api_incr_top(L);
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
@ -761,9 +768,9 @@ LUA_API void lua_pushboolean (lua_State *L, int b) {
|
|||
lua_lock(L);
|
||||
/* a.k.a. L->top->val.tt_ = b ? LUA_VTRUE : LUA_VFALSE; */
|
||||
if (b)
|
||||
setbtvalue(s2v(L->top));
|
||||
setbtvalue(s2v(L->top.p));
|
||||
else
|
||||
setbfvalue(s2v(L->top));
|
||||
setbfvalue(s2v(L->top.p));
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
@ -781,7 +788,7 @@ LUA_API void lua_pushboolean (lua_State *L, int b) {
|
|||
*/
|
||||
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
|
||||
lua_lock(L);
|
||||
setpvalue(s2v(L->top), p);
|
||||
setpvalue(s2v(L->top.p), p);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
@ -795,7 +802,7 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
|
|||
*/
|
||||
LUA_API int lua_pushthread (lua_State *L) {
|
||||
lua_lock(L);
|
||||
setthvalue(L, s2v(L->top), L);
|
||||
setthvalue(L, s2v(L->top.p), L);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return (G(L)->mainthread == L);
|
||||
|
@ -808,20 +815,20 @@ LUA_API int lua_pushthread (lua_State *L) {
|
|||
*/
|
||||
|
||||
|
||||
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
|
||||
l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
|
||||
const TValue *slot;
|
||||
TString *str = luaS_new(L, k);
|
||||
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
|
||||
setobj2s(L, L->top, slot);
|
||||
setobj2s(L, L->top.p, slot);
|
||||
api_incr_top(L);
|
||||
}
|
||||
else {
|
||||
setsvalue2s(L, L->top, str);
|
||||
setsvalue2s(L, L->top.p, str);
|
||||
api_incr_top(L);
|
||||
luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
|
||||
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
|
||||
}
|
||||
lua_unlock(L);
|
||||
return ttype(s2v(L->top - 1));
|
||||
return ttype(s2v(L->top.p - 1));
|
||||
}
|
||||
|
||||
|
||||
|
@ -848,13 +855,13 @@ LUA_API int lua_gettable (lua_State *L, int idx) {
|
|||
TValue *t;
|
||||
lua_lock(L);
|
||||
t = index2value(L, idx);
|
||||
if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) {
|
||||
setobj2s(L, L->top - 1, slot);
|
||||
if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) {
|
||||
setobj2s(L, L->top.p - 1, slot);
|
||||
}
|
||||
else
|
||||
luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
|
||||
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot);
|
||||
lua_unlock(L);
|
||||
return ttype(s2v(L->top - 1));
|
||||
return ttype(s2v(L->top.p - 1));
|
||||
}
|
||||
|
||||
|
||||
|
@ -870,27 +877,27 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
|
|||
lua_lock(L);
|
||||
t = index2value(L, idx);
|
||||
if (luaV_fastgeti(L, t, n, slot)) {
|
||||
setobj2s(L, L->top, slot);
|
||||
setobj2s(L, L->top.p, slot);
|
||||
}
|
||||
else {
|
||||
TValue aux;
|
||||
setivalue(&aux, n);
|
||||
luaV_finishget(L, t, &aux, L->top, slot);
|
||||
luaV_finishget(L, t, &aux, L->top.p, slot);
|
||||
}
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return ttype(s2v(L->top - 1));
|
||||
return ttype(s2v(L->top.p - 1));
|
||||
}
|
||||
|
||||
|
||||
static int finishrawget (lua_State *L, const TValue *val) {
|
||||
l_sinline int finishrawget (lua_State *L, const TValue *val) {
|
||||
if (isempty(val)) /* avoid copying empty items to the stack */
|
||||
setnilvalue(s2v(L->top));
|
||||
setnilvalue(s2v(L->top.p));
|
||||
else
|
||||
setobj2s(L, L->top, val);
|
||||
setobj2s(L, L->top.p, val);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
return ttype(s2v(L->top - 1));
|
||||
return ttype(s2v(L->top.p - 1));
|
||||
}
|
||||
|
||||
|
||||
|
@ -913,8 +920,8 @@ LUA_API int lua_rawget (lua_State *L, int idx) {
|
|||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
t = gettable(L, idx);
|
||||
val = luaH_get(t, s2v(L->top - 1));
|
||||
L->top--; /* remove key */
|
||||
val = luaH_get(t, s2v(L->top.p - 1));
|
||||
L->top.p--; /* remove key */
|
||||
return finishrawget(L, val);
|
||||
}
|
||||
|
||||
|
@ -961,7 +968,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
|
|||
Table *t;
|
||||
lua_lock(L);
|
||||
t = luaH_new(L);
|
||||
sethvalue2s(L, L->top, t);
|
||||
sethvalue2s(L, L->top.p, t);
|
||||
api_incr_top(L);
|
||||
if (narray > 0 || nrec > 0)
|
||||
luaH_resize(L, t, narray, nrec);
|
||||
|
@ -997,7 +1004,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
|||
break;
|
||||
}
|
||||
if (mt != NULL) {
|
||||
sethvalue2s(L, L->top, mt);
|
||||
sethvalue2s(L, L->top.p, mt);
|
||||
api_incr_top(L);
|
||||
res = 1;
|
||||
}
|
||||
|
@ -1024,12 +1031,12 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
|
|||
o = index2value(L, idx);
|
||||
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
||||
if (n <= 0 || n > uvalue(o)->nuvalue) {
|
||||
setnilvalue(s2v(L->top));
|
||||
setnilvalue(s2v(L->top.p));
|
||||
t = LUA_TNONE;
|
||||
}
|
||||
else {
|
||||
setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv);
|
||||
t = ttype(s2v(L->top));
|
||||
setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
|
||||
t = ttype(s2v(L->top.p));
|
||||
}
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
|
@ -1049,14 +1056,14 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
|
|||
TString *str = luaS_new(L, k);
|
||||
api_checknelems(L, 1);
|
||||
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
|
||||
luaV_finishfastset(L, t, slot, s2v(L->top - 1));
|
||||
L->top--; /* pop value */
|
||||
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
|
||||
L->top.p--; /* pop value */
|
||||
}
|
||||
else {
|
||||
setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
|
||||
setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
|
||||
api_incr_top(L);
|
||||
luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot);
|
||||
L->top -= 2; /* pop value and key */
|
||||
luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot);
|
||||
L->top.p -= 2; /* pop value and key */
|
||||
}
|
||||
lua_unlock(L); /* lock done by caller */
|
||||
}
|
||||
|
@ -1076,12 +1083,12 @@ LUA_API void lua_settable (lua_State *L, int idx) {
|
|||
lua_lock(L);
|
||||
api_checknelems(L, 2);
|
||||
t = index2value(L, idx);
|
||||
if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) {
|
||||
luaV_finishfastset(L, t, slot, s2v(L->top - 1));
|
||||
if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) {
|
||||
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
|
||||
}
|
||||
else
|
||||
luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot);
|
||||
L->top -= 2; /* pop index and value */
|
||||
luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot);
|
||||
L->top.p -= 2; /* pop index and value */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
@ -1108,14 +1115,14 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
|
|||
api_checknelems(L, 1);
|
||||
t = index2value(L, idx);
|
||||
if (luaV_fastgeti(L, t, n, slot)) {
|
||||
luaV_finishfastset(L, t, slot, s2v(L->top - 1));
|
||||
luaV_finishfastset(L, t, slot, s2v(L->top.p - 1));
|
||||
}
|
||||
else {
|
||||
TValue aux;
|
||||
setivalue(&aux, n);
|
||||
luaV_finishset(L, t, &aux, s2v(L->top - 1), slot);
|
||||
luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot);
|
||||
}
|
||||
L->top--; /* pop value */
|
||||
L->top.p--; /* pop value */
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
@ -1125,10 +1132,10 @@ static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
|
|||
lua_lock(L);
|
||||
api_checknelems(L, n);
|
||||
t = gettable(L, idx);
|
||||
luaH_set(L, t, key, s2v(L->top - 1));
|
||||
luaH_set(L, t, key, s2v(L->top.p - 1));
|
||||
invalidateTMcache(t);
|
||||
luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
|
||||
L->top -= n;
|
||||
luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
|
||||
L->top.p -= n;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
@ -1140,7 +1147,7 @@ static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
|
|||
* metamethods).
|
||||
*/
|
||||
LUA_API void lua_rawset (lua_State *L, int idx) {
|
||||
aux_rawset(L, idx, s2v(L->top - 2), 2);
|
||||
aux_rawset(L, idx, s2v(L->top.p - 2), 2);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1175,9 +1182,9 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
|||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
t = gettable(L, idx);
|
||||
luaH_setint(L, t, n, s2v(L->top - 1));
|
||||
luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
|
||||
L->top--;
|
||||
luaH_setint(L, t, n, s2v(L->top.p - 1));
|
||||
luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
|
||||
L->top.p--;
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
||||
|
@ -1197,11 +1204,11 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
|||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
obj = index2value(L, objindex);
|
||||
if (ttisnil(s2v(L->top - 1)))
|
||||
if (ttisnil(s2v(L->top.p - 1)))
|
||||
mt = NULL;
|
||||
else {
|
||||
api_check(L, ttistable(s2v(L->top - 1)), "table expected");
|
||||
mt = hvalue(s2v(L->top - 1));
|
||||
api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
|
||||
mt = hvalue(s2v(L->top.p - 1));
|
||||
}
|
||||
switch (ttype(obj)) {
|
||||
case LUA_TTABLE: {
|
||||
|
@ -1225,7 +1232,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
L->top--;
|
||||
L->top.p--;
|
||||
lua_unlock(L);
|
||||
return 1;
|
||||
}
|
||||
|
@ -1248,11 +1255,11 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
|
|||
if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
|
||||
res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
|
||||
else {
|
||||
setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1));
|
||||
luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
|
||||
setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
|
||||
luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
|
||||
res = 1;
|
||||
}
|
||||
L->top--;
|
||||
L->top.p--;
|
||||
lua_unlock(L);
|
||||
return res;
|
||||
}
|
||||
|
@ -1264,7 +1271,8 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
|
|||
|
||||
|
||||
#define checkresults(L,na,nr) \
|
||||
api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
|
||||
api_check(L, (nr) == LUA_MULTRET \
|
||||
|| (L->ci->top.p - L->top.p >= (nr) - (na)), \
|
||||
"results from function overflow current stack size")
|
||||
|
||||
|
||||
|
@ -1277,7 +1285,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
|||
api_checknelems(L, nargs+1);
|
||||
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
|
||||
checkresults(L, nargs, nresults);
|
||||
func = L->top - (nargs+1);
|
||||
func = L->top.p - (nargs+1);
|
||||
if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
|
||||
L->ci->u.c.k = k; /* save continuation */
|
||||
L->ci->u.c.ctx = ctx; /* save context */
|
||||
|
@ -1325,7 +1333,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
|||
api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
|
||||
func = savestack(L, o);
|
||||
}
|
||||
c.func = L->top - (nargs+1); /* function to be called */
|
||||
c.func = L->top.p - (nargs+1); /* function to be called */
|
||||
if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
|
||||
c.nresults = nresults; /* do a 'conventional' protected call */
|
||||
status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
|
||||
|
@ -1360,12 +1368,12 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
|||
luaZ_init(L, &z, reader, data);
|
||||
status = luaD_protectedparser(L, &z, chunkname, mode);
|
||||
if (status == LUA_OK) { /* no errors? */
|
||||
LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
|
||||
LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
|
||||
if (f->nupvalues >= 1) { /* does it have an upvalue? */
|
||||
/* get global table from registry */
|
||||
const TValue *gt = getGtable(L);
|
||||
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
|
||||
setobj(L, f->upvals[0]->v, gt);
|
||||
setobj(L, f->upvals[0]->v.p, gt);
|
||||
luaC_barrier(L, f->upvals[0], gt);
|
||||
}
|
||||
}
|
||||
|
@ -1379,7 +1387,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
|
|||
TValue *o;
|
||||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
o = s2v(L->top - 1);
|
||||
o = s2v(L->top.p - 1);
|
||||
if (isLfunction(o))
|
||||
status = luaU_dump(L, getproto(o), writer, data, strip);
|
||||
else
|
||||
|
@ -1400,18 +1408,19 @@ LUA_API int lua_status (lua_State *L) {
|
|||
LUA_API int lua_gc (lua_State *L, int what, ...) {
|
||||
va_list argp;
|
||||
int res = 0;
|
||||
global_State *g;
|
||||
global_State *g = G(L);
|
||||
if (g->gcstp & GCSTPGC) /* internal stop? */
|
||||
return -1; /* all options are invalid when stopped */
|
||||
lua_lock(L);
|
||||
g = G(L);
|
||||
va_start(argp, what);
|
||||
switch (what) {
|
||||
case LUA_GCSTOP: {
|
||||
g->gcrunning = 0;
|
||||
g->gcstp = GCSTPUSR; /* stopped by the user */
|
||||
break;
|
||||
}
|
||||
case LUA_GCRESTART: {
|
||||
luaE_setdebt(g, 0);
|
||||
g->gcrunning = 1;
|
||||
g->gcstp = 0; /* (GCSTPGC must be already zero here) */
|
||||
break;
|
||||
}
|
||||
case LUA_GCCOLLECT: {
|
||||
|
@ -1430,8 +1439,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|||
case LUA_GCSTEP: {
|
||||
int data = va_arg(argp, int);
|
||||
l_mem debt = 1; /* =1 to signal that it did an actual step */
|
||||
lu_byte oldrunning = g->gcrunning;
|
||||
g->gcrunning = 1; /* allow GC to run */
|
||||
lu_byte oldstp = g->gcstp;
|
||||
g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */
|
||||
if (data == 0) {
|
||||
luaE_setdebt(g, 0); /* do a basic step */
|
||||
luaC_step(L);
|
||||
|
@ -1441,7 +1450,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|||
luaE_setdebt(g, debt);
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
g->gcrunning = oldrunning; /* restore previous state */
|
||||
g->gcstp = oldstp; /* restore previous state */
|
||||
if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
|
||||
res = 1; /* signal it */
|
||||
break;
|
||||
|
@ -1459,7 +1468,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|||
break;
|
||||
}
|
||||
case LUA_GCISRUNNING: {
|
||||
res = g->gcrunning;
|
||||
res = gcrunning(g);
|
||||
break;
|
||||
}
|
||||
case LUA_GCGEN: {
|
||||
|
@ -1511,7 +1520,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|||
LUA_API int lua_error (lua_State *L) {
|
||||
TValue *errobj;
|
||||
lua_lock(L);
|
||||
errobj = s2v(L->top - 1);
|
||||
errobj = s2v(L->top.p - 1);
|
||||
api_checknelems(L, 1);
|
||||
/* error object is the memory error message? */
|
||||
if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
|
||||
|
@ -1519,7 +1528,7 @@ LUA_API int lua_error (lua_State *L) {
|
|||
else
|
||||
luaG_errormsg(L); /* raise a regular error */
|
||||
/* code unreachable; will unlock when control actually leaves the kernel */
|
||||
__builtin_unreachable();
|
||||
__builtin_unreachable(); // [jart]
|
||||
}
|
||||
|
||||
|
||||
|
@ -1558,12 +1567,12 @@ LUA_API int lua_next (lua_State *L, int idx) {
|
|||
lua_lock(L);
|
||||
api_checknelems(L, 1);
|
||||
t = gettable(L, idx);
|
||||
more = luaH_next(L, t, L->top - 1);
|
||||
more = luaH_next(L, t, L->top.p - 1);
|
||||
if (more) {
|
||||
api_incr_top(L);
|
||||
}
|
||||
else /* no more elements */
|
||||
L->top -= 1; /* remove key */
|
||||
L->top.p -= 1; /* remove key */
|
||||
lua_unlock(L);
|
||||
return more;
|
||||
}
|
||||
|
@ -1596,7 +1605,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) {
|
|||
lua_lock(L);
|
||||
o = index2stack(L, idx);
|
||||
nresults = L->ci->nresults;
|
||||
api_check(L, L->tbclist < o, "given index below or equal a marked one");
|
||||
api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
|
||||
luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
|
||||
if (!hastocloseCfunc(nresults)) /* function not marked yet? */
|
||||
L->ci->nresults = codeNresults(nresults); /* mark it */
|
||||
|
@ -1619,7 +1628,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
|
|||
if (n > 0)
|
||||
luaV_concat(L, n);
|
||||
else { /* nothing to concatenate */
|
||||
setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); /* push empty string */
|
||||
setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */
|
||||
api_incr_top(L);
|
||||
}
|
||||
luaC_checkGC(L);
|
||||
|
@ -1638,7 +1647,7 @@ LUA_API void lua_len (lua_State *L, int idx) {
|
|||
TValue *t;
|
||||
lua_lock(L);
|
||||
t = index2value(L, idx);
|
||||
luaV_objlen(L, L->top, t);
|
||||
luaV_objlen(L, L->top.p, t);
|
||||
api_incr_top(L);
|
||||
lua_unlock(L);
|
||||
}
|
||||
|
@ -1725,7 +1734,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
|
|||
lua_lock(L);
|
||||
api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
|
||||
u = luaS_newudata(L, size, nuvalue);
|
||||
setuvalue(L, s2v(L->top), u);
|
||||
setuvalue(L, s2v(L->top.p), u);
|
||||
api_incr_top(L);
|
||||
luaC_checkGC(L);
|
||||
lua_unlock(L);
|
||||
|
@ -1733,6 +1742,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
|
|||
}
|
||||
|
||||
|
||||
|
||||
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
|
||||
GCObject **owner) {
|
||||
switch (ttypetag(fi)) {
|
||||
|
@ -1750,7 +1760,7 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val,
|
|||
Proto *p = f->p;
|
||||
if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
|
||||
return NULL; /* 'n' not in [1, p->sizeupvalues] */
|
||||
*val = f->upvals[n-1]->v;
|
||||
*val = f->upvals[n-1]->v.p;
|
||||
if (owner) *owner = obj2gco(f->upvals[n - 1]);
|
||||
name = p->upvalues[n-1].name;
|
||||
return (name == NULL) ? "(no name)" : getstr(name);
|
||||
|
@ -1776,7 +1786,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
|
|||
lua_lock(L);
|
||||
name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
|
||||
if (name) {
|
||||
setobj2s(L, L->top, val);
|
||||
setobj2s(L, L->top.p, val);
|
||||
api_incr_top(L);
|
||||
}
|
||||
lua_unlock(L);
|
||||
|
@ -1806,8 +1816,8 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
|
|||
api_checknelems(L, 1);
|
||||
name = aux_upvalue(fi, n, &val, &owner);
|
||||
if (name) {
|
||||
L->top--;
|
||||
setobj(L, val, s2v(L->top));
|
||||
L->top.p--;
|
||||
setobj(L, val, s2v(L->top.p));
|
||||
luaC_barrier(L, owner, val);
|
||||
}
|
||||
lua_unlock(L);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue