diff options
author | Diego MartÃnez <kaeza@users.noreply.github.com> | 2017-03-01 07:47:26 -0300 |
---|---|---|
committer | Auke Kok <sofar+github@foo-projects.org> | 2017-03-14 21:57:01 -0700 |
commit | 7530df494f39afa99ed33640eeedae75e26d7549 (patch) | |
tree | 2838eeb4f47796461293be1581eb954864f72022 | |
parent | 196a6da26c7eaff3a6eab3dabe0ac27f6777681d (diff) |
Backwards compat code.
-rw-r--r-- | init.lua | 38 |
1 files changed, 36 insertions, 2 deletions
@@ -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") |