Advertisement
Guest User

Lua Meta API

a guest
Mar 11th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.92 KB | None | 0 0
  1. function get(object)
  2.     return getmetatable(object or {}) or {};
  3. end;
  4.  
  5. function set(object, metatable)
  6.     return setmetatable(object or {}, metatable or {});
  7. end;
  8.  
  9. function mod(object, metatable, override)
  10.     local omt = get(object);
  11.     for mk, mv in pairs(omt) do
  12.         if override then
  13.             omt[mk] = mv; else
  14.             omt[mk] = omt[mk] or mv; end;
  15.     end; return set(object, omt);
  16. end;
  17.  
  18. function clear(object)
  19.     return set(object);
  20. end;
  21.  
  22. function index(object, ...)
  23.     local index = get(object).index or {};
  24.     for ik, iv in ipairs({...}) do
  25.         index[ik] = iv;
  26.     end;
  27.    
  28.     local omt = {
  29.         __index = function(object, key)
  30.             local omt = get(object);
  31.             for _, i in ipairs(omt.index or {}) do
  32.                 if i[key] then return i[key] end
  33.             end;
  34.         end;
  35.     };
  36.    
  37.     omt.index = index;
  38.     return mod(object, omt);
  39. end;
  40.  
  41. function call(object, call)
  42.     return mod(object, {
  43.         __call = function(self, ...)
  44.             return call(self, ...);
  45.         end;
  46.     });
  47. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement