context-2021-overloadprotection.tex /size: 8611 b    last modification: 2021-10-28 13:50
1% language=us
2
3\usemodule[present-boring,abbreviations-logos]
4
5
6\definehighlight[nb][style=bold,color=middlegray,define=no]
7
8\setuptolerance[verytolerant,stretch]
9
10\definecolor[maincolor][r=.3,b=.3]
11
12\startdocument
13  [title={OVERLOAD PROTECTION},
14   banner={the downside of macros},
15   location={context\enspace {\bf 2021}\enspace meeting}]
16
17% TEX MP
18
19\starttitle[title=Primitives]
20
21A \TEX\ engine comes with a whole set of primitive operations for:
22
23\startitemize[packed]
24    \startitem
25        accessing internal variables
26    \stopitem
27    \startitem
28        defining macros
29    \stopitem
30    \startitem
31        controlling expansion
32    \stopitem
33    \startitem
34        constructing boxes
35    \stopitem
36    \startitem
37        finalizing pages
38    \stopitem
39    \startitem
40        defining characters (text and math)
41    \stopitem
42    \startitem
43        inserting kerns, glue and penalties
44    \stopitem
45    \startitem
46        defining fonts
47    \stopitem
48    \startitem
49        dealing with languages (hyphenation)
50    \stopitem
51    \startitem
52        testing properties
53    \stopitem
54    \startitem
55        manipulating tokens
56    \stopitem
57    \startitem
58        managing inserts
59    \stopitem
60    \startitem
61        handling marks
62    \stopitem
63    \startitem
64        grouping
65    \stopitem
66    \startitem
67        mathematics
68    \stopitem
69    \startitem
70        tracing
71    \stopitem
72\stopitemize
73
74\stoptitle
75
76\starttitle[title=Macros]
77
78\startitemize
79    \startitem
80        Macros are commands defined by the user and/or a macro package.
81    \stopitem
82    \startitem
83        They can overload a primitive which then can confuse the whole machinery.
84    \stopitem
85    \startitem
86        A macro package can alias primitives for instance \type {\relax} can be
87        replaced by \type {\foo_relax} after \typ {\let \foo_relax \relax}.
88    \stopitem
89    \startitem
90        That only when (at definition time) the \type {_} is a letter. By using such
91        a character some protection against overload is provided.
92    \stopitem
93    \startitem
94        In \CONTEXT\ we often use(d) aliases like \type {\normalrelax} but of course
95        these can also be overloaded.
96    \stopitem
97    \startitem
98        We only overload a very few primitives, for instance \type {\language}.
99    \stopitem
100    \startitem
101        Users who overload primitives are \quote {on their own} and \quote {without
102        support}.
103    \stopitem
104    \startitem
105        An easy way to protect yourself is using \type {\CamelCase} names.
106    \stopitem
107\stopitemize
108
109\stoptitle
110
111\starttitle[title=Overload protection]
112
113\startitemize
114    \startitem
115        The \LUAMETATEX\ engine has overload protection built in for the \TEX\ engine
116        as well as provides means to do that for \METAPOST.
117    \stopitem
118    \startitem
119        In \LMTX\ all commands have been tagged accordingly (which was quite some work).
120    \stopitem
121    \startitem
122        Processing \type {s-system-macros.mkxl} gives an overview.
123    \stopitem
124    \startitem
125        Overload protection is off by default but can be turned on:
126
127\starttyping
128\enabledirectives[overloadmode=warning]
129\enabledirectives[overloadmode=error]
130\stoptyping
131
132    \stopitem
133    \startitem
134        I myself always run with the error variant and make sure that the manuals obey
135        the rules.
136    \stopitem
137    \startitem
138        Modules and/or styles (and in a few cases the core code) can cheat and use:
139
140\starttyping
141\pushoverloadmode
142    ........................
143    ........................
144\popoverloadmode
145\stoptyping
146
147    \stopitem
148\stopitemize
149
150\stoptitle
151
152\starttitle[title=Details]
153
154\startitemize
155    \startitem
156        Traditional \TEX\ has a few so called prefixes: \type {\global}, \type {\outer},
157        \type {\long}, and type \type {\immediate}.
158    \stopitem
159    \startitem
160        The \ETEX\ engine adds \type {\protected} (because we already had that in
161        \CONTEXT\ we use what we (also already) had: \type {\unexpanded}).
162    \stopitem
163    \startitem
164        In \LUATEX\ we can force macros to be always long, something that we do
165        in \MKIV\ (as in \MKII).
166    \stopitem
167    \startitem
168        In \LUAMETATEX\ the \type {\outer} and \type {\long} prefixes have been
169        dropped (they are ignored).
170    \stopitem
171    \startitem
172        In \LUAMETATEX\ the \type {\protected} prefix acts like in other engines
173        but protection is implemented more naturally.
174    \stopitem
175    \startitem
176        In addition \LUAMETATEX\ has new prefixes: \type {\frozen}, \type
177        {\permanent}, \type {\immutable}, \type {\mutable}, \type {\noaligned},
178        \type {\instance}, \type {\untraced}, \type {\tolerant}, \type
179        {\overloaded}, \type {\aliased}, \type {\immediate} and an experimental
180        \type {\semiprotected},
181    \stopitem
182    \startitem
183        Some prefixes end up as properties of macros, some influence scanning (for instance
184        in alignments and when calling \LUA\ functions). There is no noticeable runtime
185        overhead.
186    \stopitem
187    \startitem
188        The \type {\meaningfull} primitive is like \type {\meaning} but also reports
189        properties set by prefixes; there is also \type {\meaningless}.
190    \stopitem
191\stopitemize
192
193\stoptitle
194
195\starttitle[title=Prefixes]
196
197Regular definitions:
198
199\startitemize
200    \startitem
201        \type {\global}: defines a macro or sets a register value out of scope.
202    \stopitem
203    \startitem
204        \type {\outer}: is used to issue a warning when a macro defined as such was
205        used nested (just ignored in \LUAMETATEX).
206    \stopitem
207    \startitem
208        \type {\long}: triggers a warning when an argument of a macro contains a
209        \type {\par} equivalent (just ignored in \LUAMETATEX).
210    \stopitem
211    \startitem
212        \type {\protected}: makes a macro unexpandable (being itself) in an \type
213        {\edef} equivalent situation (where it can get out of hands).
214    \stopitem
215    \startitem
216        \type {\semiprotected}: is like \type {\protected} but the property is
217        ignored when \type {\semiexpanded} is applied.
218    \stopitem
219\stopitemize
220
221Special case:
222
223\startitemize
224    \startitem
225        \type {\immediate}: tells a backend primitive to come into action
226        immediately instead of creating a delayed action (via a whatsit node). In
227        \LUAMETATEX\ we have no built|-|in backend so there is signals a \LUA\
228        interface function this property.
229    \stopitem
230\stopitemize
231
232\page
233
234Scanning related:
235
236\startitemize
237    \startitem
238        \type {\noaligned}: tags a macro as valid peek ahead macro when scanning
239        alignments; normally that only makes sense for \type {\protected} macros.
240        This permits cleaner macros in for instance table mechanisms (no
241        unexpected expansion side effects).
242    \stopitem
243    \startitem
244        \type {\untraced}: this flag makes a macro report itself as primitive in
245        traces which is sometimes nicer that presenting a user with some
246        confusing meaning.
247    \stopitem
248    \startitem
249        \type {\tolerant}: a prefix that makes the macro argument parser accept
250        all kind of new argument parsing options and continue when delimited
251        arguments fail. This makes macros with optional arguments produce less
252        noise when they are traced but more important, it makes for cleaner low
253        level interfaces.
254    \stopitem
255\stopitemize
256
257
258\page
259
260Overload protection (primitives are protected against overloads by default):
261
262\startitemize
263    \startitem
264        \type {\aliased}: create a reference (using \type {\let}) that also inherits the
265        properties.
266    \stopitem
267    \startitem
268        \type {\permanent}: sets a flag that makes a macro (or definition) immune for
269        redefinition.
270    \stopitem
271    \startitem
272        \type {\frozen}: prevents overloading but one can bypass that with some
273        more effort.
274    \stopitem
275    \startitem
276        \type {\immutable}: makes a (normally variable definition) fixed, for instance
277        constants.
278    \stopitem
279    \startitem
280        \type {\mutable}: a flag showing that this macro or definition can be used for
281        anything (so the macro package also has to assume that).
282    \stopitem
283    \startitem
284        \type {\instance}: just a flag that can be used to signal that a macro (or definition)
285        is an instance of a more general concept.
286    \stopitem
287    \startitem
288        \type {\overloaded}: bypass a frozen flag (property).
289    \stopitem
290\stopitemize
291
292\blank[2*big]
293
294{\em Show some examples in the source code and editor.}
295
296\stoptitle
297
298\stopdocument
299