Advertisement
LuaWeaver

wOOP 0.4.2

Nov 8th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. wO={}
  2. wO.global={}
  3. wO.global.clone=function(self) local cloneObj={} for i,v in pairs(self) do cloneObj[i]=v end return cloneObj end
  4. wO.global.isA=function(self, arg)
  5.     if self.className==arg then
  6.         return true
  7.     elseif self.className~="base" then
  8.         if self.extends:isA(arg) then
  9.             return true
  10.         end
  11.     else
  12.         return false
  13.     end
  14. end
  15. wO.global.className="base"
  16. wO.global.__settings={new=function(...) return {} end}
  17. wO.global.omt={}
  18.  
  19. setmetatable(wO.global, {__tostring=function(a) return "Class "..a.className end})
  20.  
  21. wO.classes={base=wO.global}
  22.  
  23. local function newClass(a, ...)
  24.     arg={...}
  25.     b=(type(arg[1])=="table" and arg[1] or arg[2])
  26.     b.extends=(type(arg[2])=="table" and wO.classes[arg[1]] or wO.classes["base"])
  27.     b.className=a
  28.     b.name=a.."Obj"
  29.     local mt={}
  30.     mt.__index=b.extends
  31.     mt.__tostring=function(a) return "Class "..a.className end
  32.     mt.__call=function(a,...) local arg={...} return newObj(a.className,...) end
  33.     if not b.mt then
  34.         b.mt=mt
  35.     else
  36.         for i,v in pairs(mt) do
  37.             table.insert(b.mt)
  38.         end
  39.     end
  40.     setmetatable(b, b.mt)
  41.     wO.classes[a]=b
  42. end
  43.  
  44. local function newObj(a,...)
  45.     local o=(wO.classes[a].__settings.new(...))
  46.     local omt={}
  47.     for i,v in pairs(wO.classes[a].omt) do
  48.         omt[i]=v
  49.     end
  50.     omt.__index=wO.classes[a]
  51.     setmetatable(o, omt)
  52.     return o
  53. end
  54.  
  55. local function addClassInheritance(name, b)
  56.     return function(...)
  57.         newClass(name, b, arg[1])
  58.     end
  59. end
  60.  
  61. local function addClass(name)
  62.     return function(...)
  63.         local arg={...}
  64.         if type(arg[1])=="table" then
  65.             newClass(name,...)
  66.         else
  67.             return addClassInheritance(name,arg[1])
  68.         end
  69.     end
  70. end
  71.  
  72. setmetatable(_G,{__index=function(_,a) return (a=="class" and addClass or (a==("newObj" or "new") and newObj or (wO.classes[a] or nil))) end}) --Ugly piece of code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement