lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 2019-04-15 6:08 p.m., Sean Conner wrote:
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

Nice! Altho I think you should use table.concat(err, '') instead. IIRC this is how vanilla require does it.

This probably doesn't enjoy changing package.searchers at runtime, tho. I'd put the whole search function in the where entry, and use named indexes. This isn't a hot path - or shouldn't be - anyway.


   -spc (Why the onerous license?  Because this is what you get when you
	don't do your own homework!)


I might use this at some point, just because.