node-res.lua /size: 18 Kb    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
node-res
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to node-ini.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
local
type
,
next
=
type
,
next
10
local
gmatch
,
format
=
string
.
gmatch
,
string
.
format
11 12
--[[ldx-- 13<p>The next function is not that much needed but in <l n='context'/> we use 14for debugging <l n='luatex'/> node management.</p> 15--ldx]]
--
16 17
local
nodes
,
node
=
nodes
,
node
18 19
local
report_nodes
=
logs
.
reporter
(
"
nodes
"
,
"
housekeeping
"
)
20 21
nodes
.
pool
=
nodes
.
pool
or
{
}
22
local
nodepool
=
nodes
.
pool
23 24
local
whatsitcodes
=
nodes
.
whatsitcodes
25
local
gluecodes
=
nodes
.
gluecodes
26
local
kerncodes
=
nodes
.
kerncodes
27
local
rulecodes
=
nodes
.
rulecodes
28
local
nodecodes
=
nodes
.
nodecodes
29
local
leadercodes
=
nodes
.
leadercodes
30
local
boundarycodes
=
nodes
.
boundarycodes
31
local
usercodes
=
nodes
.
usercodes
32 33
local
nodeproperties
=
nodes
.
properties
.
data
34 35
local
glyph_code
=
nodecodes
.
glyph
36
local
rule_code
=
nodecodes
.
rule
37
local
kern_code
=
nodecodes
.
kern
38
local
glue_code
=
nodecodes
.
glue
39
local
gluespec_code
=
nodecodes
.
gluespec
40
local
whatsit_code
=
nodecodes
.
whatsit
41 42
local
currentfont
=
font
.
current
43
local
texgetcount
=
tex
.
getcount
44 45
local
allocate
=
utilities
.
storage
.
allocate
46 47
local
reserved
=
{
}
48
local
nofreserved
=
0
49
local
userids
=
allocate
(
)
50
local
lastid
=
0
51 52
setmetatable
(
userids
,
{
53
__index
=
function
(
t
,
k
)
54
if
type
(
k
)
=
=
"
string
"
then
55
lastid
=
lastid
+
1
56
rawset
(
userids
,
lastid
,
k
)
57
rawset
(
userids
,
k
,
lastid
)
58
return
lastid
59
else
60
rawset
(
userids
,
k
,
k
)
61
return
k
62
end
63
end
,
64
__call
=
function
(
t
,
k
)
65
return
t
[
k
]
66
end
67
}
)
68 69
-- nuts overload
70 71
local
nuts
=
nodes
.
nuts
72
local
nutpool
=
{
}
73
nuts
.
pool
=
nutpool
74 75
local
tonut
=
nuts
.
tonut
76
local
tonode
=
nuts
.
tonode
77 78
local
getbox
=
nuts
.
getbox
79
local
getid
=
nuts
.
getid
80
local
getlist
=
nuts
.
getlist
81
local
getglue
=
nuts
.
getglue
82 83
local
setfield
=
nuts
.
setfield
84
local
setchar
=
nuts
.
setchar
85
local
setlist
=
nuts
.
setlist
86
local
setwhd
=
nuts
.
setwhd
87
local
setglue
=
nuts
.
setglue
88
local
setdisc
=
nuts
.
setdisc
89
local
setfont
=
nuts
.
setfont
90
local
setkern
=
nuts
.
setkern
91
local
setpenalty
=
nuts
.
setpenalty
92
local
setdir
=
nuts
.
setdir
93
local
setdirection
=
nuts
.
setdirection
94
local
setshift
=
nuts
.
setshift
95
local
setwidth
=
nuts
.
setwidth
96
local
setsubtype
=
nuts
.
setsubtype
97
local
setleader
=
nuts
.
setleader
98 99
local
setdata
=
nuts
.
setdata
100
local
setruledata
=
nuts
.
setruledata
101
local
setvalue
=
nuts
.
setvalue
102 103
local
copy_nut
=
nuts
.
copy_only
or
nuts
.
copy
104
local
new_nut
=
nuts
.
new
105
local
flush_nut
=
nuts
.
flush
106 107
-- at some point we could have a dual set (the overhead of tonut is not much larger than
108
-- metatable associations at the lua/c end esp if we also take assignments into account
109 110
-- table.setmetatableindex(nodepool,function(t,k,v)
111
-- -- report_nodes("defining nodepool[%s] instance",k)
112
-- local f = nutpool[k]
113
-- local v = function(...)
114
-- return tonode(f(...))
115
-- end
116
-- t[k] = v
117
-- return v
118
-- end)
119
--
120
-- -- we delay one step because that permits us a forward reference
121
-- -- e.g. in pdfsetmatrix
122 123
table
.
setmetatableindex
(
nodepool
,
function
(
t
,
k
,
v
)
124
-- report_nodes("defining nodepool[%s] instance",k)
125
local
v
=
function
(
...
)
126
local
f
=
nutpool
[
k
]
127
local
v
=
function
(
...
)
128
return
tonode
(
f
(
...
)
)
129
end
130
t
[
k
]
=
v
131
return
v
(
...
)
132
end
133
t
[
k
]
=
v
134
return
v
135
end
)
136 137
local
function
register_nut
(
n
)
138
nofreserved
=
nofreserved
+
1
139
reserved
[
nofreserved
]
=
n
140
return
n
141
end
142 143
local
function
register_node
(
n
)
144
nofreserved
=
nofreserved
+
1
145
if
type
(
n
)
=
=
"
number
"
then
-- isnut(n)
146
reserved
[
nofreserved
]
=
n
147
else
148
reserved
[
nofreserved
]
=
tonut
(
n
)
149
end
150
return
n
151
end
152 153
nodepool
.
userids
=
userids
154
nodepool
.
register
=
register_node
155 156
nutpool
.
userids
=
userids
157
nutpool
.
register
=
register_node
-- could be register_nut
158 159
-- so far
160 161
local
disc
=
register_nut
(
new_nut
(
nodecodes
.
disc
)
)
162
local
kern
=
register_nut
(
new_nut
(
kern_code
,
kerncodes
.
userkern
)
)
163
local
fontkern
=
register_nut
(
new_nut
(
kern_code
,
kerncodes
.
fontkern
)
)
164
local
italickern
=
register_nut
(
new_nut
(
kern_code
,
kerncodes
.
italiccorrection
)
)
165
local
penalty
=
register_nut
(
new_nut
(
nodecodes
.
penalty
)
)
166
local
glue
=
register_nut
(
new_nut
(
glue_code
)
)
167
local
gluespec
=
register_nut
(
new_nut
(
gluespec_code
)
)
168
local
glyph
=
register_nut
(
new_nut
(
glyph_code
,
0
)
)
169 170
local
textdir
=
register_nut
(
new_nut
(
nodecodes
.
dir
)
)
171 172
local
latelua
=
register_nut
(
new_nut
(
whatsit_code
,
whatsitcodes
.
latelua
)
)
173
local
savepos
=
register_nut
(
new_nut
(
whatsit_code
,
whatsitcodes
.
savepos
)
)
174 175
local
user_node
=
new_nut
(
whatsit_code
,
whatsitcodes
.
userdefined
)
176 177
if
CONTEXTLMTXMODE
=
=
0
then
178
setfield
(
user_node
,
"
type
"
,
usercodes
.
number
)
179
end
180 181
local
left_margin_kern
,
right_margin_kern
182 183
if
CONTEXTLMTXMODE
>
0
then
184
left_margin_kern
=
register_nut
(
new_nut
(
kern_code
,
kerncodes
.
leftmargincode
)
)
185
right_margin_kern
=
register_nut
(
new_nut
(
kern_code
,
kerncodes
.
rightmargincode
)
)
186
else
187
left_margin_kern
=
register_nut
(
new_nut
(
nodecodes
.
marginkern
,
0
)
)
188
right_margin_kern
=
register_nut
(
new_nut
(
nodecodes
.
marginkern
,
1
)
)
189
end
190 191
local
lineskip
=
register_nut
(
new_nut
(
glue_code
,
gluecodes
.
lineskip
)
)
192
local
baselineskip
=
register_nut
(
new_nut
(
glue_code
,
gluecodes
.
baselineskip
)
)
193
local
leftskip
=
register_nut
(
new_nut
(
glue_code
,
gluecodes
.
leftskip
)
)
194
local
rightskip
=
register_nut
(
new_nut
(
glue_code
,
gluecodes
.
rightskip
)
)
195
local
lefthangskip
=
register_nut
(
new_nut
(
glue_code
,
gluecodes
.
lefthangskip
)
)
196
local
righthangskip
=
register_nut
(
new_nut
(
glue_code
,
gluecodes
.
righthangskip
)
)
197
local
indentskip
=
register_nut
(
new_nut
(
glue_code
,
gluecodes
.
indentskip
)
)
198
local
correctionskip
=
register_nut
(
new_nut
(
glue_code
,
gluecodes
.
correctionskip
)
)
199 200
local
temp
=
register_nut
(
new_nut
(
nodecodes
.
temp
,
0
)
)
201 202
local
noad
=
register_nut
(
new_nut
(
nodecodes
.
noad
)
)
203
local
delimiter
=
register_nut
(
new_nut
(
nodecodes
.
delim
)
)
204
local
fence
=
register_nut
(
new_nut
(
nodecodes
.
fence
)
)
205
local
submlist
=
register_nut
(
new_nut
(
nodecodes
.
submlist
)
)
206
local
accent
=
register_nut
(
new_nut
(
nodecodes
.
accent
)
)
207
local
radical
=
register_nut
(
new_nut
(
nodecodes
.
radical
)
)
208
local
fraction
=
register_nut
(
new_nut
(
nodecodes
.
fraction
)
)
209
local
subbox
=
register_nut
(
new_nut
(
nodecodes
.
subbox
)
)
210
local
mathchar
=
register_nut
(
new_nut
(
nodecodes
.
mathchar
)
)
211
local
mathtextchar
=
register_nut
(
new_nut
(
nodecodes
.
mathtextchar
)
)
212
local
choice
=
register_nut
(
new_nut
(
nodecodes
.
choice
)
)
213 214
local
boundary
=
register_nut
(
new_nut
(
nodecodes
.
boundary
,
boundarycodes
.
user
)
)
215
local
wordboundary
=
register_nut
(
new_nut
(
nodecodes
.
boundary
,
boundarycodes
.
word
)
)
216 217
local
cleader
=
register_nut
(
copy_nut
(
glue
)
)
setsubtype
(
cleader
,
leadercodes
.
cleaders
)
setglue
(
cleader
,
0
,
65536
,
0
,
2
,
0
)
218 219
-- the dir field needs to be set otherwise crash:
220 221
local
lefttoright_code
=
nodes
.
dirvalues
.
lefttoright
222 223
local
rule
=
register_nut
(
new_nut
(
rule_code
)
)
setdirection
(
rule
,
lefttoright_code
)
224
local
emptyrule
=
register_nut
(
new_nut
(
rule_code
,
rulecodes
.
empty
)
)
setdirection
(
rule
,
lefttoright_code
)
225
local
userrule
=
register_nut
(
new_nut
(
rule_code
,
rulecodes
.
user
)
)
setdirection
(
rule
,
lefttoright_code
)
226
local
outlinerule
=
register_nut
(
new_nut
(
rule_code
,
rulecodes
.
outline
)
)
setdirection
(
rule
,
lefttoright_code
)
227
local
hlist
=
register_nut
(
new_nut
(
nodecodes
.
hlist
)
)
setdirection
(
hlist
,
lefttoright_code
)
228
local
vlist
=
register_nut
(
new_nut
(
nodecodes
.
vlist
)
)
setdirection
(
vlist
,
lefttoright_code
)
229 230
function
nutpool
.
glyph
(
fnt
,
chr
)
231
local
n
=
copy_nut
(
glyph
)
232
if
fnt
then
233
setfont
(
n
,
fnt
=
=
true
and
currentfont
(
)
or
fnt
,
chr
)
234
elseif
chr
then
235
setchar
(
n
,
chr
)
236
end
237
return
n
238
end
239 240
function
nutpool
.
penalty
(
p
)
241
local
n
=
copy_nut
(
penalty
)
242
if
p
and
p
~
=
0
then
243
setpenalty
(
n
,
p
)
244
end
245
return
n
246
end
247 248
function
nutpool
.
kern
(
k
)
249
local
n
=
copy_nut
(
kern
)
250
if
k
and
k
~
=
0
then
251
setkern
(
n
,
k
)
252
end
253
return
n
254
end
255 256
function
nutpool
.
boundary
(
v
)
257
local
n
=
copy_nut
(
boundary
)
258
if
v
and
v
~
=
0
then
259
setvalue
(
n
,
v
)
260
end
261
return
n
262
end
263 264
function
nutpool
.
wordboundary
(
v
)
265
local
n
=
copy_nut
(
wordboundary
)
266
if
v
and
v
~
=
0
then
267
setvalue
(
n
,
v
)
268
end
269
return
n
270
end
271 272
function
nutpool
.
fontkern
(
k
)
273
local
n
=
copy_nut
(
fontkern
)
274
if
k
and
k
~
=
0
then
275
setkern
(
n
,
k
)
276
end
277
return
n
278
end
279 280
function
nutpool
.
italickern
(
k
)
281
local
n
=
copy_nut
(
italickern
)
282
if
k
and
k
~
=
0
then
283
setkern
(
n
,
k
)
284
end
285
return
n
286
end
287 288
function
nutpool
.
gluespec
(
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
289
local
n
=
copy_nut
(
gluespec
)
290
if
width
or
stretch
or
shrink
or
stretch_order
or
shrink_order
then
291
setglue
(
n
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
292
end
293
return
n
294
end
295 296
local
function
someskip
(
skip
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
297
-- maybe setglue
298
local
n
=
copy_nut
(
skip
)
299
if
width
or
stretch
or
shrink
or
stretch_order
or
shrink_order
then
300
setglue
(
n
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
301
end
302
return
n
303
end
304 305
function
nutpool
.
stretch
(
a
,
b
)
306
-- width stretch shrink stretch_order shrink_order
307
local
n
=
copy_nut
(
glue
)
308
if
not
b
then
309
a
,
b
=
1
,
a
or
1
310
end
311
setglue
(
n
,
0
,
a
,
0
,
b
,
0
)
312
return
n
313
end
314 315
function
nutpool
.
shrink
(
a
,
b
)
316
local
n
=
copy_nut
(
glue
)
317
if
not
b
then
318
a
,
b
=
1
,
a
or
1
319
end
320
setglue
(
n
,
0
,
0
,
a
,
0
,
0
,
b
)
321
return
n
322
end
323 324
function
nutpool
.
glue
(
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
325
return
someskip
(
glue
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
326
end
327 328
function
nutpool
.
negatedglue
(
glue
)
329
local
n
=
copy_nut
(
glue
)
330
local
width
,
stretch
,
shrink
=
getglue
(
n
)
331
setglue
(
n
,
-
width
,
-
stretch
,
-
shrink
)
332
return
n
333
end
334 335
function
nutpool
.
leftskip
(
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
336
return
someskip
(
leftskip
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
337
end
338 339
function
nutpool
.
rightskip
(
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
340
return
someskip
(
rightskip
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
341
end
342 343
function
nutpool
.
lefthangskip
(
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
344
return
someskip
(
lefthangskip
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
345
end
346 347
function
nutpool
.
righthangskip
(
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
348
return
someskip
(
righthangskip
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
349
end
350 351
function
nutpool
.
indentskip
(
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
352
return
someskip
(
indentskip
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
353
end
354 355
function
nutpool
.
lineskip
(
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
356
return
someskip
(
lineskip
,
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
)
357
end
358 359
function
nutpool
.
baselineskip
(
width
,
stretch
,
shrink
)
360
return
someskip
(
baselineskip
,
width
,
stretch
,
shrink
)
361
end
362 363
function
nutpool
.
disc
(
pre
,
post
,
replace
)
364
local
d
=
copy_nut
(
disc
)
365
if
pre
or
post
or
replace
then
366
setdisc
(
d
,
pre
,
post
,
replace
)
367
end
368
return
d
369
end
370 371
function
nutpool
.
direction
(
dir
,
swap
)
372
local
t
=
copy_nut
(
textdir
)
373
if
not
dir
then
374
-- just a l2r start node
375
elseif
swap
then
376
setdirection
(
t
,
dir
,
true
)
377
else
378
setdirection
(
t
,
dir
,
false
)
379
end
380
return
t
381
end
382 383
function
nutpool
.
rule
(
width
,
height
,
depth
)
-- w/h/d == nil will let them adapt
384
local
n
=
copy_nut
(
rule
)
385
if
width
or
height
or
depth
then
386
setwhd
(
n
,
width
,
height
,
depth
)
387
end
388
return
n
389
end
390 391
function
nutpool
.
emptyrule
(
width
,
height
,
depth
)
-- w/h/d == nil will let them adapt
392
local
n
=
copy_nut
(
emptyrule
)
393
if
width
or
height
or
depth
then
394
setwhd
(
n
,
width
,
height
,
depth
)
395
end
396
return
n
397
end
398 399
function
nutpool
.
userrule
(
width
,
height
,
depth
)
-- w/h/d == nil will let them adapt
400
local
n
=
copy_nut
(
userrule
)
401
if
width
or
height
or
depth
then
402
setwhd
(
n
,
width
,
height
,
depth
)
403
end
404
return
n
405
end
406 407
function
nutpool
.
outlinerule
(
width
,
height
,
depth
,
line
)
-- w/h/d == nil will let them adapt
408
local
n
=
copy_nut
(
outlinerule
)
409
if
width
or
height
or
depth
then
410
setwhd
(
n
,
width
,
height
,
depth
)
411
end
412
if
line
then
413
setruledata
(
n
,
line
)
414
end
415
return
n
416
end
417 418
function
nutpool
.
leader
(
width
,
list
)
419
local
n
=
copy_nut
(
cleader
)
420
if
width
then
421
setwidth
(
n
,
width
)
422
end
423
if
list
then
424
setleader
(
n
,
list
)
425
end
426
return
n
427
end
428 429
function
nutpool
.
savepos
(
)
430
return
copy_nut
(
savepos
)
431
end
432 433
if
CONTEXTLMTXMODE
=
=
0
then
434 435
function
nutpool
.
latelua
(
code
)
436
local
n
=
copy_nut
(
latelua
)
437
if
type
(
code
)
=
=
"
table
"
then
438
local
action
=
code
.
action
439
local
specification
=
code
.
specification
or
code
440
code
=
function
(
)
action
(
specification
)
end
441
end
442
setdata
(
n
,
code
)
443
return
n
444
end
445 446
else
447 448
function
nutpool
.
latelua
(
code
)
449
local
n
=
copy_nut
(
latelua
)
450
nodeproperties
[
n
]
=
{
data
=
code
}
451
return
n
452
end
453 454
end
455 456
function
nutpool
.
leftmarginkern
(
glyph
,
width
)
457
local
n
=
copy_nut
(
left_margin_kern
)
458
if
not
glyph
then
459
report_nodes
(
"
invalid pointer to left margin glyph node
"
)
460
elseif
getid
(
glyph
)
~
=
glyph_code
then
461
report_nodes
(
"
invalid node type %a for %s margin glyph node
"
,
nodecodes
[
glyph
]
,
"
left
"
)
462
else
463
setfield
(
n
,
"
glyph
"
,
glyph
)
464
end
465
if
width
and
width
~
=
0
then
466
setwidth
(
n
,
width
)
467
end
468
return
n
469
end
470 471
function
nutpool
.
rightmarginkern
(
glyph
,
width
)
472
local
n
=
copy_nut
(
right_margin_kern
)
473
if
not
glyph
then
474
report_nodes
(
"
invalid pointer to right margin glyph node
"
)
475
elseif
getid
(
glyph
)
~
=
glyph_code
then
476
report_nodes
(
"
invalid node type %a for %s margin glyph node
"
,
nodecodes
[
p
]
,
"
right
"
)
477
else
478
setfield
(
n
,
"
glyph
"
,
glyph
)
479
end
480
if
width
and
width
~
=
0
then
481
setwidth
(
n
,
width
)
482
end
483
return
n
484
end
485 486
function
nutpool
.
temp
(
)
487
return
copy_nut
(
temp
)
488
end
489 490
function
nutpool
.
noad
(
)
return
copy_nut
(
noad
)
end
491
function
nutpool
.
delimiter
(
)
return
copy_nut
(
delimiter
)
end
nutpool
.
delim
=
nutpool
.
delimiter
492
function
nutpool
.
fence
(
)
return
copy_nut
(
fence
)
end
493
function
nutpool
.
submlist
(
)
return
copy_nut
(
submlist
)
end
494
function
nutpool
.
noad
(
)
return
copy_nut
(
noad
)
end
495
function
nutpool
.
fence
(
)
return
copy_nut
(
fence
)
end
496
function
nutpool
.
accent
(
)
return
copy_nut
(
accent
)
end
497
function
nutpool
.
radical
(
)
return
copy_nut
(
radical
)
end
498
function
nutpool
.
fraction
(
)
return
copy_nut
(
fraction
)
end
499
function
nutpool
.
subbox
(
)
return
copy_nut
(
subbox
)
end
500
function
nutpool
.
mathchar
(
)
return
copy_nut
(
mathchar
)
end
501
function
nutpool
.
mathtextchar
(
)
return
copy_nut
(
mathtextchar
)
end
502
function
nutpool
.
choice
(
)
return
copy_nut
(
choice
)
end
503 504
local
function
new_hlist
(
list
,
width
,
height
,
depth
,
shift
)
505
local
n
=
copy_nut
(
hlist
)
506
if
list
then
507
setlist
(
n
,
list
)
508
end
509
if
width
or
height
or
depth
then
510
setwhd
(
n
,
width
,
height
,
depth
)
511
end
512
if
shift
and
shift
~
=
0
then
513
setshift
(
n
,
shift
)
514
end
515
return
n
516
end
517 518
local
function
new_vlist
(
list
,
width
,
height
,
depth
,
shift
)
519
local
n
=
copy_nut
(
vlist
)
520
if
list
then
521
setlist
(
n
,
list
)
522
end
523
if
width
or
height
or
depth
then
524
setwhd
(
n
,
width
,
height
,
depth
)
525
end
526
if
shift
and
shift
~
=
0
then
527
setshift
(
n
,
shift
)
528
end
529
return
n
530
end
531 532
nutpool
.
hlist
=
new_hlist
533
nutpool
.
vlist
=
new_vlist
534 535
function
nodepool
.
hlist
(
list
,
width
,
height
,
depth
,
shift
)
536
return
tonode
(
new_hlist
(
list
and
tonut
(
list
)
,
width
,
height
,
depth
,
shift
)
)
537
end
538 539
function
nodepool
.
vlist
(
list
,
width
,
height
,
depth
,
shift
)
540
return
tonode
(
new_vlist
(
list
and
tonut
(
list
)
,
width
,
height
,
depth
,
shift
)
)
541
end
542 543
function
nutpool
.
usernode
(
id
,
data
)
544
local
n
=
copy_nut
(
user_node
)
545
nodeproperties
[
n
]
=
{
546
id
=
id
,
547
data
=
data
,
548
}
549
return
n
550
end
551 552
-- housekeeping
553 554
local
function
cleanup
(
nofboxes
)
-- todo
555
local
tracers
=
nodes
.
tracers
556
if
tracers
and
tracers
.
steppers
then
-- to be resolved
557
tracers
.
steppers
.
reset
(
)
-- todo: make a registration subsystem
558
end
559
local
nl
=
0
560
local
nr
=
nofreserved
561
for
i
=
1
,
nofreserved
do
562
local
ri
=
reserved
[
i
]
563
flush_nut
(
reserved
[
i
]
)
564
end
565
if
nofboxes
then
566
for
i
=
0
,
nofboxes
do
567
local
l
=
getbox
(
i
)
568
if
l
then
569
flush_nut
(
l
)
-- also list ?
570
nl
=
nl
+
1
571
end
572
end
573
end
574
reserved
=
{
}
575
nofreserved
=
0
576
return
nr
,
nl
,
nofboxes
-- can be nil
577
end
578 579
local
usage
=
CONTEXTLMTXMODE
>
0
and
node
.
inuse
or
function
(
)
580
local
t
=
{
}
581
for
n
,
tag
in
gmatch
(
status
.
node_mem_usage
,
"
(%d+) ([a-z_]+)
"
)
do
582
t
[
tag
]
=
tonumber
(
n
)
or
0
583
end
584
return
t
585
end
586 587
nutpool
.
cleanup
=
cleanup
588
nodepool
.
cleanup
=
cleanup
589 590
nutpool
.
usage
=
usage
591
nodepool
.
usage
=
usage
592 593
-- end
594 595
statistics
.
register
(
"
cleaned up reserved nodes
"
,
function
(
)
596
return
format
(
"
%s nodes, %s lists of %s
"
,
cleanup
(
texgetcount
(
"
c_syst_last_allocated_box
"
)
)
)
597
end
)
-- \topofboxstack
598 599
statistics
.
register
(
"
node memory usage
"
,
function
(
)
-- comes after cleanup !
600
local
used
=
usage
(
)
601
if
next
(
used
)
then
602
local
t
,
n
=
{
}
,
0
603
for
k
,
v
in
table
.
sortedhash
(
used
)
do
604
n
=
n
+
1
;
t
[
n
]
=
format
(
"
%s %s
"
,
v
,
k
)
605
end
606
return
table
.
concat
(
t
,
"
,
"
)
607
end
608
end
)
609 610
lua
.
registerfinalizer
(
cleanup
,
"
cleanup reserved nodes
"
)
611 612
-- experiment
613 614
do
615 616
local
glyph
=
tonode
(
glyph
)
617
local
traverse_id
=
nodes
.
traverse_id
618 619
local
traversers
=
table
.
setmetatableindex
(
function
(
t
,
k
)
620
local
v
=
traverse_id
(
type
(
k
)
=
=
"
number
"
and
k
or
nodecodes
[
k
]
,
glyph
)
621
t
[
k
]
=
v
622
return
v
623
end
)
624 625
traversers
.
node
=
nodes
.
traverse
(
glyph
)
626
traversers
.
char
=
nodes
.
traverse_char
(
glyph
)
627
if
nuts
.
traverse_glyph
then
traversers
.
glyph
=
nodes
.
traverse_glyph
(
glyph
)
end
628
if
nuts
.
traverse_list
then
traversers
.
list
=
nodes
.
traverse_list
(
glyph
)
end
629 630
nodes
.
traversers
=
traversers
631 632
end
633 634
do
635 636
local
glyph
=
glyph
637
local
traverse_id
=
nuts
.
traverse_id
638 639
local
traversers
=
table
.
setmetatableindex
(
function
(
t
,
k
)
640
local
v
=
traverse_id
(
type
(
k
)
=
=
"
number
"
and
k
or
nodecodes
[
k
]
,
glyph
)
641
t
[
k
]
=
v
642
return
v
643
end
)
644 645
traversers
.
node
=
nuts
.
traverse
(
glyph
)
646
traversers
.
char
=
nuts
.
traverse_char
(
glyph
)
647
if
nuts
.
traverse_glyph
then
traversers
.
glyph
=
nuts
.
traverse_glyph
(
glyph
)
end
648
if
nuts
.
traverse_list
then
traversers
.
list
=
nuts
.
traverse_list
(
glyph
)
end
649 650
nuts
.
traversers
=
traversers
651 652
end
653