diff --git a/init.lua b/init.lua index f6050b1..21c1c8e 100644 --- a/init.lua +++ b/init.lua @@ -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")