Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local p="0.1.0"local l,n,s,c=assert,type,rawget,rawset
- local e,d=getmetatable,setmetatable
- local b=string.format
- local o=table.insert
- local a=table.remove
- local g=table.unpack
- local _={}local e
- function _.extend(s,a)if not s or not a then
- error("table can't be nil")end
- local function r(l,t)for e,s in pairs(l)do
- if n(l[e])=="table"and
- n(t[e])=="table"then
- t[e]=r(l[e],t[e])elseif n(l[e])=="table"then
- t[e]=r(l[e],{})else
- t[e]=s
- end
- end
- return t
- end
- return r(s,a)end
- local function f(r,t)t=t or e
- l(n(r)=='string',"ctor name should be string")l(t.is_class,"Class is not is_class")t[r]=t.__ctor__
- return t[r]end
- local function u(r,t)t=t or e
- l(n(r)=='string',"dtor name should be string")l(t.is_class,"Class is not is_class")t[r]=t.__dtor__
- return t[r]end
- local function h(t,...)local r={...}local e=r[1]l(n(e)=='table'or n(e)=='string',"superCall arg not table or string")local i,l,_
- if n(e)=='table'then
- i=a(r,1)l=a(r,1)else
- l=a(r,1)end
- _=r
- local e=t.__dmc_super
- local d=(e~=nil)local c=nil
- local function _(e,l,t)if not e then return end
- local n=nil
- for r,e in ipairs(e)do
- if not t or e==t then
- if s(e,l)then
- n=e
- break
- else
- n=_(e.__parents,l)if n then break end
- end
- end
- end
- return n
- end
- local s,n
- if e==nil then
- t.__dmc_super={}e=t.__dmc_super
- n=_({t.__class},l)o(e,n)end
- s=e[#e]n=_(s.__parents,l,i)if n then
- o(e,n)c=n[l](t,g(r))a(e,#e)end
- if d==false then
- i=nil
- a(e,#e)t.__dmc_super=nil
- end
- return c
- end
- local function i(n,e)e=e or{}l(e.set_isClass~=nil,"initializeObject requires paramter 'set_isClass'")local t=e.set_isClass
- local t=e.data or{}n.__is_class=e.set_isClass
- local e=e.parent
- if e then
- c(n,'__parent_lock',e)if e.__new__ then
- e.__new__(n,table.unpack(t))end
- c(n,'__parent_lock',nil)end
- return n
- end
- local function g(e,r,t)local l,n
- l=s(e,'__setters')or{}n=l[r]if n then
- n(e,t)else
- c(e,r,t)end
- end
- local function r(l,t)local n,e
- n=s(l,'__getters')or{}if n[t]then return n[t](l)end
- e=s(l,t)if e~=nil then return e end
- n=s(l,'__parent_lock')if n then
- if n then e=n[t]end
- if e~=nil then return e end
- else
- local n=s(l,'__parents')for l,n in ipairs(n)do
- if n[t]~=nil then
- e=n[t]break
- end
- end
- if e~=nil then return e end
- end
- return nil
- end
- local function a(n,e)e=e or{}e.object=e.object or{}e.set_isClass=e.set_isClass==true and true or false
- local e=e.object
- local t=tostring(e)local t={__index=r,__newindex=g,__tostring=function(e)return e:__tostring__(t)end,__call=function(e,...)return e:__ctor__(...)end}d(e,t)e.__parents=n
- e.__is_dmc=true
- e.__setters={}e.__getters={}for t=#n,1,-1 do
- local n=n[t]if n.__getters then
- e.__getters=_.extend(n.__getters,e.__getters)end
- if n.__setters then
- e.__setters=_.extend(n.__setters,e.__setters)end
- end
- return e
- end
- local function s(t,r)t=t or{}r=r or{}r.set_isClass=true
- r.name=r.name or"<unnamed class>"l(n(t)=='table',"first parameter should be nil, a Class, or a list of Classes")if t.is_class==true then
- t={t}elseif e and#t==0 then
- o(t,e)end
- local e=a(t,{})i(e,r)e.__class=e
- e.__name=r.name
- return e
- end
- local function _(n,t,l)n=n==nil and n or{n}return s(n,t)end
- e=s(nil,{name="Class Class"})function e:__ctor__(...)local n={data={...},set_isClass=false,parent=self.__class}local e=a({self.__class},n)i(e,n)return e
- end
- function e:__dtor__()self:__destroy__()end
- function e:__new__(...)return self
- end
- function e:__tostring__(e)return b("%s (%s)",self.NAME,e)end
- function e:__destroy__()end
- function e.__getters:NAME()return self.__name
- end
- function e.__getters:class()return self.__class
- end
- function e.__getters:supers()return self.__parents
- end
- function e.__getters:is_class()return self.__is_class
- end
- function e.__getters:is_intermediate()return self.__is_class
- end
- function e.__getters:is_instance()return not self.__is_class
- end
- function e.__getters:version()return self.__version
- end
- function e:isa(t)local e=false
- local n=self.class
- if n==t then
- e=true
- else
- local n=self.__parents
- for l=1,#n do
- local n=n[l]if n.isa then
- e=n:isa(t)end
- if e==true then break end
- end
- end
- return e
- end
- function e:optimize()local function l(t,e)if not e or#e==0 then return end
- for r=#e,1,-1 do
- local e=e[r]l(t,e.__parents)for l,e in pairs(e)do
- if n(e)=='function'then
- t[l]=e
- end
- end
- end
- end
- l(self,{self.__class})end
- function e:deoptimize()for e,t in pairs(self)do
- if n(t)=='function'then
- self[e]=nil
- end
- end
- end
- f('new',e)u('destroy',e)e.superCall=h
- return d({__version=p,__superCall=h,setNewClassGlobal=makeNewClassGlobal,registerCtorName=f,registerDtorName=u,inheritsFrom=_,newClass=s,Class=e},{__call=function(e,...)return s(...)end})
Advertisement
Add Comment
Please, Sign In to add comment