/usr/share/luasandbox/modules/lsb/util.lua is in lua-sandbox-extensions 0~git20161128-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | -- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
--[[
# Lua Sandbox Utility Module
## Functions
### behead_array
Effectively removes all array values up to the provided index from an array
by copying end values to the array head and setting now unused entries at
the end of the array to `nil`.
*Arguments*
- index (number) - remove the values in the array up to the index value
- array (table) - array to behead
*Return*
- none - in-place operation
### pairs_by_key
Sorts the keys into an array, and then iterates on the array.
*Arguments*
- hash (table) - hash table to iterate in sorted key order
- sort (function) - function to specify an alternate sort order
*Return*
- function - iterator that traverses the table keys in sort function order
### merge_objects
Merge two objects. Add all data from "src" to "dest". Numeric values are added,
boolean and string values are overwritten, and arrays and objects are
recursively merged. Any data with different types in dest and src will be
skipped.
#### Example
```lua
local a = {
foo = 1,
bar = {1, 1, 3},
quux = 3
}
local b = {
foo = 5,
bar = {0, 0, 5, 1},
baz = {
hello = 100
}
}
local c = merge_objects(a, b)
-------
c contains {
foo = 6,
bar = {1, 1, 8, 1},
baz = {
hello = 100
},
quux = 3
}
```
--]]
-- Imports
local pairs = pairs
local table = require "table"
local type = type
local M = {}
setfenv(1, M) -- Remove external access to contain everything in the module
function behead_array(idx, array)
if idx <= 1 then return end
local array_len = #array
local start_nil_idx = 1 -- If idx > #array we zero it out completely.
if idx <= array_len then
-- Copy values to lower indexes.
local difference = idx - 1
for i = idx, array_len do
array[i-difference] = array[i]
end
start_nil_idx = array_len - difference + 1
end
-- Empty out the end of the array.
for i = start_nil_idx, array_len do
array[i] = nil
end
end
-- http://www.lua.org/pil/19.3.html
function pairs_by_key(t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then
return nil
else
return a[i], t[a[i]]
end
end
return iter
end
function merge_objects(dest, src)
if dest == nil then
return src
end
if src == nil then
return dest
end
local tdest = type(dest)
local tsrc = type(src)
-- Types are different. Ignore the src value, because src is wrong.
if tdest ~= tsrc then
return dest
end
-- types are the same, neither is nil.
if tdest == "number" then
return dest + src
end
-- most recent wins:
if tdest == "boolean" or tdest == "string" then
return src
end
if tdest == "table" then
-- array or object, iterate by key
for k,v in pairs(src) do
dest[k] = merge_objects(dest[k], v)
end
return dest
end
return dest -- unmergable type, leave as-is
end
return M
|