diff options
author | Karl Berry <karl@freefriends.org> | 2018-09-26 20:49:48 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2018-09-26 20:49:48 +0000 |
commit | 3a0140d05a7e481750ec23a30ed5cf3cdef61eb7 (patch) | |
tree | 8fac1dc16795d22db3b45205fa55db220a9beed4 /Master/texmf-dist/tex/luatex/lualibs/lualibs-util-sto.lua | |
parent | b573c63f79c24b5c8dff312e023c2745b49f6089 (diff) |
lualibs (26sep18)
git-svn-id: svn://tug.org/texlive/trunk@48770 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/luatex/lualibs/lualibs-util-sto.lua')
-rw-r--r-- | Master/texmf-dist/tex/luatex/lualibs/lualibs-util-sto.lua | 110 |
1 files changed, 109 insertions, 1 deletions
diff --git a/Master/texmf-dist/tex/luatex/lualibs/lualibs-util-sto.lua b/Master/texmf-dist/tex/luatex/lualibs/lualibs-util-sto.lua index d21267d7a8c..a08d25ced18 100644 --- a/Master/texmf-dist/tex/luatex/lualibs/lualibs-util-sto.lua +++ b/Master/texmf-dist/tex/luatex/lualibs/lualibs-util-sto.lua @@ -6,7 +6,7 @@ if not modules then modules = { } end modules ['util-sto'] = { license = "see context related readme files" } -local setmetatable, getmetatable, type = setmetatable, getmetatable, type +local setmetatable, getmetatable, rawset, type = setmetatable, getmetatable, rawset, type utilities = utilities or { } utilities.storage = utilities.storage or { } @@ -158,6 +158,29 @@ function table.setmetatablecall(t,f) 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 @@ -172,3 +195,88 @@ function table.getmetatablekey(t,key,value) local m = getmetatable(t) return m and m[key] end + +function table.makeweak(t) + if not t then + t = { } + end + local m = getmetatable(t) + if m then + m.__mode = "v" + else + setmetatable(t,{ __mode = "v" }) + end + return t +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 |