diff options
Diffstat (limited to 'tex/context/base/l-lua.lua')
-rw-r--r-- | tex/context/base/l-lua.lua | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/tex/context/base/l-lua.lua b/tex/context/base/l-lua.lua index 7c78a4031..837f03d2a 100644 --- a/tex/context/base/l-lua.lua +++ b/tex/context/base/l-lua.lua @@ -147,3 +147,168 @@ function optionalrequire(...) return result end end + +-- Code moved from data-lua and changed into a plug-in. + +-- We overload the regular loader. We do so because we operate mostly in +-- tds and use our own loader code. Alternatively we could use a more +-- extensive definition of package.path and package.cpath but even then +-- we're not done. Also, we now have better tracing. +-- +-- -- local mylib = require("libtest") +-- -- local mysql = require("luasql.mysql") + +local gsub, format = string.gsub, string.format + +local package = package +local searchers = package.searchers or package.loaders + +local libpaths = nil +local clibpaths = nil +local libhash = { } +local clibhash = { } +local libextras = { } +local clibextras = { } + +-- dummies + +local filejoin = file and file.join or function(path,name) return path .. "/" .. name end +local isreadable = file and file.is_readable or function(name) local f = io.open(name) if f then f:close() return true end end +local addsuffix = file and file.addsuffix or function(name,suffix) return name .. "." .. suffix end + +-- + +local function cleanpath(path) -- hm, don't we have a helper for this? + return path +end + +local helpers = package.helpers or { + libpaths = function() return { } end, + clibpaths = function() return { } end, + cleanpath = cleanpath, + trace = false, + report = function(...) print(format(...)) end, +} +package.helpers = helpers + +local function getlibpaths() + return libpaths or helpers.libpaths(libhash) +end + +local function getclibpaths() + return clibpaths or helpers.clibpaths(clibhash) +end + +package.libpaths = getlibpaths +package.clibpaths = getclibpaths + +function package.extralibpath(...) + local libpaths = getlibpaths() + local pathlist = { ... } + local cleanpath = helpers.cleanpath + local trace = helpers.trace + local report = helpers.report + for p=1,#pathlist do + local paths = pathlist[p] + for i=1,#paths do + local path = cleanpath(paths[i]) + if not libhash[path] then + if trace then + libraries("! extra lua path '%s'",path) + end + libextras[#libextras+1] = path + libpaths [#libpaths +1] = path + end + end + end +end + +function package.extraclibpath(...) + local clibpaths = getclibpaths() + local pathlist = { ... } + local cleanpath = helpers.cleanpath + local trace = helpers.trace + local report = helpers.report + for p=1,#pathlist do + local paths = pathlist[p] + for i=1,#paths do + local path = cleanpath(paths[i]) + if not clibhash[path] then + if trace then + report("! extra lib path '%s'",path) + end + clibextras[#clibextras+1] = path + clibpaths [#clibpaths +1] = path + end + end + end +end + +if not searchers[-2] then + -- use package-path and package-cpath + searchers[-2] = searchers[2] +end + +searchers[2] = function(name) + return helpers.loaded(name) +end + +local function loadedaslib(resolved,rawname) + return package.loadlib(resolved,"luaopen_" .. gsub(rawname,"%.","_")) +end + +local function loadedbylua(name) + if helpers.trace then + helpers.report("! locating %q using normal loader",name) + end + return searchers[-2](name) +end + +local function loadedbypath(name,rawname,paths,islib,what) + local trace = helpers.trace + local report = helpers.report + if trace then + report("! locating %q as %q on %q paths",rawname,name,what) + end + for p=1,#paths do + local path = paths[p] + local resolved = filejoin(path,name) + if trace then -- mode detail + report("! checking for %q using %q path %q",name,what,path) + end + if isreadable(resolved) then + if trace then + report("! lib %q located on %q",name,resolved) + end + if islib then + return loadedaslib(resolved,rawname) + else + return loadfile(resolved) + end + end + end +end + +local function notloaded(name) + if helpers.trace then + helpers.report("? unable to locate library %q",name) + end +end + +helpers.loadedaslib = loadedaslib +helpers.loadedbylua = loadedbylua +helpers.loadedbypath = loadedbypath +helpers.notloaded = notloaded + +function helpers.loaded(name) + local thename = gsub(name,"%.","/") + local luaname = addsuffix(thename,"lua") + local libname = addsuffix(thename,os.libsuffix) + local libpaths = getlibpaths() + local clibpaths = getclibpaths() + return loadedbypath(luaname,name,libpaths,false,"lua") + or loadedbypath(luaname,name,clibpaths,false,"lua") + or loadedbypath(libname,name,clibpaths,true,"lib") + or loadedbylua(name) + or notloaded(name) +end |