Skip to content

Instantly share code, notes, and snippets.

@nucular
Last active October 14, 2022 22:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nucular/349a74c2524d5316683327a2bbb31ad7 to your computer and use it in GitHub Desktop.
Save nucular/349a74c2524d5316683327a2bbb31ad7 to your computer and use it in GitHub Desktop.
Dump of Lua code snippets

Lua Dump

This is a dump of random Lua code snippets. Use them however you like.

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

--- Call cleanup functions when the program terminates
local handlers = {}
local function cleanup()
for i, v in ipairs(handlers) do
pcall(v)
end
handlers = {}
end
-- ew
_G.__atexitgc__ = newproxy(true)
getmetatable(_G.__atexitgc__).__gc = cleanup
local oldexit = os.exit
function os.exit(...)
cleanup()
return oldexit(...)
end
--- Register a function to be called when the program terminates
function atexit(f)
table.insert(handlers, f)
end
return atexit
--- Return the default tostring representation of a table even if its metatable
--- redefined __tostring. __metatable breaks this though.
function rawtostring(o)
local mt = getmetatable(o)
local s
if mt and mt.__tostring then
local oldtostring = mt.__tostring
mt.__tostring = nil
s = tostring(o)
mt.__tostring = oldtostring
else
s = tostring(o)
end
return s
end
--- Set a metatable that disallows getting undefined/nil and setting any members
--- on a table.
--- Can be disabled temporarily by setting a `_strict` key on the table to
--- false. Also adds a `global` table to the indexing chain that acts as a thin
--- wrapper around rawset.
local strict = function(env)
local env = env or _G
if env._strict ~= nil then
return
end
env._strict = true
local envmt = getmetatable(env) or {}
local envindex = {}
if envmt.__index or envmt.__newindex then
error("__index or __newindex metaevents already bound", 2)
end
--- syntactical sugar for rawset
envindex.global = setmetatable({}, {
__index = env,
__newindex = function(gl, name, value)
rawset(env, name, value)
end
})
envmt.__index = function(g, name)
return (
rawget(g, name) or envindex[name]
) or (
g._strict and error(string.format(
"attempt to get undeclared name '%s'", name
), 2)
)
end
envmt.__newindex = function(g, name, value)
if g._strict then
error(string.format(
"attempt to set undeclared name '%s'", name
), 2)
else
rawset(g, name, value)
end
end
setmetatable(env, envmt)
end
return strict
--- @example
require("strict")(_G)
global. foo = "foo"
foo = "bar"
_strict = false
foo = "baz"
_strict = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment