From 0315a7769666b8aa015f91ccc5b2ea28020006d7 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Fri, 11 Jun 2021 20:47:29 +0200 Subject: [PATCH] Localize globals per file for all modules --- b3d.lua | 3 +++ binary.lua | 3 +++ bluon.lua | 3 +++ conf.lua | 3 +++ debug.lua | 3 +++ file.lua | 3 +++ func.lua | 3 +++ heap.lua | 3 +++ init.lua | 4 +++- kdtree.lua | 3 +++ log.lua | 3 +++ math.lua | 3 +++ minetest/collisionboxes.lua | 3 +++ minetest/colorspec.lua | 3 +++ minetest/liquid.lua | 3 +++ minetest/misc.lua | 3 +++ minetest/raycast.lua | 3 +++ minetest/schematic.lua | 3 +++ minetest/wielditem_change.lua | 3 +++ mod.lua | 3 +++ persistence.lua | 3 +++ player.lua | 3 +++ quaternion.lua | 3 +++ ranked_set.lua | 3 +++ schema.lua | 3 +++ table.lua | 3 +++ text.lua | 3 +++ trie.lua | 3 +++ vector.lua | 3 +++ 29 files changed, 87 insertions(+), 1 deletion(-) diff --git a/b3d.lua b/b3d.lua index 11ffd81..9a27467 100644 --- a/b3d.lua +++ b/b3d.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, error, getfenv, math, modlib, next, pairs, rawget, setmetatable, table = assert, error, getfenv, math, modlib, next, pairs, rawget, setmetatable, table + local class = getfenv(1) local metatable = {__index = function(_self, key) return rawget(class, key) diff --git a/binary.lua b/binary.lua index 54d4c40..a7e4b2a 100644 --- a/binary.lua +++ b/binary.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, math = assert, math + -- All little endian --+ Reads doubles (f64) or floats (f32) diff --git a/bluon.lua b/bluon.lua index aac14c3..9a8787f 100644 --- a/bluon.lua +++ b/bluon.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, error, getfenv, ipairs, math, modlib, next, pairs, rawget, setmetatable, string, table, unpack = assert, error, getfenv, ipairs, math, modlib, next, pairs, rawget, setmetatable, string, table, unpack + --! experimental local bluon = getfenv(1) diff --git a/conf.lua b/conf.lua index f8552ae..67e9035 100644 --- a/conf.lua +++ b/conf.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type = assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type + -- not deprecated function build_tree(dict) local tree = {} diff --git a/debug.lua b/debug.lua index e50749f..51ffe7f 100644 --- a/debug.lua +++ b/debug.lua @@ -1,3 +1,6 @@ +-- Localize globals +local _G, debug, getfenv, table, tostring = _G, debug, getfenv, table, tostring + function variables(stacklevel) stacklevel = (stacklevel or 1) + 1 local locals = {} diff --git a/file.lua b/file.lua index 3ed5dbf..24441d6 100644 --- a/file.lua +++ b/file.lua @@ -1,3 +1,6 @@ +-- Localize globals +local io, minetest, modlib, string = io, minetest, modlib, string + function read(filename) local file = io.open(filename, "r") if file == nil then return nil end diff --git a/func.lua b/func.lua index 537f2d1..0892789 100644 --- a/func.lua +++ b/func.lua @@ -1,3 +1,6 @@ +-- Localize globals +local error, modlib, unpack = error, modlib, unpack + no_op = function() end function curry(func, ...) diff --git a/heap.lua b/heap.lua index caac50f..b66b34e 100644 --- a/heap.lua +++ b/heap.lua @@ -1,3 +1,6 @@ +-- Localize globals +local getfenv, math, setmetatable, table = getfenv, math, setmetatable, table + local metatable = {__index = getfenv(1)} function less_than(a, b) return a < b end diff --git a/init.lua b/init.lua index 85ad4ba..17c1219 100644 --- a/init.lua +++ b/init.lua @@ -1,4 +1,6 @@ -local rawget, rawset = rawget, rawset +-- Localize globals +local _G, _VERSION, assert, debug, dump, error, load, loadfile, minetest, pairs, rawget, rawset, setmetatable, table, type = _G, _VERSION, assert, debug, dump, error, load, loadfile, minetest, pairs, rawget, rawset, setmetatable, table, type + -- Lua version check if _VERSION then diff --git a/kdtree.lua b/kdtree.lua index c36d107..47f835b 100644 --- a/kdtree.lua +++ b/kdtree.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, getfenv, math, modlib, rawget, setmetatable, table, unpack = assert, getfenv, math, modlib, rawget, setmetatable, table, unpack + local class = getfenv(1) local metatable = {__index = function(_self, key) return rawget(class, key) diff --git a/log.lua b/log.lua index 37817ac..5e4c43b 100644 --- a/log.lua +++ b/log.lua @@ -1,3 +1,6 @@ +-- Localize globals +local ipairs, minetest, modlib, os, pairs, table = ipairs, minetest, modlib, os, pairs, table + -- Log helpers - write to log, force writing to file minetest.mkdir(minetest.get_worldpath() .. "/logs") channels = {} diff --git a/math.lua b/math.lua index e2be3b2..506585a 100644 --- a/math.lua +++ b/math.lua @@ -1,3 +1,6 @@ +-- Localize globals +local math, minetest, modlib, os, string, table = math, minetest, modlib, os, string, table + -- Make random random math.randomseed(minetest and minetest.get_us_time() or os.time() + os.clock()) for _ = 1, 100 do math.random() end diff --git a/minetest/collisionboxes.lua b/minetest/collisionboxes.lua index 2fa9992..55bf06a 100644 --- a/minetest/collisionboxes.lua +++ b/minetest/collisionboxes.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, ipairs, math, minetest, pairs, table, type, vector = assert, ipairs, math, minetest, pairs, table, type, vector + -- Minetest allows shorthand collisionbox = {...} instead of {{...}} local function get_collisionboxes(box_or_boxes) return type(box_or_boxes[1]) == "number" and {box_or_boxes} or box_or_boxes diff --git a/minetest/colorspec.lua b/minetest/colorspec.lua index a2cdc0e..b91b118 100644 --- a/minetest/colorspec.lua +++ b/minetest/colorspec.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, error, math, minetest, setmetatable, tonumber, type = assert, error, math, minetest, setmetatable, tonumber, type + -- As in src/util/string.cpp named_colors = { aliceblue = 0xf0f8ff, diff --git a/minetest/liquid.lua b/minetest/liquid.lua index 30c9d49..09aa93f 100644 --- a/minetest/liquid.lua +++ b/minetest/liquid.lua @@ -1,3 +1,6 @@ +-- Localize globals +local minetest, modlib, pairs = minetest, modlib, pairs + liquid_level_max = 8 --+ Calculates the corner levels of a flowingliquid node --> 4 corner levels from -0.5 to 0.5 as list of `modlib.vector` diff --git a/minetest/misc.lua b/minetest/misc.lua index 1a0500a..7f739b3 100644 --- a/minetest/misc.lua +++ b/minetest/misc.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, minetest, modlib, next, pairs, string, table, type = assert, minetest, modlib, next, pairs, string, table, type + max_wear = 2 ^ 16 - 1 function override(function_name, function_builder) local func = minetest[function_name] diff --git a/minetest/raycast.lua b/minetest/raycast.lua index f1daabf..ee578ce 100644 --- a/minetest/raycast.lua +++ b/minetest/raycast.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, math, minetest, modlib, pairs, setmetatable, vector = assert, math, minetest, modlib, pairs, setmetatable, vector + --+ Raycast wrapper with proper flowingliquid intersections function raycast(_pos1, _pos2, objects, liquids) local raycast = minetest.raycast(_pos1, _pos2, objects, liquids) diff --git a/minetest/schematic.lua b/minetest/schematic.lua index 400b5a7..6faaf31 100644 --- a/minetest/schematic.lua +++ b/minetest/schematic.lua @@ -1,3 +1,6 @@ +-- Localize globals +local VoxelArea, assert, error, io, ipairs, math, minetest, modlib, next, pairs, setmetatable, table, vector = VoxelArea, assert, error, io, ipairs, math, minetest, modlib, next, pairs, setmetatable, table, vector + schematic = {} local metatable = {__index = schematic} diff --git a/minetest/wielditem_change.lua b/minetest/wielditem_change.lua index c63b338..0f49cd5 100644 --- a/minetest/wielditem_change.lua +++ b/minetest/wielditem_change.lua @@ -1,3 +1,6 @@ +-- Localize globals +local minetest, modlib, pairs, table = minetest, modlib, pairs, table + players = {} registered_on_wielditem_changes = {function(...) diff --git a/mod.lua b/mod.lua index f545ee2..eb19c89 100644 --- a/mod.lua +++ b/mod.lua @@ -1,3 +1,6 @@ +-- Localize globals +local Settings, _G, assert, dofile, error, getmetatable, ipairs, loadfile, loadstring, minetest, modlib, pairs, rawget, rawset, setfenv, setmetatable, tonumber, type = Settings, _G, assert, dofile, error, getmetatable, ipairs, loadfile, loadstring, minetest, modlib, pairs, rawget, rawset, setfenv, setmetatable, tonumber, type + -- get resource + dofile function include(modname, file) if not file then diff --git a/persistence.lua b/persistence.lua index 46c4c0b..b59d1fe 100644 --- a/persistence.lua +++ b/persistence.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, error, io, ipairs, loadfile, math, minetest, modlib, pairs, setfenv, setmetatable, type = assert, error, io, ipairs, loadfile, math, minetest, modlib, pairs, setfenv, setmetatable, type + lua_log_file = {} local files = {} local metatable = {__index = lua_log_file} diff --git a/player.lua b/player.lua index cd3df5b..badc36f 100644 --- a/player.lua +++ b/player.lua @@ -1,3 +1,6 @@ +-- Localize globals +local ipairs, minetest, modlib = ipairs, minetest, modlib + forbidden_names = {} function register_forbidden_name(name) forbidden_names[name] = true end diff --git a/quaternion.lua b/quaternion.lua index fe497b8..473ec1d 100644 --- a/quaternion.lua +++ b/quaternion.lua @@ -1,3 +1,6 @@ +-- Localize globals +local math, modlib, pairs, unpack, vector = math, modlib, pairs, unpack, vector + -- TODO OOP, extend vector function from_euler_rotation(rotation) diff --git a/ranked_set.lua b/ranked_set.lua index ecd0ebe..4b2b243 100644 --- a/ranked_set.lua +++ b/ranked_set.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, getfenv, modlib, pairs, rawget, setmetatable, table = assert, getfenv, modlib, pairs, rawget, setmetatable, table + local class = getfenv(1) local metatable = {__index = function(_self, key) return rawget(class, key) diff --git a/schema.lua b/schema.lua index 82e4e50..cdc4972 100644 --- a/schema.lua +++ b/schema.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, error, getfenv, ipairs, math, minetest, modlib, pairs, rawget, setmetatable, table, tonumber, tostring, type = assert, error, getfenv, ipairs, math, minetest, modlib, pairs, rawget, setmetatable, table, tonumber, tostring, type + local class = getfenv(1) local metatable = {__index = function(_self, key) return rawget(class, key) diff --git a/table.lua b/table.lua index 5169240..b35f072 100644 --- a/table.lua +++ b/table.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, error, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, table, type = assert, error, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, table, type + -- Table helpers function map_index(table, func) diff --git a/text.lua b/text.lua index 0ed6a03..c34583c 100644 --- a/text.lua +++ b/text.lua @@ -1,3 +1,6 @@ +-- Localize globals +local math, modlib, pairs, setmetatable, string, table = math, modlib, pairs, setmetatable, string, table + function upper_first(text) return text:sub(1, 1):upper() .. text:sub(2) end function lower_first(text) return text:sub(1, 1):lower() .. text:sub(2) end diff --git a/trie.lua b/trie.lua index 5581b89..06dbdc6 100644 --- a/trie.lua +++ b/trie.lua @@ -1,3 +1,6 @@ +-- Localize globals +local getfenv, math, next, pairs, rawget, setmetatable, string, table, unpack = getfenv, math, next, pairs, rawget, setmetatable, string, table, unpack + local class = getfenv(1) local metatable = {__index = function(_self, key) return rawget(class, key) diff --git a/vector.lua b/vector.lua index 9f2162a..f76062e 100644 --- a/vector.lua +++ b/vector.lua @@ -1,3 +1,6 @@ +-- Localize globals +local assert, getfenv, math, modlib, pairs, rawget, rawset, setmetatable, unpack, vector = assert, getfenv, math, modlib, pairs, rawget, rawset, setmetatable, unpack, vector + local mt_vector = vector local class = getfenv(1)