• 71.83% Rate
  • 51 Hits
  • 20 Missed
  • 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
  • 1x
  • 1x
  • 1x
  • 1x
  • 39x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 6x
  • 6x
  • 6x
  • 1x
  • 1x
  • 1x
  • 1x
  • 323x
  • 323x
  • 1x
  • 322x
  • 6x
  • 316x
  • 316x
  • 4x
  • 312x
  • 1x
  • 6x
  • 6x
  • 1x
  • 49x
  • 49x
  • 1x
  • 164x
  • 4x
  • 4x
  • 80x
  • 80x
  • 5x
  • 75x
  • 2x
  • 2x
  • 79x
  • 2x
  • 2x
  • 78x
  • 78x
  • 78x
  • 1x
  • 1x
  • 1x
  • -------------------------------------------------------------------------------
  • -- Wrapper for emulating a read-only table
  • -- Author: Valentin Niess
  • -- License: GNU LGPL-3.0
  • -------------------------------------------------------------------------------
  • local error = require('pumas.error')
  • local metatype_ = require('pumas.metatype')
  • local readonly = {}
  • -------------------------------------------------------------------------------
  • -- Local bookmarking of readonly tables
  • -------------------------------------------------------------------------------
  • local instances = setmetatable({}, {__mode = 'k'})
  • function readonly.rawget (t) return instances[t].table end
  • -------------------------------------------------------------------------------
  • -- The Readonly metatype
  • -------------------------------------------------------------------------------
  • -- XXX clone method for Readonly tables?
  • local Readonly = {}
  • Readonly.__metatable = 'Readonly'
  • do
  • -- Vanilla Lua 5.1 does not have the __pairs and __ipairs metamethods.
  • -- Therefore we provide explicit iterators
  • local function ipairs_ (self)
  • if self == nil then
  • error.raise{fname = 'ipairs', argnum = 1, expected = 'a table',
  • got = 'nil'}
  • end
  • self = instances[self]
  • return ipairs(self.table)
  • end
  • local function pairs_ (self)
  • if self == nil then
  • error.raise{fname = 'pairs', argnum = 1, expected = 'a table',
  • got = 'nil'}
  • end
  • self = instances[self]
  • return pairs(self.table)
  • end
  • local inspect
  • local function inspect_ (self)
  • if self == nil then
  • error.raise{fname = 'inspect', argnum = 1, expected = 'a table',
  • got = 'nil'}
  • end
  • if not inspect then
  • local ok, result = pcall(require, 'inspect')
  • if not ok then
  • error.raise{fname = 'inspect', description = result}
  • else
  • inspect = result
  • end
  • end
  • return inspect(instances[self], {process = function (item)
  • return instances[item] or item
  • end})
  • end
  • error.register('Readonly.__ipairs', ipairs_)
  • error.register('Readonly.__pairs', pairs_)
  • error.register('Readonly.__index.inspect', inspect_)
  • function Readonly:__index (k)
  • if k == 'inspect' then
  • return inspect_
  • elseif k == 'ipairs' then
  • return ipairs_
  • elseif k == 'pairs' then
  • return pairs_
  • else
  • self = instances[self]
  • if k == '__metatype' then
  • return self.metatype
  • else
  • return self.table[k]
  • end
  • end
  • end
  • end
  • function Readonly:__newindex (k)
  • self = instances[self]
  • error.raise{['type'] = self.type, not_mutable = k}
  • end
  • function Readonly:__len ()
  • self = instances[self]
  • return #self.table
  • end
  • function Readonly:__tostring ()
  • self = instances[self]
  • return tostring(self.table)
  • end
  • -------------------------------------------------------------------------------
  • -- The Readonly constructor
  • -------------------------------------------------------------------------------
  • do
  • local function wrap (cls, t, type_, metatype)
  • if metatype_(t) ~= 'table' then
  • error.raise{fname = 'Readonly', argnum = 1,
  • expected = 'a raw table', got = metatype_.a(t)}
  • elseif getmetatable(t) ~= nil then
  • error.raise{fname = 'Readonly', argnum = 1,
  • expected = 'a raw table', got = 'a metatable'}
  • end
  • if type_ == nil then
  • type_ = 'table'
  • elseif type(type_) ~= 'string' then
  • error.raise{fname = 'Readonly', argnum = 2,
  • expected = 'a string', got = metatype_.a(type_)}
  • end
  • if (metatype ~= nil) and (type(metatype) ~= 'string') then
  • error.raise{fname = 'Readonly', argnum = 3,
  • expected = 'a string', got = metatype_.a(metatype)}
  • end
  • local self = {}
  • instances[self] = {table = t, ['type'] = type_, metatype = metatype}
  • return setmetatable(self, cls)
  • end
  • readonly.Readonly = setmetatable(Readonly, {__call = wrap})
  • error.register('Readonly', Readonly)
  • end
  • -------------------------------------------------------------------------------
  • -- Return the sub-package
  • -------------------------------------------------------------------------------
  • return readonly