summaryrefslogtreecommitdiff
path: root/tex/context/base/util-sto.lua
blob: 49abd8c82b710ae86eb7ca3f2fe0cab56cd1798b (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
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 = setmetatable, getmetatable

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

function storage.mark(t)
    if not t then
        texio.write_nl("fatal error: storage '%s' cannot be marked",t)
        os.exit()
    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
        texio.write_nl("fatal error: storage '%s' has not been allocated",t)
        os.exit()
    end
    return t
end

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

function getmetatablekey(t,key,value)
    local m = getmetatable(t)
    return m and m[key]
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)