Fix relative loading of embedded scripts when local file exists

This commit is contained in:
Jason Perkins 2014-12-22 17:08:47 -05:00
parent 25255da22d
commit 247018cef0

View File

@ -27,46 +27,56 @@ static int chunk_wrapper(lua_State* L);
LUALIB_API int luaL_loadfile (lua_State* L, const char* filename) LUALIB_API int luaL_loadfile (lua_State* L, const char* filename)
{ {
int z; const char* script_dir;
const char* test_name;
/* Try to locate the script on the filesystem */ int bottom = lua_gettop(L);
lua_pushcfunction(L, os_locate); int z = !OKAY;
lua_pushstring(L, filename);
lua_call(L, 1, 1);
/* If I found it, load it from the file system... */ /* If the currently running script was embedded, try to load this file
if (!lua_isnil(L, -1)) { * as it if were embedded too. */
z = original_luaL_loadfile(L, lua_tostring(L, -1)); lua_getglobal(L, "_SCRIPT_DIR");
script_dir = lua_tostring(L, -1);
if (script_dir && script_dir[0] == '$') {
/* Call `path.getabsolute(filename, _SCRIPT_DIR)` to resolve any
* "../" sequences in the filename */
lua_pushcfunction(L, path_getabsolute);
lua_pushstring(L, filename);
lua_pushvalue(L, -3);
lua_call(L, 2, 1);
test_name = lua_tostring(L, -1);
/* if successful, filename and chunk will be on top of stack */
z = premake_load_embedded_script(L, test_name + 2); /* Skip over leading "$/" */
/* remove test_name */
lua_remove(L, bottom + 1);
} }
/* ...otherwise try to load from embedded scripts */ /* remove _SCRIPT_DIR */
else { lua_remove(L, bottom);
lua_pop(L, 1);
z = premake_load_embedded_script(L, filename);
/* Special case relative loading of embedded scripts */ /* Try to locate the script on the filesystem */
if (z != OKAY) { if (z != OKAY) {
const char* script_dir; lua_pushcfunction(L, os_locate);
lua_getglobal(L, "_SCRIPT_DIR"); lua_pushstring(L, filename);
script_dir = lua_tostring(L, -1); lua_call(L, 1, 1);
if (script_dir && script_dir[0] == '$') { test_name = lua_tostring(L, -1);
/* call path.getabsolute() to handle ".." if present */
lua_pushcfunction(L, path_getabsolute);
lua_pushstring(L, filename);
lua_pushvalue(L, -3);
lua_call(L, 2, 1);
filename = lua_tostring(L, -1); if (!lua_isnil(L, -1)) {
z = premake_load_embedded_script(L, filename + 2); z = original_luaL_loadfile(L, lua_tostring(L, -1));
lua_remove(L, -3);
lua_remove(L, -3);
}
else {
lua_pop(L, 1);
}
} }
/* on failure, remove test_name */
if (z != OKAY) {
lua_pop(L, 1);
}
}
/* Try to load from embedded scripts */
if (z != OKAY) {
z = premake_load_embedded_script(L, filename);
} }
/* Either way I should have ended up with the file name followed by the /* Either way I should have ended up with the file name followed by the