webmcp

view libraries/nihil/nihil.lua @ 10:e017c47d43b5

Modified encode.json to avoid special CDATA sequences in output
author jbe
date Wed Feb 03 00:57:18 2010 +0100 (2010-02-03)
parents 9fdfb27f8e67
children 3d43a5cf17c1
line source
1 #!/usr/bin/env lua
3 local error = error
4 local getmetatable = getmetatable
5 local module = module
6 local rawset = rawset
7 local setmetatable = setmetatable
9 module(...)
11 metatable = {
12 __tostring = function(self)
13 return "nil" .. self[1]
14 end,
15 __newindex = function()
16 error("Objects representing nil are immutable.")
17 end
18 }
20 nils = setmetatable({}, {
21 __mode = "v",
22 __index = function(self, level)
23 if level > 0 then
24 local result = setmetatable({ level }, metatable)
25 rawset(self, level, result)
26 return result
27 end
28 end,
29 __newindex = function()
30 error("Table is immutable.")
31 end
32 })
34 function lift(value)
35 if value == nil then
36 return nils[1]
37 elseif getmetatable(value) == metatable then
38 return nils[value[1]+1]
39 else
40 return value
41 end
42 end
44 function lower(value)
45 if value == nil then
46 error("Cannot lower nil.")
47 elseif getmetatable(value) == metatable then
48 return nils[value[1]-1]
49 else
50 return value
51 end
52 end

Impressum / About Us