[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: luaL_loadfilex returns LUA_ERRSYNTAX when file has wrong mode
- From: Gabríel Arthúr Pétursson <gabriel@...>
- Date: Tue, 06 Dec 2016 02:07:37 +0000
Hello,
The documentation for luaL_loadfilex specifies that it'll return
LUA_ERRFILE when the file has the wrong mode:
"This function returns the same results as lua_load, but it has an
extra error code LUA_ERRFILE if it cannot open/read the file or the
file has a wrong mode."
Sadly, this does not seem to be the case as the error code
LUA_ERRSYNTAX is returned instead.
Here we attempt to load the empty file 'test.lua' in binary mode:
int status = luaL_loadfilex(L, "test.lua", "b");
Since 'test.lua' is not a valid Lua bytecode binary file, I would've
expected the status code to be LUA_ERRFILE as documented. Is this a
documentation error by any chance?
I've attached a sample program that demonstrates this behaviour.
Thanks!
#include "lua.h"
#include "lauxlib.h"
#include <assert.h>
#include <stdlib.h>
int
main(int argc, char **argv)
{
(void) argc;
(void) argv;
lua_State *L = luaL_newstate();
assert(L);
/* Make sure a valid Lua source file called 'test.lua' is present in
* the current directory. */
int status = luaL_loadfilex(L, "test.lua", "b");
switch (status)
{
case LUA_OK:
printf("file loaded -- all is good\n");
break;
case LUA_ERRFILE:
printf("got LUA_ERRFILE\n");
lua_error(L);
break;
case LUA_ERRSYNTAX:
printf("got LUA_ERRSYNTAX\n");
lua_error(L);
break;
default:
printf("got %d\n", status);
lua_error(L);
}
lua_close(L);
return EXIT_SUCCESS;
}