diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/data-lua.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/data-lua.lua | 241 |
1 files changed, 114 insertions, 127 deletions
diff --git a/Master/texmf-dist/tex/context/base/data-lua.lua b/Master/texmf-dist/tex/context/base/data-lua.lua index 906a611ee16..7e3d92585ab 100644 --- a/Master/texmf-dist/tex/context/base/data-lua.lua +++ b/Master/texmf-dist/tex/context/base/data-lua.lua @@ -6,167 +6,154 @@ if not modules then modules = { } end modules ['data-lua'] = { license = "see context related readme files" } --- some loading stuff ... we might move this one to slot 2 depending --- on the developments (the loaders must not trigger kpse); we could --- of course use a more extensive lib path spec +-- This is now a plug in into l-lua (as we also use the extra paths elsewhere). -local trace_locating = false trackers.register("resolvers.locating", function(v) trace_locating = v end) +local resolvers, package = resolvers, package -local report_libraries = logs.reporter("resolvers","libraries") +local gsub = string.gsub +local concat = table.concat +local addsuffix = file.addsuffix -local gsub, insert = string.gsub, table.insert -local unpack = unpack or table.unpack +local P, S, Cs, lpegmatch = lpeg.P, lpeg.S, lpeg.Cs, lpeg.match -local resolvers, package = resolvers, package +local libsuffixes = { 'tex', 'lua' } +local clibsuffixes = { 'lib' } +local libformats = { 'TEXINPUTS', 'LUAINPUTS' } +local clibformats = { 'CLUAINPUTS' } +local helpers = package.helpers -local libformats = { 'luatexlibs', 'tex', 'texmfscripts', 'othertextfiles' } -- 'luainputs' -local clibformats = { 'lib' } +trackers.register("resolvers.libraries", function(v) helpers.trace = v end) +trackers.register("resolvers.locating", function(v) helpers.trace = v end) -local _path_, libpaths, _cpath_, clibpaths +helpers.report = logs.reporter("resolvers","libraries") -function package.libpaths() - if not _path_ or package.path ~= _path_ then - _path_ = package.path - libpaths = file.splitpath(_path_,";") - end - return libpaths -end +local pattern = Cs(P("!")^0 / "" * (P("/") * P(-1) / "/" + P("/")^1 / "/" + 1)^0) -function package.clibpaths() - if not _cpath_ or package.cpath ~= _cpath_ then - _cpath_ = package.cpath - clibpaths = file.splitpath(_cpath_,";") - end - return clibpaths +local function cleanpath(path) -- hm, don't we have a helper for this? + return resolvers.resolve(lpegmatch(pattern,path)) end -local function thepath(...) - local t = { ... } t[#t+1] = "?.lua" - local path = file.join(unpack(t)) - if trace_locating then - report_libraries("! appending '%s' to 'package.path'",path) - end - return path -end +helpers.cleanpath = cleanpath -local p_libpaths, a_libpaths = { }, { } +local loadedaslib = helpers.loadedaslib +local loadedbylua = helpers.loadedbylua +local loadedbypath = helpers.loadedbypath +local notloaded = helpers.notloaded -function package.appendtolibpath(...) - insert(a_libpath,thepath(...)) -end +local getlibpaths = package.libpaths +local getclibpaths = package.clibpaths -function package.prependtolibpath(...) - insert(p_libpaths,1,thepath(...)) +function helpers.libpaths(libhash) + local libpaths = { } + for i=1,#libformats do + local paths = resolvers.expandedpathlistfromvariable(libformats[i]) + for i=1,#paths do + local path = cleanpath(paths[i]) + if not libhash[path] then + libpaths[#libpaths+1] = path + libhash[path] = true + end + end + end + return libpaths end --- beware, we need to return a loadfile result ! - -local function loaded(libpaths,name,simple) - for i=1,#libpaths do -- package.path, might become option - local libpath = libpaths[i] - local resolved = gsub(libpath,"%?",simple) - if trace_locating then -- more detail - report_libraries("! checking for '%s' on 'package.path': '%s' => '%s'",simple,libpath,resolved) - end - if file.is_readable(resolved) then - if trace_locating then - report_libraries("! lib '%s' located via 'package.path': '%s'",name,resolved) +function helpers.clibpaths(clibhash) + local clibpaths = { } + for i=1,#clibformats do + local paths = resolvers.expandedpathlistfromvariable(clibformats[i]) + for i=1,#paths do + local path = cleanpath(paths[i]) + if not clibhash[path] then + clibpaths[#clibpaths+1] = path + clibhash[path] = true end - return loadfile(resolved) end end + return clibpaths end -package.loaders[2] = function(name) -- was [#package.loaders+1] - if file.suffix(name) == "" then - name = file.addsuffix(name,"lua") -- maybe a list - if trace_locating then -- mode detail - report_libraries("! locating '%s' with forced suffix",name) - end - else - if trace_locating then -- mode detail - report_libraries("! locating '%s'",name) - end +local function loadedbyformat(name,rawname,suffixes,islib) + local trace = helpers.trace + local report = helpers.report + if trace then + report("locating %a as %a using formats %a",rawname,name,suffixes) end - for i=1,#libformats do - local format = libformats[i] + for i=1,#suffixes do -- so we use findfile and not a lookup loop + local format = suffixes[i] local resolved = resolvers.findfile(name,format) or "" - if trace_locating then -- mode detail - report_libraries("! checking for '%s' using 'libformat path': '%s'",name,format) + if trace then + report("checking %a using format %a",name,format) end if resolved ~= "" then - if trace_locating then - report_libraries("! lib '%s' located via environment: '%s'",name,resolved) + if trace then + report("lib %a located on %a",name,resolved) + end + if islib then + return true, loadedaslib(resolved,rawname) + else + return true, loadfile(resolved) end - return loadfile(resolved) end end - -- libpaths - local libpaths, clibpaths = package.libpaths(), package.clibpaths() - local simple = gsub(name,"%.lua$","") - local simple = gsub(simple,"%.","/") - local resolved = loaded(p_libpaths,name,simple) or loaded(libpaths,name,simple) or loaded(a_libpaths,name,simple) - if resolved then - return resolved +end + +helpers.loadedbyformat = loadedbyformat + +-- alternatively we could set the package.searchers + +local pattern = Cs((((1-S("\\/"))^0 * (S("\\/")^1/"/"))^0 * (P(".")^1/"/"+P(1))^1) * -1) + +local function lualibfile(name) + return lpegmatch(pattern,name) or name +end + +helpers.lualibfile = lualibfile + +-- print(lualibfile("bar")) +-- print(lualibfile("foo.bar")) +-- print(lualibfile("crap/foo...bar")) +-- print(lualibfile("crap//foo.bar")) +-- print(lualibfile("crap/../foo.bar")) +-- print(lualibfile("crap/.././foo.bar")) + +-- alternatively we could split in path and base and temporary set the libpath to path + +function helpers.loaded(name) + local thename = lualibfile(name) + local luaname = addsuffix(thename,"lua") + local libname = addsuffix(thename,os.libsuffix) + local libpaths = getlibpaths() + local clibpaths = getclibpaths() + local done, result = loadedbyformat(luaname,name,libsuffixes,false) + if done then + return result end - -- - local libname = file.addsuffix(simple,os.libsuffix) - for i=1,#clibformats do - -- better have a dedicated loop - local format = clibformats[i] - local paths = resolvers.expandedpathlistfromvariable(format) - for p=1,#paths do - local path = paths[p] - local resolved = file.join(path,libname) - if trace_locating then -- mode detail - report_libraries("! checking for '%s' using 'clibformat path': '%s'",libname,path) - end - if file.is_readable(resolved) then - if trace_locating then - report_libraries("! lib '%s' located via 'clibformat': '%s'",libname,resolved) - end - return package.loadlib(resolved,name) - end - end + local done, result = loadedbyformat(libname,name,clibsuffixes,true) + if done then + return result end - for i=1,#clibpaths do -- package.path, might become option - local libpath = clibpaths[i] - local resolved = gsub(libpath,"?",simple) - if trace_locating then -- more detail - report_libraries("! checking for '%s' on 'package.cpath': '%s'",simple,libpath) - end - if file.is_readable(resolved) then - if trace_locating then - report_libraries("! lib '%s' located via 'package.cpath': '%s'",name,resolved) - end - return package.loadlib(resolved,name) - end + local done, result = loadedbypath(luaname,name,libpaths,false,"lua") + if done then + return result end - -- just in case the distribution is messed up - if trace_loading then -- more detail - report_libraries("! checking for '%s' using 'luatexlibs': '%s'",name) + local done, result = loadedbypath(luaname,name,clibpaths,false,"lua") + if done then + return result end - local resolved = resolvers.findfile(file.basename(name),'luatexlibs') or "" - if resolved ~= "" then - if trace_locating then - report_libraries("! lib '%s' located by basename via environment: '%s'",name,resolved) - end - return loadfile(resolved) + local done, result = loadedbypath(libname,name,clibpaths,true,"lib") + if done then + return result end - if trace_locating then - report_libraries('? unable to locate lib: %s',name) + local done, result = loadedbylua(name) + if done then + return result end --- return "unable to locate " .. name + return notloaded(name) end -resolvers.loadlualib = require - --- -- -- -- +-- package.searchers[3] = nil -- get rid of the built in one (done in l-lua) -package.obsolete = package.obsolete or { } +-- package.extraclibpath(environment.ownpath) -package.append_libpath = appendtolibpath -- will become obsolete -package.prepend_libpath = prependtolibpath -- will become obsolete - -package.obsolete.append_libpath = appendtolibpath -- will become obsolete -package.obsolete.prepend_libpath = prependtolibpath -- will become obsolete +resolvers.loadlualib = require |