Hi
First of all, many compliments to the authors of Lua.
I have been learning LUA since Saturday.
I am trying to do my best to improve LUA; I hope not to waste your time, I don't know if you have already talked about these topics.
I read the manual and I look at LUA source code.
Please correct me, I take the manual as the public, formal specification of LUA.
Two questions:
1) return code
The manual says "
It returns false if there are no errors or true in case of errors."
I read the source code (both 5.3 and 5.4) and I am trying to follow all the return paths.
lauxlib.h
#define luaL_dofile(L, fn) (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
lauxlib.c
luaL_loadfilex
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
errfile
return LUA_ERRFILE;
lauxlib.h
#define LUA_ERRFILE (LUA_ERRERR+1)
lua.h
#define LUA_ERRERR 6
So it seems to me the return values can - at least - be 0, 1, 7.
Am I right ?
2) loading compiled files
luaL_dofile can load plain source code and compiled scripts (bytecode).
I have to check users don't pass filenames of compiled scripts.
From what I see, there isn't any LUA library to help this and the common solutions is:
- open the file in binary mode
- read the first byte: the value ESC marks a binary script (lua.h : LUA_SIGNATURE "\x1bLua")
I want to be sure so I dig into the source files:
lauxlib.c
luaL_loadfilex
Reading the code, it seems the function reads the file and checks for
- BOM ?
- 1st line: comment ?
before this check
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
So
luaL_loadfilex accepts binary files even if they start with BOM and/or a comment. I tested this, it works indeed.
Considerations:
2a) The manual doesn't say anything about this strange possibility
2b) For what I see around, users don't know this; so to "block" the execution of compiled scripts they code the naive and insufficient
load the file and check the first byte: ESC ?
I don't know if this is the intended behaviour or just a side effect of using
luaL_loadfilex
to load both for plain text and bytecode scripts..
I propose this change: the binary file have to start, at offset 0, with LUA_SIGNATURE.
What do you think ?
Best regards, Massimo