2018-06-22 23:29:16 +02:00
|
|
|
--
|
|
|
|
-- File: _init.lua
|
|
|
|
--[[
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
This is a template for the LFS equivalent of the SPIFFS init.lua.
|
2018-06-22 23:29:16 +02:00
|
|
|
|
|
|
|
It is a good idea to such an _init.lua module to your LFS and do most of the LFS
|
|
|
|
module related initialisaion in this. This example uses standard Lua features to
|
2019-02-17 19:26:29 +01:00
|
|
|
simplify the LFS API.
|
2018-06-22 23:29:16 +02:00
|
|
|
|
2020-06-18 21:42:13 +02:00
|
|
|
For Lua 5.1, the first section adds a 'LFS' table to _G and uses the __index
|
|
|
|
metamethod to resolve functions in the LFS, so you can execute the main
|
|
|
|
function of module 'fred' by executing LFS.fred(params), etc.
|
|
|
|
It also implements some standard readonly properties:
|
2018-06-22 23:29:16 +02:00
|
|
|
|
|
|
|
LFS._time The Unix Timestamp when the luac.cross was executed. This can be
|
|
|
|
used as a version identifier.
|
|
|
|
|
|
|
|
LFS._config This returns a table of useful configuration parameters, hence
|
|
|
|
print (("0x%6x"):format(LFS._config.lfs_base))
|
|
|
|
gives you the parameter to use in the luac.cross -a option.
|
|
|
|
|
|
|
|
LFS._list This returns a table of the LFS modules, hence
|
2018-08-11 13:48:46 +02:00
|
|
|
print(table.concat(LFS._list,'\n'))
|
2018-06-22 23:29:16 +02:00
|
|
|
gives you a single column listing of all modules in the LFS.
|
|
|
|
|
2020-06-18 21:42:13 +02:00
|
|
|
For Lua 5.3 LFS table is populated by the LFS implementation in C so this part
|
|
|
|
of the code is skipped.
|
2019-02-17 19:26:29 +01:00
|
|
|
---------------------------------------------------------------------------------]]
|
2018-06-22 23:29:16 +02:00
|
|
|
|
2020-09-22 21:41:58 +02:00
|
|
|
local lfsindex = node.LFS and node.LFS.get or node.flashindex
|
2020-06-18 21:42:13 +02:00
|
|
|
local G=_ENV or getfenv()
|
|
|
|
local lfs_t
|
|
|
|
if _VERSION == 'Lua 5.1' then
|
|
|
|
lfs_t = {
|
|
|
|
__index = function(_, name)
|
2020-09-22 21:41:58 +02:00
|
|
|
local fn_ut, ba, ma, size, modules = lfsindex(name)
|
2020-06-18 21:42:13 +02:00
|
|
|
if not ba then
|
|
|
|
return fn_ut
|
|
|
|
elseif name == '_time' then
|
|
|
|
return fn_ut
|
|
|
|
elseif name == '_config' then
|
|
|
|
local fs_ma, fs_size = file.fscfg()
|
|
|
|
return {lfs_base = ba, lfs_mapped = ma, lfs_size = size,
|
|
|
|
fs_mapped = fs_ma, fs_size = fs_size}
|
|
|
|
elseif name == '_list' then
|
|
|
|
return modules
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
__newindex = function(_, name, value) -- luacheck: no unused
|
|
|
|
error("LFS is readonly. Invalid write to LFS." .. name, 2)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(lfs_t,lfs_t)
|
|
|
|
G.module = nil -- disable Lua 5.0 style modules to save RAM
|
|
|
|
package.seeall = nil
|
|
|
|
else
|
|
|
|
lfs_t = node.LFS
|
|
|
|
end
|
|
|
|
G.LFS = lfs_t
|
2018-06-22 23:29:16 +02:00
|
|
|
|
|
|
|
--[[-------------------------------------------------------------------------------
|
|
|
|
The second section adds the LFS to the require searchlist, so that you can
|
|
|
|
require a Lua module 'jean' in the LFS by simply doing require "jean". However
|
|
|
|
note that this is at the search entry following the FS searcher, so if you also
|
2019-02-17 19:26:29 +01:00
|
|
|
have jean.lc or jean.lua in SPIFFS, then this SPIFFS version will get loaded into
|
2018-06-22 23:29:16 +02:00
|
|
|
RAM instead of using. (Useful, for development).
|
|
|
|
|
2018-08-11 13:48:46 +02:00
|
|
|
See docs/en/lfs.md and the 'loaders' array in app/lua/loadlib.c for more details.
|
2018-06-22 23:29:16 +02:00
|
|
|
|
|
|
|
---------------------------------------------------------------------------------]]
|
|
|
|
|
2018-08-22 12:09:04 +02:00
|
|
|
package.loaders[3] = function(module) -- loader_flash
|
2020-06-18 21:42:13 +02:00
|
|
|
return lfs_t[module]
|
2018-08-22 12:09:04 +02:00
|
|
|
end
|
2018-06-22 23:29:16 +02:00
|
|
|
|
2020-09-22 21:41:58 +02:00
|
|
|
--[[----------------------------------------------------------------------------
|
|
|
|
These replace the builtins loadfile & dofile with ones which preferentially
|
|
|
|
load from the filesystem and fall back to LFS. Flipping the search order
|
2018-09-29 15:57:51 +02:00
|
|
|
is an exercise left to the reader.-
|
2020-09-22 21:41:58 +02:00
|
|
|
------------------------------------------------------------------------------]]
|
2018-09-29 15:57:51 +02:00
|
|
|
|
2020-09-22 21:41:58 +02:00
|
|
|
local lf = loadfile
|
2018-09-29 15:57:51 +02:00
|
|
|
G.loadfile = function(n)
|
2020-09-22 21:41:58 +02:00
|
|
|
if file.exists(n) then return lf(n) end
|
|
|
|
local mod = n:match("(.*)%.l[uc]a?$")
|
|
|
|
local fn = mod and lfsindex(mod)
|
|
|
|
return (fn or error (("Cannot find '%s' in FS or LFS"):format(n))) and fn
|
2018-09-29 15:57:51 +02:00
|
|
|
end
|
|
|
|
|
2020-09-22 21:41:58 +02:00
|
|
|
-- Lua's dofile (luaB_dofile) reaches directly for luaL_loadfile; shim instead
|
|
|
|
G.dofile = function(n) return assert(loadfile(n))() end
|