Skip to content

Instantly share code, notes, and snippets.

@TerryE
Last active May 25, 2020 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TerryE/cf74d2a496332cadb0fb506e24cb48d0 to your computer and use it in GitHub Desktop.
Save TerryE/cf74d2a496332cadb0fb506e24cb48d0 to your computer and use it in GitHub Desktop.
Fash Lua module loader
// 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