Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes lfs/_init.lua compatible with Lua 5.3 #3168

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 40 additions & 43 deletions lua_examples/lfs/_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
module related initialisaion in this. This example uses standard Lua features to
simplify the LFS API.

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:
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.
nwf marked this conversation as resolved.
Show resolved Hide resolved
It also implements some standard readonly properties:

LFS._time The Unix Timestamp when the luac.cross was executed. This can be
used as a version identifier.
Expand All @@ -24,36 +24,44 @@
print(table.concat(LFS._list,'\n'))
gives you a single column listing of all modules in the LFS.

For Lua 5.3 LFS table is populated by the LFS implementation in C so this part
of the code is skipped.
---------------------------------------------------------------------------------]]

local index = node.flashindex

local lfs_t = {
__index = function(_, name)
local fn_ut, ba, ma, size, modules = index(name)
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,

}

local G=getfenv()
G.LFS = setmetatable(lfs_t,lfs_t)
local G=_ENV or getfenv()
local lfs_t
if _VERSION == 'Lua 5.1' then
vsky279 marked this conversation as resolved.
Show resolved Hide resolved
lfs_t = {
__index = function(_, name)
local fn_ut, ba, ma, size, modules = index(name)
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

--[[-------------------------------------------------------------------------------
The second section adds the LFS to the require searchlist, so that you can
Expand All @@ -67,18 +75,9 @@ G.LFS = setmetatable(lfs_t,lfs_t)
---------------------------------------------------------------------------------]]

package.loaders[3] = function(module) -- loader_flash
local fn, ba = index(module)
return ba and "Module not in LFS" or fn
return lfs_t[module]
end

--[[-------------------------------------------------------------------------------
You can add any other initialisation here, for example a couple of the globals
are never used, so setting them to nil saves a couple of global entries
---------------------------------------------------------------------------------]]

G.module = nil -- disable Lua 5.0 style modules to save RAM
package.seeall = nil

--[[-------------------------------------------------------------------------------
These replaces the builtins loadfile & dofile with ones which preferentially
loads the corresponding module from LFS if present. Flipping the search order
Expand All @@ -97,5 +96,3 @@ G.dofile = function(n)
local fn, ba = index(mod)
vsky279 marked this conversation as resolved.
Show resolved Hide resolved
if ba or (ext ~= 'lc' and ext ~= 'lua') then return df(n) else return fn() end
end


13 changes: 12 additions & 1 deletion tools/luacheck_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,16 @@ stds.nodemcu_libs = {
MEDIUM_PRIORITY = empty,
HIGH_PRIORITY = empty
}
},
LFS = {
read_only = true,
fields = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a question for this PR, but since you're in the neighborhood: should we suggest to advanced developers how to add entries here? People using their own LFS might want their code to still be luacheck-clean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nwf I think it would be a good idea to make some kind of documentation on how to use luacheck on NodeMCU code. I've written luacheck_config_helper.lua script when I was adding luacheck support but I haven't documented it anywhere besides some comments in the script itself. Should this be added to GitHub Wiki, Extension Developer FAQ or somewhere else?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be a good idea to make some kind of documentation on how to use luacheck on NodeMCU code.

I am more of the view: what's wrong with RTFM? If anyone wants to read up on how to use luacehck then it comes with a lot of good documentation. May a reference to it in our FAQs.

config = empty,
get = empty,
list = empty,
reload = empty,
time = empty
}
}
}
},
Expand Down Expand Up @@ -919,7 +929,8 @@ stds.nodemcu_libs = {
pack = empty,
unpack = empty,
size = empty,
package = {fields = {seeall = read_write}}
package = {fields = {seeall = read_write}},
_ENV = empty
}
}

Expand Down