File: mstrict.lua

package info (click to toggle)
lua-bit32 5.3.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 192 kB
  • sloc: ansic: 1,026; makefile: 4
file content (107 lines) | stat: -rw-r--r-- 2,815 bytes parent folder | download | duplicates (4)
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

--- Stricter version of compat52.
-- Attempts to emulate Lua 5.2 when built without LUA_COMPAT_ALL.


if _VERSION == "Lua 5.1" then

   require("compat52")

   local function not_available()
      error("This function is not available in Lua 5.2!", 2)
   end

   local exclude_from_G = {
      module = not_available,
      getfenv = not_available,
      setfenv = not_available,
      loadstring = not_available,
      unpack = not_available,
      loadlib = not_available,
      math = {
         log10 = not_available,
         mod = not_available,
      },
      table = {
         getn = not_available,
         setn = not_available,
      },
      string = {
         gfind = not_available,
      },
   }

   local next = next
   local function make_pairs_iterator(lookup)
      return function(st, var)
         local k, v = next(st, var)
         if k ~= nil then
            local new_v = lookup[k]
            if new_v ~= nil then v = new_v end
            return k, v
         end
      end
   end

   local rawget = rawget
   local function make_ipairs_iterator(lookup)
      return function(st, var)
         var = var + 1
         local v = rawget(st, var)
         if v ~= nil then
            local new_v = lookup[var]
            if new_v ~= nil then v = new_v end
            return var, v
         end
      end
   end

   local function make_copy(value, excl)
      local v_type, e_type = type(value), type(excl)
      if v_type == e_type then
         if v_type == "table" then
            local l_table = {}
            for k, v in pairs(excl) do
               l_table[k] = make_copy(rawget(value, k), v)
            end
            local pairs_iterator = make_pairs_iterator(l_table)
            local ipairs_iterator = make_ipairs_iterator(l_table)
            return setmetatable({}, {
               __index = function(_, k)
                  local v = l_table[k]
                  if v ~= nil then
                     return v
                  else
                     return value[k]
                  end
               end,
               __newindex = function(_, k, v)
                  if l_table[k] ~= nil then
                     l_table[k] = nil
                  end
                  value[k] = v
               end,
               __pairs = function()
                  return pairs_iterator, value, nil
               end,
               __ipairs = function()
                  return ipairs_iterator, value, 0
               end,
            }), l_table
         elseif v_type == "function" then
            return excl
         end
      end
   end

   local new_G, G_lookup = make_copy(_G, exclude_from_G)
   G_lookup._G = new_G

   return function()
      setfenv(2, new_G)
   end
else
   return function() end
end

-- vi: set expandtab softtabstop=3 shiftwidth=3 :