diff --git a/README.md b/README.md index 159fd2f3..d4f88930 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,13 @@ Tencent QQ group: 309957875
- cross compiler # Change log +2015-02-13
+add node.compile() api to compile lua text file into lua bytecode file.
+this will reduce memory usage noticeably when require modules into NodeMCU.
+raise internal LUA_BUFFERSIZE from 1024 to 4096.
+lua require("mod") will load "mod.lc" file first if exist.
+build latest pre_build bin. + 2015-02-12
fix float print.
update spiffs, add file.rename api to file module.
@@ -297,6 +304,7 @@ cu:send("hello") ####Use DS18B20 module extends your esp8266 ```lua -- read temperature with DS18B20 + node.compile("ds18b20.lua") -- run this only once to compile and save to "ds18b20.lc" t=require("ds18b20") t.setup(9) addrs=t.addrs() diff --git a/app/include/user_config.h b/app/include/user_config.h index a47054fb..c6ec1e0e 100644 --- a/app/include/user_config.h +++ b/app/include/user_config.h @@ -7,7 +7,7 @@ #define NODE_VERSION_INTERNAL 0U #define NODE_VERSION "NodeMCU 0.9.5" -#define BUILD_DATE "build 20150212" +#define BUILD_DATE "build 20150213" // #define DEVKIT_VERSION_0_9 1 // define this only if you use NodeMCU devkit v0.9 diff --git a/app/lua/luaconf.h b/app/lua/luaconf.h index 90da40dc..3a57e1e7 100644 --- a/app/lua/luaconf.h +++ b/app/lua/luaconf.h @@ -108,7 +108,7 @@ #define LUA_CDIR LUA_ROOT "lib/lua/5.1/" #ifndef LUA_RPC -#define LUA_PATH_DEFAULT "?.lua;?.lc" +#define LUA_PATH_DEFAULT "?.lc;?.lua" #define LUA_CPATH_DEFAULT "" #else // #ifndef LUA_RPC #define LUA_PATH_DEFAULT \ @@ -542,7 +542,7 @@ extern int readline4lua(const char *prompt, char *buffer, int length); /* @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. */ -#define LUAL_BUFFERSIZE BUFSIZ +#define LUAL_BUFFERSIZE (BUFSIZ*4) /* }================================================================== */ diff --git a/app/modules/node.c b/app/modules/node.c index 420b369b..90dd0d96 100644 --- a/app/modules/node.c +++ b/app/modules/node.c @@ -1,8 +1,16 @@ // Module for interfacing with system -//#include "lua.h" -#include "lualib.h" +#include "lua.h" #include "lauxlib.h" + +#include "ldo.h" +#include "lfunc.h" +#include "lmem.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lstring.h" +#include "lundump.h" + #include "platform.h" #include "auxmods.h" #include "lrotable.h" @@ -14,6 +22,7 @@ //#include "spi_flash.h" #include "user_interface.h" #include "flash_api.h" +#include "flash_fs.h" // Lua: restart() static int node_restart( lua_State* L ) @@ -311,6 +320,73 @@ static int node_output( lua_State* L ) return 0; } +static int writer(lua_State* L, const void* p, size_t size, void* u) +{ + UNUSED(L); + int file_fd = *( (int *)u ); + if((FS_OPEN_OK - 1)==file_fd) + return 1; + NODE_DBG("get fd:%d,size:%d\n",file_fd,size); + + if(size!=0 && (size!=fs_write(file_fd, (const char *)p, size)) ) + return 1; + NODE_DBG("write fd:%d,size:%d\n",file_fd,size); + return 0; +} + +#define toproto(L,i) (clvalue(L->top+(i))->l.p) +// Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc +static int node_compile( lua_State* L ) +{ + Proto* f; + int file_fd = FS_OPEN_OK - 1; + size_t len; + const char *fname = luaL_checklstring( L, 1, &len ); + if( len > FS_NAME_MAX_LENGTH ) + return luaL_error(L, "filename too long"); + + char output[FS_NAME_MAX_LENGTH]; + c_strcpy(output, fname); + // check here that filename end with ".lua". + if(len<4 || (c_strcmp( output+len-4,".lua")!=0) ) + return luaL_error(L, "not a .lua file"); + + output[c_strlen(output)-2] = 'c'; + output[c_strlen(output)-1] = '\0'; + NODE_DBG(output); + NODE_DBG("\n"); + if (luaL_loadfsfile(L,fname)!=0){ + return luaL_error(L, lua_tostring(L,-1)); + } + + f = toproto(L,-1); + + int stripping = 1; /* strip debug information? */ + + file_fd = fs_open(output, fs_mode2flag("w+")); + if(file_fd < FS_OPEN_OK) + { + return luaL_error(L, "cannot open/write to file"); + } + + lua_lock(L); + int result=luaU_dump(L,f,writer,&file_fd,stripping); + lua_unlock(L); + + fs_flush(file_fd); + fs_close(file_fd); + file_fd = FS_OPEN_OK - 1; + + if (result==LUA_ERR_CC_INTOVERFLOW){ + return luaL_error(L, "value too big or small for target integer type"); + } + if (result==LUA_ERR_CC_NOTINTEGER){ + return luaL_error(L, "target lua_Number is integral but fractional value found"); + } + + return 0; +} + // Module function map #define MIN_OPT_LEVEL 2 #include "lrodefs.h" @@ -330,6 +406,7 @@ const LUA_REG_TYPE node_map[] = { LSTRKEY( "input" ), LFUNCVAL( node_input ) }, { LSTRKEY( "output" ), LFUNCVAL( node_output ) }, { LSTRKEY( "readvdd33" ), LFUNCVAL( node_readvdd33) }, + { LSTRKEY( "compile" ), LFUNCVAL( node_compile) }, // Combined to dsleep(us, option) // { LSTRKEY( "dsleepsetoption" ), LFUNCVAL( node_deepsleep_setoption) }, #if LUA_OPTIMIZE_MEMORY > 0 diff --git a/examples/fragment.lua b/examples/fragment.lua index b4fb10dd..bceadaf3 100644 --- a/examples/fragment.lua +++ b/examples/fragment.lua @@ -339,3 +339,11 @@ uart.on("data",4, function(data) end end, 0) +file.open("hello.lua","w+") +file.writeline([[print("hello nodemcu")]]) +file.writeline([[print(node.heap())]]) +file.close() + +node.compile("hello.lua") +dofile("hello.lua") +dofile("hello.lc") diff --git a/pre_build/0.9.5/nodemcu_20150212.bin b/pre_build/0.9.5/nodemcu_20150212.bin new file mode 100644 index 00000000..887e4833 Binary files /dev/null and b/pre_build/0.9.5/nodemcu_20150212.bin differ diff --git a/pre_build/latest/nodemcu_latest.bin b/pre_build/latest/nodemcu_latest.bin index 887e4833..cb4fa0b1 100644 Binary files a/pre_build/latest/nodemcu_latest.bin and b/pre_build/latest/nodemcu_latest.bin differ