_init.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. --
  2. -- File: _init.lua
  3. --[[
  4. This is a template for the LFS equivalent of the SPIFFS init.lua.
  5. It is a good idea to such an _init.lua module to your LFS and do most of the LFS
  6. module related initialisaion in this. This example uses standard Lua features to
  7. simplify the LFS API.
  8. For Lua 5.1, the first section adds a 'LFS' table to _G and uses the __index
  9. metamethod to resolve functions in the LFS, so you can execute the main
  10. function of module 'fred' by executing LFS.fred(params), etc.
  11. It also implements some standard readonly properties:
  12. LFS._time The Unix Timestamp when the luac.cross was executed. This can be
  13. used as a version identifier.
  14. LFS._config This returns a table of useful configuration parameters, hence
  15. print (("0x%6x"):format(LFS._config.lfs_base))
  16. gives you the parameter to use in the luac.cross -a option.
  17. LFS._list This returns a table of the LFS modules, hence
  18. print(table.concat(LFS._list,'\n'))
  19. gives you a single column listing of all modules in the LFS.
  20. For Lua 5.3 LFS table is populated by the LFS implementation in C so this part
  21. of the code is skipped.
  22. ---------------------------------------------------------------------------------]]
  23. local lfsindex = node.LFS and node.LFS.get or node.flashindex
  24. local G=_ENV or getfenv()
  25. local lfs_t
  26. if _VERSION == 'Lua 5.1' then
  27. lfs_t = {
  28. __index = function(_, name)
  29. local fn_ut, ba, ma, size, modules = lfsindex(name)
  30. if not ba then
  31. return fn_ut
  32. elseif name == '_time' then
  33. return fn_ut
  34. elseif name == '_config' then
  35. local fs_ma, fs_size = file.fscfg()
  36. return {lfs_base = ba, lfs_mapped = ma, lfs_size = size,
  37. fs_mapped = fs_ma, fs_size = fs_size}
  38. elseif name == '_list' then
  39. return modules
  40. else
  41. return nil
  42. end
  43. end,
  44. __newindex = function(_, name, value) -- luacheck: no unused
  45. error("LFS is readonly. Invalid write to LFS." .. name, 2)
  46. end,
  47. }
  48. setmetatable(lfs_t,lfs_t)
  49. G.module = nil -- disable Lua 5.0 style modules to save RAM
  50. package.seeall = nil
  51. else
  52. lfs_t = node.LFS
  53. end
  54. G.LFS = lfs_t
  55. --[[-------------------------------------------------------------------------------
  56. The second section adds the LFS to the require searchlist, so that you can
  57. require a Lua module 'jean' in the LFS by simply doing require "jean". However
  58. note that this is at the search entry following the FS searcher, so if you also
  59. have jean.lc or jean.lua in SPIFFS, then this SPIFFS version will get loaded into
  60. RAM instead of using. (Useful, for development).
  61. See docs/en/lfs.md and the 'loaders' array in app/lua/loadlib.c for more details.
  62. ---------------------------------------------------------------------------------]]
  63. package.loaders[3] = function(module) -- loader_flash
  64. return lfs_t[module]
  65. end
  66. --[[----------------------------------------------------------------------------
  67. These replace the builtins loadfile & dofile with ones which preferentially
  68. load from the filesystem and fall back to LFS. Flipping the search order
  69. is an exercise left to the reader.-
  70. ------------------------------------------------------------------------------]]
  71. local lf = loadfile
  72. G.loadfile = function(n)
  73. if file.exists(n) then return lf(n) end
  74. local mod = n:match("(.*)%.l[uc]a?$")
  75. local fn = mod and lfsindex(mod)
  76. return (fn or error (("Cannot find '%s' in FS or LFS"):format(n))) and fn
  77. end
  78. -- Lua's dofile (luaB_dofile) reaches directly for luaL_loadfile; shim instead
  79. G.dofile = function(n) return assert(loadfile(n))() end