Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local classes={}
- --Objects/proxies
- local function functionToTable(tab)
- local mt={}
- mt.__call=
- function(a,...)
- local prevSelf=self
- self=tab
- local src=a.func
- local ret={pcall(src,...)}
- if not ret[1] then
- assert(pcall(src))
- end
- table.remove(ret,1)
- self=prevSelf
- return unpack(ret)
- end
- for i,v in pairs(tab) do
- if type(v)=="function" then
- local oldV=v
- tab[i]={func=oldV}
- setmetatable(tab[i],mt)
- end
- end
- end
- local function createProxy(o,omt)
- local proxy=newproxy(true)
- local mt=getmetatable(proxy)
- for i,v in pairs(omt) do
- mt[i]=v
- end
- mt.__index=
- function(tab,ind)
- if not ind:find("__") then
- if o[ind]~=nil then
- return o[ind]
- else
- error("Value not found : "..ind)
- end
- elseif bypassing and ind=="___bypassing" then
- return tab.___self
- else
- error("Private value : "..ind)
- end
- end
- mt.__newindex=
- function(tab,ind,val)
- if not ind:find("_") then
- if o[ind] then
- o[ind]=val
- else
- error("Value not found : "..ind)
- end
- else
- error("Read only or private value : "..ind)
- end
- end
- mt.__metatable=false
- return proxy
- end
- local function newObj(a,...)
- local arg={...}
- local o
- local class
- if arg[1]=="bypassNewFunc" then
- o={}
- class=classes[a] or {}
- else
- o=((classes[a]) and classes[a].__settings.new(...) or {})
- class=classes[a] or {}
- end
- local omt={}
- for i,v in pairs((class.__omt or (classes["base"] or {}))) do
- omt[i]=v
- end
- omt.__index=class
- setmetatable(o,omt)
- for i,v in pairs(class) do
- o[i]=(o[i] or v)
- end
- o.___self=o
- functionToTable(o)
- local proxy=createProxy(o,omt)
- return proxy
- end
- --Classes
- local function newClass(a, ...)
- arg={...}
- b=(type(arg[1])=="table" and arg[1] or arg[2])
- b.extends=classes[(type(arg[2])=="table" and arg[1] or "base")] or {}
- b.className=a
- b.name=a.."Obj"
- local mt=(b.__mt or {})
- mt.__tostring=function(a) return "Class "..a.className end
- mt.__call=function(a,...) local arg={...} return newObj(a.className,...) end
- mt.__concat=function(a,b) return tostring(a)..tostring(b) end
- if not b.__mt then
- b.__mt=mt
- end
- setmetatable(b, b.__mt)
- classes[a]=b
- for i,v in pairs(b.extends) do
- if not b[i] then
- b[i]=v
- end
- end
- b.extends=newObj(b.extends.className,"bypassNewFunc")
- _G[a]=b
- end
- local function addClassInheritance(name, b)
- return function(...)
- newClass(name, b, arg[1])
- end
- end
- local function addClass(name)
- return function(...)
- local arg={...}
- if type(arg[1])=="table" then
- newClass(name,...)
- else
- return addClassInheritance(name,arg[1])
- end
- end
- end
- function class(name)
- return addClass(name)
- end
- --load default classes and such
- class "base"
- {
- isA=
- function(arg)
- if self.className==arg then
- return true
- elseif self.className~="base" then
- if self.extends.isA(arg) then
- return true
- end
- else
- return false
- end
- end,
- className="base",
- __settings={new=function(...) return {} end},
- __omt={__tostring=function(a) return "Object "..a.className end,__concat=function(a,b) return tostring(a)..tostring(b) end},
- __mt={__tostring=function(a) return "Class "..a.className end}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement