diff options
author | Hans Hagen <pragma@wxs.nl> | 2013-04-17 17:29:00 +0200 |
---|---|---|
committer | Hans Hagen <pragma@wxs.nl> | 2013-04-17 17:29:00 +0200 |
commit | 32bd9347bda99504e36ef02e816e98c811d24a97 (patch) | |
tree | ac50b34a9baaf92fd520c47458bcfe6a77b637ad /tex/context/base/data-lua.lua | |
parent | 959b0d65aa998bf38a7d468451666a709677a6a7 (diff) | |
download | context-32bd9347bda99504e36ef02e816e98c811d24a97.tar.gz |
beta 2013.04.17 17:29
Diffstat (limited to 'tex/context/base/data-lua.lua')
-rw-r--r-- | tex/context/base/data-lua.lua | 160 |
1 files changed, 83 insertions, 77 deletions
diff --git a/tex/context/base/data-lua.lua b/tex/context/base/data-lua.lua index 7e3d92585..14aebf959 100644 --- a/tex/context/base/data-lua.lua +++ b/tex/context/base/data-lua.lua @@ -16,61 +16,65 @@ local addsuffix = file.addsuffix local P, S, Cs, lpegmatch = lpeg.P, lpeg.S, lpeg.Cs, lpeg.match -local libsuffixes = { 'tex', 'lua' } -local clibsuffixes = { 'lib' } -local libformats = { 'TEXINPUTS', 'LUAINPUTS' } -local clibformats = { 'CLUAINPUTS' } -local helpers = package.helpers +local luasuffixes = { 'tex', 'lua' } +local libsuffixes = { 'lib' } +local luaformats = { 'TEXINPUTS', 'LUAINPUTS' } +local libformats = { 'CLUAINPUTS' } +local helpers = package.helpers or { } +local methods = helpers.methods or { } trackers.register("resolvers.libraries", function(v) helpers.trace = v end) trackers.register("resolvers.locating", function(v) helpers.trace = v end) helpers.report = logs.reporter("resolvers","libraries") +helpers.sequence = { + "already loaded", + "preload table", + "lua variable format", + "lib variable format", + "lua extra list", + "lib extra list", + "path specification", + "cpath specification", + "all in one fallback", + "not loaded", +} + local pattern = Cs(P("!")^0 / "" * (P("/") * P(-1) / "/" + P("/")^1 / "/" + 1)^0) -local function cleanpath(path) -- hm, don't we have a helper for this? +function helpers.cleanpath(path) -- hm, don't we have a helper for this? return resolvers.resolve(lpegmatch(pattern,path)) end -helpers.cleanpath = cleanpath - -local loadedaslib = helpers.loadedaslib -local loadedbylua = helpers.loadedbylua -local loadedbypath = helpers.loadedbypath -local notloaded = helpers.notloaded - -local getlibpaths = package.libpaths -local getclibpaths = package.clibpaths - -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 +local loadedaslib = helpers.loadedaslib +local loadedbypath = helpers.loadedbypath +local getextraluapaths = package.extraluapaths +local getextralibpaths = package.extralibpaths +local registerpath = helpers.registerpath +local lualibfile = helpers.lualibfile + +local luaformatpaths +local libformatpaths + +local function getluaformatpaths() + if not luaformatpaths then + luaformatpaths = { } + for i=1,#luaformats do + registerpath("lua format","lua",luaformatpaths,resolvers.expandedpathlistfromvariable(luaformats[i])) end end - return libpaths + return luaformatpaths end -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 +local function getlibformatpaths() + if not libformatpaths then + libformatpaths = { } + for i=1,#libformats do + registerpath("lib format","lib",libformatpaths,resolvers.expandedpathlistfromvariable(libformats[i])) end end - return clibpaths + return libformatpaths end local function loadedbyformat(name,rawname,suffixes,islib) @@ -89,10 +93,14 @@ local function loadedbyformat(name,rawname,suffixes,islib) if trace then report("lib %a located on %a",name,resolved) end + local result = nil if islib then - return true, loadedaslib(resolved,rawname) + result = loadedaslib(resolved,rawname) else - return true, loadfile(resolved) + result = loadfile(resolved) + end + if result then + return true, result() end end end @@ -100,16 +108,6 @@ 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")) @@ -119,41 +117,49 @@ helpers.lualibfile = lualibfile -- 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 done, result = loadedbyformat(libname,name,clibsuffixes,true) - if done then - return result - end - local done, result = loadedbypath(luaname,name,libpaths,false,"lua") - if done then - return result +-- we could build a list of relevant paths but for tracing it's better to have the +-- whole lot (ok, we could skip the duplicates) + +local shown = false + +methods["lua variable format"] = function(name) + if not shown and helpers.trace then + local luapaths = getluaformatpaths() -- triggers building + if #luapaths > 0 then + helpers.report("using %s lua format paths",#luapaths) + else + helpers.report("no lua format paths defined") + end + shown = true end - local done, result = loadedbypath(luaname,name,clibpaths,false,"lua") + local thename = lualibfile(name) + local luaname = addsuffix(thename,"lua") + local done, result = loadedbyformat(luaname,name,luasuffixes,false) if done then - return result + return true, result end - local done, result = loadedbypath(libname,name,clibpaths,true,"lib") - if done then - return result +end + +local shown = false + +methods["lib variable format"] = function(name) + if not shown and helpers.trace then + local libpaths = getlibformatpaths() -- triggers building + if #libpaths > 0 then + helpers.report("using %s lib format paths",#libpaths) + else + helpers.report("no lib format paths defined") + end + shown = true end - local done, result = loadedbylua(name) + local thename = lualibfile(name) + local libname = addsuffix(thename,os.libsuffix) + local done, result = loadedbyformat(libname,name,libsuffixes,true) if done then - return result + return true, result end - return notloaded(name) end --- package.searchers[3] = nil -- get rid of the built in one (done in l-lua) - -- package.extraclibpath(environment.ownpath) resolvers.loadlualib = require |