Redo environments

This commit is contained in:
Lars Mueller 2021-06-17 19:45:08 +02:00
parent 0315a77696
commit 4175974def
30 changed files with 264 additions and 116 deletions

14
b3d.lua

@ -1,10 +1,11 @@
-- Localize globals -- Localize globals
local assert, error, getfenv, math, modlib, next, pairs, rawget, setmetatable, table = assert, error, getfenv, math, modlib, next, pairs, rawget, setmetatable, table local assert, error, math, modlib, next, pairs, setmetatable, table = assert, error, math, modlib, next, pairs, setmetatable, table
local class = getfenv(1) -- Set environment
local metatable = {__index = function(_self, key) local _ENV = {}
return rawget(class, key) setfenv(1, _ENV)
end}
local metatable = {__index = _ENV}
--! experimental --! experimental
--+ Reads a single BB3D chunk from a stream --+ Reads a single BB3D chunk from a stream
@ -364,3 +365,6 @@ function get_animated_bone_properties(self, keyframe, interpolate)
get_props(self.node) get_props(self.node)
return bone_properties return bone_properties
end end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local assert, math = assert, math local assert, math = assert, math
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- All little endian -- All little endian
--+ Reads doubles (f64) or floats (f32) --+ Reads doubles (f64) or floats (f32)
@ -116,3 +120,6 @@ end
function write_double(write_byte, number) function write_double(write_byte, number)
return write_float(write_byte, number, nil, true) return write_float(write_byte, number, nil, true)
end end
-- Export environment
return _ENV

@ -1,15 +1,16 @@
-- Localize globals -- 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 local assert, error, ipairs, math, modlib, next, pairs, setmetatable, string, table, type, unpack = assert, error, ipairs, math, modlib, next, pairs, setmetatable, string, table, type, unpack
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
--! experimental --! experimental
local bluon = getfenv(1)
local no_op = modlib.func.no_op local no_op = modlib.func.no_op
local write_float = modlib.binary.write_float local write_float = modlib.binary.write_float
local metatable = {__index = function(_self, key) local metatable = {__index = _ENV}
return rawget(bluon, key)
end}
function new(self) function new(self)
return setmetatable(self or {}, metatable) return setmetatable(self or {}, metatable)
@ -327,3 +328,6 @@ function read(self, stream)
end end
return _read(type) return _read(type)
end end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- 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 local assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type = assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- not deprecated -- not deprecated
function build_tree(dict) function build_tree(dict)
local tree = {} local tree = {}
@ -297,3 +301,6 @@ function check_constraints(value, constraints)
end end
end end
end end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local _G, debug, getfenv, table, tostring = _G, debug, getfenv, table, tostring local _G, debug, getfenv, table, tostring = _G, debug, getfenv, table, tostring
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
function variables(stacklevel) function variables(stacklevel)
stacklevel = (stacklevel or 1) + 1 stacklevel = (stacklevel or 1) + 1
local locals = {} local locals = {}
@ -44,3 +48,6 @@ function stack(stacklevel)
end end
return stack return stack
end end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local io, minetest, modlib, string = io, minetest, modlib, string local io, minetest, modlib, string = io, minetest, modlib, string
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
function read(filename) function read(filename)
local file = io.open(filename, "r") local file = io.open(filename, "r")
if file == nil then return nil end if file == nil then return nil end
@ -108,3 +112,6 @@ function process_bridge_start(name, command, os_execute)
local bridge = process_bridges[name] local bridge = process_bridges[name]
os_execute(string.format(command, bridge.output, bridge.input, bridge.logs)) os_execute(string.format(command, bridge.output, bridge.input, bridge.logs))
end end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local error, modlib, unpack = error, modlib, unpack local error, modlib, unpack = error, modlib, unpack
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
no_op = function() end no_op = function() end
function curry(func, ...) function curry(func, ...)
@ -114,3 +118,6 @@ end
function or_(a, b) function or_(a, b)
return a or b return a or b
end end
-- Export environment
return _ENV

