if not modules then modules = { } end modules ['luat-cbk'] = { version = 1.001, comment = "companion to luat-lib.tex", author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", copyright = "PRAGMA ADE / ConTeXt Development Team", license = "see context related readme files" } --[[ldx--

Callbacks are the real asset of . They permit you to hook your own code into the engine. Here we implement a few handy auxiliary functions.

--ldx]]-- callbacks = { } callbacks.stack = { } --[[ldx--

When you (temporarily) want to install a callback function, and after a while wants to revert to the original one, you can use the following two functions.

--ldx]]-- function callbacks.push(name, func) if not callbacks.stack[name] then callbacks.stack[name] = { } end table.insert(callbacks.stack[name],callback.find(name)) callback.register(name, func) end function callbacks.pop(name) -- this fails: callback.register(name, table.remove(callbacks.stack[name])) local func = table.remove(callbacks.stack[name]) callback.register(name, func) end --[[ldx--

The simple case is to remove the callback:

callbacks.push('linebreak_filter') ... some actions ... callbacks.pop('linebreak_filter')

Often, in such case, another callback or a macro call will pop the original.

In practice one will install a new handler, like in:

callbacks.push('linebreak_filter', function(...) return something_done(...) end)

Even more interesting is:

callbacks.push('linebreak_filter', function(...) callbacks.pop('linebreak_filter') return something_done(...) end)

This does a one-shot.

--ldx]]-- --[[ldx--

Callbacks may result in doing some hard work which takes time and above all resourses. Sometimes it makes sense to disable or tune the garbage collector in order to keep the use of resources acceptable.

At some point in the development we did some tests with counting nodes (in this case 121049).

setstepmulsecondsmegabytes
20024.080.5
17521.078.2
15022.074.6
16022.074.6
16521.077.6
12521.589.2
10021.588.4

The following code is kind of experimental. In the documents that describe the development of we report on speed tests. One observation is thta it sometimes helps to restart the collector.

--ldx]]-- garbagecollector = { } do local level = 0 --~ collectgarbage("setstepmul", 165) --~ collectgarbage("setstepmul",50) garbagecollector.trace = false garbagecollector.tune = false -- for the moment local function report(format) if garbagecollector.trace then -- texio.write_nl(string.format(format,level,status.luastate_bytes)) texio.write_nl(string.format(format,level,collectgarbage("count"))) end end function garbagecollector.update() report("%s: memory before update: %s") collectgarbage("restart") end function garbagecollector.push() if garbagecollector.tune then level = level + 1 if level == 1 then collectgarbage("stop") end report("%s: memory after push: %s") else garbagecollector.update() end end function garbagecollector.pop() if garbagecollector.tune then report("%s: memory before pop: %s") if level == 1 then collectgarbage("restart") end level = level - 1 end end function garbagecollector.cycle() if garbagecollector.tune then report("%s: memory before collect: %s") collectgarbage("collect") report("%s: memory after collect: %s") end end end