From 7530df494f39afa99ed33640eeedae75e26d7549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Mart=C3=ADnez?= Date: Wed, 1 Mar 2017 07:47:26 -0300 Subject: [PATCH] Backwards compat code. --- init.lua | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) 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")