Advertisement
LuaWeaver

wOOP 0.5.3

Nov 8th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local classes={}
  2.  
  3. --Objects/proxies
  4.  
  5. local function functionToTable(tab)
  6.     local mt={}
  7.     mt.__call=
  8.     function(a,...)
  9.         local prevSelf=self
  10.         self=tab
  11.         local src=a.func
  12.         local ret={pcall(src,...)}
  13.         if not ret[1] then
  14.             assert(pcall(src))
  15.         end
  16.         table.remove(ret,1)
  17.         self=prevSelf
  18.         return unpack(ret)
  19.     end
  20.     for i,v in pairs(tab) do
  21.         if type(v)=="function" then
  22.             local oldV=v
  23.             tab[i]={func=oldV}
  24.             setmetatable(tab[i],mt)
  25.         end
  26.     end
  27. end
  28.  
  29. local function createProxy(o,omt)
  30.     local proxy=newproxy(true)
  31.     local mt=getmetatable(proxy)
  32.  
  33.     for i,v in pairs(omt) do
  34.         mt[i]=v
  35.     end
  36.    
  37.     mt.__index=
  38.     function(tab,ind)
  39.         if not ind:find("__") then
  40.             if o[ind]~=nil then
  41.                 return o[ind]
  42.             else
  43.                 error("Value not found : "..ind)
  44.             end
  45.         elseif bypassing and ind=="___bypassing" then
  46.             return tab.___self
  47.         else
  48.             error("Private value : "..ind)
  49.         end
  50.     end
  51.  
  52.     mt.__newindex=
  53.     function(tab,ind,val)
  54.         if not ind:find("_") then
  55.             if o[ind] then
  56.                 o[ind]=val
  57.             else
  58.                 error("Value not found : "..ind)
  59.             end
  60.         else
  61.             error("Read only or private value : "..ind)
  62.         end
  63.     end
  64.    
  65.     mt.__metatable=false
  66.     return proxy
  67. end
  68.  
  69. local function newObj(a,...)
  70.     local arg={...}
  71.     local o
  72.     local class
  73.     if arg[1]=="bypassNewFunc" then
  74.         o={}
  75.         class=classes[a] or {}
  76.     else
  77.         o=((classes[a]) and classes[a].__settings.new(...) or {})
  78.         class=classes[a] or {}
  79.     end
  80.     local omt={}
  81.     for i,v in pairs((class.__omt or (classes["base"] or {}))) do
  82.         omt[i]=v
  83.     end
  84.     omt.__index=class
  85.     setmetatable(o,omt)
  86.     for i,v in pairs(class) do
  87.         o[i]=(o[i] or v)
  88.     end
  89.     o.___self=o
  90.     functionToTable(o)
  91.     local proxy=createProxy(o,omt)
  92.     return proxy
  93. end
  94.  
  95. --Classes
  96.  
  97. local function newClass(a, ...)
  98.     arg={...}
  99.     b=(type(arg[1])=="table" and arg[1] or arg[2])
  100.     b.extends=classes[(type(arg[2])=="table" and arg[1] or "base")] or {}
  101.     b.className=a
  102.     b.name=a.."Obj"
  103.     local mt=(b.__mt or {})
  104.     mt.__tostring=function(a) return "Class "..a.className end
  105.     mt.__call=function(a,...) local arg={...} return newObj(a.className,...) end
  106.     mt.__concat=function(a,b) return tostring(a)..tostring(b) end
  107.     if not b.__mt then
  108.         b.__mt=mt
  109.     end
  110.     setmetatable(b, b.__mt)
  111.     classes[a]=b
  112.     for i,v in pairs(b.extends) do
  113.         if not b[i] then
  114.             b[i]=v
  115.         end
  116.     end
  117.     b.extends=newObj(b.extends.className,"bypassNewFunc")
  118.     _G[a]=b
  119. end
  120.  
  121. local function addClassInheritance(name, b)
  122.     return function(...)
  123.         newClass(name, b, arg[1])
  124.     end
  125. end
  126.  
  127. local function addClass(name)
  128.     return function(...)
  129.         local arg={...}
  130.         if type(arg[1])=="table" then
  131.             newClass(name,...)
  132.         else
  133.             return addClassInheritance(name,arg[1])
  134.         end
  135.     end
  136. end
  137.  
  138. function class(name)
  139.     return addClass(name)
  140. end
  141.  
  142. --load default classes and such
  143.  
  144. class "base"
  145. {
  146.     isA=
  147.     function(arg)
  148.         if self.className==arg then
  149.             return true
  150.         elseif self.className~="base" then
  151.             if self.extends.isA(arg) then
  152.                 return true
  153.             end
  154.         else
  155.             return false
  156.         end
  157.     end,
  158.    
  159.     className="base",
  160.     __settings={new=function(...) return {} end},
  161.     __omt={__tostring=function(a) return "Object "..a.className end,__concat=function(a,b) return tostring(a)..tostring(b) end},
  162.     __mt={__tostring=function(a) return "Class "..a.className end}
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement