refactor(lua): call loadfile internally

.. instead of luaL_loadfile

allows files to be cached
This commit is contained in:
Lewis Russell
2022-01-26 10:23:59 +00:00
parent faeff49cbf
commit adad10284d

View File

@@ -1241,6 +1241,9 @@ void ex_luafile(exarg_T *const eap)
/// execute lua code from a file. /// execute lua code from a file.
/// ///
/// Note: we call the lua global loadfile as opposed to calling luaL_loadfile
/// in case loadfile has been overridden in the users environment.
///
/// @param path path of the file /// @param path path of the file
/// ///
/// @return true if everything ok, false if there was an error (echoed) /// @return true if everything ok, false if there was an error (echoed)
@@ -1249,11 +1252,30 @@ bool nlua_exec_file(const char *path)
{ {
lua_State *const lstate = global_lstate; lua_State *const lstate = global_lstate;
if (luaL_loadfile(lstate, path)) { lua_getglobal(lstate, "loadfile");
nlua_error(lstate, _("E5112: Error while creating lua chunk: %.*s")); lua_pushstring(lstate, path);
if (nlua_pcall(lstate, 1, 2)) {
nlua_error(lstate, _("E5111: Error calling lua: %.*s"));
return false; return false;
} }
// loadstring() returns either:
// 1. nil, error
// 2. chunk, nil
if (lua_isnil(lstate, -2)) {
// 1
nlua_error(lstate, _("E5112: Error while creating lua chunk: %.*s"));
assert(lua_isnil(lstate, -1));
lua_pop(lstate, 1);
return false;
}
// 2
assert(lua_isnil(lstate, -1));
lua_pop(lstate, 1);
if (nlua_pcall(lstate, 0, 0)) { if (nlua_pcall(lstate, 0, 0)) {
nlua_error(lstate, _("E5113: Error while calling lua chunk: %.*s")); nlua_error(lstate, _("E5113: Error while calling lua chunk: %.*s"));
return false; return false;