Class modules: Monkey-patch metatables

This commit is contained in:
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

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

@ -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 distance = modlib.vector.distance

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

@ -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) function new(def)
-- TODO type inference, sanity checking etc. -- TODO type inference, sanity checking etc.
return setmetatable(def, {__index = schema}) return setmetatable(def, metatable)
end end
local function field_name_to_title(name) local function field_name_to_title(name)

@ -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) function insert(self, word, value, overwrite)
for i = 1, word:len() do for i = 1, word:len() do

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