no more nil-in-table

This commit is contained in:
Roberto Ierusalimschy 2018-04-04 11:23:41 -03:00
parent 3d0b5edfe4
commit 03c6a05ec8
16 changed files with 23 additions and 172 deletions

23
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 2.289 2018/02/27 17:48:28 roberto Exp roberto $
** $Id: lapi.c,v 2.290 2018/02/27 20:01:55 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -705,27 +705,6 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
}
static int auxkeydef (lua_State *L, int idx, int remove) {
int res;
lua_lock(L);
api_checknelems(L, 1);
res = luaT_keydef(L, index2value(L, idx), s2v(L->top - 1), remove);
L->top--; /* remove key */
lua_unlock(L);
return res;
}
LUA_API void lua_removekey (lua_State *L, int idx) {
auxkeydef(L, idx, 1);
}
LUA_API int lua_keyin (lua_State *L, int idx) {
return auxkeydef(L, idx, 0);
}
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
Table *t;
lua_lock(L);

55
lcode.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 2.159 2018/03/07 15:55:38 roberto Exp roberto $
** $Id: lcode.c,v 2.160 2018/03/16 14:22:09 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -678,10 +678,6 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) {
e->k = VNONRELOC; /* becomes a non-relocatable value */
break;
}
case VUNDEF: { /* not a real expression */
luaK_semerror(fs->ls, "'undef' is not a value!!");
break;
}
case VUPVAL: { /* move value to some (pending) register */
e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
e->k = VRELOC;
@ -1410,48 +1406,6 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
}
static void normalizeindexed (FuncState *fs, expdesc *v) {
if (v->k != VINDEXED) { /* not in proper form? */
int key = fs->freereg; /* register with key value */
luaK_reserveregs(fs, 1);
switch (v->k) {
case VINDEXI:
luaK_int(fs, key, v->u.ind.idx);
break;
case VINDEXSTR:
luaK_codek(fs, key, v->u.ind.idx);
break;
case VINDEXUP:
luaK_codek(fs, key, v->u.ind.idx);
luaK_codeABC(fs, OP_GETUPVAL, fs->freereg, v->u.ind.t, 0);
v->u.ind.t = fs->freereg;
luaK_reserveregs(fs, 1); /* one more register for the upvalue */
break;
default:
luaK_semerror(fs->ls, "'undef' is not a value!!");
break;
}
v->u.ind.idx = key;
v->k = VINDEXED;
}
freeregs(fs, v->u.ind.t, v->u.ind.idx);
}
static void codeisdef (FuncState *fs, int eq, expdesc *v) {
normalizeindexed(fs, v);
v->u.info = luaK_codeABCk(fs, OP_ISDEF, 0, v->u.ind.t, v->u.ind.idx, eq);
v->k = VRELOC;
}
void luaK_codeundef (FuncState *fs, expdesc *v) {
normalizeindexed(fs, v);
v->u.info = luaK_codeABC(fs, OP_UNDEF, v->u.ind.t, v->u.ind.idx, 0);
v->k = VRELOC;
}
/*
** Apply prefix operation 'op' to expression 'e'.
*/
@ -1500,7 +1454,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
break;
}
case OPR_EQ: case OPR_NE: {
if (!tonumeral(v, NULL) && fs->ls->t.token != TK_UNDEF)
if (!tonumeral(v, NULL))
luaK_exp2RK(fs, v);
/* else keep numeral, which may be an immediate operand */
break;
@ -1597,10 +1551,7 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
break;
}
case OPR_EQ: case OPR_NE: {
if (e2->k == VUNDEF)
codeisdef(fs, opr == OPR_NE, e1);
else
codeeq(fs, opr, e1, e2);
codeeq(fs, opr, e1, e2);
break;
}
case OPR_LT: case OPR_LE: {

View File

@ -1,5 +1,5 @@
/*
** $Id: lcode.h,v 1.71 2018/03/07 15:55:38 roberto Exp roberto $
** $Id: lcode.h,v 1.72 2018/03/19 20:03:44 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -90,7 +90,6 @@ LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
expdesc *v2, int line);
LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
LUAI_FUNC void luaK_finish (FuncState *fs);
LUAI_FUNC void luaK_codeundef (FuncState *fs, expdesc *e);
LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg);

View File

@ -79,8 +79,6 @@ static void *disptab[] = {
&&L_OP_GEI,
&&L_OP_TEST,
&&L_OP_TESTSET,
&&L_OP_UNDEF,
&&L_OP_ISDEF,
&&L_OP_CALL,
&&L_OP_TAILCALL,
&&L_OP_RETURN,

4
llex.c
View File

@ -1,5 +1,5 @@
/*
** $Id: llex.c,v 2.100 2018/02/23 13:13:31 roberto Exp roberto $
** $Id: llex.c,v 2.101 2018/03/07 15:55:38 roberto Exp roberto $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
@ -41,7 +41,7 @@ static const char *const luaX_tokens [] = {
"and", "break", "do", "else", "elseif",
"end", "false", "for", "function", "goto", "if",
"in", "local", "nil", "not", "or", "repeat",
"return", "then", "true", "undef", "until", "while",
"return", "then", "true", "until", "while",
"//", "..", "...", "==", ">=", "<=", "~=",
"<<", ">>", "::", "<eof>",
"<number>", "<integer>", "<name>", "<string>"

4
llex.h
View File

@ -1,5 +1,5 @@
/*
** $Id: llex.h,v 1.80 2018/01/28 15:13:26 roberto Exp roberto $
** $Id: llex.h,v 1.81 2018/03/07 15:55:38 roberto Exp roberto $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
@ -28,7 +28,7 @@ enum RESERVED {
TK_AND = FIRST_RESERVED, TK_BREAK,
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
TK_RETURN, TK_THEN, TK_TRUE, TK_UNDEF, TK_UNTIL, TK_WHILE,
TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
/* other terminal symbols */
TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
TK_SHL, TK_SHR,

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.h,v 2.140 2018/02/26 13:35:03 roberto Exp roberto $
** $Id: lobject.h,v 2.141 2018/02/26 14:16:05 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@ -160,15 +160,9 @@ typedef StackValue *StkId; /* index to stack elements */
#define isreallyempty(v) checktag((v), LUA_TEMPTY)
#if defined(LUA_NILINTABLE)
#define isempty(v) isreallyempty(v)
#else /* By default, entries with any kind of nil are considered empty */
/* By default, entries with any kind of nil are considered empty */
#define isempty(v) ttisnilorempty(v)
#endif
/* macro defining an empty value */
#define EMPTYCONSTANT {NULL}, LUA_TEMPTY

View File

@ -1,5 +1,5 @@
/*
** $Id: lopcodes.c,v 1.79 2018/02/21 15:49:32 roberto Exp roberto $
** $Id: lopcodes.c,v 1.80 2018/03/07 15:55:38 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -79,8 +79,6 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
"GEI",
"TEST",
"TESTSET",
"UNDEF",
"ISDEF",
"CALL",
"TAILCALL",
"RETURN",
@ -164,8 +162,6 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
,opmode(0, 0, 1, 0, iABC) /* OP_GEI */
,opmode(0, 0, 1, 0, iABC) /* OP_TEST */
,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */
,opmode(0, 0, 0, 0, iABC) /* OP_UNDEF */
,opmode(0, 0, 0, 1, iABC) /* OP_ISDEF */
,opmode(1, 1, 0, 1, iABC) /* OP_CALL */
,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */
,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */

View File

@ -1,5 +1,5 @@
/*
** $Id: lopcodes.h,v 1.189 2018/02/21 15:49:32 roberto Exp roberto $
** $Id: lopcodes.h,v 1.190 2018/03/07 15:55:38 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -266,9 +266,6 @@ OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */
OP_TEST,/* A if (not R(A) == k) then pc++ */
OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */
OP_UNDEF,/* A B R(A)[R(B)] = undef */
OP_ISDEF,/* A B C R(A) = (R(B)[R(C)] == undef */
OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */

View File

@ -1,5 +1,5 @@
/*
** $Id: lparser.c,v 2.178 2018/02/17 19:20:00 roberto Exp roberto $
** $Id: lparser.c,v 2.179 2018/03/07 15:55:38 roberto Exp roberto $
** Lua Parser
** See Copyright Notice in lua.h
*/
@ -893,11 +893,6 @@ static void primaryexp (LexState *ls, expdesc *v) {
singlevar(ls, v);
return;
}
case TK_UNDEF: {
luaX_next(ls);
init_exp(v, VUNDEF, 0);
return;
}
default: {
luaX_syntaxerror(ls, "unexpected symbol");
}
@ -1183,10 +1178,6 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
else { /* assignment -> '=' explist */
int nexps;
checknext(ls, '=');
if (nvars == 1 && testnext(ls, TK_UNDEF)) {
luaK_codeundef(ls->fs, &lh->v);
return;
}
nexps = explist(ls, &e);
if (nexps != nvars)
adjust_assign(ls, nvars, nexps, &e);
@ -1652,11 +1643,6 @@ static void statement (LexState *ls) {
luaX_next(ls); /* skip LOCAL */
if (testnext(ls, TK_FUNCTION)) /* local function? */
localfunc(ls);
else if (testnext(ls, TK_UNDEF))
(void)0; /* ignore */
/* old versions may need to declare 'local undef'
when using 'undef' with no environment; so this
version accepts (and ignores) these declarations */
else
localstat(ls);
break;

View File

@ -1,5 +1,5 @@
/*
** $Id: lparser.h,v 1.80 2017/12/14 14:24:02 roberto Exp roberto $
** $Id: lparser.h,v 1.81 2018/03/07 15:55:38 roberto Exp roberto $
** Lua Parser
** See Copyright Notice in lua.h
*/
@ -52,8 +52,7 @@ typedef enum {
VRELOC, /* 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 */
VUNDEF /* the 'undef' "expression" */
VVARARG /* vararg expression; info = instruction pc */
} expkind;

View File

@ -1,5 +1,5 @@
/*
** $Id: ltablib.c,v 1.95 2018/02/27 18:47:32 roberto Exp roberto $
** $Id: ltablib.c,v 1.96 2018/03/16 14:18:18 roberto Exp roberto $
** Library for Table Manipulation
** See Copyright Notice in lua.h
*/
@ -95,8 +95,8 @@ static int tremove (lua_State *L) {
lua_geti(L, 1, pos + 1);
lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
}
lua_pushinteger(L, pos);
lua_removekey(L, 1); /* remove entry t[pos] */
lua_pushnil(L);
lua_seti(L, 1, pos); /* remove entry t[pos] */
return 1;
}

30
ltm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: ltm.c,v 2.65 2018/02/26 14:16:05 roberto Exp roberto $
** $Id: ltm.c,v 2.66 2018/02/27 17:48:28 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@ -38,7 +38,6 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
void luaT_init (lua_State *L) {
static const char *const luaT_eventname[] = { /* ORDER TM */
"__index", "__newindex",
"__undef", "__isdef",
"__gc", "__mode", "__len", "__eq",
"__add", "__sub", "__mul", "__mod", "__pow",
"__div", "__idiv",
@ -250,30 +249,3 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
setnilvalue(s2v(where + i));
}
int luaT_keydef (lua_State *L, TValue *obj, TValue *key, int remove) {
const TValue *tm;
TMS event = remove ? TM_UNDEF : TM_ISDEF;
if (!ttistable(obj)) { /* not a table? */
tm = luaT_gettmbyobj(L, obj, event); /* get its metamethod */
if (notm(tm)) { /* no metamethod? */
const char *msg = remove ? "remove key from" : "check key from";
luaG_typeerror(L, obj, msg); /* error */
}
/* else will call metamethod 'tm' */
}
else { /* 'obj' is a table */
Table *t = hvalue(obj);
tm = fasttm(L, t->metatable, event);
if (tm == NULL) { /* no metamethod? */
const TValue *val = luaH_get(t, key); /* get entry */
int res = !isempty(val); /* true if entry is not empty */
if (remove && res) /* key is present and should be removed? */
setempty(cast(TValue*, val)); /* remove it */
return res;
}
/* else will call metamethod 'tm' */
}
luaT_callTMres(L, tm, obj, key, L->top);
return !l_isfalse(s2v(L->top));
}

6
ltm.h
View File

@ -1,5 +1,5 @@
/*
** $Id: ltm.h,v 2.33 2018/02/23 13:13:31 roberto Exp roberto $
** $Id: ltm.h,v 2.34 2018/02/27 17:48:28 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@ -19,8 +19,6 @@
typedef enum {
TM_INDEX,
TM_NEWINDEX,
TM_UNDEF,
TM_ISDEF,
TM_GC,
TM_MODE,
TM_LEN,
@ -91,7 +89,5 @@ LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams,
LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci,
StkId where, int wanted);
LUAI_FUNC int luaT_keydef (lua_State *L, TValue *obj, TValue *key, int remove);
#endif

5
lua.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lua.h,v 1.344 2018/03/05 14:15:32 roberto Exp roberto $
** $Id: lua.h,v 1.345 2018/03/16 15:33:34 roberto Exp roberto $
** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file
@ -331,9 +331,6 @@ LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
LUA_API void (lua_removekey) (lua_State *L, int idx);
LUA_API int (lua_keyin) (lua_State *L, int idx);
/*
** {==============================================================

15
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.351 2018/03/16 14:21:20 roberto Exp roberto $
** $Id: lvm.c,v 2.352 2018/04/02 17:52:07 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -1560,19 +1560,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
}
vmbreak;
}
vmcase(OP_UNDEF) {
TValue *rb = vRB(i);
luaT_keydef(L, vra, rb, 1);
vmbreak;
}
vmcase(OP_ISDEF) {
TValue *rb = vRB(i);
TValue *rc = vRC(i);
int res;
Protect(res = luaT_keydef(L, rb, rc, 0));
setbvalue(vra, res == GETARG_k(i));
vmbreak;
}
vmcase(OP_CALL) {
int b = GETARG_B(i);
int nresults = GETARG_C(i) - 1;