Advertisement
tahg

system.lua

Aug 21st, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. local super_t = {}
  2. local stack = {}
  3.  
  4. local super_mt = {
  5.   __index = function(obj, field)
  6.     local cls = stack[#stack].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. local stack_mt = {
  17.   __index = function(obj, field)
  18.     if #stack > 0 then
  19.       return stack[#stack][field]
  20.     else return _G[field]
  21.     end
  22.   end,
  23.   __newindex = function(obj, field, value)
  24.     if #stack > 0 then stack[#stack][field] = value
  25.     else _G[field] = value
  26.     end
  27.   end
  28. }
  29.  
  30. local Object_idx = function(obj, field)
  31.   local cls = obj.class
  32.   local f
  33.   if field == "super" then
  34.     return stack[#stack].super
  35.   elseif field == "this" then
  36.     return stack[#stack]
  37.   end
  38.   if obj == super_t then cls = cls.super end
  39.   if obj == _ENV and cls.private[field] then f = cls.private[field]
  40.   else
  41.     while cls do
  42.       if cls.public[field] then
  43.         f = cls.public[field]
  44.         break
  45.       elseif obj == _ENV and cls.protected[field] then
  46.         f = cls.protected[field]
  47.         break
  48.       end
  49.       cls = cls.super
  50.     end
  51.   end
  52.   if f == nil then return _G[field] end
  53.   if type(f) == "function" then
  54.     return function(...)
  55.       table.insert(stack, obj)
  56.       local ret = table.pack(f(...))
  57.       table.remove(stack)
  58.       return table.unpack(ret, 1, ret.n)
  59.     end
  60.   end
  61.   return f
  62. end
  63.  
  64. setmetatable(super_t, super_mt)
  65.  
  66. local function create(t,e)
  67.   if t then
  68.     local f,err = load("return"..t,nil,nil,e)
  69.     if f then return f()
  70.     else print(err)
  71.     end
  72.   end
  73.   return {}
  74. end
  75.  
  76. local classes = {}
  77. --local setmetatable = setmetatable
  78. function class(name)
  79.   local super
  80.   local e  = {}
  81.   setmetatable(e, stack_mt)
  82.   local function func(o)
  83.     if type(o) == "string" then
  84.       super = o
  85.     elseif type(o) == "table" then
  86.       o.super = super and classes[super] or classes.Object
  87.       o.public = create(o.public, e)
  88.       o.private = create(o.private, e)
  89.       o.protected = create(o.protected, e)
  90.       classes[name] = o
  91.       _G[name] = o
  92.     end
  93.     return func
  94.   end
  95.   return func
  96. end
  97. _G.class = class
  98.  
  99. class "Object" {
  100. public = [[{
  101. __init = function(...)
  102.   local chain = {}
  103.   local cls = class
  104.   while cls do
  105.     table.insert(chain, cls)
  106.     cls = cls.super
  107.   end
  108.   while #chain > 0 do
  109.     cls = chain[#chain]
  110.     if cls.init then cls.init(...) end
  111.     table.remove(chain)
  112.   end
  113. end,
  114.  
  115. instanceof = function(c)
  116.   if type(c) == "string" then c = classes[c] end
  117.   if c then
  118.     local cls = class
  119.     while cls do
  120.       if cls == c then return true end
  121.     end
  122.   end
  123.   return false
  124. end
  125. }]]
  126. }
  127.  
  128. function new(class)
  129.   local cls = {}
  130.   cls.class = classes[class] or classes.Object
  131.   cls.super = super_t
  132.   local cls_mt = {__index = Object_idx, __call = function(...)
  133.     cls.__init(...)
  134.     return cls
  135.   end}
  136.   setmetatable(cls, cls_mt)
  137.   return cls
  138. end
  139. _G.new = new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement