Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated ROTables 1st tranch #2742

Merged
merged 12 commits into from
May 8, 2019
2 changes: 0 additions & 2 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ DEPENDS_eagle.app.v6 = \
# -DWLAN_CONFIG_CCX
CONFIGURATION_DEFINES = -D__ets__ \
-DICACHE_FLASH \
-DLUA_OPTIMIZE_MEMORY=2 \
-DMIN_OPT_LEVEL=2 \
-DLWIP_OPEN_SRC \
-DPBUF_RSV_FOR_WLAN \
-DEBUF_LWIP \
Expand Down
15 changes: 5 additions & 10 deletions app/include/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define __MODULE_H__

#include "user_modules.h"
#include "lrodefs.h"
#include "lrotable.h"

/* Registering a module within NodeMCU is really easy these days!
*
Expand All @@ -18,11 +18,11 @@
*
* Then simply put a line like this at the bottom of your module file:
*
* NODEMCU_MODULE(MYNAME, "myname", myname_map, luaopen_myname);
* NODEMCU_MODULE(MYNAME, "myname", myname, luaopen_myname);
*
* or perhaps
*
* NODEMCU_MODULE(MYNAME, "myname", myname_map, NULL);
* NODEMCU_MODULE(MYNAME, "myname", myname, NULL);
*
* if you don't need an init function.
*
Expand Down Expand Up @@ -67,13 +67,8 @@
*/
#define NODEMCU_MODULE(cfgname, luaname, map, initfunc) \
const LOCK_IN_SECTION(libs) \
luaL_Reg MODULE_PASTE_(lua_lib_,cfgname) = { luaname, initfunc }; \
luaR_entry MODULE_PASTE_(lua_lib_,cfgname) = { luaname, LRO_FUNCVAL(initfunc) }; \
const LOCK_IN_SECTION(rotable) \
luaR_entry MODULE_EXPAND_PASTE_(cfgname,MODULE_EXPAND_PASTE_(_module_selected,MODULE_PASTE_(LUA_USE_MODULES_,cfgname))) \
= {LSTRKEY(luaname), LROVAL(map)}

#if !defined(LUA_CROSS_COMPILER) && !(MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2)
# error "NodeMCU modules must be built with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)"
#endif

