Install the unicode-compatible Lua module loader before loading moonscript

Closes #1760.
This commit is contained in:
Thomas Goyne 2014-07-07 08:39:10 -07:00
parent 6c0752035c
commit 365c04333c
2 changed files with 8 additions and 67 deletions

View File

@ -46,29 +46,6 @@ package.preload['moonscript.base'] = function()
end end
return code, ltable return code, ltable
end end
moon_loader = function(name)
local name_path = name:gsub("%.", dirsep)
local file, file_path
local _list_0 = split(package.moonpath, ";")
for _index_0 = 1, #_list_0 do
local path = _list_0[_index_0]
file_path = path:gsub("?", name_path)
file = io.open(file_path)
if file then
break
end
end
if file then
local text = file:read("*a")
file:close()
local res, err = loadstring(text, file_path)
if not res then
error(file_path .. ": " .. err)
end
return res
end
return nil, "Could not find moon file"
end
loadstring = function(...) loadstring = function(...)
local options, str, chunk_name, mode, env = get_options(...) local options, str, chunk_name, mode, env = get_options(...)
chunk_name = chunk_name or "=(moonscript.loadstring)" chunk_name = chunk_name or "=(moonscript.loadstring)"
@ -97,40 +74,10 @@ package.preload['moonscript.base'] = function()
local f = assert(loadfile(...)) local f = assert(loadfile(...))
return f() return f()
end end
insert_loader = function(pos)
if pos == nil then
pos = 2
end
if not package.moonpath then
package.moonpath = create_moonpath(package.path)
end
local loaders = package.loaders or package.searchers
for _index_0 = 1, #loaders do
local loader = loaders[_index_0]
if loader == moon_loader then
return false
end
end
insert(loaders, pos, moon_loader)
return true
end
remove_loader = function()
local loaders = package.loaders or package.searchers
for i, loader in ipairs(loaders) do
if loader == moon_loader then
remove(loaders, i)
return true
end
end
return false
end
return { return {
_NAME = "moonscript", _NAME = "moonscript",
insert_loader = insert_loader,
remove_loader = remove_loader,
to_lua = to_lua, to_lua = to_lua,
moon_chunk = moon_chunk, moon_chunk = moon_chunk,
moon_loader = moon_loader,
dirsep = dirsep, dirsep = dirsep,
dofile = dofile, dofile = dofile,
loadfile = loadfile, loadfile = loadfile,
@ -2056,12 +2003,7 @@ package.preload['moonscript.errors'] = function()
end end
package.preload['moonscript'] = function() package.preload['moonscript'] = function()
do return require("moonscript.base")
local _with_0 = require("moonscript.base")
_with_0.insert_loader()
return _with_0
end
end end
package.preload['moonscript.line_tables'] = function() package.preload['moonscript.line_tables'] = function()
return { } return { }

View File

@ -139,18 +139,17 @@ 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); // loaders, package
luaL_loadstring(L, "return require('moonscript').loadstring");
if (lua_pcall(L, 0, 1, 0)) {
return false; // leave error message
}
lua_setfield(L, LUA_REGISTRYINDEX, "moonscript");
return true; return true;
} }