Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
a0d27059
Commit
a0d27059
authored
Jun 18, 2020
by
vsky
Committed by
Nathaniel Wesley Filardo
Sep 03, 2020
Browse files
LFS lua example Lua 5.3 compatibility
parent
ba611101
Changes
2
Hide whitespace changes
Inline
Side-by-side
lua_examples/lfs/_init.lua
View file @
a0d27059
...
@@ -8,10 +8,10 @@
...
@@ -8,10 +8,10 @@
module related initialisaion in this. This example uses standard Lua features to
module related initialisaion in this. This example uses standard Lua features to
simplify the LFS API.
simplify the LFS API.
T
he first section adds a 'LFS' table to _G and uses the __index
metamethod to
For Lua 5.1, t
he first section adds a 'LFS' table to _G and uses the __index
resolve functions in the LFS, so you can execute the main
function of module
metamethod to
resolve functions in the LFS, so you can execute the main
'fred' by executing LFS.fred(params), etc.
It also implements some standard
function of module
'fred' by executing LFS.fred(params), etc.
readonly properties:
It also implements some standard
readonly properties:
LFS._time The Unix Timestamp when the luac.cross was executed. This can be
LFS._time The Unix Timestamp when the luac.cross was executed. This can be
used as a version identifier.
used as a version identifier.
...
@@ -24,36 +24,44 @@
...
@@ -24,36 +24,44 @@
print(table.concat(LFS._list,'\n'))
print(table.concat(LFS._list,'\n'))
gives you a single column listing of all modules in the LFS.
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
index
=
node
.
flashindex
local
G
=
_ENV
or
getfenv
()
local
lfs_t
=
{
local
lfs_t
__index
=
function
(
_
,
name
)
if
_VERSION
==
'Lua 5.1'
then
local
fn_ut
,
ba
,
ma
,
size
,
modules
=
index
(
name
)
lfs_t
=
{
if
not
ba
then
__index
=
function
(
_
,
name
)
return
fn_ut
local
fn_ut
,
ba
,
ma
,
size
,
modules
=
index
(
name
)
elseif
name
==
'_time'
then
if
not
ba
then
return
fn_ut
return
fn_ut
elseif
name
==
'_config'
then
elseif
name
==
'_time'
then
local
fs_ma
,
fs_size
=
file
.
fscfg
()
return
fn_ut
return
{
lfs_base
=
ba
,
lfs_mapped
=
ma
,
lfs_size
=
size
,
elseif
name
==
'_config'
then
fs_mapped
=
fs_ma
,
fs_size
=
fs_size
}
local
fs_ma
,
fs_size
=
file
.
fscfg
()
elseif
name
==
'_list'
then
return
{
lfs_base
=
ba
,
lfs_mapped
=
ma
,
lfs_size
=
size
,
return
modules
fs_mapped
=
fs_ma
,
fs_size
=
fs_size
}
else
elseif
name
==
'_list'
then
return
nil
return
modules
end
else
end
,
return
nil
end
__newindex
=
function
(
_
,
name
,
value
)
-- luacheck: no unused
end
,
error
(
"LFS is readonly. Invalid write to LFS."
..
name
,
2
)
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
)
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
The second section adds the LFS to the require searchlist, so that you can
...
@@ -67,18 +75,9 @@ G.LFS = setmetatable(lfs_t,lfs_t)
...
@@ -67,18 +75,9 @@ G.LFS = setmetatable(lfs_t,lfs_t)
---------------------------------------------------------------------------------]]
---------------------------------------------------------------------------------]]
package
.
loaders
[
3
]
=
function
(
module
)
-- loader_flash
package
.
loaders
[
3
]
=
function
(
module
)
-- loader_flash
local
fn
,
ba
=
index
(
module
)
return
lfs_t
[
module
]
return
ba
and
"Module not in LFS"
or
fn
end
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
These replaces the builtins loadfile & dofile with ones which preferentially
loads the corresponding module from LFS if present. Flipping the search order
loads the corresponding module from LFS if present. Flipping the search order
...
@@ -97,5 +96,3 @@ G.dofile = function(n)
...
@@ -97,5 +96,3 @@ G.dofile = function(n)
local
fn
,
ba
=
index
(
mod
)
local
fn
,
ba
=
index
(
mod
)
if
ba
or
(
ext
~=
'lc'
and
ext
~=
'lua'
)
then
return
df
(
n
)
else
return
fn
()
end
if
ba
or
(
ext
~=
'lc'
and
ext
~=
'lua'
)
then
return
df
(
n
)
else
return
fn
()
end
end
end
tools/luacheck_config.lua
View file @
a0d27059
...
@@ -430,6 +430,16 @@ stds.nodemcu_libs = {
...
@@ -430,6 +430,16 @@ stds.nodemcu_libs = {
MEDIUM_PRIORITY
=
empty
,
MEDIUM_PRIORITY
=
empty
,
HIGH_PRIORITY
=
empty
HIGH_PRIORITY
=
empty
}
}
},
LFS
=
{
read_only
=
true
,
fields
=
{
config
=
empty
,
get
=
empty
,
list
=
empty
,
reload
=
empty
,
time
=
empty
}
}
}
}
}
},
},
...
@@ -919,7 +929,8 @@ stds.nodemcu_libs = {
...
@@ -919,7 +929,8 @@ stds.nodemcu_libs = {
pack
=
empty
,
pack
=
empty
,
unpack
=
empty
,
unpack
=
empty
,
size
=
empty
,
size
=
empty
,
package
=
{
fields
=
{
seeall
=
read_write
}}
package
=
{
fields
=
{
seeall
=
read_write
}},
_ENV
=
empty
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment