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.
This commit is contained in:
parent
606e3f4882
commit
431b096f51
3 changed files with 17 additions and 5 deletions
|
@ -25,5 +25,5 @@ namespace agi { namespace lua {
|
||||||
bool LoadFile(lua_State *L, agi::fs::path const& filename);
|
bool LoadFile(lua_State *L, agi::fs::path const& filename);
|
||||||
/// Install our module loader and add include_path to the module search
|
/// Install our module loader and add include_path to the module search
|
||||||
/// path of the given lua state
|
/// path of the given lua state
|
||||||
void Install(lua_State *L, std::vector<fs::path> const& include_path);
|
bool Install(lua_State *L, std::vector<fs::path> const& include_path);
|
||||||
} }
|
} }
|
||||||
|
|
|
@ -50,8 +50,7 @@ namespace agi { namespace lua {
|
||||||
// We have a MoonScript file, so we need to load it with that
|
// 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
|
// It might be nice to have a dedicated lua state for compiling
|
||||||
// MoonScript to Lua
|
// MoonScript to Lua
|
||||||
if (luaL_dostring(L, "return require('moonscript').loadstring"))
|
lua_getfield(L, LUA_REGISTRYINDEX, "moonscript");
|
||||||
return false; // Leaves error message on stack
|
|
||||||
|
|
||||||
// Save the text we'll be loading for the line number rewriting in the
|
// Save the text we'll be loading for the line number rewriting in the
|
||||||
// error handling
|
// error handling
|
||||||
|
@ -117,7 +116,7 @@ namespace agi { namespace lua {
|
||||||
return lua_gettop(L) - pretop;
|
return lua_gettop(L) - pretop;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Install(lua_State *L, std::vector<fs::path> const& include_path) {
|
bool Install(lua_State *L, std::vector<fs::path> const& include_path) {
|
||||||
// set the module load path to include_path
|
// set the module load path to include_path
|
||||||
lua_getglobal(L, "package");
|
lua_getglobal(L, "package");
|
||||||
push_value(L, "path");
|
push_value(L, "path");
|
||||||
|
@ -137,10 +136,19 @@ namespace agi { namespace lua {
|
||||||
|
|
||||||
lua_settable(L, -3);
|
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
|
// Replace the default lua module loader with our unicode compatible one
|
||||||
lua_getfield(L, -1, "loaders");
|
lua_getfield(L, -1, "loaders");
|
||||||
push_value(L, exception_wrapper<module_loader>);
|
push_value(L, exception_wrapper<module_loader>);
|
||||||
lua_rawseti(L, -2, 2);
|
lua_rawseti(L, -2, 2);
|
||||||
lua_pop(L, 2);
|
lua_pop(L, 2);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} }
|
} }
|
||||||
|
|
|
@ -397,7 +397,11 @@ namespace {
|
||||||
|
|
||||||
// Replace the default lua module loader with our unicode compatible
|
// Replace the default lua module loader with our unicode compatible
|
||||||
// one and set the module search path
|
// 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);
|
stackcheck.check_stack(0);
|
||||||
|
|
||||||
// prepare stuff in the registry
|
// prepare stuff in the registry
|
||||||
|
|
Loading…
Reference in a new issue