| 1 |
if not modules then modules = { } end modules ['util-lua'] = { |
| 2 |
version = 1.001, |
| 3 |
comment = "companion to luat-lib.mkiv", |
| 4 |
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 5 |
comment = "the strip code is written by Peter Cawley", |
| 6 |
copyright = "PRAGMA ADE / ConTeXt Development Team", |
| 7 |
license = "see context related readme files" |
| 8 |
} |
| 9 |
|
| 10 |
-- we will remove the 5.1 code some day soon |
| 11 |
|
| 12 |
local rep, sub, byte, dump, format = string.rep, string.sub, string.byte, string.dump, string.format |
| 13 |
local load, loadfile, type, collectgarbage = load, loadfile, type, collectgarbage |
| 14 |
|
| 15 |
utilities = utilities or {} |
| 16 |
utilities.lua = utilities.lua or { } |
| 17 |
local luautilities = utilities.lua |
| 18 |
|
| 19 |
local report_lua = logs.reporter("system","lua") |
| 20 |
local report_mem = logs.reporter("system","lua memory") |
| 21 |
|
| 22 |
local tracestripping = false |
| 23 |
local tracememory = false |
| 24 |
luautilities.stripcode = true -- support stripping when asked for |
| 25 |
luautilities.alwaysstripcode = false -- saves 1 meg on 7 meg compressed format file (2012.08.12) |
| 26 |
luautilities.nofstrippedchunks = 0 |
| 27 |
luautilities.nofstrippedbytes = 0 |
| 28 |
local strippedchunks = { } -- allocate() |
| 29 |
luautilities.strippedchunks = strippedchunks |
| 30 |
|
| 31 |
if not LUATEXENGINE then |
| 32 |
--- probably mtxrun ... |
| 33 |
LUATEXENGINE = status.luatex_engine and string.lower(status.luatex_engine) |
| 34 |
JITSUPPORTED = LUATEXENGINE == "luajittex" or jit |
| 35 |
CONTEXTLMTXMODE = CONTEXTLMTXMODE or (LUATEXENGINE == "luametatex" and 1) or 0 |
| 36 |
end |
| 37 |
|
| 38 |
luautilities.suffixes = { |
| 39 |
tma = "tma", |
| 40 |
tmc = (CONTEXTLMTXMODE and CONTEXTLMTXMODE > 0 and "tmd") or (jit and "tmb") or "tmc", |
| 41 |
lua = "lua", |
| 42 |
luc = (CONTEXTLMTXMODE and CONTEXTLMTXMODE > 0 and "lud") or (jit and "lub") or "luc", |
| 43 |
lui = "lui", |
| 44 |
luv = "luv", |
| 45 |
luj = "luj", |
| 46 |
tua = "tua", |
| 47 |
-- tuc = "tuc", |
| 48 |
tuc = (CONTEXTLMTXMODE and CONTEXTLMTXMODE > 0 and "tud") or (jit and "tub") or "tuc", |
| 49 |
} |
| 50 |
|
| 51 |
-- environment.loadpreprocessedfile can be set to a preprocessor |
| 52 |
|
| 53 |
local function register(name) -- makes no sense runtime |
| 54 |
if tracestripping then |
| 55 |
report_lua("stripped bytecode from %a",name or "unknown") |
| 56 |
end |
| 57 |
strippedchunks[#strippedchunks+1] = name |
| 58 |
luautilities.nofstrippedchunks = luautilities.nofstrippedchunks + 1 |
| 59 |
end |
| 60 |
|
| 61 |
local function stupidcompile(luafile,lucfile,strip) |
| 62 |
local code = io.loaddata(luafile) |
| 63 |
if code and code ~= "" then |
| 64 |
code = load(code) |
| 65 |
if code then |
| 66 |
code = dump(code,strip and luautilities.stripcode or luautilities.alwaysstripcode) |
| 67 |
if code and code ~= "" then |
| 68 |
register(name) |
| 69 |
io.savedata(lucfile,code) |
| 70 |
return true, 0 |
| 71 |
end |
| 72 |
else |
| 73 |
report_lua("fatal error %a in file %a",1,luafile) |
| 74 |
end |
| 75 |
else |
| 76 |
report_lua("fatal error %a in file %a",2,luafile) |
| 77 |
end |
| 78 |
return false, 0 |
| 79 |
end |
| 80 |
|
| 81 |
-- quite subtle ... doing this wrong incidentally can give more bytes |
| 82 |
|
| 83 |
function luautilities.loadedluacode(fullname,forcestrip,name,macros) |
| 84 |
-- quite subtle ... doing this wrong incidentally can give more bytes |
| 85 |
name = name or fullname |
| 86 |
if macros then |
| 87 |
macros = lua.macros |
| 88 |
end |
| 89 |
local code, message |
| 90 |
if macros then |
| 91 |
code, message = macros.loaded(fullname,true,false) |
| 92 |
else |
| 93 |
code, message = loadfile(fullname) |
| 94 |
end |
| 95 |
if code then |
| 96 |
code() |
| 97 |
else |
| 98 |
report_lua("loading of file %a failed:\n\t%s",fullname,message or "no message") |
| 99 |
code, message = loadfile(fullname) |
| 100 |
end |
| 101 |
if forcestrip and luautilities.stripcode then |
| 102 |
if type(forcestrip) == "function" then |
| 103 |
forcestrip = forcestrip(fullname) |
| 104 |
end |
| 105 |
if forcestrip or luautilities.alwaysstripcode then |
| 106 |
register(name) |
| 107 |
return load(dump(code,true)), 0 |
| 108 |
else |
| 109 |
return code, 0 |
| 110 |
end |
| 111 |
elseif luautilities.alwaysstripcode then |
| 112 |
register(name) |
| 113 |
return load(dump(code,true)), 0 |
| 114 |
else |
| 115 |
return code, 0 |
| 116 |
end |
| 117 |
end |
| 118 |
|
| 119 |
function luautilities.strippedloadstring(code,name,forcestrip) -- not executed |
| 120 |
local code, message = load(code) |
| 121 |
if not code then |
| 122 |
report_lua("loading of file %a failed:\n\t%s",name,message or "no message") |
| 123 |
end |
| 124 |
if forcestrip and luautilities.stripcode or luautilities.alwaysstripcode then |
| 125 |
register(name) |
| 126 |
return load(dump(code,true)), 0 -- not yet executed |
| 127 |
else |
| 128 |
return code, 0 |
| 129 |
end |
| 130 |
end |
| 131 |
|
| 132 |
function luautilities.loadstring(code,name) -- not executed |
| 133 |
local code, message = load(code) |
| 134 |
if not code then |
| 135 |
report_lua("loading of file %a failed:\n\t%s",name,message or "no message") |
| 136 |
end |
| 137 |
return code, 0 |
| 138 |
end |
| 139 |
|
| 140 |
function luautilities.compile(luafile,lucfile,cleanup,strip,fallback) -- defaults: cleanup=false strip=true |
| 141 |
report_lua("compiling %a into %a",luafile,lucfile) |
| 142 |
os.remove(lucfile) |
| 143 |
local done = stupidcompile(luafile,lucfile,strip ~= false) |
| 144 |
if done then |
| 145 |
report_lua("dumping %a into %a stripped",luafile,lucfile) |
| 146 |
if cleanup == true and lfs.isfile(lucfile) and lfs.isfile(luafile) then |
| 147 |
report_lua("removing %a",luafile) |
| 148 |
os.remove(luafile) |
| 149 |
end |
| 150 |
end |
| 151 |
return done |
| 152 |
end |
| 153 |
|
| 154 |
function luautilities.loadstripped(...) |
| 155 |
local l = load(...) |
| 156 |
if l then |
| 157 |
return load(dump(l,true)) |
| 158 |
end |
| 159 |
end |
| 160 |
|
| 161 |
-- local getmetatable, type = getmetatable, type |
| 162 |
-- |
| 163 |
-- local types = { } |
| 164 |
-- |
| 165 |
-- function luautilities.registerdatatype(d,name) |
| 166 |
-- types[getmetatable(d)] = name |
| 167 |
-- end |
| 168 |
-- |
| 169 |
-- function luautilities.datatype(d) |
| 170 |
-- local t = type(d) |
| 171 |
-- if t == "userdata" then |
| 172 |
-- local m = getmetatable(d) |
| 173 |
-- return m and types[m] or "userdata" |
| 174 |
-- else |
| 175 |
-- return t |
| 176 |
-- end |
| 177 |
-- end |
| 178 |
-- |
| 179 |
-- luautilities.registerdatatype(lpeg.P("!"),"lpeg") |
| 180 |
-- |
| 181 |
-- print(luautilities.datatype(lpeg.P("oeps"))) |
| 182 |
|
| 183 |
-- These finalizers will only be invoked when we have a proper lua_close |
| 184 |
-- call (which is not happening in luatex tex node yes) or finish with an |
| 185 |
-- os.exit(n,true). |
| 186 |
|
| 187 |
local finalizers = { } |
| 188 |
|
| 189 |
setmetatable(finalizers, { |
| 190 |
__gc = function(t) |
| 191 |
for i=1,#t do |
| 192 |
pcall(t[i]) -- let's not crash |
| 193 |
end |
| 194 |
end |
| 195 |
} ) |
| 196 |
|
| 197 |
function luautilities.registerfinalizer(f) |
| 198 |
finalizers[#finalizers+1] = f |
| 199 |
end |
| 200 |
|
| 201 |
function luautilities.checkmemory(previous,threshold,trace) -- threshold in MB |
| 202 |
local current = collectgarbage("count") |
| 203 |
if previous then |
| 204 |
local checked = (threshold or 64)*1024 |
| 205 |
local delta = current - previous |
| 206 |
if current - previous > checked then |
| 207 |
collectgarbage("collect") |
| 208 |
local afterwards = collectgarbage("count") |
| 209 |
if trace or tracememory then |
| 210 |
report_mem("previous %r MB, current %r MB, delta %r MB, threshold %r MB, afterwards %r MB", |
| 211 |
previous/1024,current/1024,delta/1024,threshold,afterwards) |
| 212 |
end |
| 213 |
return afterwards |
| 214 |
elseif trace or tracememory then |
| 215 |
report_mem("previous %r MB, current %r MB, delta %r MB, threshold %r MB", |
| 216 |
previous/1024,current/1024,delta/1024,threshold) |
| 217 |
end |
| 218 |
end |
| 219 |
return current |
| 220 |
end |