Post your best Lua script examples here

User avatar
By TerryE
#16456 For those interested in what the nodeMCU compiler generates, you will need to copy the .lc file onto your dev PC and use this utility: ChunkySpy. The latest version has 5.1 and 5.2 variants. You will need to use the 5.1 version (0.9.8). Note that because the eLua system has added extra constant types (rotables, light functions and light user data), you will need to apply the following patch, and use the --elua option to disassemble the nodeMCU complied code files:
Code: Select all--- a/ChunkSpy-0.9.8/5.1/ChunkSpy.lua   2015-05-04 12:39:01.267975498 +0100
+++ b/ChunkSpy-0.9.8/5.1/ChunkSpy.lua   2015-05-04 12:35:59.623983095 +0100
@@ -2193,6 +2193,9 @@
           config.AUTO_DETECT = true
         elseif a == "--brief" then
           config.DISPLAY_BRIEF = true
+        elseif a == "--elua" then
+          config.LUA_TNUMBER = 5
+          config.LUA_TSTRING = 6
         elseif a == "--interact" then
           perform = ChunkSpy_Interact



Happy dissassembly!
User avatar
By TerryE
#16549 One immediate point to note from this is that there are two methods of saving compiled Lua to flash
  1. the first is to use node.compile() on the lua file
  2. the second is to use loadfile() to load it, then use string.dump()to convert it in-RAM to a serialised load format and then write this back to the file system.
It is well worth noting that these do not generate the same output. Basically, option (1) strips out all line and variable information, that the runtime error system uses to report line numbers, etc. and the second doesn't. What this means is that (1) is typically 60% the size of [2], but that any runtime errors are extremely limited.

So my recommendation is to use (1) if you have stable production code that you want to run in as low a RAM footprint as possible and (2) if you are still debugging, but want to avoid compilation overhead as any errors will be reported just the same as with load-and-go lua source.