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
a92da3c3
Unverified
Commit
a92da3c3
authored
Aug 22, 2020
by
Terry Ellison
Committed by
GitHub
Aug 22, 2020
Browse files
Lua 5.1 / 5.3 alignment and document (#3193)
parent
1f386e93
Changes
76
Show whitespace changes
Inline
Side-by-side
app/lua53/lauxlib.h
View file @
a92da3c3
...
...
@@ -14,6 +14,9 @@
#include "lua.h"
#ifdef LUA_LIB
#include "lnodemcu.h"
#endif
/* extra error code for 'luaL_loadfilex' */
...
...
@@ -79,7 +82,7 @@ LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
LUALIB_API
int
(
luaL_ref
)
(
lua_State
*
L
,
int
t
);
LUALIB_API
void
(
luaL_unref
)
(
lua_State
*
L
,
int
t
,
int
ref
);
#define luaL_unref2(l,t,r) luaL_unref(L, (t), (r)); r = LUA_NOREF
#define luaL_unref2(l,t,r)
do {
luaL_unref(L, (t), (r)); r = LUA_NOREF
;} while (0)
LUALIB_API
void
(
luaL_reref
)
(
lua_State
*
L
,
int
t
,
int
*
ref
);
LUALIB_API
int
(
luaL_loadfilex
)
(
lua_State
*
L
,
const
char
*
filename
,
...
...
@@ -282,9 +285,14 @@ extern void dbg_printf(const char *fmt, ...);
#define LUA_TASK_MEDIUM 1
#define LUA_TASK_HIGH 2
LUALIB_API
int
(
luaL_pushlfsmodules
)
(
lua_State
*
L
);
LUALIB_API
int
(
luaL_pushlfsdts
)
(
lua_State
*
L
);
LUALIB_API
void
(
luaL_lfsreload
)
(
lua_State
*
L
);
LUALIB_API
int
(
luaL_posttask
)
(
lua_State
*
L
,
int
prio
);
LUALIB_API
int
(
luaL_pcallx
)
(
lua_State
*
L
,
int
narg
,
int
nres
);
#define luaL_pushlfsmodule(l) lua_pushlfsfunc(L)
/* }============================================================ */
#endif
...
...
app/lua53/lbaselib.c
View file @
a92da3c3
...
...
@@ -18,7 +18,6 @@
#include "lauxlib.h"
#include "lualib.h"
#include "lnodemcu.h"
static
int
luaB_print
(
lua_State
*
L
)
{
...
...
app/lua53/lcorolib.c
View file @
a92da3c3
...
...
@@ -16,7 +16,6 @@
#include "lauxlib.h"
#include "lualib.h"
#include "lnodemcu.h"
static
lua_State
*
getco
(
lua_State
*
L
)
{
...
...
app/lua53/ldblib.c
View file @
a92da3c3
...
...
@@ -18,7 +18,6 @@
#include "lauxlib.h"
#include "lualib.h"
#include "lnodemcu.h"
/*
...
...
@@ -48,16 +47,15 @@ static int db_getregistry (lua_State *L) {
static
int
db_getstrings
(
lua_State
*
L
)
{
static
const
char
*
const
opts
[]
=
{
"RAM"
,
"ROM"
,
NULL
};
int
opt
=
luaL_checkoption
(
L
,
1
,
"RAM"
,
opts
);
int
st
=
lua_getstrings
(
L
,
opt
);
/* return the relevant strt as an array */
if
(
st
)
{
lua_pushvalue
(
L
,
-
1
);
/* dup the array TValue */
lua_getglobal
(
L
,
"table"
);
if
(
lua_pushstringsarray
(
L
,
opt
))
{
if
(
lua_getglobal
(
L
,
"table"
)
==
LUA_TTABLE
)
{
lua_getfield
(
L
,
-
1
,
"sort"
);
/* look up table.sort function */
lua_replace
(
L
,
-
2
);
/* dump the table entry */
lua_insert
(
L
,
-
2
);
/* swap table/sort and the strings_table */
lua_pushvalue
(
L
,
-
3
);
/* dup the strings_table to ToS */
lua_call
(
L
,
1
,
0
);
/* table.sort(strings_table) */
lua_pop
(
L
,
1
);
/* dump the table entry */
}
return
st
?
1
:
0
;
}
return
1
;
}
static
int
db_getmetatable
(
lua_State
*
L
)
{
...
...
app/lua53/ldump.c
View file @
a92da3c3
...
...
@@ -17,6 +17,7 @@
#include "lauxlib.h"
#include "llex.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
...
...
@@ -263,6 +264,36 @@ int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data,
return
D
.
status
;
}
static
int
stripdebug
(
lua_State
*
L
,
Proto
*
f
,
int
level
)
{
int
i
,
len
=
0
;
switch
(
level
)
{
case
2
:
if
(
f
->
lineinfo
)
{
f
->
lineinfo
=
luaM_freearray
(
L
,
f
->
lineinfo
,
f
->
sizelineinfo
);
len
+=
f
->
sizelineinfo
;
}
case
1
:
for
(
i
=
0
;
i
<
f
->
sizeupvalues
;
i
++
)
f
->
upvalues
[
i
].
name
=
NULL
;
f
->
locvars
=
luaM_freearray
(
L
,
f
->
locvars
,
f
->
sizelocvars
);
len
+=
f
->
sizelocvars
*
sizeof
(
LocVar
);
f
->
sizelocvars
=
0
;
}
return
len
;
}
/* This is a recursive function so it's stack size has been kept to a minimum! */
int
luaU_stripdebug
(
lua_State
*
L
,
Proto
*
f
,
int
level
,
int
recv
){
int
len
=
0
,
i
;
if
(
recv
!=
0
&&
f
->
sizep
!=
0
)
{
for
(
i
=
0
;
i
<
f
->
sizep
;
i
++
)
len
+=
luaU_stripdebug
(
L
,
f
->
p
[
i
],
level
,
recv
);
}
len
+=
stripdebug
(
L
,
f
,
level
);
return
len
;
}
/*============================================================================**
**
** NodeMCU extensions for LFS support and dumping. Note that to keep lua_lock
...
...
@@ -279,6 +310,8 @@ int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data,
*/
static
void
addTS
(
TString
*
ts
,
DumpState
*
D
)
{
lua_State
*
L
=
D
->
L
;
if
(
!
ts
)
return
;
if
(
ttisnil
(
luaH_getstr
(
D
->
stringIndex
,
ts
)))
{
TValue
k
,
v
,
*
slot
;
gettt
(
ts
)
<=
LUA_TSHRSTR
?
D
->
sTScnt
++
:
D
->
lTScnt
++
;
...
...
app/lua53/lgc.c
View file @
a92da3c3
...
...
@@ -1133,6 +1133,13 @@ static l_mem getdebt (global_State *g) {
/*
** performs a basic GC step when collector is running
*/
#ifdef LUA_USE_ESP8266
/*DEBUG*/
extern
void
dbg_printf
(
const
char
*
fmt
,
...);
#define CCOUNT_REG ({ int32_t r; asm volatile("rsr %0, ccount" : "=r"(r)); r;})
#else
/*DEBUG*/
#define dbg_printf(...)
#define CCOUNT_REG 0
#endif
/*DEBUG*/
void
luaC_step
(
lua_State
*
L
)
{
global_State
*
g
=
G
(
L
);
l_mem
debt
=
getdebt
(
g
);
/* GC deficit (be paid now) */
...
...
@@ -1141,15 +1148,19 @@ void luaC_step (lua_State *L) {
return
;
}
do
{
/* repeat until pause or enough "credit" (negative debt) */
/*DEBUG int32_t start = CCOUNT_REG; */
lu_mem
work
=
singlestep
(
L
);
/* perform one single step */
debt
-=
work
;
/*DEBUG dbg_printf("singlestep - %d, %d, %u \n", debt, lua_freeheap(), CCOUNT_REG-start); */
}
while
(
debt
>
-
GCSTEPSIZE
&&
g
->
gcstate
!=
GCSpause
);
if
(
g
->
gcstate
==
GCSpause
)
setpause
(
g
);
/* pause until next cycle */
else
{
/*DEBUG int32_t start = CCOUNT_REG; */
debt
=
(
debt
/
g
->
gcstepmul
)
*
STEPMULADJ
;
/* convert 'work units' to Kb */
luaE_setdebt
(
g
,
debt
);
runafewfinalizers
(
L
);
/*DEBUG dbg_printf("new debt - %d, %d, %u \n", debt, lua_freeheap(), CCOUNT_REG-start); */
}
}
...
...
app/lua53/linit.c
View file @
a92da3c3
...
...
@@ -47,7 +47,6 @@ extern LROT_TABLE(dblib);
extern
LROT_TABLE
(
co_funcs
);
extern
LROT_TABLE
(
mathlib
);
extern
LROT_TABLE
(
utf8
);
extern
LROT_TABLE
(
LFS
);
#define LROT_ROM_ENTRIES \
LROT_TABENTRY( string, strlib ) \
...
...
@@ -56,7 +55,6 @@ extern LROT_TABLE(LFS);
LROT_TABENTRY( coroutine, co_funcs ) \
LROT_TABENTRY( math, mathlib ) \
LROT_TABENTRY( utf8, utf8 ) \
LROT_TABENTRY( LFS, LFS ) \
LROT_TABENTRY( ROM, rotables )
#define LROT_LIB_ENTRIES \
...
...
@@ -75,7 +73,7 @@ extern LROT_TABLE(LFS);
/* _G __index -> rotables __index -> base_func */
extern
LROT_TABLE
(
rotables_meta
);
LROT_TABLE
(
base_func
);
extern
LROT_TABLE
(
base_func
);
LROT_BEGIN
(
rotables_meta
,
NULL
,
LROT_MASK_INDEX
)
LROT_TABENTRY
(
__index
,
base_func
)
...
...
app/lua53/lmathlib.c
View file @
a92da3c3
...
...
@@ -17,8 +17,7 @@
#include "lauxlib.h"
#include "lualib.h"
#include "lnodemcu.h"
#include "ldebug.h"
#undef PI
#define PI (l_mathop(3.141592653589793238462643383279502884))
...
...
@@ -135,7 +134,7 @@ static int math_fmod (lua_State *L) {
lua_pushinteger
(
L
,
lua_tointeger
(
L
,
1
)
%
d
);
}
else
{
lua_Number
m
,
a
=
luaL_checknumber
(
L
,
1
),
b
=
luaL_checknumber
(
L
,
2
);
if
(
b
==
0
)
lua
G_run
error
(
L
,
"modulo by zero"
);
if
(
b
==
0
)
lua
L_
error
(
L
,
"modulo by zero"
);
m
=
a
/
b
;
lua_pushnumber
(
L
,
a
-
b
*
(
m
>
0
.
0
?
floor
(
m
)
:
ceil
(
m
)));
}
...
...
app/lua53/lnodemcu.c
View file @
a92da3c3
...
...
@@ -30,8 +30,8 @@
** * POSIX vs VFS file API abstraction
** * Emulate Platform_XXX() API
** * ESP and HOST lua_debugbreak() test stubs
** * NodeMCU lua.h API extensions
** * NodeMCU
LFS Table emulator
** * NodeMCU lua.h
LUA_
API extensions
** * NodeMCU
lauxlib.h LUALIB_API extensions
** * NodeMCU bootstrap to set up and to reimage LFS resources
**
** Just search down for //== or ==// to flip through the sections.
...
...
@@ -39,12 +39,12 @@
#define byte_addr(p) cast(char *,p)
#define byteptr(p) cast(lu_byte *, p)
#define byteoffset(p,q) (byteptr(p) - byteptr(q))
#define byteoffset(p,q)
((int) cast(ptrdiff_t,
(byteptr(p) - byteptr(q))
))
#define wordptr(p) cast(lu_int32 *, p)
#define wordoffset(p,q) (wordptr(p) - wordptr(q))
//== Wrap POSIX and VFS file API
====================
=========================//
//==
====================
Wrap POSIX and VFS file API =========================//
#ifdef LUA_USE_ESP
int
luaopen_file
(
lua_State
*
L
);
# define l_file(f) int f
...
...
@@ -62,7 +62,6 @@ int luaopen_file(lua_State *L);
# define l_rewind(f) rewind(f)
#endif
//== Emulate Platform_XXX() API ==============================================//
#ifdef LUA_USE_ESP
extern
void
dbg_printf
(
const
char
*
fmt
,
...);
// DEBUG
...
...
@@ -79,6 +78,8 @@ extern void dbg_printf(const char *fmt, ...); // DEBUG
#else // LUA_USE_HOST
//==== Emulate Platform_XXX() API within host luac.cross -e environement =====//
#include<stdio.h> // DEBUG
/*
** The ESP implementation use a platform_XXX() API to provide a level of
...
...
@@ -124,9 +125,9 @@ extern char *LFSimageName;
#define platform_rcr_write(id,rec,l) (128)
#define platform_flash_phys2mapped(n) \
(
byteptr(
LFSaddr)
+ (n)
- LFSbase)
(
(ptrdiff_t)(((size_t)
LFSaddr) - LFSbase)
+ (n))
#define platform_flash_mapped2phys(n) \
(
byteoffset(n,
LFSaddr) + LFSbase)
(
(size_t)(n) - ((size_t)
LFSaddr) + LFSbase)
#define platform_flash_get_sector_of_address(n) ((n)>>12)
#define platform_rcr_delete(id) LFSimageName = NULL
#define platform_rcr_read(id,s) \
...
...
@@ -170,7 +171,7 @@ static void platform_s_flash_write(const void *from, lu_int32 to, lu_int32 len)
#endif
//== ESP and HOST lua_debugbreak() test stubs
===========
=====================//
//==
===========
ESP and HOST lua_debugbreak() test stubs =====================//
#ifdef DEVELOPMENT_USE_GDB
/*
...
...
@@ -204,7 +205,7 @@ LUALIB_API void lua_debugbreak(void) {
}
#endif
//== NodeMCU lua.h API extensions
===================
=========================//
//==
===================
NodeMCU lua.h API extensions =========================//
LUA_API
int
lua_freeheap
(
void
)
{
#ifdef LUA_USE_HOST
...
...
@@ -214,34 +215,39 @@ LUA_API int lua_freeheap (void) {
#endif
}
LUA_API
int
lua_getstrings
(
lua_State
*
L
,
int
opt
)
{
stringtable
*
tb
=
NULL
;
Table
*
t
;
int
i
,
j
,
n
=
0
;
LUA_API
int
lua_pushstringsarray
(
lua_State
*
L
,
int
opt
)
{
stringtable
*
strt
=
NULL
;
int
i
,
j
=
1
;
if
(
n
==
0
)
tb
=
&
G
(
L
)
->
strt
;
lua_lock
(
L
);
if
(
opt
==
0
)
strt
=
&
G
(
L
)
->
strt
;
#ifdef LUA_USE_ESP
else
if
(
n
==
1
&&
G
(
L
)
->
ROstrt
.
hash
)
t
b
=
&
G
(
L
)
->
ROstrt
;
else
if
(
opt
==
1
&&
G
(
L
)
->
ROstrt
.
hash
)
str
t
=
&
G
(
L
)
->
ROstrt
;
#endif
if
(
tb
==
NULL
)
if
(
strt
==
NULL
)
{
setnilvalue
(
L
->
top
);
api_incr_top
(
L
);
lua_unlock
(
L
);
return
0
;
}
lua_lock
(
L
);
t
=
luaH_new
(
L
);
Table
*
t
=
luaH_new
(
L
);
sethvalue
(
L
,
L
->
top
,
t
);
api_incr_top
(
L
);
luaH_resize
(
L
,
t
,
t
b
->
nuse
,
0
);
luaH_resize
(
L
,
t
,
str
t
->
nuse
,
0
);
luaC_checkGC
(
L
);
lua_unlock
(
L
);
for
(
i
=
0
,
j
=
1
;
i
<
tb
->
size
;
i
++
)
{
TString
*
o
;
for
(
o
=
tb
->
hash
[
i
];
o
;
o
=
o
->
u
.
hnext
)
{
/* loop around all strt hash entries */
for
(
i
=
0
,
j
=
1
;
i
<
strt
->
size
;
i
++
)
{
TString
*
e
;
/* loop around all TStings in this entry's chain */
for
(
e
=
strt
->
hash
[
i
];
e
;
e
=
e
->
u
.
hnext
)
{
TValue
s
;
setsvalue
(
L
,
&
s
,
o
);
luaH_setint
(
L
,
hvalue
(
L
->
top
-
1
),
j
++
,
&
s
);
/* table[n] = true */
setsvalue
(
L
,
&
s
,
e
);
luaH_setint
(
L
,
hvalue
(
L
->
top
-
1
),
j
++
,
&
s
);
}
}
return
1
;
...
...
@@ -274,72 +280,91 @@ LUA_API void lua_createrotable (lua_State *L, ROTable *t,
t
->
entry
=
cast
(
ROTable_entry
*
,
e
);
}
//== NodeMCU LFS Table emulator ==============================================//
static
int
lfs_func
(
lua_State
*
L
);
LROT_BEGIN
(
LFS_meta
,
NULL
,
LROT_MASK_INDEX
)
LROT_FUNCENTRY
(
__index
,
lfs_func
)
LROT_END
(
LFS_meta
,
NULL
,
LROT_MASK_INDEX
)
LROT_BEGIN
(
LFS
,
LROT_TABLEREF
(
LFS_meta
),
0
)
LROT_END
(
LFS
,
LROT_TABLEREF
(
LFS_meta
),
0
)
LUA_API
void
lua_getlfsconfig
(
lua_State
*
L
,
int
*
config
)
{
global_State
*
g
=
G
(
L
);
LFSHeader
*
l
=
g
->
l_LFS
;
if
(
!
config
)
return
;
config
[
0
]
=
(
int
)
(
size_t
)
l
;
/* LFS region mapped address */
config
[
1
]
=
platform_flash_mapped2phys
(
config
[
0
]);
/* ditto phys address */
config
[
2
]
=
g
->
LFSsize
;
/* LFS region actual size */
if
(
g
->
ROstrt
.
hash
)
{
config
[
3
]
=
l
->
flash_size
;
/* LFS region used */
config
[
4
]
=
l
->
timestamp
;
/* LFS region timestamp */
}
else
{
config
[
3
]
=
config
[
4
]
=
0
;
}
}
LUA_API
int
(
lua_pushlfsindex
)
(
lua_State
*
L
)
{
lua_lock
(
L
);
setobj2n
(
L
,
L
->
top
,
&
G
(
L
)
->
LFStable
);
api_incr_top
(
L
);
lua_unlock
(
L
);
return
ttnov
(
L
->
top
-
1
);
}
static
int
lfs_func
(
lua_State
*
L
)
{
/*T[1] = LFS, T[2] = fieldname */
const
char
*
name
=
lua_tostring
(
L
,
2
);
LFSHeader
*
fh
=
G
(
L
)
->
l_LFS
;
Proto
*
f
;
LClosure
*
cl
;
lua_settop
(
L
,
2
);
if
(
!
fh
)
{
/* return nil if LFS not loaded */
lua_pushnil
(
L
);
return
1
;
}
if
(
!
strcmp
(
name
,
"_config"
))
{
size_t
ba
=
cast
(
size_t
,
fh
);
lua_createtable
(
L
,
0
,
3
);
lua_pushinteger
(
L
,
cast
(
lua_Integer
,
ba
));
lua_setfield
(
L
,
-
2
,
"lfs_mapped"
);
lua_pushinteger
(
L
,
cast
(
lua_Integer
,
platform_flash_mapped2phys
(
ba
)));
lua_setfield
(
L
,
-
2
,
"lfs_base"
);
lua_pushinteger
(
L
,
G
(
L
)
->
LFSsize
);
lua_setfield
(
L
,
-
2
,
"lfs_size"
);
return
1
;
}
else
if
(
!
strcmp
(
name
,
"_list"
))
{
int
i
=
1
;
setobjs2s
(
L
,
L
->
top
-
2
,
&
G
(
L
)
->
LFStable
);
/* overwrite T[1] with LSFtable */
lua_newtable
(
L
);
/* new list table at T[3] */
lua_pushnil
(
L
);
/* first key (nil) at T4] */
while
(
lua_next
(
L
,
1
)
!=
0
)
{
/* loop over LSFtable k,v at T[4:5] */
lua_pop
(
L
,
1
);
/* dump value */
lua_pushvalue
(
L
,
-
1
);
/* dup key */
lua_rawseti
(
L
,
3
,
i
++
);
/* table[i]=key */
/*
* In Lua 5.3 luac.cross generates a top level Proto for each source file with
* one upvalue that must be the set to the _ENV variable when its closure is
* created, and as such this parallels some ldo.c processing.
*/
LUA_API
int
(
lua_pushlfsfunc
)
(
lua_State
*
L
)
{
lua_lock
(
L
);
const
TValue
*
t
=
&
G
(
L
)
->
LFStable
;
if
(
ttisstring
(
L
->
top
-
1
)
&&
ttistable
(
t
))
{
const
TValue
*
v
=
luaH_getstr
(
hvalue
(
t
),
tsvalue
(
L
->
top
-
1
));
if
(
ttislightuserdata
(
v
))
{
Proto
*
f
=
pvalue
(
v
);
/* The pvalue is a Proto * for the Lua function */
LClosure
*
cl
=
luaF_newLclosure
(
L
,
f
->
sizeupvalues
);
setclLvalue
(
L
,
L
->
top
-
1
,
cl
);
luaF_initupvals
(
L
,
cl
);
cl
->
p
=
f
;
if
(
cl
->
nupvalues
>=
1
)
{
/* does it have an upvalue? */
UpVal
*
uv1
=
cl
->
upvals
[
0
];
TValue
*
val
=
uv1
->
v
;
/* set 1st upvalue as global env table from registry */
setobj
(
L
,
val
,
luaH_getint
(
hvalue
(
&
G
(
L
)
->
l_registry
),
LUA_RIDX_GLOBALS
));
luaC_upvalbarrier
(
L
,
uv1
);
}
return
1
;
}
else
if
(
!
strcmp
(
name
,
"_time"
))
{
lua_pushinteger
(
L
,
fh
->
timestamp
);
return
1
;
}
setobjs2s
(
L
,
L
->
top
-
2
,
&
G
(
L
)
->
LFStable
);
/* overwrite T[1] with LSFtable */
if
(
lua_rawget
(
L
,
1
)
!=
LUA_TNIL
)
{
/* get LFStable[name] */
lua_pushglobaltable
(
L
);
f
=
cast
(
Proto
*
,
lua_topointer
(
L
,
-
2
));
lua_lock
(
L
);
cl
=
luaF_newLclosure
(
L
,
f
->
sizeupvalues
);
setclLvalue
(
L
,
L
->
top
-
2
,
cl
);
/* overwrite f addr slot with closure */
cl
->
p
=
f
;
/* bind f to it */
if
(
cl
->
nupvalues
>=
1
)
{
/* does it have at least one upvalue? */
luaF_initupvals
(
L
,
cl
);
/* initialise upvals */
setobj
(
L
,
cl
->
upvals
[
0
]
->
v
,
L
->
top
-
1
);
/* set UV[1] to _ENV */
}
setnilvalue
(
L
->
top
-
1
);
lua_unlock
(
L
);
lua_pop
(
L
,
1
);
/* pop _ENV leaving closure at ToS */
return
0
;
}
//================ NodeMCU lauxlib.h LUALIB_API extensions ===================//
/*
* Return an array of functions in LFS
*/
LUALIB_API
int
(
luaL_pushlfsmodules
)
(
lua_State
*
L
)
{
int
i
=
1
;
if
(
lua_pushlfsindex
(
L
)
==
LUA_TNIL
)
return
0
;
/* return nil if LFS not loaded */
lua_newtable
(
L
);
/* create dest table and move above LFS index ROTable */
lua_insert
(
L
,
-
2
);
lua_pushnil
(
L
);
while
(
lua_next
(
L
,
-
2
)
!=
0
)
{
lua_pop
(
L
,
1
);
/* dump the value (ptr to the Proto) */
lua_pushvalue
(
L
,
-
1
);
/* dup key (module name) */
lua_rawseti
(
L
,
-
4
,
i
++
);
}
lua_pop
(
L
,
1
);
/* dump the LFS index ROTable */
return
1
;
}
LUALIB_API
int
(
luaL_pushlfsdts
)
(
lua_State
*
L
)
{
int
config
[
5
];
lua_getlfsconfig
(
L
,
config
);
lua_pushinteger
(
L
,
config
[
4
]);
return
1
;
}
//== NodeMCU bootstrap to set up and to reimage LFS resources
======
==========//
//==
======
NodeMCU bootstrap to set up and to reimage LFS resources ==========//
/*
** This processing uses 2 init hooks during the Lua startup. The first is
** called early in the Lua state setup to initialize the LFS if present. The
...
...
@@ -494,9 +519,9 @@ LUAI_FUNC int luaN_init (lua_State *L) {
if
(
n
<
0
)
{
global_State
*
g
=
G
(
L
);
g
->
LFSsize
=
F
->
size
;
g
->
l_LFS
=
fh
;
/* Set up LFS hooks on normal Entry */
if
(
fh
->
flash_sig
==
FLASH_SIG
)
{
g
->
l_LFS
=
fh
;
g
->
seed
=
fh
->
seed
;
g
->
ROstrt
.
hash
=
cast
(
TString
**
,
F
->
addr
+
fh
->
oROhash
);
g
->
ROstrt
.
nuse
=
fh
->
nROuse
;
...
...
@@ -565,9 +590,9 @@ LUAI_FUNC int luaN_init (lua_State *L) {
#define getfield(L,t,f) \
lua_getglobal(L, #t); luaL_getmetafield( L, 1, #f ); lua_remove(L, -2);
LUAI_FUNC
int
lua_lfsreload
(
lua_State
*
L
)
{
int
n
=
0
;
LUALIB_API
void
luaL_lfsreload
(
lua_State
*
L
)
{
#ifdef LUA_USE_ESP
int
n
=
0
;
size_t
l
;
int
off
=
0
;
const
char
*
img
=
lua_tolstring
(
L
,
1
,
&
l
);
...
...
@@ -579,30 +604,72 @@ LUAI_FUNC int lua_lfsreload (lua_State *L) {
lua_getglobal
(
L
,
"file"
);
if
(
lua_isnil
(
L
,
2
))
{
lua_pushstring
(
L
,
"No file system mounted"
);
return
1
;
return
;
}
lua_getfield
(
L
,
2
,
"exists"
);
lua_pushstring
(
L
,
img
+
off
);
lua_call
(
L
,
1
,
1
);
if
(
G
(
L
)
->
LFSsize
==
0
||
lua_toboolean
(
L
,
-
1
)
==
0
)
{
lua_pushstring
(
L
,
"No LFS partition allocated"
);
return
1
;
return
;
}
n
=
platform_rcr_write
(
PLATFORM_RCR_FLASHLFS
,
img
,
l
+
1
);
/* incl trailing \0 */
if
(
n
>
0
)
if
(
n
>
0
)
{
system_restart
();
luaL_error
(
L
,
"system restarting"
);
}
#endif
lua_pushboolean
(
L
,
n
>
0
);
return
1
;
}
LUAI_FUNC
int
lua_lfsindex
(
lua_State
*
L
)
{
lua_settop
(
L
,
1
);
if
(
lua_isstring
(
L
,
1
)){
lua_getglobal
(
L
,
"LFS"
);
lua_getfield
(
L
,
2
,
lua_tostring
(
L
,
1
));
#ifdef LUA_USE_ESP
extern
void
lua_main
(
void
);
/*
** Task callback handler. Uses luaN_call to do a protected call with full traceback
*/
static
void
do_task
(
platform_task_param_t
task_fn_ref
,
uint8_t
prio
)
{
lua_State
*
L
=
lua_getstate
();
if
(
task_fn_ref
==
(
platform_task_param_t
)
~
0
&&
prio
==
LUA_TASK_HIGH
)
{
lua_main
();
/* Undocumented hook for lua_main() restart */
return
;
}
if
(
prio
<
LUA_TASK_LOW
||
prio
>
LUA_TASK_HIGH
)
luaL_error
(
L
,
"invalid posk task"
);
/* Pop the CB func from the Reg */
lua_rawgeti
(
L
,
LUA_REGISTRYINDEX
,
(
int
)
task_fn_ref
);
luaL_checktype
(
L
,
-
1
,
LUA_TFUNCTION
);
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
(
int
)
task_fn_ref
);
lua_pushinteger
(
L
,
prio
);
luaL_pcallx
(
L
,
1
,
0
);
}
/*
** Schedule a Lua function for task execution
*/
LUALIB_API
int
luaL_posttask
(
lua_State
*
L
,
int
prio
)
{
// [-1, +0, -]
static
platform_task_handle_t
task_handle
=
0
;
if
(
!
task_handle
)
task_handle
=
platform_task_get_id
(
do_task
);
if
(
L
==
NULL
&&
prio
==
LUA_TASK_HIGH
+
1
)
{
/* Undocumented hook for lua_main */
platform_post
(
LUA_TASK_HIGH
,
task_handle
,
(
platform_task_param_t
)
~
0
);
return
-
1
;
}
if
(
lua_isfunction
(
L
,
-
1
)
&&
prio
>=
LUA_TASK_LOW
&&
prio
<=
LUA_TASK_HIGH
)
{
int
task_fn_ref
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
if
(
!
platform_post
(
prio
,
task_handle
,
(
platform_task_param_t
)
task_fn_ref
))
{
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
task_fn_ref
);
luaL_error
(
L
,
"Task queue overflow. Task not posted"
);
}
return
task_fn_ref
;
}
else
{
lua_pushnil
(
L
);
return
luaL_error
(
L
,
"invalid posk task"
);
}
return
1
;
}
#else
/*
** Task execution isn't supported on HOST builds so returns a -1 status
*/
LUALIB_API
int
luaL_posttask
(
lua_State
*
L
,
int
prio
)
{
// [-1, +0, -]
return
-
1
;
}
#endif
app/lua53/lnodemcu.h
View file @
a92da3c3
...
...
@@ -40,12 +40,12 @@
#define LROT_ENTRYREF(rt) (rt ##_entries)
#define LROT_TABLEREF(rt) (&rt ##_ROTable)
#define LROT_BEGIN(rt,mt,f) LROT_TABLE(rt); \
static
const
ROTable_entry rt ## _entries[] = {
static ROTable_entry rt ## _entries[] = {
#define LROT_ENTRIES_IN_SECTION(rt,s) \
static
const
ROTable_entry LOCK_IN_SECTION(s) rt ## _entries[] = {
static ROTable_entry LOCK_IN_SECTION(s) rt ## _entries[] = {
#define LROT_END(rt,mt,f) {NULL, LRO_NILVAL} }; \
const ROTable rt ## _ROTable = { \
(GCObject *)1,LUA_TTBLROF, LROT_MARKED, \
(GCObject *)1,
LUA_TTBLROF, LROT_MARKED, \
cast(lu_byte, ~(f)), (sizeof(rt ## _entries)/sizeof(ROTable_entry)) - 1, \
cast(Table *, mt), cast(ROTable_entry *, rt ## _entries) };
#define LROT_BREAK(rt) };
...
...
app/lua53/loadlib.c
View file @
a92da3c3
...
...
@@ -22,7 +22,6 @@
#include "lauxlib.h"
#include "lualib.h"
#include "lnodemcu.h"
#ifndef LUA_USE_HOST
#include <fcntl.h>
...
...
app/lua53/lobject.h
View file @
a92da3c3
...
...
@@ -584,12 +584,6 @@ typedef struct ROTable {
*/
#define luaO_nilobject (&luaO_nilobject_)
/*
** KeyCache used for resolution of ROTable entries and Cstrings
*/
typedef
size_t
KeyCache
;
typedef
KeyCache
KeyCacheLine
[
KEYCACHE_M
];
LUAI_DDEC
const
TValue
luaO_nilobject_
;
/* size of buffer for 'luaO_utf8esc' function */
...
...
app/lua53/lparser.c
View file @
a92da3c3
...
...
@@ -23,9 +23,9 @@
#include "lobject.h"
#include "lopcodes.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "lundump.h"
...
...
@@ -1633,6 +1633,13 @@ static void mainfunc (LexState *ls, FuncState *fs) {
}
static
void
compile_stripdebug
(
lua_State
*
L
,
Proto
*
f
)
{
int
level
=
G
(
L
)
->
stripdefault
;
if
(
level
>
0
)
luaU_stripdebug
(
L
,
f
,
level
,
1
);
}
LClosure
*
luaY_parser
(
lua_State
*
L
,
ZIO
*
z
,
Mbuffer
*
buff
,
Dyndata
*
dyd
,
const
char
*
name
,
int
firstchar
)
{
LexState
lexstate
;
...
...
@@ -1654,6 +1661,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
lua_assert
(
!
funcstate
.
prev
&&
funcstate
.
nups
==
1
&&
!
lexstate
.
fs
);
/* all scopes should be correctly finished */
lua_assert
(
dyd
->
actvar
.
n
==
0
&&
dyd
->
gt
.
n
==
0
&&
dyd
->
label
.
n
==
0
);
compile_stripdebug
(
L
,
funcstate
.
f
);
L
->
top
--
;
/* remove scanner's table */
return
cl
;
/* closure is on the stack, too */
}
...
...
app/lua53/lstate.c
View file @
a92da3c3
...
...
@@ -307,7 +307,7 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
luaM_free
(
L
,
l
);
}
LUA
_API
KeyCache
*
(
lua_getcache
)
(
int
lineno
)
{
LUA
I_FUNC
KeyCache
*
lua
E
_getcache
(
int
lineno
)
{
return
&
G
(
L0
)
->
cache
[
lineno
][
0
];
}
...
...
@@ -347,6 +347,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g
->
gcfinnum
=
0
;
g
->
gcpause
=
LUAI_GCPAUSE
;
g
->
gcstepmul
=
LUAI_GCMUL
;
g
->
stripdefault
=
LUAI_OPTIMIZE_DEBUG
;
g
->
ROstrt
.
size
=
0
;
g
->
ROstrt
.
nuse
=
0
;
g
->
ROstrt
.
hash
=
NULL
;
...
...
app/lua53/lstate.h
View file @
a92da3c3
...
...
@@ -133,6 +133,11 @@ typedef struct CallInfo {
#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
#define getoah(st) ((st) & CIST_OAH)
/*
** KeyCache used for resolution of ROTable entries and Cstrings
*/
typedef
size_t
KeyCache
;
typedef
KeyCache
KeyCacheLine
[
KEYCACHE_M
];
/*
** 'global state', shared by all threads of this state
...
...
@@ -168,6 +173,7 @@ typedef struct global_State {
unsigned
int
gcfinnum
;
/* number of finalizers to call in each GC step */
int
gcpause
;
/* size of pause between successive GCs */
int
gcstepmul
;
/* GC 'granularity' */
int
stripdefault
;
/* default stripping level for compilation */
l_mem
gcmemfreeboard
;
/* Free board which triggers EGC */
lua_CFunction
panic
;
/* to be called in unprotected errors */
struct
lua_State
*
mainthread
;
...
...
@@ -229,8 +235,6 @@ union GCUnion {
struct
Proto
p
;
struct
lua_State
th
;
/* thread */
};
#define cast_u(o) cast(union GCUnion *, (o))
/* macros to convert a GCObject into a specific value */
...
...
@@ -262,7 +266,6 @@ LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
LUAI_FUNC
CallInfo
*
luaE_extendCI
(
lua_State
*
L
);
LUAI_FUNC
void
luaE_freeCI
(
lua_State
*
L
);
LUAI_FUNC
void
luaE_shrinkCI
(
lua_State
*
L
);
LUAI_FUNC
KeyCache
*
luaE_getcache
(
int
cl
);
#endif
app/lua53/lstrlib.c
View file @
a92da3c3
...
...
@@ -23,7 +23,6 @@
#include "lauxlib.h"
#include "lualib.h"
#include "lnodemcu.h"
/*
...
...
@@ -189,11 +188,16 @@ static int writer (lua_State *L, const void *b, size_t size, void *B) {
static
int
str_dump
(
lua_State
*
L
)
{
luaL_Buffer
b
;
int
strip
=
lua_tointeger
(
L
,
2
);
if
(
lua_isboolean
(
L
,
2
)
&&
lua_toboolean
(
L
,
2
))
strip
=
2
;
int
strip
,
tstrip
=
lua_type
(
L
,
2
);
if
(
tstrip
==
LUA_TBOOLEAN
)
{
strip
=
lua_toboolean
(
L
,
2
)
?
2
:
0
;
}
else
if
(
tstrip
==
LUA_TNONE
||
tstrip
==
LUA_TNIL
)
{
strip
=
-
1
;
/* This tells lua_dump to use the global strip default */
}
else
{
strip
=
lua_tointeger
(
L
,
2
);
luaL_argcheck
(
L
,
(
unsigned
)(
strip
)
<
3
,
2
,
"strip out of range"
);
}
luaL_checktype
(
L
,
1
,
LUA_TFUNCTION
);
luaL_argcheck
(
L
,
3
>
(
unsigned
)(
strip
),
1
,
"strip out of range"
);
lua_settop
(
L
,
1
);
luaL_buffinit
(
L
,
&
b
);
if
(
lua_dump
(
L
,
writer
,
&
b
,
strip
)
!=
0
)
...
...
@@ -203,7 +207,6 @@ static int str_dump (lua_State *L) {
}
/*
** {======================================================
** PATTERN MATCHING
...
...
app/lua53/ltable.c
View file @
a92da3c3
...
...
@@ -739,7 +739,7 @@ static const TValue* rotable_findentry(ROTable *t, TString *key, unsigned *ppos)
const
int
tl
=
getlsizenode
(
t
);
const
char
*
strkey
=
getstr
(
key
);
const
int
hash
=
HASH
(
t
,
key
);
KeyCache
*
cl
=
lua_getcache
(
hash
);
KeyCache
*
cl
=
lua
E
_getcache
(
hash
);
int
i
,
j
=
1
,
l
;
if
(
!
e
||
gettt
(
key
)
!=
LUA_TSHRSTR
)
...
...
app/lua53/ltablib.c
View file @
a92da3c3
...
...
@@ -18,7 +18,6 @@
#include "lauxlib.h"
#include "lualib.h"
#include "lnodemcu.h"
/*
...
...
app/lua53/lua.c
View file @
a92da3c3
...
...
@@ -238,9 +238,20 @@ static int pmain (lua_State *L) {
* then attempting the open will trigger a file system format.
*/
platform_rcr_read
(
PLATFORM_RCR_INITSTR
,
(
void
**
)
&
init
);
if
(
init
[
0
]
==
'!'
)
{
/* !module is a compile-free way of executing LFS module */
luaL_pushlfsmodule
(
L
);
lua_pushstring
(
L
,
init
+
1
);
lua_call
(
L
,
1
,
1
);
/* return LFS.module or nil */
status
=
LUA_OK
;
if
(
!
lua_isfunction
(
L
,
-
1
))
{
lua_pushfstring
(
L
,
"cannot load LFS.%s"
,
init
+
1
);
status
=
LUA_ERRRUN
;
}
}
else
{
status
=
(
init
[
0
]
==
'@'
)
?
luaL_loadfile
(
L
,
init
+
1
)
:
luaL_loadbuffer
(
L
,
init
,
strlen
(
init
),
"=INIT"
);
}
if
(
status
==
LUA_OK
)
status
=
docall
(
L
,
0
);
if
(
status
!=
LUA_OK
)
...
...
app/lua53/lua.h
View file @
a92da3c3
...
...
@@ -153,6 +153,10 @@ LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
LUA_API
const
lua_Number
*
(
lua_version
)
(
lua_State
*
L
);
#ifndef LUAI_OPTIMIZE_DEBUG
#define LUAI_OPTIMIZE_DEBUG 2
#endif
/*
** basic stack manipulation
...
...
@@ -283,6 +287,7 @@ LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
const
char
*
chunkname
,
const
char
*
mode
);
LUA_API
int
(
lua_dump
)
(
lua_State
*
L
,
lua_Writer
writer
,
void
*
data
,
int
strip
);
LUA_API
int
(
lua_stripdebug
)
(
lua_State
*
L
,
int
stripping
);
/*
...
...
@@ -465,17 +470,17 @@ struct lua_Debug {
typedef
struct
ROTable
ROTable
;
typedef
const
struct
ROTable_entry
ROTable_entry
;
typedef
size_t
KeyCache
;
LUA_API
void
(
lua_pushrotable
)
(
lua_State
*
L
,
const
ROTable
*
p
);
LUA_API
void
(
lua_createrotable
)
(
lua_State
*
L
,
ROTable
*
t
,
const
ROTable_entry
*
e
,
ROTable
*
mt
);
LUA_API
lua_State
*
(
lua_getstate
)
(
void
);
LUA_API
KeyCache
*
(
lua_getcache
)
(
int
cl
);
LUA_API
int
(
lua_getstrings
)
(
lua_State
*
L
,
int
opt
);
LUA_API
int
(
lua_pushstringsarray
)
(
lua_State
*
L
,
int
opt
);
LUA_API
int
(
lua_freeheap
)
(
void
);
LUAI_FUNC
int
lua_lfsreload
(
lua_State
*
L
);
LUAI_FUNC
int
lua_lfsindex
(
lua_State
*
L
);
LUA_API
void
(
lua_getlfsconfig
)
(
lua_State
*
L
,
int
*
);
LUA_API
int
(
lua_pushlfsindex
)
(
lua_State
*
L
);
LUA_API
int
(
lua_pushlfsfunc
)
(
lua_State
*
L
);
#define luaN_freearray(L,a,l) luaM_freearray(L,a,l)
...
...
@@ -487,14 +492,12 @@ typedef struct Proto Proto;
#ifdef DEVELOPMENT_USE_GDB
LUALIB_API
void
(
lua_debugbreak
)(
void
);
#define ASSERT(s)
if
(
!
(s)
) {
lua_debugbreak()
;}
#define ASSERT(s) ((s)
? (void)0 :
lua_debugbreak()
)
#else
#define lua_debugbreak() (void)(0)
#define ASSERT(s) (void)(0)
#endif
LUAI_FUNC
int
luaG_stripdebug
(
lua_State
*
L
,
Proto
*
f
,
int
level
,
int
recv
);
// from undump.c
#define LUA_ERR_CC_INTOVERFLOW 101
...
...
Prev
1
2
3
4
Next
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