1if not modules then modules = { } end modules ['util-sto'] = {
2 version = 1.001,
3 comment = "companion to luat-lib.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
9local setmetatable, getmetatable, rawset, type = setmetatable, getmetatable, rawset, type
10
11utilities = utilities or { }
12utilities.storage = utilities.storage or { }
13local storage = utilities.storage
14
15function storage.mark(t)
16 if not t then
17 print("\nfatal error: storage cannot be marked\n")
18 os.exit()
19 return
20 end
21 local m = getmetatable(t)
22 if not m then
23 m = { }
24 setmetatable(t,m)
25 end
26 m.__storage__ = true
27 return t
28end
29
30function storage.allocate(t)
31 t = t or { }
32 local m = getmetatable(t)
33 if not m then
34 m = { }
35 setmetatable(t,m)
36 end
37 m.__storage__ = true
38 return t
39end
40
41function storage.marked(t)
42 local m = getmetatable(t)
43 return m and m.__storage__
44end
45
46function storage.checked(t)
47 if not t then
48 report("\nfatal error: storage has not been allocated\n")
49 os.exit()
50 return
51 end
52 return t
53end
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80function storage.setinitializer(data,initialize)
81 local m = getmetatable(data) or { }
82 m.__index = function(data,k)
83 m.__index = nil
84 initialize()
85 return data[k]
86 end
87 setmetatable(data, m)
88end
89
90local keyisvalue = { __index = function(t,k)
91 t[k] = k
92 return k
93end }
94
95function storage.sparse(t)
96 t = t or { }
97 setmetatable(t,keyisvalue)
98 return t
99end
100
101
102
103local function f_empty () return "" end
104local function f_self (t,k) t[k] = k return k end
105local function f_table (t,k) local v = { } t[k] = v return v end
106local function f_number(t,k) t[k] = 0 return 0 end
107local function f_ignore() end
108
109local f_index = {
110 ["empty"] = f_empty,
111 ["self"] = f_self,
112 ["table"] = f_table,
113 ["number"] = f_number,
114}
115
116function table.setmetatableindex(t,f)
117 if type(t) ~= "table" then
118 f, t = t, { }
119 end
120 local m = getmetatable(t)
121 local i = f_index[f] or f
122 if m then
123 m.__index = i
124 else
125 setmetatable(t,{ __index = i })
126 end
127 return t
128end
129
130local f_index = {
131 ["ignore"] = f_ignore,
132}
133
134function table.setmetatablenewindex(t,f)
135 if type(t) ~= "table" then
136 f, t = t, { }
137 end
138 local m = getmetatable(t)
139 local i = f_index[f] or f
140 if m then
141 m.__newindex = i
142 else
143 setmetatable(t,{ __newindex = i })
144 end
145 return t
146end
147
148function table.setmetatablecall(t,f)
149 if type(t) ~= "table" then
150 f, t = t, { }
151 end
152 local m = getmetatable(t)
153 if m then
154 m.__call = f
155 else
156 setmetatable(t,{ __call = f })
157 end
158 return t
159end
160
161
162
163
164function table.setmetatableindices(t,f,n,c)
165 if type(t) ~= "table" then
166 f, t = t, { }
167 end
168 local m = getmetatable(t)
169 local i = f_index[f] or f
170 if m then
171 m.__index = i
172 m.__newindex = n
173 m.__call = c
174 else
175 setmetatable(t,{
176 __index = i,
177 __newindex = n,
178 __call = c,
179 })
180 end
181 return t
182end
183
184function table.setmetatablekey(t,key,value)
185 local m = getmetatable(t)
186 if not m then
187 m = { }
188 setmetatable(t,m)
189 end
190 m[key] = value
191 return t
192end
193
194function table.getmetatablekey(t,key,value)
195 local m = getmetatable(t)
196 return m and m[key]
197end
198
199function table.makeweak(t)
200 if not t then
201 t = { }
202 end
203 local m = getmetatable(t)
204 if m then
205 m.__mode = "v"
206 else
207 setmetatable(t,{ __mode = "v" })
208 end
209 return t
210end
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 |