@ -1,7 +1,11 @@
-- Localize globals -- Localize globals
local getfenv, math, setmetatable, table = getfenv, math, setmetatable, table local math, setmetatable, table = math, setmetatable, table
local metatable = {__index = getfenv(1)} -- Set environment
local _ENV = {}
setfenv(1, _ENV)
local metatable = {__index = _ENV}
function less_than(a, b) return a < b end function less_than(a, b) return a < b end
@ -51,3 +55,6 @@ function pop(self)
heapify(1) heapify(1)
return value return value
end end
-- Export environment
return _ENV

@ -1,6 +1,5 @@
-- Localize globals -- 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 local _G, _VERSION, assert, debug, dump, error, load, loadfile, minetest, pairs, rawget, rawset, setmetatable, table = _G, _VERSION, assert, debug, dump, error, load, loadfile, minetest, pairs, rawget, rawset, setmetatable, table
-- Lua version check -- Lua version check
if _VERSION then if _VERSION then
@ -58,33 +57,40 @@ for _, file in pairs{
"persistence", "persistence",
"debug" "debug"
} do } do
modules[file] = file modules[file] = true
end end
if minetest then if minetest then
modules.minetest = {
"misc",
"collisionboxes",
"liquid",
"raycast",
"wielditem_change",
"colorspec",
"schematic"
}
for _, file in pairs{ for _, file in pairs{
"minetest",
"log", "log",
"player", "player",
-- deprecated -- deprecated
"conf" "conf"
} do } do
modules[file] = file modules[file] = true
end end
end end
local load_module, get_resource, loadfile_exports local parent_dir
if not minetest then
local init_path = arg and arg[0]
parent_dir = init_path and init_path:match"^.[/\\]" or ""
end
-- only used if Minetest is available
local function get_resource(modname, resource, ...)
if not resource then
resource = modname
modname = minetest.get_current_modname()
end
return table.concat({minetest.get_modpath(modname), resource, ...}, modlib.dir_delim)
end
modlib = setmetatable({ modlib = setmetatable({
-- TODO bump on release -- TODO bump on release
version = 69, version = 69,
modname = minetest and minetest.get_current_modname(), modname = minetest and minetest.get_current_modname(),
-- TODO
dir_delim = rawget(_G, "DIR_DELIM") or "/", dir_delim = rawget(_G, "DIR_DELIM") or "/",
_RG = setmetatable({}, { _RG = setmetatable({}, {
__index = function(_, index) __index = function(_, index)
@ -101,55 +107,19 @@ modlib = setmetatable({
end end
}, { }, {
__index = function(self, module_name) __index = function(self, module_name)
local files = modules[module_name] if not modules[module_name] then
local environment -- module not available, return nil
if type(files) == "string" then
environment = load_module(files)
elseif files then
environment = loadfile_exports(get_resource(self.modname, module_name, files[1] .. ".lua"))
for index = 2, #files do
self.mod.include_env(get_resource(self.modname, module_name, files[index] .. ".lua"), environment)
end
else
return return
end end
local module = {} local environment = dofile(minetest
for key, value in pairs(environment) do and get_resource(self.modname, module_name .. ".lua")
-- Shallow copy. Doesn't use `modlib.table.shallowcopy` as that is part of a module, too. or (parent_dir .. module_name .. ".lua"))
module[key] = value self[module_name] = environment
end return environment
self[module_name] = module
return module
end end
}) })
function get_resource(modname, resource, ...) modlib.mod = minetest and dofile(get_resource(modlib.modname, "mod.lua"))
if not resource then
resource = modname
modname = minetest.get_current_modname()
end
return table.concat({minetest.get_modpath(modname), resource, ...}, modlib.dir_delim)
end
function loadfile_exports(filename)
local env = setmetatable({}, {__index = _G})
local file = assert(loadfile(filename))
setfenv(file, env)
file()
return env
end
local parent_dir
if not minetest then
local init_path = arg and arg[0]
parent_dir = init_path and init_path:match"^.[/\\]" or ""
end
function load_module(module_name)
local file = module_name .. ".lua"
return loadfile_exports(minetest and get_resource(modlib.modname, file) or (parent_dir .. file))
end
modlib.mod = minetest and loadfile_exports(get_resource(modlib.modname, "mod.lua"))
-- Aliases -- Aliases
modlib.string = modlib.text modlib.string = modlib.text
@ -157,15 +127,15 @@ modlib.number = modlib.math
if minetest then if minetest then
modlib.conf.build_setting_tree() modlib.conf.build_setting_tree()
modlib.mod.get_resource = get_resource modlib.mod.get_resource = get_resource
modlib.mod.loadfile_exports = loadfile_exports
end end
_ml = modlib _ml = modlib
modlib.mod.include"test.lua"
--[[ --[[
--modlib.mod.include"test.lua" --modlib.mod.include"test.lua"
]] ]]
-- TODO verify localizations suffice
return modlib return modlib

@ -1,10 +1,11 @@
-- Localize globals -- Localize globals
local assert, getfenv, math, modlib, rawget, setmetatable, table, unpack = assert, getfenv, math, modlib, rawget, setmetatable, table, unpack local assert, math, modlib, setmetatable, table, unpack = assert, math, modlib, setmetatable, table, unpack
local class = getfenv(1) -- Set environment
local metatable = {__index = function(_self, key) local _ENV = {}
return rawget(class, key) setfenv(1, _ENV)
end}
local metatable = {__index = _ENV}
distance = modlib.vector.distance distance = modlib.vector.distance
@ -58,3 +59,6 @@ function get_nearest_neighbor(self, vector)
end end
-- TODO insertion & deletion + rebalancing -- TODO insertion & deletion + rebalancing
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local ipairs, minetest, modlib, os, pairs, table = ipairs, minetest, modlib, os, pairs, table local ipairs, minetest, modlib, os, pairs, table = ipairs, minetest, modlib, os, pairs, table
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- Log helpers - write to log, force writing to file -- Log helpers - write to log, force writing to file
minetest.mkdir(minetest.get_worldpath() .. "/logs") minetest.mkdir(minetest.get_worldpath() .. "/logs")
channels = {} channels = {}
@ -77,3 +81,7 @@ minetest.register_on_shutdown(
write_all_to_file() write_all_to_file()
end end
) )
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local math, minetest, modlib, os, string, table = math, minetest, modlib, os, string, table local math, minetest, modlib, os, string, table = math, minetest, modlib, os, string, table
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- Make random random -- Make random random
math.randomseed(minetest and minetest.get_us_time() or os.time() + os.clock()) math.randomseed(minetest and minetest.get_us_time() or os.time() + os.clock())
for _ = 1, 100 do math.random() end for _ = 1, 100 do math.random() end
@ -72,3 +76,6 @@ function fround(number)
end end
return sign * powexp * (leading - mantissa / 0x800000) return sign * powexp * (leading - mantissa / 0x800000)
end end
-- Export environment
return _ENV

12
minetest.lua Normal file

@ -0,0 +1,12 @@
local _ENV = setmetatable({}, {__index = _G})
local function load(filename)
assert(loadfile(modlib.mod.get_resource(modlib.modname, "minetest", filename .. ".lua")))(_ENV)
end
load"misc"
load"collisionboxes"
load"liquid"
load"raycast"
load"wielditem_change"
load"colorspec"
load"schematic"
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local assert, ipairs, math, minetest, pairs, table, type, vector = assert, ipairs, math, minetest, pairs, table, type, vector local assert, ipairs, math, minetest, pairs, table, type, vector = assert, ipairs, math, minetest, pairs, table, type, vector
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
-- Minetest allows shorthand collisionbox = {...} instead of {{...}} -- Minetest allows shorthand collisionbox = {...} instead of {{...}}
local function get_collisionboxes(box_or_boxes) local function get_collisionboxes(box_or_boxes)
return type(box_or_boxes[1]) == "number" and {box_or_boxes} or box_or_boxes return type(box_or_boxes[1]) == "number" and {box_or_boxes} or box_or_boxes

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local assert, error, math, minetest, setmetatable, tonumber, type = assert, error, math, minetest, setmetatable, tonumber, type local assert, error, math, minetest, setmetatable, tonumber, type = assert, error, math, minetest, setmetatable, tonumber, type
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
-- As in src/util/string.cpp -- As in src/util/string.cpp
named_colors = { named_colors = {
aliceblue = 0xf0f8ff, aliceblue = 0xf0f8ff,

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local minetest, modlib, pairs = minetest, modlib, pairs local minetest, modlib, pairs = minetest, modlib, pairs
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
liquid_level_max = 8 liquid_level_max = 8
--+ Calculates the corner levels of a flowingliquid node --+ Calculates the corner levels of a flowingliquid node
--> 4 corner levels from -0.5 to 0.5 as list of `modlib.vector` --> 4 corner levels from -0.5 to 0.5 as list of `modlib.vector`

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local assert, minetest, modlib, next, pairs, string, table, type = assert, minetest, modlib, next, pairs, string, table, type local assert, minetest, modlib, next, pairs, string, table, type = assert, minetest, modlib, next, pairs, string, table, type
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
max_wear = 2 ^ 16 - 1 max_wear = 2 ^ 16 - 1
function override(function_name, function_builder) function override(function_name, function_builder)
local func = minetest[function_name] local func = minetest[function_name]

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local assert, math, minetest, modlib, pairs, setmetatable, vector = assert, math, minetest, modlib, pairs, setmetatable, vector local assert, math, minetest, modlib, pairs, setmetatable, vector = assert, math, minetest, modlib, pairs, setmetatable, vector
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
--+ Raycast wrapper with proper flowingliquid intersections --+ Raycast wrapper with proper flowingliquid intersections
function raycast(_pos1, _pos2, objects, liquids) function raycast(_pos1, _pos2, objects, liquids)
local raycast = minetest.raycast(_pos1, _pos2, objects, liquids) local raycast = minetest.raycast(_pos1, _pos2, objects, liquids)

@ -1,6 +1,10 @@
-- Localize globals -- 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 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
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
schematic = {} schematic = {}
local metatable = {__index = schematic} local metatable = {__index = schematic}

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local minetest, modlib, pairs, table = minetest, modlib, pairs, table local minetest, modlib, pairs, table = minetest, modlib, pairs, table
-- Set environment
local _ENV = ...
setfenv(1, _ENV)
players = {} players = {}
registered_on_wielditem_changes = {function(...) registered_on_wielditem_changes = {function(...)

15
mod.lua

@ -1,6 +1,18 @@
-- Localize globals -- 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 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
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
function loadfile_exports(filename)
local env = setmetatable({}, {__index = _G})
local file = assert(loadfile(filename))
setfenv(file, env)
file()
return env
end
-- get resource + dofile -- get resource + dofile
function include(modname, file) function include(modname, file)
if not file then if not file then
@ -132,3 +144,6 @@ function configuration(modname)
end end
return conf return conf
end end
-- Export environment
return _ENV

@ -1,5 +1,9 @@
-- Localize globals -- 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 local assert, error, io, ipairs, loadfile, math, minetest, modlib, pairs, setfenv, setmetatable, table, type = assert, error, io, ipairs, loadfile, math, minetest, modlib, pairs, setfenv, setmetatable, table, type
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
lua_log_file = {} lua_log_file = {}
local files = {} local files = {}
@ -162,3 +166,6 @@ function lua_log_file:rewrite()
self:_rewrite() self:_rewrite()
self:open() self:open()
end end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local ipairs, minetest, modlib = ipairs, minetest, modlib local ipairs, minetest, modlib = ipairs, minetest, modlib
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
forbidden_names = {} forbidden_names = {}
function register_forbidden_name(name) forbidden_names[name] = true end function register_forbidden_name(name) forbidden_names[name] = true end
@ -79,3 +83,6 @@ function datatable(table, default)
minetest.register_on_leaveplayer(function(player) table[player:get_player_name()] = nil end) minetest.register_on_leaveplayer(function(player) table[player:get_player_name()] = nil end)
return table return table
end end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local math, modlib, pairs, unpack, vector = math, modlib, pairs, unpack, vector local math, modlib, pairs, unpack, vector = math, modlib, pairs, unpack, vector
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- TODO OOP, extend vector -- TODO OOP, extend vector
function from_euler_rotation(rotation) function from_euler_rotation(rotation)
@ -130,3 +134,6 @@ function to_euler_rotation_irrlicht(self)
return vector.apply(_calc(), math.deg) return vector.apply(_calc(), math.deg)
end end
-- Export environment
return _ENV

@ -1,10 +1,11 @@
-- Localize globals -- Localize globals
local assert, getfenv, modlib, pairs, rawget, setmetatable, table = assert, getfenv, modlib, pairs, rawget, setmetatable, table local assert, modlib, pairs, setmetatable, table = assert, modlib, pairs, setmetatable, table
local class = getfenv(1) -- Set environment
local metatable = {__index = function(_self, key) local _ENV = {}
return rawget(class, key) setfenv(1, _ENV)
end}
local metatable = {__index = _ENV}
comparator = modlib.table.default_comparator comparator = modlib.table.default_comparator
@ -315,3 +316,6 @@ function get_by_rank(self, rank)
end end
until not tree until not tree
end end
-- Export environment
return _ENV

@ -1,10 +1,11 @@
-- Localize globals -- 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 assert, error, ipairs, math, minetest, modlib, pairs, setmetatable, table, tonumber, tostring, type = assert, error, ipairs, math, minetest, modlib, pairs, setmetatable, table, tonumber, tostring, type
local class = getfenv(1) -- Set environment
local metatable = {__index = function(_self, key) local _ENV = {}
return rawget(class, key) setfenv(1, _ENV)
end}
local metatable = {__index = _ENV}
function new(def) function new(def)
-- TODO type inference, sanity checking etc. -- TODO type inference, sanity checking etc.
@ -299,3 +300,6 @@ function load(self, override, params)
if self.func then self.func(override) end if self.func then self.func(override) end
return override return override
end end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- 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 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
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- Table helpers -- Table helpers
function map_index(table, func) function map_index(table, func)
@ -590,3 +594,6 @@ function repetition(value, count)
end end
return table return table
end end
-- Export environment
return _ENV

@ -1,6 +1,10 @@
-- Localize globals -- Localize globals
local math, modlib, pairs, setmetatable, string, table = math, modlib, pairs, setmetatable, string, table local math, modlib, pairs, setmetatable, string, table = math, modlib, pairs, setmetatable, string, table
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
function upper_first(text) return text:sub(1, 1):upper() .. text:sub(2) end 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 function lower_first(text) return text:sub(1, 1):lower() .. text:sub(2) end
@ -167,3 +171,6 @@ function handle_ifdefs(code, vars)
table.insert(finalcode, string.sub(code, after_endif + 2)) table.insert(finalcode, string.sub(code, after_endif + 2))
return table.concat(finalcode, "") return table.concat(finalcode, "")
end end
-- Export environment
return _ENV

@ -1,10 +1,11 @@
-- Localize globals -- Localize globals
local getfenv, math, next, pairs, rawget, setmetatable, string, table, unpack = getfenv, math, next, pairs, rawget, setmetatable, string, table, unpack local math, next, pairs, setmetatable, string, table, unpack = math, next, pairs, setmetatable, string, table, unpack
local class = getfenv(1) -- Set environment
local metatable = {__index = function(_self, key) local _ENV = {}
return rawget(class, key) setfenv(1, _ENV)
end}
local metatable = {__index = _ENV}
function new(table) return setmetatable(table or {}, metatable) end function new(table) return setmetatable(table or {}, metatable) end
@ -125,3 +126,6 @@ function find_longest(self, query, query_offset)
end end
return last_leaf, leaf_pos return last_leaf, leaf_pos
end end
-- Export environment
return _ENV

@ -1,8 +1,11 @@
-- Localize globals -- Localize globals
local assert, getfenv, math, modlib, pairs, rawget, rawset, setmetatable, unpack, vector = assert, getfenv, math, modlib, pairs, rawget, rawset, setmetatable, unpack, vector local assert, math, modlib, pairs, rawset, setmetatable, unpack, vector = assert, math, modlib, pairs, rawset, setmetatable, unpack, vector
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
local mt_vector = vector local mt_vector = vector
local class = getfenv(1)
index_aliases = { index_aliases = {
x = 1, x = 1,
@ -19,9 +22,10 @@ metatable = {
if index ~= nil then if index ~= nil then
return table[index] return table[index]
end end
return rawget(class, key) return _ENV[key]
end, end,
__newindex = function(table, key, value) __newindex = function(table, key, value)
-- TODO
local index = letters[key] local index = letters[key]
if index ~= nil then if index ~= nil then
return rawset(table, index, value) return rawset(table, index, value)
@ -226,3 +230,6 @@ function triangle_normal(triangle)
local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1) local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1)
return normalize(cross3(edge_1, edge_2)) return normalize(cross3(edge_1, edge_2))
end end
-- Export environment
return _ENV