🔧 build: 调整库
parent
1d1b0338f1
commit
2ebfd2654c
@ -1,186 +0,0 @@
|
||||
-- beholder.lua - v2.1.1 (2011-11)
|
||||
-- Copyright (c) 2011 Enrique García Cota
|
||||
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN callback OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
local function copy(t)
|
||||
local c = {}
|
||||
for i = 1, #t do
|
||||
c[i] = t[i]
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
local function hash2array(t)
|
||||
local arr, i = {}, 0
|
||||
for _, v in pairs(t) do
|
||||
i = i + 1
|
||||
arr[i] = v
|
||||
end
|
||||
return arr, i
|
||||
end
|
||||
-- private Node class
|
||||
|
||||
local nodesById = nil
|
||||
local root = nil
|
||||
|
||||
local function newNode()
|
||||
return {
|
||||
callbacks = {},
|
||||
children = setmetatable({}, {
|
||||
__mode = "k",
|
||||
}),
|
||||
}
|
||||
end
|
||||
|
||||
local function findNodeById(id)
|
||||
return nodesById[id]
|
||||
end
|
||||
|
||||
local function findOrCreateChildNode(self, key)
|
||||
self.children[key] = self.children[key] or newNode()
|
||||
return self.children[key]
|
||||
end
|
||||
|
||||
local function findOrCreateDescendantNode(self, keys)
|
||||
local node = self
|
||||
for i = 1, #keys do
|
||||
node = findOrCreateChildNode(node, keys[i])
|
||||
end
|
||||
return node
|
||||
end
|
||||
|
||||
local function invokeNodeCallbacks(self, params)
|
||||
-- copy the hash into an array, for safety (self-erasures)
|
||||
local callbacks, count = hash2array(self.callbacks)
|
||||
for i = 1, #callbacks do
|
||||
callbacks[i](unpack(params))
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
local function invokeAllNodeCallbacksInSubTree(self, params)
|
||||
local counter = invokeNodeCallbacks(self, params)
|
||||
for _, child in pairs(self.children) do
|
||||
counter = counter + invokeAllNodeCallbacksInSubTree(child, params)
|
||||
end
|
||||
return counter
|
||||
end
|
||||
|
||||
local function invokeNodeCallbacksFromPath(self, path)
|
||||
local node = self
|
||||
local params = copy(path)
|
||||
local counter = invokeNodeCallbacks(node, params)
|
||||
|
||||
for i = 1, #path do
|
||||
node = node.children[path[i]]
|
||||
if not node then
|
||||
break
|
||||
end
|
||||
table.remove(params, 1)
|
||||
counter = counter + invokeNodeCallbacks(node, params)
|
||||
end
|
||||
|
||||
return counter
|
||||
end
|
||||
|
||||
local function addCallbackToNode(self, callback)
|
||||
local id = {}
|
||||
self.callbacks[id] = callback
|
||||
nodesById[id] = self
|
||||
return id
|
||||
end
|
||||
|
||||
local function removeCallbackFromNode(self, id)
|
||||
self.callbacks[id] = nil
|
||||
nodesById[id] = nil
|
||||
end
|
||||
|
||||
------ beholder table
|
||||
|
||||
local beholder = {}
|
||||
|
||||
-- beholder private functions/vars
|
||||
|
||||
local groups = nil
|
||||
local currentGroupId = nil
|
||||
|
||||
local function addIdToCurrentGroup(id)
|
||||
if currentGroupId then
|
||||
groups[currentGroupId] = groups[currentGroupId] or setmetatable({}, {
|
||||
__mode = "k",
|
||||
})
|
||||
local group = groups[currentGroupId]
|
||||
group[#group + 1] = id
|
||||
end
|
||||
return id
|
||||
end
|
||||
|
||||
local function stopObservingGroup(group)
|
||||
local count = #group
|
||||
for i = 1, count do
|
||||
beholder.stopObserving(group[i])
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
local function falseIfZero(n)
|
||||
return n > 0 and n
|
||||
end
|
||||
|
||||
local function extractEventAndCallbackFromParams(params)
|
||||
assert(#params > 0,
|
||||
"beholder.observe requires at least one parameter - the callback. You usually want to use two, i.e.: beholder.observe('EVENT', callback)")
|
||||
local callback = table.remove(params, #params)
|
||||
return params, callback
|
||||
end
|
||||
|
||||
------ Public interface
|
||||
|
||||
function beholder.observe(...)
|
||||
local event, callback = extractEventAndCallbackFromParams({...})
|
||||
local node = findOrCreateDescendantNode(root, event)
|
||||
return addIdToCurrentGroup(addCallbackToNode(node, callback))
|
||||
end
|
||||
|
||||
function beholder.stopObserving(id)
|
||||
local node = findNodeById(id)
|
||||
if node then
|
||||
removeCallbackFromNode(node, id)
|
||||
end
|
||||
|
||||
local group, count = groups[id], 0
|
||||
if group then
|
||||
count = stopObservingGroup(group)
|
||||
end
|
||||
|
||||
return (node or count > 0) and true or false
|
||||
end
|
||||
|
||||
function beholder.group(groupId, f)
|
||||
assert(not currentGroupId, "beholder.group can not be nested!")
|
||||
currentGroupId = groupId
|
||||
f()
|
||||
currentGroupId = nil
|
||||
end
|
||||
|
||||
function beholder.trigger(...)
|
||||
return falseIfZero(invokeNodeCallbacksFromPath(root, {...}))
|
||||
end
|
||||
|
||||
function beholder.triggerAll(...)
|
||||
return falseIfZero(invokeAllNodeCallbacksInSubTree(root, {...}))
|
||||
end
|
||||
|
||||
function beholder.reset()
|
||||
root = newNode()
|
||||
nodesById = setmetatable({}, {
|
||||
__mode = "k",
|
||||
})
|
||||
groups = {}
|
||||
currentGroupId = nil
|
||||
end
|
||||
|
||||
beholder.reset()
|
||||
|
||||
return beholder
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,93 +0,0 @@
|
||||
-- --[[
|
||||
-- lua-rubble: Rust-like struct-trait-impl system
|
||||
-- https://github.com/evolbug/lua-rubble
|
||||
-- ]]
|
||||
|
||||
-- function struct(members)
|
||||
-- assert(type(members) == 'table', 'must use table for call')
|
||||
-- return setmetatable({
|
||||
-- impls = {},
|
||||
-- traits = {},
|
||||
-- }, {
|
||||
-- __index = {
|
||||
-- impl = function(self, methods)
|
||||
-- assert(type(methods) == 'table', 'must use table for methods')
|
||||
-- assert(#methods == 0, 'only named members allowed')
|
||||
-- for k, v in pairs(methods) do
|
||||
-- assert(type(k) == 'string', 'name must be a string')
|
||||
-- assert(type(v) == 'function', 'not a method')
|
||||
-- assert(not self.impls[k], 'method ' .. k .. ' already defined')
|
||||
-- self.impls[k] = v
|
||||
-- end
|
||||
-- end,
|
||||
-- },
|
||||
|
||||
-- __call = function(self, init)
|
||||
-- assert(type(init) == 'table', 'must use table for init')
|
||||
-- assert(#init == 0, 'only named members allowed')
|
||||
-- for key, value in pairs(init) do
|
||||
-- assert(members[key], 'initializing nonexistent field ' .. key)
|
||||
-- assert(type(value) == members[key],
|
||||
-- "type mismatch for member " .. key .. ' expected:' .. members[key] .. ' got:' .. type(value))
|
||||
-- end
|
||||
-- return setmetatable(init, {
|
||||
-- __index = self.impls,
|
||||
-- __newindex = function(self, key, value)
|
||||
-- assert(members[key], "setting nonexistent member " .. key)
|
||||
-- assert(type(value) == members[key],
|
||||
-- "type mismatch for member " .. key .. ' expected:' .. members[key] .. ' got:' .. type(value))
|
||||
-- self[key] = value
|
||||
-- end,
|
||||
-- })
|
||||
-- end,
|
||||
|
||||
-- __newindex = function(self, key, value)
|
||||
-- error('struct definitions are immutable')
|
||||
-- end,
|
||||
-- })
|
||||
-- end
|
||||
|
||||
-- function trait(members)
|
||||
-- assert(type(members) == 'table', 'must use table for call')
|
||||
-- return setmetatable(members, {
|
||||
-- __index = {
|
||||
-- impl = function(self, struct)
|
||||
-- return function(methods)
|
||||
-- assert(type(methods) == 'table', 'must use table for methods')
|
||||
-- assert(#methods == 0, 'only named members allowed')
|
||||
-- for k, v in pairs(self) do
|
||||
-- assert(methods[k], 'must implement method ' .. k)
|
||||
-- assert(type(methods[k]) == 'function', 'method ' .. k .. ' not a function')
|
||||
-- end
|
||||
-- for k, v in pairs(methods) do
|
||||
-- assert(type(k) == 'string', 'name must be a string')
|
||||
-- assert(type(v) == 'function', 'not a method')
|
||||
-- assert(self[k], 'method not specified in trait')
|
||||
-- assert(not struct.impls[k], 'method ' .. k .. ' already defined')
|
||||
-- struct.impls[k] = v
|
||||
-- end
|
||||
-- struct.traits[self] = true
|
||||
-- end
|
||||
-- end,
|
||||
-- },
|
||||
-- __call = function(self, init)
|
||||
-- assert(type(init) == 'table', 'must use table for init')
|
||||
-- assert(#init == 0, 'only named members allowed')
|
||||
-- for k, v in pairs(init) do
|
||||
-- assert(members[k], 'initializing nonexistent field')
|
||||
-- end
|
||||
-- return setmetatable(init, {
|
||||
-- __index = self.impls,
|
||||
-- __newindex = function(self, key, value)
|
||||
-- assert(members[key], "setting nonexistent member " .. key)
|
||||
-- assert(type(value) == members[key], "type mismatch for member")
|
||||
-- self[key] = value
|
||||
-- end,
|
||||
-- })
|
||||
-- end,
|
||||
|
||||
-- __newindex = function(self, key, value)
|
||||
-- error('trait definitions are immutable')
|
||||
-- end,
|
||||
-- })
|
||||
-- end
|
||||
@ -1,170 +0,0 @@
|
||||
local sandbox = {
|
||||
_VERSION = "sandbox 0.5",
|
||||
_DESCRIPTION = "A pure-lua solution for running untrusted Lua code.",
|
||||
_URL = "https://github.com/kikito/sandbox.lua",
|
||||
|
||||
}
|
||||
|
||||
-- quotas don't work in LuaJIT since debug.sethook works differently there
|
||||
local quota_supported = type(_G.jit) == "nil"
|
||||
sandbox.quota_supported = quota_supported
|
||||
|
||||
-- PUC-Rio Lua 5.1 does not support deactivation of bytecode
|
||||
local bytecode_blocked = _ENV or type(_G.jit) == "table"
|
||||
sandbox.bytecode_blocked = bytecode_blocked
|
||||
|
||||
-- The base environment is merged with the given env option (or an empty table, if no env provided)
|
||||
--
|
||||
local BASE_ENV = {}
|
||||
|
||||
-- List of unsafe packages/functions:
|
||||
--
|
||||
-- * string.rep: can be used to allocate millions of bytes in 1 operation
|
||||
-- * {set|get}metatable: can be used to modify the metatable of global objects (strings, integers)
|
||||
-- * collectgarbage: can affect performance of other systems
|
||||
-- * dofile: can access the server filesystem
|
||||
-- * _G: It has access to everything. It can be mocked to other things though.
|
||||
-- * load{file|string}: All unsafe because they can grant acces to global env
|
||||
-- * raw{get|set|equal}: Potentially unsafe
|
||||
-- * module|require|module: Can modify the host settings
|
||||
-- * string.dump: Can display confidential server info (implementation of functions)
|
||||
-- * math.randomseed: Can affect the host sytem
|
||||
-- * io.*, os.*: Most stuff there is unsafe, see below for exceptions
|
||||
|
||||
-- Safe packages/functions below
|
||||
([[
|
||||
|
||||
_VERSION assert error ipairs next pairs
|
||||
pcall select tonumber tostring type unpack xpcall
|
||||
|
||||
coroutine.create coroutine.resume coroutine.running coroutine.status
|
||||
coroutine.wrap coroutine.yield
|
||||
|
||||
math.abs math.acos math.asin math.atan math.atan2 math.ceil
|
||||
math.cos math.cosh math.deg math.exp math.fmod math.floor
|
||||
math.frexp math.huge math.ldexp math.log math.log10 math.max
|
||||
math.min math.modf math.pi math.pow math.rad math.random
|
||||
math.sin math.sinh math.sqrt math.tan math.tanh
|
||||
|
||||
os.clock os.difftime os.time
|
||||
|
||||
string.byte string.char string.find string.format string.gmatch
|
||||
string.gsub string.len string.lower string.match string.reverse
|
||||
string.sub string.upper
|
||||
|
||||
table.insert table.maxn table.remove table.sort
|
||||
|
||||
]]):gsub('%S+', function(id)
|
||||
local module, method = id:match('([^%.]+)%.([^%.]+)')
|
||||
if module then
|
||||
BASE_ENV[module] = BASE_ENV[module] or {}
|
||||
BASE_ENV[module][method] = _G[module][method]
|
||||
else
|
||||
BASE_ENV[id] = _G[id]
|
||||
end
|
||||
end)
|
||||
|
||||
local function protect_module(module, module_name)
|
||||
return setmetatable({}, {
|
||||
__index = module,
|
||||
__newindex = function(_, attr_name, _)
|
||||
error('Can not modify ' .. module_name .. '.' .. attr_name .. '. Protected by the sandbox.')
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
('coroutine math os string table'):gsub('%S+', function(module_name)
|
||||
BASE_ENV[module_name] = protect_module(BASE_ENV[module_name], module_name)
|
||||
end)
|
||||
|
||||
-- auxiliary functions/variables
|
||||
|
||||
local string_rep = string.rep
|
||||
|
||||
local function sethook(f, key, quota)
|
||||
if type(debug) ~= 'table' or type(debug.sethook) ~= 'function' then
|
||||
return
|
||||
end
|
||||
debug.sethook(f, key, quota)
|
||||
end
|
||||
|
||||
local function cleanup()
|
||||
sethook()
|
||||
string.rep = string_rep -- luacheck: no global
|
||||
|
||||
end
|
||||
|
||||
-- Public interface: sandbox.protect
|
||||
function sandbox.protect(code, options)
|
||||
options = options or {}
|
||||
|
||||
local quota = false
|
||||
if options.quota and not quota_supported then
|
||||
error("options.quota is not supported on this environment (usually LuaJIT). Please unset options.quota")
|
||||
end
|
||||
if options.quota ~= false then
|
||||
quota = options.quota or 500000
|
||||
end
|
||||
|
||||
assert(type(code) == 'string', "expected a string")
|
||||
|
||||
local passed_env = options.env or {}
|
||||
local env = {}
|
||||
for k, v in pairs(BASE_ENV) do
|
||||
local pv = passed_env[k]
|
||||
if pv ~= nil then
|
||||
env[k] = pv
|
||||
else
|
||||
env[k] = v
|
||||
end
|
||||
end
|
||||
setmetatable(env, {
|
||||
__index = options.env,
|
||||
})
|
||||
env._G = env
|
||||
|
||||
local f
|
||||
if bytecode_blocked then
|
||||
f = assert(load(code, nil, 't', env))
|
||||
else
|
||||
f = assert(loadstring(code))
|
||||
setfenv(f, env)
|
||||
end
|
||||
|
||||
return function(...)
|
||||
|
||||
if quota and quota_supported then
|
||||
local timeout = function()
|
||||
cleanup()
|
||||
error('Quota exceeded: ' .. tostring(quota))
|
||||
end
|
||||
sethook(timeout, "", quota)
|
||||
end
|
||||
|
||||
string.rep = nil -- luacheck: no global
|
||||
|
||||
local t = table.pack(pcall(f, ...))
|
||||
|
||||
cleanup()
|
||||
|
||||
if not t[1] then
|
||||
error(t[2])
|
||||
end
|
||||
|
||||
return table.unpack(t, 2, t.n)
|
||||
end
|
||||
end
|
||||
|
||||
-- Public interface: sandbox.run
|
||||
function sandbox.run(code, options, ...)
|
||||
return sandbox.protect(code, options)(...)
|
||||
end
|
||||
|
||||
-- make sandbox(f) == sandbox.protect(f)
|
||||
setmetatable(sandbox, {
|
||||
__call = function(_, code, o)
|
||||
return sandbox.protect(code, o)
|
||||
end,
|
||||
})
|
||||
|
||||
return sandbox
|
||||
Loading…
Reference in New Issue