summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/util-sto.lua
blob: 5b6915eaf018ff5cef010d66f17b0a161b741f0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
if not modules then modules = { } end modules ['util-sto'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local setmetatable, getmetatable, rawset, type = setmetatable, getmetatable, rawset, type

utilities         = utilities or { }
utilities.storage = utilities.storage or { }
local storage     = utilities.storage

function storage.mark(t)
    if not t then
        print("\nfatal error: storage cannot be marked\n")
        os.exit()
        return
    end
    local m = getmetatable(t)
    if not m then
        m = { }
        setmetatable(t,m)
    end
    m.__storage__ = true
    return t
end

function storage.allocate(t)
    t = t or { }
    local m = getmetatable(t)
    if not m then
        m = { }
        setmetatable(t,m)
    end
    m.__storage__ = true
    return t
end

function storage.marked(t)
    local m = getmetatable(t)
    return m and m.__storage__
end

function storage.checked(t)
    if not t then
        report("\nfatal error: storage has not been allocated\n")
        os.exit()
        return
    end
    return t
end

-- function utilities.storage.delay(parent,name,filename)
--     local m = getmetatable(parent)
--     m.__list[name] = filename
-- end
--
-- function utilities.storage.predefine(parent)
--     local list = { }
--     local m = getmetatable(parent) or {
--         __list = list,
--         __index = function(t,k)
--             local l = require(list[k])
--             t[k] = l
--             return l
--         end
--     }
--     setmetatable(parent,m)
-- end
--
-- bla = { }
-- utilities.storage.predefine(bla)
-- utilities.storage.delay(bla,"test","oepsoeps")
-- local t = bla.test
-- table.print(t)
-- print(t.a)

function storage.setinitializer(data,initialize)
    local m = getmetatable(data) or { }
    m.__index = function(data,k)
        m.__index = nil -- so that we can access the entries during initializing
        initialize()
        return data[k]
    end
    setmetatable(data, m)
end

local keyisvalue = { __index = function(t,k)
    t[k] = k
    return k
end }

function storage.sparse(t)
    t = t or { }
    setmetatable(t,keyisvalue)
    return t
end

-- table namespace ?

local function f_empty ()                           return "" end -- t,k
local function f_self  (t,k) t[k] = k               return k  end
local function f_table (t,k) local v = { } t[k] = v return v  end
local function f_number(t,k) t[k] = 0               return 0  end -- t,k,v
local function f_ignore()                                     end -- t,k,v

local f_index = {
    ["empty"]  = f_empty,
    ["self"]   = f_self,
    ["table"]  = f_table,
    ["number"] = f_number,
}

function table.setmetatableindex(t,f)
    if type(t) ~= "table" then
        f, t = t, { }
    end
    local m = getmetatable(t)
    local i = f_index[f] or f
    if m then
        m.__index = i
    else
        setmetatable(t,{ __index = i })
    end
    return t
end

local f_index = {
    ["ignore"] = f_ignore,
}

function table.setmetatablenewindex(t,f)
    if type(t) ~= "table" then
        f, t = t, { }
    end
    local m = getmetatable(t)
    local i = f_index[f] or f
    if m then
        m.__newindex = i
    else
        setmetatable(t,{ __newindex = i })
    end
    return t
end

function table.setmetatablecall(t,f)
    if type(t) ~= "table" then
        f, t = t, { }
    end
    local m = getmetatable(t)
    if m then
        m.__call = f
    else
        setmetatable(t,{ __call = f })
    end
    return t
end

-- the manual is somewhat fuzzy about this but suggests that one can best
-- set all fields before assigning a metatable

function table.setmetatableindices(t,f,n,c)
    if type(t) ~= "table" then
        f, t = t, { }
    end
    local m = getmetatable(t)
    local i = f_index[f] or f
    if m then
        m.__index    = i
        m.__newindex = n
        m.__call     = c
    else
        setmetatable(t,{
            __index    = i,
            __newindex = n,
            __call     = c,
        })
    end
    return t
end

function table.setmetatablekey(t,key,value)
    local m = getmetatable(t)
    if not m then
        m = { }
        setmetatable(t,m)
    end
    m[key] = value
    return t
end

function table.getmetatablekey(t,key,value)
    local m = getmetatable(t)
    return m and m[key]
end

-- Problem: we have no __next (which is ok as it would probably slow down lua) so
-- we cannot loop over the keys.

-- local parametersets = table.autokeys()
--
-- parametersets.foo.bar = function(t,k) return "OEPS" end
-- parametersets.foo.foo = "SPEO"
-- parametersets.crap = { a = "a", b = table.autokey { function() return "b" end } }
--
-- print(parametersets.foo.bar)
-- print(parametersets.foo.foo)
-- print(parametersets.crap.b)
-- print(parametersets.crap.b[1])

-- function table.autotables(t)
--     local t = t or { }
--     local m = getmetatable(t)
--     if not m then
--         m = { }
--         setmetatable(t,m)
--     end
--     m.__newindex = function(t,k,p)
--         local v = { }
--         local m = {
--             __index = function(t,k)
--                 local v = p[k]
--                 if type(v) == "function" then
--                     return v(t,k) -- so we can have multiple arguments
--                 else
--                     return v
--                 end
--             end,
--             __newindex = function(t,k,v)
--                 p[k] = v
--             end,
--             __len = function(t)
--                 return #p
--             end,
--         }
--         setmetatable(v,m)
--         rawset(t,k,v)
--         return v
--     end
--     m.__index = function(t,k)
--         local v = { }
--         t[k] = v -- calls newindex
--         return v
--     end
--     return t
-- end
--
-- function table.autokeys(p)
--     local t = { }
--     setmetatable(t, {
--         __newindex = function(t,k,v)
--             p[k] = v
--         end,
--         __index = function(t,k)
--             local v = p[k]
--             if type(v) == "function" then
--                 return v(t,k) -- so we can have multiple arguments
--             else
--                 return v
--             end
--         end,
--         __len = function(t)
--             return #p
--         end,
--     })
--     return t
-- end