[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Non-uniqueness of module names
- From: Sean Conner <sean@...>
- Date: Mon, 15 Apr 2019 17:08:42 -0400
It was thus said that the Great Dirk Laurie once stated:
>
> It is not important to anticipate how the filename is used, only to
> preserve the location where the module loader was found.
Sigh.
Did it not occur to you that you could write your own require() function
and get what you want? Even if it's just a proof-of-concept and a way to
see if it breaks any existing code. It's not hard. Here's a version that
will store the location information in package.where (as well as which
searcher in package.searchers[] returned the information, since not all
searchers return path information), in addition to returning the path name
as a second return value (per Roberto's suggestion):
-- ***********************************************************************
--
-- A replacement require() function.
-- Copyright (C) 2019 by Sean Conner.
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
--
-- Comments, questions and criticisms can be sent to: sean@conman.org
--
-- ***********************************************************************
package.where = {}
function require(modname)
if package.loaded[modname] then
return package.loaded[modname]
end
local err = {}
for i,search in ipairs(package.searchers) do
local modf,param = search(modname)
if type(modf) == 'string' then
table.insert(err,modf)
elseif type(modf) == 'function' then
local rc = modf(modname,param)
package.loaded[modname] = package.loaded[modname] or rc or true
package.where [modname] = { i , param }
return package.loaded[modname],param
end
end
error(table.concat(err,'\n'),2)
end
-spc (Why the onerous license? Because this is what you get when you
don't do your own homework!)