diff options
author | Karl Berry <karl@freefriends.org> | 2016-04-22 22:14:39 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2016-04-22 22:14:39 +0000 |
commit | fc4466b32ed330a956ac603b00fd145524cff49a (patch) | |
tree | 2c50e2b8de13aa9233b2c76dffe201558f169e86 /Master/texmf-dist/tex/context/base/mkiv/luat-env.lua | |
parent | 50e2368597d5f6fe2057195d0ae6a9f2044923e4 (diff) |
context (22apr16)
git-svn-id: svn://tug.org/texlive/trunk@40691 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mkiv/luat-env.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/mkiv/luat-env.lua | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/context/base/mkiv/luat-env.lua b/Master/texmf-dist/tex/context/base/mkiv/luat-env.lua new file mode 100644 index 00000000000..5f2a0d281a0 --- /dev/null +++ b/Master/texmf-dist/tex/context/base/mkiv/luat-env.lua @@ -0,0 +1,184 @@ + if not modules then modules = { } end modules ['luat-env'] = { + version = 1.001, + comment = "companion to luat-lib.mkiv", + author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", + copyright = "PRAGMA ADE / ConTeXt Development Team", + license = "see context related readme files" +} + +-- A former version provided functionality for non embeded core scripts i.e. runtime +-- library loading. Given the amount of Lua code we use now, this no longer makes +-- sense. Much of this evolved before bytecode arrays were available and so a lot of +-- code has disappeared already. + +local rawset, rawget, loadfile, assert = rawset, rawget, loadfile, assert + +local trace_locating = false trackers.register("resolvers.locating", function(v) trace_locating = v end) + +local report_lua = logs.reporter("resolvers","lua") + +local luautilities = utilities.lua +local luasuffixes = luautilities.suffixes + +local texgettoks = tex and tex.gettoks + +environment = environment or { } +local environment = environment + +-- environment + +local mt = { + __index = function(_,k) + if k == "version" then + local version = texgettoks and texgettoks("contextversiontoks") + if version and version ~= "" then + rawset(environment,"version",version) + return version + else + return "unknown" + end + elseif k == "kind" then + local kind = texgettoks and texgettoks("contextkindtoks") + if kind and kind ~= "" then + rawset(environment,"kind",kind) + return kind + else + return "unknown" + end + elseif k == "jobname" or k == "formatname" then + local name = tex and tex[k] + if name or name== "" then + rawset(environment,k,name) + return name + else + return "unknown" + end + elseif k == "outputfilename" then + local name = environment.jobname + rawset(environment,k,name) + return name + end + end +} + +setmetatable(environment,mt) + +-- weird place ... depends on a not yet loaded module + +function environment.texfile(filename) + return resolvers.findfile(filename,'tex') +end + +function environment.luafile(filename) -- needs checking + local resolved = resolvers.findfile(filename,'tex') or "" + if resolved ~= "" then + return resolved + end + resolved = resolvers.findfile(filename,'texmfscripts') or "" + if resolved ~= "" then + return resolved + end + return resolvers.findfile(filename,'luatexlibs') or "" +end + +-- local function checkstrip(filename) +-- local modu = modules[file.nameonly(filename)] +-- return modu and modu.dataonly +-- end + +local stripindeed = false directives.register("system.compile.strip", function(v) stripindeed = v end) + +local function strippable(filename) + if stripindeed then + local modu = modules[file.nameonly(filename)] + return modu and modu.dataonly + else + return false + end +end + +function environment.luafilechunk(filename,silent) -- used for loading lua bytecode in the format + filename = file.replacesuffix(filename, "lua") + local fullname = environment.luafile(filename) + if fullname and fullname ~= "" then + local data = luautilities.loadedluacode(fullname,strippable,filename) -- can be overloaded +-- if trace_locating then +-- report_lua("loading file %a %s",fullname,not data and "failed" or "succeeded") +-- elseif not silent then +-- texio.write("<",data and "+ " or "- ",fullname,">") +-- end + if not silent then + report_lua("loading file %a %s",fullname,not data and "failed" or "succeeded") + end + return data + else +-- if trace_locating then +-- report_lua("unknown file %a",filename) +-- end + if not silent then + report_lua("unknown file %a",filename) + end + return nil + end +end + +-- the next ones can use the previous ones / combine + +function environment.loadluafile(filename, version) + local lucname, luaname, chunk + local basename = file.removesuffix(filename) + if basename == filename then + luaname = file.addsuffix(basename,luasuffixes.lua) + lucname = file.addsuffix(basename,luasuffixes.luc) + else + luaname = basename -- forced suffix + lucname = nil + end + -- when not overloaded by explicit suffix we look for a luc file first + local fullname = (lucname and environment.luafile(lucname)) or "" + if fullname ~= "" then + if trace_locating then + report_lua("loading %a",fullname) + end + -- maybe: package.loaded[file.nameonly(fullname)] = true + chunk = loadfile(fullname) -- this way we don't need a file exists check + end + if chunk then + assert(chunk)() + if version then + -- we check of the version number of this chunk matches + local v = version -- can be nil + if modules and modules[filename] then + v = modules[filename].version -- new method + elseif versions and versions[filename] then + v = versions[filename] -- old method + end + if v == version then + return true + else + if trace_locating then + report_lua("version mismatch for %a, lua version %a, luc version %a",filename,v,version) + end + environment.loadluafile(filename) + end + else + return true + end + end + fullname = (luaname and environment.luafile(luaname)) or "" + if fullname ~= "" then + if trace_locating then + report_lua("loading %a",fullname) + end + chunk = loadfile(fullname) -- this way we don't need a file exists check + if not chunk then + if trace_locating then + report_lua("unknown file %a",filename) + end + else + assert(chunk)() + return true + end + end + return false +end |