mirror of
https://github.com/minetest-mods/digilines.git
synced 2024-12-05 02:33:49 +01:00
Backwards compat code.
This commit is contained in:
parent
196a6da26c
commit
7530df494f
38
init.lua
38
init.lua
@ -1,8 +1,42 @@
|
|||||||
|
|
||||||
digilines = {}
|
digilines = {}
|
||||||
|
|
||||||
-- Backwards compat.
|
-- Backwards compatibility code.
|
||||||
rawset(_G, "digiline", digilines)
|
-- 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")
|
local modpath = minetest.get_modpath("digilines")
|
||||||
dofile(modpath .. "/presetrules.lua")
|
dofile(modpath .. "/presetrules.lua")
|
||||||
|
Loading…
Reference in New Issue
Block a user