Last active
May 25, 2020 03:48
-
-
Save TerryE/cf74d2a496332cadb0fb506e24cb48d0 to your computer and use it in GitHub Desktop.
Fash Lua module loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Module for interfacing with system | |
#include "module.h" | |
#include "lauxlib.h" | |
#include <string.h> | |
#include "user_interface.h" | |
#define LUA_USE_MODULES_FAST | |
static int compile (lua_State *L, int ndx) { | |
size_t l; | |
const char *name = lua_tolstring(L, ndx, &l); | |
const char *src = NULL; | |
switch (l) { | |
#include "fast_int.h" | |
default: src = NULL; | |
} | |
if (src) { | |
uint32_t start = system_get_time(); | |
// NODE_DBG("Module %s is \n%s\n", name, src); | |
int status = luaL_loadbuffer(L, src, strlen(src), name); | |
if (status != LUA_OK) | |
lua_error(L); | |
NODE_DBG("Module %s compiled in %u mSec\n", name, (system_get_time()-start)>>10); | |
} else { | |
lua_pushnil(L); | |
} | |
return 1; | |
} | |
// Lua: f=fast.load(name) | |
static int fast_load (lua_State *L) { | |
return compile(L, 1); | |
} | |
// Lua: f=fast.name -- uses meta entry __index=fastindex(table, name) | |
static int fast_index (lua_State *L) { | |
return compile(L, 2); | |
} | |
LROT_BEGIN(fast_meta, NULL, LROT_MASK_INDEX) | |
LROT_FUNCENTRY( __index, fast_index ) | |
LROT_END(fast_meta, NULL, LROT_MASK_INDEX) | |
LROT_BEGIN(fast, LROT_TABLEREF(fast_meta), 0) | |
LROT_FUNCENTRY( load, fast_load ) | |
LROT_END(fast, LROT_TABLEREF(fast_meta), 0) | |
NODEMCU_MODULE(FAST, "fast", fast, NULL); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment