Skip to content

Commit

Permalink
Allow environment to explicitly enable keywords, preventing auto-dete…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
Sainan authored and well-in-that-case committed Apr 14, 2024
1 parent 17c3c30 commit 2d1697c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 66 deletions.
65 changes: 29 additions & 36 deletions src/lparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5666,6 +5666,17 @@ static void mainfunc (LexState *ls, FuncState *fs) {
}


static void applyenvkeywordpreference (LexState *ls, int t, bool b) {
if (b) {
ls->setKeywordState(t, KS_ENABLED_BY_ENV);
}
else {
disablekeyword(ls, t);
ls->setKeywordState(t, KS_DISABLED_BY_ENV);
}
}


LClosure *luaY_parser (lua_State *L, LexState& lexstate, ZIO *z, Mbuffer *buff,
Dyndata *dyd, const char *name, int firstchar) {
FuncState funcstate;
Expand All @@ -5683,42 +5694,24 @@ LClosure *luaY_parser (lua_State *L, LexState& lexstate, ZIO *z, Mbuffer *buff,
lexstate.dyd = dyd;
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
luaX_setinput(L, &lexstate, z, funcstate.f->source, firstchar);
if (L->l_G->compatible_switch) {
disablekeyword(&lexstate, TK_SWITCH);
lexstate.setKeywordState(TK_SWITCH, KS_DISABLED_BY_ENV);
}
if (L->l_G->compatible_continue) {
disablekeyword(&lexstate, TK_CONTINUE);
lexstate.setKeywordState(TK_CONTINUE, KS_DISABLED_BY_ENV);
}
if (L->l_G->compatible_enum) {
disablekeyword(&lexstate, TK_ENUM);
lexstate.setKeywordState(TK_ENUM, KS_DISABLED_BY_ENV);
}
if (L->l_G->compatible_new) {
disablekeyword(&lexstate, TK_NEW);
lexstate.setKeywordState(TK_NEW, KS_DISABLED_BY_ENV);
}
if (L->l_G->compatible_class) {
disablekeyword(&lexstate, TK_CLASS);
lexstate.setKeywordState(TK_CLASS, KS_DISABLED_BY_ENV);
}
if (L->l_G->compatible_parent) {
disablekeyword(&lexstate, TK_PARENT);
lexstate.setKeywordState(TK_PARENT, KS_DISABLED_BY_ENV);
}
if (L->l_G->compatible_export) {
disablekeyword(&lexstate, TK_EXPORT);
lexstate.setKeywordState(TK_EXPORT, KS_DISABLED_BY_ENV);
}
if (L->l_G->compatible_try) {
disablekeyword(&lexstate, TK_TRY);
lexstate.setKeywordState(TK_TRY, KS_DISABLED_BY_ENV);
}
if (L->l_G->compatible_catch) {
disablekeyword(&lexstate, TK_CATCH);
lexstate.setKeywordState(TK_CATCH, KS_DISABLED_BY_ENV);
}
if (L->l_G->have_preference_switch)
applyenvkeywordpreference(&lexstate, TK_SWITCH, L->l_G->preference_switch);
if (L->l_G->have_preference_continue)
applyenvkeywordpreference(&lexstate, TK_CONTINUE, L->l_G->preference_continue);
if (L->l_G->have_preference_enum)
applyenvkeywordpreference(&lexstate, TK_ENUM, L->l_G->preference_enum);
if (L->l_G->have_preference_new)
applyenvkeywordpreference(&lexstate, TK_NEW, L->l_G->preference_new);
if (L->l_G->have_preference_class)
applyenvkeywordpreference(&lexstate, TK_CLASS, L->l_G->preference_class);
if (L->l_G->have_preference_parent)
applyenvkeywordpreference(&lexstate, TK_PARENT, L->l_G->preference_parent);
if (L->l_G->have_preference_export)
applyenvkeywordpreference(&lexstate, TK_EXPORT, L->l_G->preference_export);
if (L->l_G->have_preference_try)
applyenvkeywordpreference(&lexstate, TK_TRY, L->l_G->preference_try);
if (L->l_G->have_preference_catch)
applyenvkeywordpreference(&lexstate, TK_CATCH, L->l_G->preference_catch);
#ifndef PLUTO_USE_LET
disablekeyword(&lexstate, TK_LET);
#endif
Expand Down
48 changes: 38 additions & 10 deletions src/lstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,34 +414,62 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
g->genminormul = LUAI_GENMINORMUL;
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
g->setCompatibilityMode(false);

#ifdef PLUTO_COMPATIBLE_SWITCH
g->compatible_switch = true;
g->have_preference_switch = true;
g->preference_switch = false;
#else
g->have_preference_switch = false;
#endif
#ifdef PLUTO_COMPATIBLE_CONTINUE
g->compatible_continue = true;
g->have_preference_continue = true;
g->preference_continue = false;
#else
g->have_preference_continue = false;
#endif
#ifdef PLUTO_COMPATIBLE_ENUM
g->compatible_enum = true;
g->have_preference_enum = true;
g->preference_enum = false;
#else
g->have_preference_enum = false;
#endif
#ifdef PLUTO_COMPATIBLE_NEW
g->compatible_new = true;
g->have_preference_new = true;
g->preference_new = false;
#else
g->have_preference_new = false;
#endif
#ifdef PLUTO_COMPATIBLE_CLASS
g->compatible_class = true;
g->have_preference_class = true;
g->preference_class = false;
#else
g->have_preference_class = false;
#endif
#ifdef PLUTO_COMPATIBLE_PARENT
g->compatible_parent = true;
g->have_preference_parent = true;
g->preference_parent = false;
#else
g->have_preference_parent = false;
#endif
#ifdef PLUTO_COMPATIBLE_EXPORT
g->compatible_export = true;
g->have_preference_export = true;
g->preference_export = false;
#else
g->have_preference_export = false;
#endif
#ifdef PLUTO_COMPATIBLE_TRY
g->compatible_try = true;
g->have_preference_try = true;
g->preference_try = false;
#else
g->have_preference_try = false;
#endif
#ifdef PLUTO_COMPATIBLE_CATCH
g->compatible_catch = true;
g->have_preference_catch = true;
g->preference_catch = false;
#else
g->have_preference_catch = false;
#endif

g->scheduler = nullptr;
#ifdef PLUTO_ETL_ENABLE
g->deadline = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() + PLUTO_ETL_NANOS;
Expand Down
62 changes: 44 additions & 18 deletions src/lstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,32 @@ typedef struct global_State {
void *ud_warn; /* auxiliary data to 'warnf' */
#ifndef PLUTO_LUA_LINKABLE
void* user_data; /* a pointer to data you, the user, would like to specify */
bool compatible_switch : 1;
bool compatible_continue : 1;
bool compatible_enum : 1;
bool compatible_new : 1;
bool compatible_class : 1;
bool compatible_parent : 1;
bool compatible_export : 1;
bool compatible_try : 1;
bool compatible_catch : 1;

/*
** Each non-compatible keyword has 2 bools here: "have_preference" and "preference".
** "have_preference" declares that you would like to overwrite the default enable/disable state of a given keyword,
** and "preference" declares if you would like it to be enabled or disabled.
** For example: If have_preference_switch is true, and preference_switch is false, the 'switch' keyword will be disabled.
*/
bool have_preference_switch : 1;
bool preference_switch : 1;
bool have_preference_continue : 1;
bool preference_continue : 1;
bool have_preference_enum : 1;
bool preference_enum : 1;
bool have_preference_new : 1;
bool preference_new : 1;
bool have_preference_class : 1;
bool preference_class : 1;
bool have_preference_parent : 1;
bool preference_parent : 1;
bool have_preference_export : 1;
bool preference_export : 1;
bool have_preference_try : 1;
bool preference_try : 1;
bool have_preference_catch : 1;
bool preference_catch : 1;

void* scheduler; /* internal use only; do not use this in your own code. */
#endif
#ifdef PLUTO_ETL_ENABLE
Expand All @@ -323,15 +340,24 @@ typedef struct global_State {
#endif

void setCompatibilityMode(bool b) noexcept {
compatible_switch = b;
compatible_continue = b;
compatible_enum = b;
compatible_new = b;
compatible_class = b;
compatible_parent = b;
compatible_export = b;
compatible_try = b;
compatible_catch = b;
have_preference_switch = true;
preference_switch = !b;
have_preference_continue = true;
preference_continue = !b;
have_preference_enum = true;
preference_enum = !b;
have_preference_new = true;
preference_new = !b;
have_preference_class = true;
preference_class = !b;
have_preference_parent = true;
preference_parent = !b;
have_preference_export = true;
preference_export = !b;
have_preference_try = true;
preference_try = !b;
have_preference_catch = true;
preference_catch = !b;
}
} global_State;

Expand Down
4 changes: 3 additions & 1 deletion src/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,9 @@ static int pmain (lua_State *L) {
lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
}
L->l_G->setCompatibilityMode(args & has_c);
if (args & has_c) {
L->l_G->setCompatibilityMode(true);
}
luaL_openlibs(L); /* open standard libraries */
createargtable(L, argv, argc, script); /* create table 'arg' */
lua_gc(L, LUA_GCRESTART); /* start GC... */
Expand Down
4 changes: 3 additions & 1 deletion src/luac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ static int pmain(lua_State* L)
int i;
tmname=G(L)->tmname;
if (!lua_checkstack(L,argc)) fatal("too many input files");
L->l_G->setCompatibilityMode(compat);
if (compat) {
L->l_G->setCompatibilityMode(compat);
}
for (i=0; i<argc; i++)
{
const char* filename=IS("-") ? NULL : argv[i];
Expand Down

0 comments on commit 2d1697c

Please sign in to comment.