Advertisement
Guest User

system.lua

a guest
Mar 3rd, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1. local _G = _G
  2.  
  3. local super_t = {}
  4. local super_mt = {
  5.   __index = function(obj, field)
  6.     local cls = class.super
  7.     while(cls) do
  8.       if cls.public[field] then return cls.public[field] end
  9.       if cls.protected[field] then return cls.protected[field] end
  10.       cls = cls.super
  11.     end
  12.     return nil
  13.   end
  14. }
  15. setmetatable(super_t, super_mt)
  16.  
  17. local Object_mt = {
  18.   __index = function(obj, field)
  19. print(obj, field)
  20.     local cls = obj.class
  21.     local f
  22.     if obj == _ENV and cls.private[field] then f = cls.private[field]
  23.     else
  24.       while cls do
  25.         if cls.public[field] then
  26.           f = cls.public[field]
  27.           break
  28.         elseif obj == _ENV and cls.protected[field] then
  29.           f = cls.protected[field]
  30.           break
  31.         end
  32.         cls = cls.super
  33.       end
  34.     end
  35. print(f)
  36.     if f == nil then return _G[field] end
  37. print(f)
  38.     if type(f) == "function" then
  39.       return function(...)
  40.         local olde = _ENV
  41. print(_ENV)
  42. print(obj)
  43.         _ENV = obj
  44. print(_ENV)
  45.         local ret = f(...)
  46.         _ENV = olde
  47.         return ret
  48.       end
  49.     end
  50.     return f
  51.   end
  52. }
  53. local Object = {}
  54.  
  55. local classes = {}
  56.  
  57.  
  58.  
  59. function class(name)
  60.   local super
  61.   local function func(o)
  62.     if type(o) == "string" then
  63.       super = o
  64.     elseif type(o) == "table" then
  65.       classes[name] = o
  66.       setmetatable(o, {__index = super and classes[super] or Object})
  67.       o.public = o.public or {}
  68.       o.private = o.private or {}
  69.       o.protected = o.protected or {}
  70.     end
  71.     return func
  72.   end
  73.   return func
  74. end
  75. _G.class = class
  76.  
  77. function new(class)
  78.   local cls = {}
  79.   cls.class = classes[class] or Object
  80.   cls.super = super_t
  81.   cls._G = _G
  82.   setmetatable(cls, Object_mt)
  83.   return cls
  84. end
  85. _G.new = new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement