Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/lualib/mwInit.lua

raw symbols SCIP
1 -- This file is for anything that needs to be set up before a Lua engine can
2 -- start. Things in this file may run more than once, so avoid putting anything
3 -- other than function definitions in it. Also, because this can run before
4 -- PHP can do anything, mw_interface is unavailable here.
5
6 mw = mw or {}
7
8 -- Extend pairs and ipairs to recognize __pairs and __ipairs, if they don't already
9 do
10 local t = {}
11 setmetatable( t, { __pairs = function() return 1, 2, 3 end } )
12 local f = pairs( t )
13 if f ~= 1 then
14 local old_pairs = pairs
15 pairs = function ( t )
16 local mt = getmetatable( t )
17 local f, s, var = ( mt and mt.__pairs or old_pairs )( t )
18 return f, s, var
19 end
20 local old_ipairs = ipairs
21 ipairs = function ( t )
22 local mt = getmetatable( t )
23 local f, s, var = ( mt and mt.__ipairs or old_ipairs )( t )
24 return f, s, var
25 end
26 end
27 end
28
29 -- Reduce precision on os.clock to mitigate timing attacks
30 do
31 local old_clock = os.clock
32 os.clock = function ()
33 local v = old_clock()
34 return math.floor( v * 50000 + 0.5 ) / 50000
35 end
36 end
37
38 -- Avoid segfault in unpack (T408135)
39 -- Necessary before Lua 5.2.3
40 do
41 local old_unpack = unpack
42 unpack = function ( t, i, j )
43 if type( t ) == "table" then
44 local ival, jval = i, j
45 local max_stack = 8000
46 local max_int = 2^31 - 1
47 if ival == nil then
48 ival = 1
49 end
50 if jval == nil then
51 jval = #t
52 end
53 if math.abs( ival ) > max_int or math.abs( jval ) > max_int or jval - ival > max_stack then
54 error( "too many results to unpack", 2 )
55 end
56 end
57 return old_unpack( t, i, j )
58 end
59 end
60
61 --- Do a "deep copy" of a table or other value.
62 do
63 -- Declare global variables as locals to reduce access times
64 local getmetatable = getmetatable
65 local pairs = pairs
66 local setmetatable = setmetatable
67 local type = type
68 local function recursiveClone( val, tableRefs )
69 local retVal = {}
70 -- Encode circular references correctly
71 tableRefs[val] = retVal
72
73 local mt = getmetatable( val )
74 if mt then
75 setmetatable( retVal, tableRefs[mt] or recursiveClone( mt, tableRefs ) )
76 end
77
78 for key, elt in pairs( val ) do
79 if type( elt ) == 'table' then
80 retVal[key] = tableRefs[elt] or recursiveClone( elt, tableRefs )
81 else
82 retVal[key] = elt
83 end
84 end
85 return retVal
86 end
87
88 function mw.clone( val )
89 if type( val ) ~= 'table' then
90 return val
91 end
92 return recursiveClone( val, {} )
93 end
94 end
95
96 --- Make isolation-safe setfenv and getfenv functions
97 --
98 -- @param protectedEnvironments A table where the keys are protected environment
99 -- tables. These environments cannot be accessed with getfenv(), and
100 -- functions with these environments cannot be modified or accessed using
101 -- integer indexes to setfenv(). However, functions with these environments
102 -- can have their environment set with setfenv() with a function value
103 -- argument.
104 --
105 -- @param protectedFunctions A table where the keys are protected functions,
106 -- which cannot have their environments set by setfenv() with a function
107 -- value argument.
108 --
109 -- @return setfenv
110 -- @return getfenv
111 function mw.makeProtectedEnvFuncs( protectedEnvironments, protectedFunctions )
112 local old_setfenv = setfenv
113 local old_getfenv = getfenv
114
115 local function my_setfenv( func, newEnv )
116 if type( func ) == 'number' then
117 local stackIndex = math.floor( func )
118 if stackIndex <= 0 then
119 error( "'setfenv' cannot set the global environment, it is protected", 2 )
120 end
121 if stackIndex > 10 then
122 error( "'setfenv' cannot set an environment at a level greater than 10", 2 )
123 end
124
125 -- Add one because we are still in Lua and 1 is right here
126 stackIndex = stackIndex + 1
127
128 local env = old_getfenv( stackIndex )
129 if env == nil or protectedEnvironments[ env ] then
130 error( "'setfenv' cannot set the requested environment, it is protected", 2 )
131 end
132 func = old_setfenv( stackIndex, newEnv )
133 elseif type( func ) == 'function' then
134 if protectedFunctions[func] then
135 error( "'setfenv' cannot be called on a protected function", 2 )
136 end
137 local env = old_getfenv( func )
138 if env == nil or protectedEnvironments[ env ] then
139 error( "'setfenv' cannot set the requested environment, it is protected", 2 )
140 end
141 old_setfenv( func, newEnv )
142 else
143 error( "'setfenv' can only be called with a function or integer as the first argument", 2 )
144 end
145 return func
146 end
147
148 local function my_getfenv( func )
149 local env
150 if type( func ) == 'number' then
151 if func <= 0 then
152 error( "'getfenv' cannot get the global environment" )
153 end
154 env = old_getfenv( func + 1 )
155 elseif type( func ) == 'function' then
156 env = old_getfenv( func )
157 else
158 error( "'getfenv' cannot get the global environment" )
159 end
160
161 if protectedEnvironments[env] then
162 return nil
163 else
164 return env
165 end
166 end
167
168 return my_setfenv, my_getfenv
169 end
170
171 return mw