webmcp

view libraries/nihil/nihil.lua @ 288:c51f283dfb0d

Request/response timeouts
author jbe
date Sun Mar 22 12:21:46 2015 +0100 (2015-03-22)
parents 3d43a5cf17c1
children
line source
1 #!/usr/bin/env lua
3 local error = error
4 local getmetatable = getmetatable
5 local rawset = rawset
6 local setmetatable = setmetatable
8 local _M = {}
9 if _ENV then
10 _ENV = _M
11 else
12 _G[...] = _M
13 setfenv(1, _M)
14 end
16 metatable = {
17 __tostring = function(self)
18 return "nil" .. self[1]
19 end,
20 __newindex = function()
21 error("Objects representing nil are immutable.")
22 end
23 }
25 nils = setmetatable({}, {
26 __mode = "v",
27 __index = function(self, level)
28 if level > 0 then
29 local result = setmetatable({ level }, metatable)
30 rawset(self, level, result)
31 return result
32 end
33 end,
34 __newindex = function()
35 error("Table is immutable.")
36 end
37 })
39 function lift(value)
40 if value == nil then
41 return nils[1]
42 elseif getmetatable(value) == metatable then
43 return nils[value[1]+1]
44 else
45 return value
46 end
47 end
49 function lower(value)
50 if value == nil then
51 error("Cannot lower nil.")
52 elseif getmetatable(value) == metatable then
53 return nils[value[1]-1]
54 else
55 return value
56 end
57 end
59 return _M

Impressum / About Us