luaL_loadfile
иногда не удается найти file.lua
. Это не зависит от относительного пути, поскольку я устал /path/to/file.lua
, но получил ту же ошибку:
"cannot open file.lua : No such file or directory"
Я использовал luaL_loadfile
в exec_lua_file
.
Эта проблема возникает в основном после вызова exec_lua
. Я не знаю, как это может быть связано.
void exec_lua(char *command, uint8_t trim) {
lua_State *L = luaL_newstate();
if (!L) {
fprintf(stderr, "Lua init error!\n");
goto end;
}
lua_pushinteger(L, a);
lua_setglobal(L, "a");
command = command + trim;
luaL_openlibs(L);
if(luaL_dostring(L, command)) {
fprintf(stderr, "Can't execute lua!\n");
goto end;
}
lua_getglobal(L, "a");
a = lua_tointeger(L, -1);
end:
lua_close(L);
return;
}
void exec_lua_file(char *filename, uint8_t trim) {
char path[1024];
lua_State *L = luaL_newstate();
if (!L) {
fprintf(stderr, "Lua init error!\n");
goto end;
}
lua_pushinteger(L, a);
lua_setglobal(L, "a");
filename = filename + trim;
luaL_openlibs(L);
getcwd(path, 1024);
puts(path);
if(luaL_loadfile(L, filename) || lua_pcall(L, 0, 1, 0)) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
goto end;
}
lua_pop(L, lua_gettop(L));
lua_getglobal(L, "a");
a = lua_tointeger(L, -1);
end:
lua_close(L);
return;
}