From b5064f8fc97413dfc2a6a2887e1fb4fd2199c511 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Thu, 28 Jan 2010 01:13:13 +0000 Subject: [PATCH] Replace lua's default lua module loader with one which uses our UTF-8-supporting script reader. Originally committed to SVN as r4060. --- aegisub/src/auto4_lua.cpp | 40 +++++++++++++++++++++++++++++++++++++-- aegisub/src/auto4_lua.h | 1 + 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/aegisub/src/auto4_lua.cpp b/aegisub/src/auto4_lua.cpp index 2b3bdfa35..0200b27a9 100644 --- a/aegisub/src/auto4_lua.cpp +++ b/aegisub/src/auto4_lua.cpp @@ -201,13 +201,18 @@ namespace Automation4 { wxFileName path(StandardPaths::DecodePath(toker.GetNextToken())); if (path.IsOk() && !path.IsRelative() && path.DirExists()) { wxCharBuffer p = path.GetLongPath().utf8_str(); - lua_pushfstring(L, ";%s/?.lua;%s/?/init.lua", p.data(), p.data()); + lua_pushfstring(L, ";%s?.lua;%s?/init.lua", p.data(), p.data()); lua_concat(L, 2); } } lua_settable(L, -3); - lua_pop(L, 1); // pop package + + // Replace the default lua module loader with our utf-8 compatible one + lua_getfield(L, -1, "loaders"); + lua_pushcfunction(L, LuaModuleLoader); + lua_rawseti(L, -2, 2); + lua_pop(L, 2); _stackcheck.check_stack(0); // prepare stuff in the registry @@ -408,6 +413,37 @@ namespace Automation4 { return 4; } + /// @brief Module loader which uses our include rather than Lua's, for unicode file support + /// @param L The Lua state + /// @return Always 1 per loader_Lua? + int LuaScript::LuaModuleLoader(lua_State *L) + { + int pretop = lua_gettop(L); + wxString module(lua_tostring(L, -1), wxConvUTF8); + module.Replace(".", LUA_DIRSEP); + + lua_getglobal(L, "package"); + lua_pushstring(L, "path"); + lua_gettable(L, -2); + wxString package_paths(lua_tostring(L, -1), wxConvUTF8); + lua_pop(L, 2); + + wxStringTokenizer toker(package_paths, L";", wxTOKEN_STRTOK); + while (toker.HasMoreTokens()) { + wxString filename = toker.GetNextToken(); + filename.Replace(L"?", module); + if (wxFileName::FileExists(filename)) { + LuaScriptReader script_reader(filename); + if (lua_load(L, script_reader.reader_func, &script_reader, filename.utf8_str())) { + lua_pushfstring(L, "Error loading Lua module \"%s\":\n\n%s", filename.utf8_str().data(), lua_tostring(L, -1)); + lua_error(L); + return lua_gettop(L) - pretop; + } + } + } + return lua_gettop(L) - pretop; + } + /// @brief DOCME /// @param L /// @return diff --git a/aegisub/src/auto4_lua.h b/aegisub/src/auto4_lua.h index 08552e686..6c3565683 100644 --- a/aegisub/src/auto4_lua.h +++ b/aegisub/src/auto4_lua.h @@ -293,6 +293,7 @@ namespace Automation4 { static int LuaTextExtents(lua_State *L); static int LuaInclude(lua_State *L); + static int LuaModuleLoader(lua_State *L); static int LuaFrameFromMs(lua_State *L); static int LuaMsFromFrame(lua_State *L); static int LuaVideoSize(lua_State *L);