From 09367dc3bd2dea18d3f12e5aae05e7a0e6d5749f Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Mon, 22 Aug 2011 18:04:19 +0200 Subject: [PATCH] Make love loaders the first 2, preventing lua loaders from loading the wrong files (issue #283) --- src/common/runtime.cpp | 32 ++++++++++++++++--- src/common/runtime.h | 12 ++++++- .../filesystem/physfs/wrap_Filesystem.cpp | 7 ++-- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index 47660db42..0d7e8f064 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -215,8 +215,32 @@ namespace love lua_pop(L, 1); // Pops metatable. return 0; } + + int luax_table_insert(lua_State * L, int tindex, int vindex, int pos) + { + if (tindex < 0) + tindex = lua_gettop(L)+1+tindex; + if (vindex < 0) + vindex = lua_gettop(L)+1+vindex; + if (pos == -1) + { + lua_pushvalue(L, vindex); + lua_rawseti(L, tindex, lua_objlen(L, tindex)+1); + return 0; + } + else if (pos < 0) + pos = lua_objlen(L, tindex)+1+pos; + for (int i = lua_objlen(L, tindex)+1; i > pos; i--) + { + lua_rawgeti(L, tindex, i-1); + lua_rawseti(L, tindex, i); + } + lua_pushvalue(L, vindex); + lua_rawseti(L, tindex, pos); + return 0; + } - int luax_register_searcher(lua_State * L, lua_CFunction f) + int luax_register_searcher(lua_State * L, lua_CFunction f, int pos) { // Add the package loader to the package.loaders table. lua_getglobal(L, "package"); @@ -229,11 +253,9 @@ namespace love if(lua_isnil(L, -1)) return luaL_error(L, "Can't register searcher: package.loaders table does not exist."); - int len = lua_objlen(L, -1); - lua_pushinteger(L, len+1); lua_pushcfunction(L, f); - lua_settable(L, -3); - lua_pop(L, 2); + luax_table_insert(L, -2, -1, pos); + lua_pop(L, 3); return 0; } diff --git a/src/common/runtime.h b/src/common/runtime.h index 1543758b2..a4cca86dc 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -176,13 +176,23 @@ namespace love **/ int luax_register_type(lua_State * L, const char * tname, const luaL_Reg * f = 0); + /** + * Do a table.insert from C + * @param L the state + * @param tindex the stack index of the table + * @param vindex the stack index of the value + * @param pos the position to insert it in + **/ + int luax_table_insert(lua_State * L, int tindex, int vindex, int pos = -1); + /** * Register a new searcher function for package.loaders. This can for instance enable * loading of files through love.filesystem using standard require. * @param L The Lua state. * @param f The searcher function. + * @param pos The position to insert the loader in. **/ - int luax_register_searcher(lua_State * L, lua_CFunction f); + int luax_register_searcher(lua_State * L, lua_CFunction f, int pos = -1); /** * Creates a new Lua-accessible object of the given type, and put it on the stack. diff --git a/src/modules/filesystem/physfs/wrap_Filesystem.cpp b/src/modules/filesystem/physfs/wrap_Filesystem.cpp index c5575ffc0..dd6c266ad 100644 --- a/src/modules/filesystem/physfs/wrap_Filesystem.cpp +++ b/src/modules/filesystem/physfs/wrap_Filesystem.cpp @@ -389,8 +389,8 @@ namespace physfs try { instance = new Filesystem(); - love::luax_register_searcher(L, loader); - love::luax_register_searcher(L, extloader); + love::luax_register_searcher(L, loader, 1); + love::luax_register_searcher(L, extloader, 2); } catch(Exception & e) { @@ -400,7 +400,8 @@ namespace physfs else { instance->retain(); - love::luax_register_searcher(L, loader); + love::luax_register_searcher(L, loader, 1); + love::luax_register_searcher(L, extloader, 2); } WrappedModule w;