diff options
Diffstat (limited to 'tex/context/base/util-sto.lua')
-rw-r--r-- | tex/context/base/util-sto.lua | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/tex/context/base/util-sto.lua b/tex/context/base/util-sto.lua new file mode 100644 index 000000000..49abd8c82 --- /dev/null +++ b/tex/context/base/util-sto.lua @@ -0,0 +1,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) |