lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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;
}