Further adaptation of lfs/_init.lua

See https://github.com/nodemcu/nodemcu-firmware/issues/3278 .  Thanks to
@vsky279 and @bazooka07 for suggestions.
This commit is contained in:
Nathaniel Wesley Filardo 2020-09-22 20:41:58 +01:00 committed by Marcel Stör
parent 3a17258d39
commit 29e5108876
1 changed files with 13 additions and 15 deletions

View File

@ -28,13 +28,13 @@
of the code is skipped. of the code is skipped.
---------------------------------------------------------------------------------]] ---------------------------------------------------------------------------------]]
local index = node.flashindex local lfsindex = node.LFS and node.LFS.get or node.flashindex
local G=_ENV or getfenv() local G=_ENV or getfenv()
local lfs_t local lfs_t
if _VERSION == 'Lua 5.1' then if _VERSION == 'Lua 5.1' then
lfs_t = { lfs_t = {
__index = function(_, name) __index = function(_, name)
local fn_ut, ba, ma, size, modules = index(name) local fn_ut, ba, ma, size, modules = lfsindex(name)
if not ba then if not ba then
return fn_ut return fn_ut
elseif name == '_time' then elseif name == '_time' then
@ -78,21 +78,19 @@ package.loaders[3] = function(module) -- loader_flash
return lfs_t[module] return lfs_t[module]
end end
--[[------------------------------------------------------------------------------- --[[----------------------------------------------------------------------------
These replaces the builtins loadfile & dofile with ones which preferentially These replace the builtins loadfile & dofile with ones which preferentially
loads the corresponding module from LFS if present. Flipping the search order load from the filesystem and fall back to LFS. Flipping the search order
is an exercise left to the reader.- is an exercise left to the reader.-
---------------------------------------------------------------------------------]] ------------------------------------------------------------------------------]]
local lf, df = loadfile, dofile local lf = loadfile
G.loadfile = function(n) G.loadfile = function(n)
local mod, ext = n:match("(.*)%.(l[uc]a?)"); if file.exists(n) then return lf(n) end
local fn, ba = index(mod) local mod = n:match("(.*)%.l[uc]a?$")
if ba or (ext ~= 'lc' and ext ~= 'lua') then return lf(n) else return fn end local fn = mod and lfsindex(mod)
return (fn or error (("Cannot find '%s' in FS or LFS"):format(n))) and fn
end end
G.dofile = function(n) -- Lua's dofile (luaB_dofile) reaches directly for luaL_loadfile; shim instead
local mod, ext = n:match("(.*)%.(l[uc]a?)"); G.dofile = function(n) return assert(loadfile(n))() end
local fn, ba = index(mod)
if ba or (ext ~= 'lc' and ext ~= 'lua') then return df(n) else return fn() end
end