= {luaname, LRO_ROVAL(map ## _map)}
#endif
16 changes: 5 additions & 11 deletions app/include/pm/pmSleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,17 @@
#define PMSLEEP_ERR(...)
#endif





#define PMSLEEP_SLEEP_MIN_TIME 50000
#define PMSLEEP_SLEEP_MAX_TIME 268435454 //FPM_MAX_SLEEP_TIME-1
#define pmSleep_INIT_CFG(X) pmSleep_param_t X = {.sleep_duration=0, .wake_pin=255, \
.preserve_opmode=TRUE, .suspend_cb_ptr=NULL, .resume_cb_ptr=NULL}

#define PMSLEEP_INT_MAP \
{ LSTRKEY( "INT_BOTH" ), LNUMVAL( GPIO_PIN_INTR_ANYEDGE ) }, \
{ LSTRKEY( "INT_UP" ), LNUMVAL( GPIO_PIN_INTR_POSEDGE ) }, \
{ LSTRKEY( "INT_DOWN" ), LNUMVAL( GPIO_PIN_INTR_NEGEDGE ) }, \
{ LSTRKEY( "INT_HIGH" ), LNUMVAL( GPIO_PIN_INTR_HILEVEL ) }, \
{ LSTRKEY( "INT_LOW" ), LNUMVAL( GPIO_PIN_INTR_LOLEVEL ) }


LROT_NUMENTRY( INT_BOTH, GPIO_PIN_INTR_ANYEDGE ) \
LROT_NUMENTRY( INT_UP, GPIO_PIN_INTR_POSEDGE ) \
LROT_NUMENTRY( INT_DOWN, GPIO_PIN_INTR_NEGEDGE ) \
LROT_NUMENTRY( INT_HIGH, GPIO_PIN_INTR_HILEVEL ) \
LROT_NUMENTRY( INT_LOW, GPIO_PIN_INTR_LOLEVEL )

typedef struct pmSleep_param{
uint32 sleep_duration;
Expand Down
2 changes: 1 addition & 1 deletion app/include/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ extern void luaL_dbgbreak(void);
#endif
#endif

#if !defined(LUA_NUMBER_INTEGRAL) && defined (LUA_DWORD_ALIGNED_TVALUES)
#if !defined(LUA_NUMBER_INTEGRAL) && !defined (LUA_DWORD_ALIGNED_TVALUES)
#define LUA_PACK_TVALUES
#else
#undef LUA_PACK_TVALUES
Expand Down
4 changes: 0 additions & 4 deletions app/lua/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,7 @@ LUALIB_API void (luaL_register) (lua_State *L, const char *libname,

LUALIB_API void (luaL_register_light) (lua_State *L, const char *libname,
const luaL_Reg *l) {
#if LUA_OPTIMIZE_MEMORY > 0
luaI_openlib(L, libname, l, 0, LUA_USELIGHTFUNCTIONS);
#else
luaI_openlib(L, libname, l, 0, LUA_USECCLOSURES);
#endif
}

static int libsize (const luaL_Reg *l) {
Expand Down
128 changes: 59 additions & 69 deletions app/lua/lbaselib.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include C_HEADER_STDLIB
#include "lauxlib.h"
#include "lualib.h"
#include "lrodefs.h"
#include "lrotable.h"



Expand Down Expand Up @@ -462,64 +462,59 @@ static int luaB_newproxy (lua_State *L) {
return 1;
}

#include "lrodefs.h"
#include "lrotable.h"

extern const luaR_entry lua_rotable_base[];
LROT_EXTERN(lua_rotable_base);

/*
* ESP builds use specific linker directives to marshal all ROTable declarations
* into a single ROTable in the PSECT ".lua_rotable".
* Separate ROTables are used for the base functions and library ROTables, with
* the base functions ROTable declared below. The library ROTable is chained
* from this using its __index meta-method.
*
* This is not practical on Posix builds using a standard link so for cross
* compiler builds, separate ROTables are used for the base functions and library
* ROTables, with the latter chained from the former using its __index meta-method.
* In this case all library ROTables are defined in linit.c.
* ESP builds use specific linker directives to marshal all the ROTable entries
* for the library modules into a single ROTable in the PSECT ".lua_rotable".
* This is not practical on Posix builds using a standard GNU link, so the
* equivalent ROTable for the core libraries defined in linit.c for the cross-
* compiler build.
*/
#ifdef LUA_CROSS_COMPILER
#define BASE_ROTABLE base_func_map
#define LOCK_IN_ROTABLE
static const LUA_REG_TYPE base_func_meta[] = {
LROT_TABENTRY(__index, lua_rotable_base),
LROT_END};
#else
#define BASE_ROTABLE lua_rotable_base
#define LOCK_IN_ROTABLE __attribute__((used,unused,section(".lua_rotable")))
#endif

static const LUA_REG_TYPE LOCK_IN_ROTABLE base_func_map[] = {
LROT_FUNCENTRY(assert, luaB_assert),
LROT_FUNCENTRY(collectgarbage, luaB_collectgarbage),
LROT_FUNCENTRY(dofile, luaB_dofile),
LROT_FUNCENTRY(error, luaB_error),
LROT_FUNCENTRY(gcinfo, luaB_gcinfo),
LROT_FUNCENTRY(getfenv, luaB_getfenv),
LROT_FUNCENTRY(getmetatable, luaB_getmetatable),
LROT_FUNCENTRY(loadfile, luaB_loadfile),
LROT_FUNCENTRY(load, luaB_load),
LROT_FUNCENTRY(loadstring, luaB_loadstring),
LROT_FUNCENTRY(next, luaB_next),
LROT_FUNCENTRY(pcall, luaB_pcall),
LROT_FUNCENTRY(print, luaB_print),
LROT_FUNCENTRY(rawequal, luaB_rawequal),
LROT_FUNCENTRY(rawget, luaB_rawget),
LROT_FUNCENTRY(rawset, luaB_rawset),
LROT_FUNCENTRY(select, luaB_select),
LROT_FUNCENTRY(setfenv, luaB_setfenv),
LROT_FUNCENTRY(setmetatable, luaB_setmetatable),
LROT_FUNCENTRY(tonumber, luaB_tonumber),
LROT_FUNCENTRY(tostring, luaB_tostring),
LROT_FUNCENTRY(type, luaB_type),
LROT_FUNCENTRY(unpack, luaB_unpack),
LROT_EXTERN(lua_rotables);

LROT_PUBLIC_BEGIN(base_func_meta)
LROT_TABENTRY( __index, lua_rotables )
LROT_END(base_func, base_func_meta, LROT_MASK_INDEX)

LROT_PUBLIC_BEGIN(base_func)
LROT_FUNCENTRY(assert, luaB_assert)
LROT_FUNCENTRY(collectgarbage, luaB_collectgarbage)
LROT_FUNCENTRY(dofile, luaB_dofile)
LROT_FUNCENTRY(error, luaB_error)
LROT_FUNCENTRY(gcinfo, luaB_gcinfo)
LROT_FUNCENTRY(getfenv, luaB_getfenv)
LROT_FUNCENTRY(getmetatable, luaB_getmetatable)
LROT_FUNCENTRY(loadfile, luaB_loadfile)
LROT_FUNCENTRY(load, luaB_load)
LROT_FUNCENTRY(loadstring, luaB_loadstring)
LROT_FUNCENTRY(next, luaB_next)
LROT_FUNCENTRY(pcall, luaB_pcall)
LROT_FUNCENTRY(print, luaB_print)
LROT_FUNCENTRY(rawequal, luaB_rawequal)
LROT_FUNCENTRY(rawget, luaB_rawget)
LROT_FUNCENTRY(rawset, luaB_rawset)
LROT_FUNCENTRY(select, luaB_select)
LROT_FUNCENTRY(setfenv, luaB_setfenv)
LROT_FUNCENTRY(setmetatable, luaB_setmetatable)
LROT_FUNCENTRY(tonumber, luaB_tonumber)
LROT_FUNCENTRY(tostring, luaB_tostring)
LROT_FUNCENTRY(type, luaB_type)
LROT_FUNCENTRY(unpack, luaB_unpack)
LROT_FUNCENTRY(xpcall, luaB_xpcall)
#ifdef LUA_CROSS_COMPILER
,LROT_TABENTRY(__metatable, base_func_meta),
LROT_END
#endif
};
LROT_TABENTRY(__metatable, base_func_meta)
LROT_END(base_func, base_func_meta, LROT_MASK_INDEX)

static const luaL_Reg base_funcs[] = {
{NULL, NULL}
};
LROT_BEGIN(G_meta)
LROT_TABENTRY( __index, base_func )
LROT_END(G_meta, NULL, 0)


/*
Expand Down Expand Up @@ -650,17 +645,14 @@ static int luaB_corunning (lua_State *L) {
return 1;
}

#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 1
const LUA_REG_TYPE co_funcs[] = {
{LSTRKEY("create"), LFUNCVAL(luaB_cocreate)},
{LSTRKEY("resume"), LFUNCVAL(luaB_coresume)},
{LSTRKEY("running"), LFUNCVAL(luaB_corunning)},
{LSTRKEY("status"), LFUNCVAL(luaB_costatus)},
{LSTRKEY("wrap"), LFUNCVAL(luaB_cowrap)},
{LSTRKEY("yield"), LFUNCVAL(luaB_yield)},
{LNILKEY, LNILVAL}
};
LROT_PUBLIC_BEGIN(co_funcs)
LROT_FUNCENTRY( create, luaB_cocreate )
LROT_FUNCENTRY( resume, luaB_coresume )
LROT_FUNCENTRY( running, luaB_corunning )
LROT_FUNCENTRY( status, luaB_costatus )
LROT_FUNCENTRY( wrap, luaB_cowrap )
LROT_FUNCENTRY( yield, luaB_yield )
LROT_END (co_funcs, NULL, 0)

/* }====================================================== */

Expand All @@ -676,14 +668,12 @@ static void base_open (lua_State *L) {
/* set global _G */
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_setglobal(L, "_G");

/* open lib into global table */
luaL_register_light(L, "_G", base_funcs);
#if LUA_OPTIMIZE_MEMORY > 0
lua_pushvalue(L, -1);
lua_setmetatable(L, -2);
lua_pushrotable(L, (void *)BASE_ROTABLE);
lua_setglobal(L, "__index");
#endif
luaL_register_light(L, "_G", &((luaL_Reg) {0}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait what? Am I reading this right that we're registering a pointer to a temporary variable here? Shouldn't this be referring to base_funcs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

luaL_register_light() does multiple things. It registers the library in the global table (_G itself in this case) and also registers a list of functions. There aren't any functions to be registered here since these are in a ROTable for the baselib, so we either call luaL_register_light() with an empty list or clone and copy the subset of the functionality that we used inline. Doing the call is simpler than cut and paste.

lua_pushrotable(L, LROT_TABLEREF(G_meta));
lua_setmetatable(L, LUA_GLOBALSINDEX);

lua_pushliteral(L, LUA_VERSION);
lua_setglobal(L, "_VERSION"); /* set global _VERSION */
/* `ipairs' and `pairs' need auxliliary functions as upvalues */
Expand Down
44 changes: 20 additions & 24 deletions app/lua/ldblib.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#include "lualib.h"
#include "lstring.h"
#include "lflash.h"
#include "user_modules.h"

#include "lrotable.h"

#include "user_modules.h"

static int db_getregistry (lua_State *L) {
lua_pushvalue(L, LUA_REGISTRYINDEX);
Expand Down Expand Up @@ -417,32 +417,28 @@ static int db_errorfb (lua_State *L) {
return 1;
}

#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 1
#include "lrodefs.h"
const LUA_REG_TYPE dblib[] = {
LROT_PUBLIC_BEGIN(dblib)
#ifndef LUA_USE_BUILTIN_DEBUG_MINIMAL
{LSTRKEY("debug"), LFUNCVAL(db_debug)},
{LSTRKEY("getfenv"), LFUNCVAL(db_getfenv)},
{LSTRKEY("gethook"), LFUNCVAL(db_gethook)},
{LSTRKEY("getinfo"), LFUNCVAL(db_getinfo)},
{LSTRKEY("getlocal"), LFUNCVAL(db_getlocal)},
LROT_FUNCENTRY( debug, db_debug )
LROT_FUNCENTRY( getfenv, db_getfenv )
LROT_FUNCENTRY( gethook, db_gethook )
LROT_FUNCENTRY( getinfo, db_getinfo )
LROT_FUNCENTRY( getlocal, db_getlocal )
#endif
{LSTRKEY("getregistry"), LFUNCVAL(db_getregistry)},
{LSTRKEY("getstrings"), LFUNCVAL(db_getstrings)},
LROT_FUNCENTRY( getregistry, db_getregistry )
LROT_FUNCENTRY( getstrings, db_getstrings )
#ifndef LUA_USE_BUILTIN_DEBUG_MINIMAL
{LSTRKEY("getmetatable"), LFUNCVAL(db_getmetatable)},
{LSTRKEY("getupvalue"), LFUNCVAL(db_getupvalue)},
{LSTRKEY("setfenv"), LFUNCVAL(db_setfenv)},
{LSTRKEY("sethook"), LFUNCVAL(db_sethook)},
{LSTRKEY("setlocal"), LFUNCVAL(db_setlocal)},
{LSTRKEY("setmetatable"), LFUNCVAL(db_setmetatable)},
{LSTRKEY("setupvalue"), LFUNCVAL(db_setupvalue)},
LROT_FUNCENTRY( getmetatable, db_getmetatable )
LROT_FUNCENTRY( getupvalue, db_getupvalue )
LROT_FUNCENTRY( setfenv, db_setfenv )
LROT_FUNCENTRY( sethook, db_sethook )
LROT_FUNCENTRY( setlocal, db_setlocal )
LROT_FUNCENTRY( setmetatable, db_setmetatable )
LROT_FUNCENTRY( setupvalue, db_setupvalue )
#endif
{LSTRKEY("traceback"), LFUNCVAL(db_errorfb)},
{LNILKEY, LNILVAL}
};
LROT_FUNCENTRY( traceback, db_errorfb )
LROT_END(dblib, NULL, 0)

LUALIB_API int luaopen_debug (lua_State *L) {
LREGISTER(L, LUA_DBLIBNAME, dblib);
return 0;
}
Loading