Backwards compat code.

This commit is contained in:
Diego Martínez 2017-03-01 07:47:26 -03:00 committed by Auke Kok
parent 196a6da26c
commit 7530df494f

@ -1,8 +1,42 @@
digilines = {}
-- Backwards compat.
rawset(_G, "digiline", digilines)
-- Backwards compatibility code.
-- We define a proxy table whose methods can be called with the
-- `foo:bar` notation, and it will redirect the call to the
-- real function, dropping the first implicit argument.
local digiline; digiline = setmetatable({}, {
__index = function(_, k)
-- Get method from real table.
local v = digilines[k]
if type(v) == "function" then
-- We need to wrap functions in order to ignore
-- the implicit `self` argument.
local f = v
return function(self, ...)
-- Trap invalid calls of the form `digiline.foo(...)`.
assert(self == digiline)
return f(...)
end
end
return v
end,
})
rawset(_G, "digiline", digiline)
-- Let's test our proxy table.
function digilines._testproxy(x)
return x
end
-- Test using old `digiline:foobar` form.
assert(digiline:_testproxy("foobar") == "foobar")
-- Test using new `digilines.foobar` form.
assert(digilines._testproxy("foobar") == "foobar")
-- Test calling incorrect form raises an error.
assert(not pcall(function() digiline._testproxy("foobar") end))
local modpath = minetest.get_modpath("digilines")
dofile(modpath .. "/presetrules.lua")