From 431b096f5158d193fb4c2bf43590bb51b3e66240 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 28 Apr 2014 14:19:16 -0700 Subject: [PATCH] Fix loading modules with UTF-8 BOMs Ensure MoonScript gets loaded before we install our package loader to avoid having its package loader used over ours. --- libaegisub/include/libaegisub/lua/script_reader.h | 2 +- libaegisub/lua/script_reader.cpp | 14 +++++++++++--- src/auto4_lua.cpp | 6 +++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libaegisub/include/libaegisub/lua/script_reader.h b/libaegisub/include/libaegisub/lua/script_reader.h index f9ee6f371..9c9de1300 100644 --- a/libaegisub/include/libaegisub/lua/script_reader.h +++ b/libaegisub/include/libaegisub/lua/script_reader.h @@ -25,5 +25,5 @@ namespace agi { namespace lua { bool LoadFile(lua_State *L, agi::fs::path const& filename); /// Install our module loader and add include_path to the module search /// path of the given lua state - void Install(lua_State *L, std::vector const& include_path); + bool Install(lua_State *L, std::vector const& include_path); } } diff --git a/libaegisub/lua/script_reader.cpp b/libaegisub/lua/script_reader.cpp index 53c4f13be..7e49909a7 100644 --- a/libaegisub/lua/script_reader.cpp +++ b/libaegisub/lua/script_reader.cpp @@ -50,8 +50,7 @@ namespace agi { namespace lua { // We have a MoonScript file, so we need to load it with that // It might be nice to have a dedicated lua state for compiling // MoonScript to Lua - if (luaL_dostring(L, "return require('moonscript').loadstring")) - return false; // Leaves error message on stack + lua_getfield(L, LUA_REGISTRYINDEX, "moonscript"); // Save the text we'll be loading for the line number rewriting in the // error handling @@ -117,7 +116,7 @@ namespace agi { namespace lua { return lua_gettop(L) - pretop; } - void Install(lua_State *L, std::vector const& include_path) { + bool Install(lua_State *L, std::vector const& include_path) { // set the module load path to include_path lua_getglobal(L, "package"); push_value(L, "path"); @@ -137,10 +136,19 @@ namespace agi { namespace lua { lua_settable(L, -3); + luaL_loadstring(L, "return require('moonscript').loadstring"); + if (lua_pcall(L, 0, 1, 0)) { + lua_remove(L, -2); // remove package.path table + return false; // leave error message + } + lua_setfield(L, LUA_REGISTRYINDEX, "moonscript"); + // Replace the default lua module loader with our unicode compatible one lua_getfield(L, -1, "loaders"); push_value(L, exception_wrapper); lua_rawseti(L, -2, 2); lua_pop(L, 2); + + return true; } } } diff --git a/src/auto4_lua.cpp b/src/auto4_lua.cpp index 0a1bdf690..0116b933c 100644 --- a/src/auto4_lua.cpp +++ b/src/auto4_lua.cpp @@ -397,7 +397,11 @@ namespace { // Replace the default lua module loader with our unicode compatible // one and set the module search path - Install(L, include_path); + if (!Install(L, include_path)) { + description = get_string_or_default(L, 1); + lua_pop(L, 1); + return; + } stackcheck.check_stack(0); // prepare stuff in the registry