| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
mw = mw or {} |
| 7 |
|
| 8 |
|
| 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 |
|
| 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 |
|
| 39 |
|
| 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 |
|
| 62 |
do |
| 63 |
|
| 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 |
|
| 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 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 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 |
|
| 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 |