Added string constants to the read-only tables in Lua

This commit is contained in:
jfrickmann
2021-11-29 12:00:50 +10:00
committed by Peter Feerick
parent a09e318e64
commit 81cb94a4bf
6 changed files with 99 additions and 40 deletions
+22
View File
@@ -2311,3 +2311,25 @@ const luaR_value_entry opentxConstants[] = {
{ nullptr, 0 } /* sentinel */
};
const luaR_string_entry edgetxStrings[] = {
{ "CHAR_RIGHT", STR_CHAR_RIGHT },
{ "CHAR_LEFT", STR_CHAR_LEFT },
{ "CHAR_UP", STR_CHAR_UP },
{ "CHAR_DOWN", STR_CHAR_DOWN },
{ "CHAR_DELTA", STR_CHAR_DELTA },
{ "CHAR_STICK", STR_CHAR_STICK },
{ "CHAR_POT", STR_CHAR_POT },
{ "CHAR_SLIDER", STR_CHAR_SLIDER },
{ "CHAR_SWITCH", STR_CHAR_SWITCH },
{ "CHAR_TRIM", STR_CHAR_TRIM },
{ "CHAR_INPUT", STR_CHAR_INPUT },
{ "CHAR_FUNCTION", STR_CHAR_FUNCTION },
{ "CHAR_CYC", STR_CHAR_CYC },
{ "CHAR_TRAINER", STR_CHAR_TRAINER },
{ "CHAR_CHANNEL", STR_CHAR_CHANNEL },
{ "CHAR_TELEMETRY", STR_CHAR_TELEMETRY },
{ "CHAR_LUA", STR_CHAR_LUA },
{ nullptr, "" } /* sentinel */
};
+1 -1
View File
@@ -626,7 +626,7 @@ LUA_API void lua_getglobal (lua_State *L, const char *var) {
const TValue *gt; /* global table */
lua_lock(L);
found = luaR_findglobal(var, &value);
found = luaR_findglobal(L, var, &value);
if (found && ttislightfunction(&value)) {
setsvalue2s(L, L->top++, luaS_new(L, var));
setlfvalue(L->top - 1, lfvalue(&value))
+10 -10
View File
@@ -45,18 +45,18 @@ static const luaL_Reg loadedlibs[] = {
/* The read-only tables are defined here */
const luaR_table lua_rotable[] =
{
{"__opentx", opentxLib, opentxConstants},
{"lcd", lcdLib, NULL},
{"model", modelLib, NULL},
{"__baselib", baselib, NULL},
{LUA_IOLIBNAME, iolib, NULL},
{LUA_STRLIBNAME, strlib, NULL},
{LUA_MATHLIBNAME, mathlib, mathlib_vals},
{LUA_BITLIBNAME, bitlib, NULL},
{"__opentx", opentxLib, opentxConstants, edgetxStrings},
{"lcd", lcdLib, NULL, NULL},
{"model", modelLib, NULL, NULL},
{"__baselib", baselib, NULL, NULL},
{LUA_IOLIBNAME, iolib, NULL, NULL},
{LUA_STRLIBNAME, strlib, NULL, NULL},
{LUA_MATHLIBNAME, mathlib, mathlib_vals, NULL},
{LUA_BITLIBNAME, bitlib, NULL, NULL},
#if defined(COLORLCD)
{LUA_TABLIBNAME, tab_funcs, NULL},
{LUA_TABLIBNAME, tab_funcs, NULL, NULL},
#endif
{NULL, NULL, NULL}
{NULL, NULL, NULL, NULL}
};
/*
+53 -25
View File
@@ -3,40 +3,63 @@
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lstring.h"
#include "lrotable.h"
/* Local defines */
#define LUAR_FINDFUNCTION 0
#define LUAR_FINDVALUE 1
/* Utility function: find a key in a given table of functions */
static luaR_result luaR_findfunctionkey(const luaL_Reg* pf, const char * key, TValue * found) {
if(!pf)
return 0;
/* Utility function: find a key in a given table (of functions or constants) */
static luaR_result luaR_findkey(const void * where, const char * key, int type, TValue * found) {
const char *pname;
const luaL_Reg *pf = (luaL_Reg*)where;
const luaR_value_entry *pv = (luaR_value_entry*)where;
int isfunction = type == LUAR_FINDFUNCTION;
if(!where)
return 0;
while(1) {
if (!(pname = isfunction ? pf->name : pv->name))
if (!pf->name)
break;
if (!strcmp(pname, key)) {
if (isfunction) {
setlfvalue(found, pf->func);
}
else {
setnvalue(found, pv->value);
}
if (!strcmp(pf->name, key)) {
setlfvalue(found, pf->func);
return 1;
}
pf ++; pv ++;
pf++;
}
return 0;
}
/* Utility function: find a key in a given table of constants */
static luaR_result luaR_findconstantkey(const luaR_value_entry* pv, const char * key, TValue * found) {
if(!pv)
return 0;
while(1) {
if (!pv->name)
break;
if (!strcmp(pv->name, key)) {
setnvalue(found, pv->value);
return 1;
}
pv++;
}
return 0;
}
/* Utility function: find a key in a given table of strings */
static luaR_result luaR_findstringkey(lua_State *L, const luaR_string_entry* ps, const char * key, TValue * found) {
if(!ps)
return 0;
while(1) {
if (!ps->name)
break;
if (!strcmp(ps->name, key)) {
TString *ts = luaS_new(L, ps->string);
setsvalue(L, found, ts);
return 1;
}
ps++;
}
return 0;
}
/* Find a global "read only table" in the constant lua_rotable array */
luaR_result luaR_findglobal(const char * name, TValue * val) {
luaR_result luaR_findglobal(lua_State *L, const char * name, TValue * val) {
unsigned i;
if (strlen(name) > LUA_MAX_ROTABLE_NAME) {
TRACE_LUA_INTERNALS("luaR_findglobal('%s') = NAME TOO LONG", name);
@@ -50,7 +73,7 @@ luaR_result luaR_findglobal(const char * name, TValue * val) {
return 1;
}
if (!strncmp(lua_rotable[i].name, "__", 2)) {
if (luaR_findentry(table, name, val)) {
if (luaR_findentry(L, table, name, val)) {
TRACE_LUA_INTERNALS("luaR_findglobal('%s') = FOUND in table '%s'", name, lua_rotable[i].name);
return 1;
}
@@ -61,18 +84,23 @@ luaR_result luaR_findglobal(const char * name, TValue * val) {
}
luaR_result luaR_findentry(void *data, const char * key, TValue * val) {
luaR_result luaR_findentry(lua_State *L, void *data, const char * key, TValue * val) {
luaR_table * table = (luaR_table *)data;
/* First look at the functions */
if (luaR_findkey(table->pfuncs, key, LUAR_FINDFUNCTION, val)) {
if (luaR_findfunctionkey(table->pfuncs, key, val)) {
TRACE_LUA_INTERNALS("luaR_findentry(%p[%s], '%s') = FUNCTION %p", table, table->name, key, lfvalue(val));
return 1;
}
else if (luaR_findkey(table->pvalues, key, LUAR_FINDVALUE, val)) {
else if (luaR_findconstantkey(table->pvalues, key, val)) {
/* Then at the values */
TRACE_LUA_INTERNALS("luaR_findentry(%p[%s], '%s') = NUMBER %g", table, table->name, key, nvalue(val));
return 1;
}
else if (luaR_findstringkey(L, table->pstrings, key, val)) {
/* Then at the strings */
TRACE_LUA_INTERNALS("luaR_findentry(%p[%s], '%s') = STRING %s", table, table->name, key, svalue(val));
return 1;
}
TRACE_LUA_INTERNALS("luaR_findentry(%p[%s], '%s') = NOT FOUND", table, table->name, key);
return 0;
}
+11 -2
View File
@@ -17,9 +17,17 @@ typedef struct
lua_Number value;
} luaR_value_entry;
// A string entry in the read only table
typedef struct
{
const char *name;
const char *string;
} luaR_string_entry;
extern const luaR_value_entry baselib_vals[];
extern const luaR_value_entry mathlib_vals[];
extern const luaR_value_entry opentxConstants[];
extern const luaR_string_entry edgetxStrings[];
// A mapping between table name and its entries
typedef struct
@@ -27,11 +35,12 @@ typedef struct
const char *name;
const luaL_Reg *pfuncs;
const luaR_value_entry *pvalues;
const luaR_string_entry *pstrings;
} luaR_table;
extern const luaR_table lua_rotable[];
luaR_result luaR_findglobal(const char * name, TValue * val);
luaR_result luaR_findentry(void * data, const char * key, TValue * val);
luaR_result luaR_findglobal(lua_State *L, const char * name, TValue * val);
luaR_result luaR_findentry(lua_State *L, void * data, const char * key, TValue * val);
#endif
+2 -2
View File
@@ -123,7 +123,7 @@ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
char keyname[LUA_MAX_ROTABLE_NAME + 1];
lua_getcstr(keyname, rawtsvalue(key), LUA_MAX_ROTABLE_NAME);
TRACE_LUA_INTERNALS("luaV_gettable(%s)", keyname);
luaR_findentry(rvalue(t), keyname, val);
luaR_findentry(L, rvalue(t), keyname, val);
}
return;
}
@@ -624,7 +624,7 @@ void luaV_execute (lua_State *L) {
char keyname[LUA_MAX_ROTABLE_NAME + 1];
lua_getcstr(keyname, rawtsvalue(RKC(i)), LUA_MAX_ROTABLE_NAME);
TRACE_LUA_INTERNALS("luaV_execute(OP_GETTABUP, %s)", keyname);
if (luaR_findglobal(keyname, ra)) {
if (luaR_findglobal(L, keyname, ra)) {
break;
}
}