Class modules: Monkey-patch metatables

master
Lars Mueller 2021-04-22 21:04:07 +02:00
parent 7253e2837a
commit 2825dc60aa
6 changed files with 34 additions and 15 deletions

22
b3d.lua
View File

@ -1,4 +1,7 @@
local metatable = {__index = getfenv(1)}
local class = getfenv(1)
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
--! experimental
--+ Reads a single BB3D chunk from a stream
@ -195,12 +198,17 @@ function read(stream)
flags = flags
}
while content() do
table.insert(bone, {
frame = int(),
position = position and vector3() or nil,
scale = scale and vector3() or nil,
rotation = rotation and quaternion() or nil
})
local frame = {frame = int()}
if position then
frame.position = vector3()
end
if scale then
frame.scale = vector3()
end
if rotation then
frame.rotation = quaternion()
end
table.insert(bone, frame)
end
-- Ensure frames are sorted ascending
table.sort(bone, function(a, b) return a.frame < b.frame end)

View File

@ -1,4 +1,7 @@
local metatable = {__index = getfenv(1)}
local class = getfenv(1)
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
distance = modlib.vector.distance

View File

@ -1,5 +1,7 @@
local class = getfenv(1)
local metatable = {__index = class}
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
comparator = modlib.table.default_comparator

View File

@ -1,8 +1,11 @@
local schema = getfenv(1)
local class = getfenv(1)
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
function new(def)
-- TODO type inference, sanity checking etc.
return setmetatable(def, {__index = schema})
return setmetatable(def, metatable)
end
local function field_name_to_title(name)

View File

@ -1,6 +1,9 @@
local trie = getfenv(1)
local class = getfenv(1)
local metatable = {__index = function(_self, key)
return rawget(class, key)
end}
function new(table) return setmetatable(table or {}, trie) end
function new(table) return setmetatable(table or {}, metatable) end
function insert(self, word, value, overwrite)
for i = 1, word:len() do

View File

@ -1,5 +1,5 @@
local mt_vector = vector
local vector = getfenv(1)
local class = getfenv(1)
index_aliases = {
x = 1,
@ -16,7 +16,7 @@ metatable = {
if index ~= nil then
return table[index]
end
return vector[key]
return rawget(class, key)
end,
__newindex = function(table, key, value)
local index = letters[key]