Kouksi44

Classic - pretty OOP

Mar 25th, 2016
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.23 KB | None | 0 0
  1.  
  2.  
  3. -- Classic by Kouksi44 : Class framework for Lua / CC
  4.  
  5.  
  6. local Class={}
  7. local classes={}
  8. local interfaces={}
  9. local _class;
  10. local Object={};
  11. local ObjectProxy={}
  12. local Property={}
  13. local SecurityContext={}
  14. local Utils={}
  15.  
  16. Utils.luaTypes={
  17.   ["method"]=true;
  18.   ["String"]=true;
  19.   ["Table"]=true;
  20.   ["number"]=true;
  21.   ["thread"]=true;
  22.   ["boolean"]=true;
  23. }
  24.  
  25. function Utils.isTypeValue(obj)
  26.   for k,v in pairs(obj.modifiers) do
  27.     if v == "Table" or v== "String" then
  28.       return "LuaType", string.lower(v)
  29.     end
  30.     if v == "method" then
  31.       return "LuaType","function"
  32.     end
  33.     if Utils.luaTypes[v] then
  34.       return "LuaType" , v
  35.     elseif classes[v] then
  36.       return "ClassType" , v
  37.     end
  38.   end
  39.   return false
  40. end
  41.  
  42. function Utils.isClassType(value)
  43.   if value.isInnerProxy or value.isOuterProxy then
  44.     if value.getClass then
  45.       return true
  46.     end
  47.   end
  48.   return false
  49. end
  50.  
  51. function Utils.getClassType(value)
  52.    if value.isInnerProxy or value.isOuterProxy then
  53.     if value.getClass then
  54.       return value:getClass().name
  55.     end
  56.   end
  57.   return "Unknown ClassType"
  58. end
  59.  
  60. function Utils.getValueType(value)
  61.   if Utils.isClassType(value) then
  62.     return Utils.getClassType(value)
  63.   end
  64.   if type(value) == "table" or type(value) == "string" then
  65.     return type(value)
  66.   end
  67.   if Utils.luaTypes[type(value)] then
  68.     return type(value)
  69.   end
  70.   error("[ValueTypeError] Unknown TypeValue (no ClassValue or LuaType)")
  71. end
  72.  
  73. function Utils.hasType(value,_type)
  74.   if type(value)=="nil" then
  75.     return true
  76.   end
  77.   if classes[_type] and Utils.isClassType(value) then
  78.     return (Utils.getClassType(value) == _type) or value:getClass():subclassOf(_type)
  79.   end
  80.   return type(value) == _type
  81. end
  82.  
  83. function Utils.removeModifier(mod,_type)
  84.   for k,v in pairs(mod.modifiers) do
  85.     if v == _type then
  86.       table.remove(mod.modifiers,k)
  87.     end
  88.   end
  89. end
  90.  
  91. function Utils.checkObjArguments(args)
  92.   for k,param in pairs(args) do
  93.     if typeof(param) == "instance" and args.isOuterProxy ~= true then
  94.       args[k] = param.invokeMethod("getProxy","outer")
  95.     end
  96.   end
  97.   return args
  98. end
  99.      
  100.      
  101.  
  102. function createAbstractMethod(className,methodName)
  103.   local abstractObj = {
  104.     classname = className;
  105.     methodname = methodName;
  106.     __type = "AbstractMethod";
  107.   }
  108.   return abstractObj
  109. end
  110.      
  111.  
  112. local mods={
  113.   ["public"] = true;
  114.   ["private"] = true;
  115.   ["final"] = true;
  116.   ["shared"] = true;
  117.   ["property"] = true;
  118.   ["abstract"] = true;
  119.   --["protected"]=true;
  120. }
  121.  
  122. local chainModifiers=function(left,right)
  123.   if(type(left)=="table" and left._type=="modifier") and (type(right)=="table" and right._type=="modifier") then
  124.     for _,v in pairs(left.additional) do
  125.       table.insert(right.additional,v)
  126.     end
  127.     return right
  128.   elseif (type(left)=="table" and left.type=="class") or (type(right)=="table" and right.type=="class") then
  129.     local name = ((type(left)=="table" and left.type=="class") and left.name) or right.name
  130.     local rMod={}
  131.     if (type(left)=="table" and left.type=="class") then
  132.       if not (type(right)=="table" and right._type=="modifier") then
  133.         local mVal={
  134.           modifiers={};
  135.           value=right;
  136.           _type="modified";
  137.         }
  138.         for _,v in pairs(rawget(left,"additional")) do
  139.           table.insert(mVal.modifiers,v)
  140.         end
  141.         return mVal
  142.       end
  143.       for _,v in pairs(rawget(left,"additional")) do
  144.         table.insert(right.additional,v)
  145.       end
  146.       return right
  147.     else
  148.       for _,v in pairs(left.additional) do
  149.         table.insert(rawget(right,"additional"),v)
  150.       end
  151.       return right
  152.     end
  153.   elseif (type(left)=="table" and left._type=="modifier") and not (type(right)=="table" and (right._type=="modifier" or right.type=="class")) then
  154.     local mVal={ -- value object with one or several modifiers ( e.g. public or static)
  155.       modifiers={};
  156.       value=right;
  157.       _type="modified"
  158.     }
  159.     for _,v in pairs(left.additional) do
  160.       table.insert(mVal.modifiers,v)
  161.     end
  162.     return mVal
  163.   end
  164. end
  165.  
  166. local Modifier=function(_type)
  167.   if not mods[_type] and not (type(_type) == "table" and _type._type == "ValuedType") then
  168.     error("Unknown modifier",2)
  169.   end
  170.   local mod={
  171.     _type="modifier";
  172.     additional={[1]=(type(_type) == "table" and _type.valueType) or _type} ;
  173.   }
  174.   return setmetatable(mod,{__sub=chainModifiers})
  175. end
  176.  
  177. local ValueType = function(name)
  178.   local obj = {}
  179.   obj.valueType=name
  180.   obj._type="ValuedType"
  181.   return obj
  182. end
  183.  
  184. _G.public = Modifier("public")
  185. _G.private = Modifier("private")
  186. _G.final = Modifier("final")
  187. _G.shared = Modifier("shared")
  188. _G.property = Modifier("property")
  189. _G.abstract = Modifier("abstract")
  190.  
  191.  
  192. for k,v in pairs(Utils.luaTypes) do
  193.   _G[k]=Modifier(ValueType(k))
  194. end
  195.  
  196.  
  197. local function len(tbl)
  198.  local c=0;
  199.  for _ in pairs(tbl) do
  200.      c=c+1
  201.  end
  202.  return c
  203. end
  204.  
  205. local function unique(tbl)
  206.  local t={}
  207.  for k,v in pairs(tbl) do
  208.      if type(v)=="table" then
  209.      if typeof(v) == "instance" then
  210.        t[k] = v.invokeMethod("clone")
  211.          elseif len(v)==0 then
  212.              t[k]={}
  213.          else
  214.              t[k]=unique(v)
  215.          end
  216.      else
  217.          t[k]=v
  218.      end
  219.  end
  220.  return t
  221. end
  222.  
  223. local function createObjectProxy(t,class,inner)
  224.     local object=Object(class)
  225.   local super=nil
  226.   local proxy={isOuterProxy=true}
  227.   local innerProxy={isOuterProxy=false}
  228.     if class.inherits then
  229.     if class.super:hasInit() then
  230.         super=class.super:instance(true,true)
  231.     else
  232.       super=class.super:instance(false,true)
  233.     end
  234.    
  235.     setmetatable(innerProxy,{__index=function(t,k) if k=="super" then return super end local level = object:getContext():getAccessLevel() if object:getValue(k,level,false)==nil then super.securityContext:setAccessLevel(level+1)   return super[k] end if type(object:getValue(k,level,false)) == "function" then return function(s,...) local args = {...} if (typeof(s) == "instance" and s.hashValue()==object.raw.hashValue()) then args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) else if (typeof(s) == "instance" and s.getClass():subclassOf(object.raw.getClass().name)) then args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) else table.insert(args,1,s) args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) end end end else return object:getValue(k,level) end end;
  236.                                                      __newindex=function(t,k,v) local level = object:getContext():getAccessLevel() super.securityContext:setAccessLevel(level+1) if super[k] and not object:getValue(k,level,false) then super.securityContext:setAccessLevel(level+1) super[k] = v else object:setValue(k,v,level) end  end; } )
  237.    
  238.    
  239.         setmetatable(proxy,{__index=function(t,k)   if k=="super" then return super end  local level = object:getContext():getAccessLevel()+1 if object:getValue(k,level,false)==nil then  super.securityContext:setAccessLevel(level+1) return super[k] end if type(object:getValue(k,level,false))=="function" then  return function(s,...) local args = {...} if (typeof(s) == "instance" and s.hashValue()==object.raw.hashValue()) then args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) else if (typeof(s) == "instance" and s.getClass():subclassOf(object.raw.getClass().name)) then args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) else table.insert(args,1,s) args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) end end end else return object:getValue(k,level) end end;
  240.                                                 __newindex=function(t,k,v) local level = object:getContext():getAccessLevel()+1 super.securityContext:setAccessLevel(level+1) if super[k] and not object:getValue(k,level,false) then super.securityContext:setAccessLevel(level+1) super[k] = v else object:setValue(k,v,level) end end; } )
  241.     else
  242.     setmetatable(innerProxy,{__index=function(t,k)   local level=object:getContext():getAccessLevel() if type(object:getValue(k,level,false)) == "function" then return function(s,...) local args = {...} if (typeof(s) == "instance" and s.hashValue()==object.raw.hashValue()) then args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) else if (typeof(s) == "instance" and s.getClass():subclassOf(object.raw.getClass().name)) then args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) else table.insert(args,1,s) args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) end end end else return object:getValue(k,level) end end;
  243.                                                      __newindex=function(t,k,v)  object:setValue(k,v,0)  end; } )
  244.    
  245.    
  246.         setmetatable(proxy,{__index=function(t,k)   local level=object:getContext():getAccessLevel()+1 if type(object:getValue(k,level,false))=="function" then  return function(s,...) local args = {...} if (typeof(s) == "instance" and s.hashValue()==object.raw.hashValue()) then args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) else if (typeof(s) == "instance" and s.getClass():subclassOf(object.raw.getClass().name)) then args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) else table.insert(args,1,s) args = Utils.checkObjArguments(args) return object:getValue(k,level)(innerProxy,unpack(args)) end end end else return object:getValue(k,level) end end;
  247.                                                 __newindex=function(t,k,v)  object:setValue(k,v,1) end; } )
  248.     end
  249.  
  250.   object:setInner(innerProxy)
  251.   object:setOuter(proxy)
  252.  
  253.     return ((inner and innerProxy) or proxy), innerProxy
  254. end
  255.  
  256. function typeof(object)
  257.     if type(object)=="table" then
  258.         if type(object.getClass) == "function" then
  259.       return "instance"
  260.     end
  261.   end
  262.   return type(object)
  263. end
  264.  
  265. function using(path,env)
  266.   local path = path:gsub("%.","/")
  267.     if not fs.exists(path) then
  268.     if not fs.exists(path..".lua") then
  269.       error("Can`t import class from: "..path..". Unknown classpath.",2)
  270.     else
  271.       path=path..".lua"
  272.       local f = io.open(path,"r")
  273.       local c = f:read()
  274.       local func = setfenv(loadstring(c),env)
  275.       f:close()
  276.       local okay,err = pcall(func)
  277.       if not okay then
  278.         error("Failed to load class from: "..path.." . An error occured while loading the class.",2)
  279.       end
  280.     end
  281.   else
  282.     local f = io.open(path,"r")
  283.     local c = f:read()
  284.     local func = setfenv(loadstring(c),env)
  285.     f:close()
  286.     local okay,err = pcall(func)
  287.     if not okay then
  288.       error("Failed to load class from: "..path..". An error occured while loading the class.",2)
  289.     end
  290.   end
  291. end
  292.  
  293. function Class:getField(field)
  294.     return type(self.raw[field])~="function" and self.raw[field]
  295. end
  296.  
  297. function Class:getSuper()
  298.     return self.super:newProxy()
  299. end
  300.  
  301. function Class:subclassOf(super)
  302.     if self.inherits then
  303.         if self.super.name==super then
  304.             return true
  305.         else
  306.             return self.super:subclassOf(super)
  307.         end
  308.     end
  309.     return false
  310. end
  311.  
  312. function Class:superOf(sub)
  313.     return classes[sub]:subclassOf(self.name)
  314. end
  315.  
  316. function Class:hashValue()
  317.   return tostring(self.raw):sub(8)
  318. end
  319.  
  320.  
  321.  
  322. function Class.new(name)
  323.     local obj={}
  324.     obj.type="class"
  325.     obj.name=name
  326.     obj.constructed=false
  327.     obj.inherits=false
  328.     obj.super=nil
  329.     obj.raw={}
  330.     obj.properties={}
  331.   obj.interfaces={}
  332.   obj.modifiedMembers={}
  333.   obj.valueTypes={}
  334.   obj.additional={[1]=name}
  335.   obj.abstractMethods = {}
  336.     setmetatable(obj,{__index=Class,__newindex=obj.raw,__call=function(self,...) return obj:instance((obj.raw.init~=nil),false,...) end,__sub=chainModifiers})
  337.     if not classes[name] then
  338.         classes[name]=obj
  339.         _G[obj.name]=obj
  340.         _class=classes[name]
  341.     end
  342.     return function(body)
  343.         obj:construct(body)
  344.     end
  345. end
  346.  
  347. function Class:extend(name)
  348.     if not classes[name] then
  349.         error("Super class not found",3)
  350.     end
  351.     rawset(self,"inherits",true)
  352.     rawset(self,"super",classes[name])
  353.     return function(body)
  354.         self:construct(body)
  355.     end
  356. end
  357.  
  358. function Class:instance(hasInit,isInherit,...)
  359.   if hasInit then
  360.     if not self.raw.init then
  361.         error("Cant´t load class without init",3)
  362.     end
  363.     local args = { n = select("#",...),...}
  364.     local proxy,innerProxy=ObjectProxy(self,isInherit)
  365.     proxy:init(unpack(args,1,args.n))
  366.     return proxy
  367.   else
  368.     return ObjectProxy(self,isInherit)
  369.   end
  370. end
  371.  
  372. function Class:checkMembers()
  373.   for k,v in pairs(self.raw) do
  374.     if type(v)=="table" and v._type=="modified" then
  375.       local isAbstract = false
  376.       for k,v in pairs(v.modifiers) do
  377.         if v == "abstract" then
  378.           isAbstract = true
  379.         end
  380.       end
  381.       if isAbstract then
  382.         self.raw[k] = createAbstractMethod(self.name,k)
  383.         self.abstractMethods[k] = true
  384.       else
  385.         self.modifiedMembers[k] = v
  386.       end
  387.     end
  388.   end
  389. end
  390.  
  391. function Class:getModifiedMembers()
  392.   return self.modifiedMembers
  393. end
  394.  
  395. function Class:getShared(index)
  396.   return self.raw[index].value
  397. end
  398.  
  399. function Class:setShared(index,value)
  400.   self.raw[index].value=value
  401. end
  402.  
  403. function Class:checkTypeValues()
  404.   for k,v in pairs(self.raw) do
  405.     if type(v) == "table" and v._type == "modified" then
  406.       local isTypeVal, _type = Utils.isTypeValue(v)
  407.       if isTypeVal then
  408.         self.valueTypes[k] = {valueType=_type}
  409.         if not Utils.hasType(v.value,_type) then
  410.           error("[ValueTypeError] Attempt to set member: "..k.." with wrong type: ".. _type,4)
  411.         end
  412.       end
  413.     end
  414.   end
  415. end
  416.  
  417. function Class:construct(body)
  418.     for k,v in pairs(body) do
  419.           self.raw[k]=v
  420.   end
  421.   self:checkTypeValues()
  422.   self:checkMembers()
  423. end
  424.  
  425. function Class:newProxy()
  426.     return setmetatable({},{__index=self,__newindex=function() error("Attempt to modify class",3) end})
  427. end
  428.  
  429. function Class:implement(interface)
  430.     if not interfaces[interface] then
  431.         error("Interface does not exist",3)
  432.     end
  433.         self.raw=unique(interfaces[interface])
  434.     getmetatable(self).__newindex=self.raw
  435.     self.interfaces[interface]=interfaces[interface]
  436.     return function(body)
  437.         self:construct(body)
  438.     end
  439. end
  440.  
  441. function Class:hasInit()
  442.   return type(self.raw.init)=="function"
  443. end
  444.  
  445. local function extends(name)
  446.     return _class:extend(name)
  447. end
  448.  
  449. local function implements(name)
  450.     return _class:implement(name)
  451. end
  452.  
  453. local function interface(name)
  454.     if interfaces[name] then
  455.         error("Interface already exists")
  456.     end
  457.     interfaces[name]={}
  458.     return function(body)
  459.         interfaces[name]=body
  460.     end
  461. end
  462.  
  463.  
  464.  
  465. function Object.new(class)
  466.     local obj={}
  467.   obj.proxy = {}
  468.   obj.outerProxy = {}
  469.     obj.type="object"
  470.     obj.raw=unique(class.raw)
  471.   obj.modified=class:getModifiedMembers()
  472.     obj.raw.getClass=function(...) return class:newProxy() end
  473.   obj.raw.hashValue = function(...) return tostring(obj.raw):sub(8) end
  474.   obj.raw.invokeMethod = setmetatable({},{__call = function(_,method,...) if not type(Object[method]) == "function" then error("Invalid method invokation.") end return Object[method](obj,...) end})
  475.   obj.__accessTypeGet="GET_VALUE"
  476.   obj.__accessTypeSet="SET_VALUE"
  477.   obj.securityContext=SecurityContext()
  478.   obj.raw.securityContext=obj.securityContext
  479.   obj.typedValues = class.valueTypes
  480.   obj.propertyFlags = {}
  481.   setmetatable(obj,{__index=Object})
  482.   Object.prepareMembers(obj)
  483.     return obj
  484. end
  485.  
  486. function Object:prepareMembers()
  487.   for k,v in pairs(self.modified) do
  488.     rawset(self.raw,k,v.value)
  489.   end
  490. end
  491.  
  492. -- function Object:checkForAbstractDefinitions()
  493. --   for k,v in pairs(self.abstracts) do
  494. --     if type(self.raw[k]) ~= "function" then
  495. --       if self.raw[k].classname ~= self.raw.getClass().name then
  496. --         error("Abstract method:"..k.." must be overriden in any extended classes.",2)
  497. --       end
  498. --     else
  499. --       self.abstracts[k] = false
  500. --     end
  501. --   end
  502. --
  503. --   if self.raw.getClass().inherits then
  504. --     for k,v in pairs
  505. -- end
  506.  
  507. function Object:clone()
  508.   local nObj = self.raw.getClass():instance((self.raw.init~=nil),false)
  509.   for k,v in pairs(self.raw) do
  510.     if not k == "getClass" or k == "hashValue" or k == "invokeMethod" then
  511.       if self:isModifiedMember(k,"shared") then end
  512.       if not self:isModifiedMember(l,"final") then
  513.         nObj.invokeMethod("setValue",k,v,0)
  514.       end
  515.     end
  516.   end
  517.   return nObj
  518. end
  519.  
  520. function Object:getContext()
  521.   return self.securityContext
  522. end
  523.  
  524. function Object:getValueType(index)
  525.   return self.typedValues[index].valueType
  526. end
  527.  
  528. function Object:isTypedValue(index)
  529.   if self.typedValues[index] then
  530.     return true
  531.   end
  532.   return false
  533. end
  534.  
  535. function Object:getValue(index,accessLevel,cProp)
  536.   --print("Getting value with level: ".. (accessLevel or " "))
  537.   local canAccess
  538.   if accessLevel~=0 then
  539.     canAccess = self:checkMemberAccess(index,self.__accessTypeGet)
  540.   else
  541.     canAccess=true
  542.   end
  543.   if canAccess then
  544.     if self:isModifiedMember(index,"shared") then
  545.       return self.raw.getClass():getShared(index)
  546.     else
  547.       if self:isModifiedMember(index,"property") then
  548.                 if cProp == false then
  549.                     return self.raw[index] or nil
  550.                 end
  551.         if self.propertyFlags[index] ~= true  then
  552.           return self:callGetter(index)
  553.         else
  554.           return self.raw[index] or nil
  555.         end
  556.       else
  557.         return self.raw[index] or nil
  558.       end
  559.     end
  560.   end
  561.   return nil
  562. end
  563.  
  564. function Object:isProperty(index)
  565.     if type(self.raw[index])=="table" then
  566.     if self.raw[index]._type=="property" and self.raw[index].get and self.raw[index].set then
  567.       return true
  568.     end
  569.   end
  570.   return false
  571. end
  572.  
  573. function Object:checkMemberAccess(index,accessType)
  574.   if accessType==self.__accessTypeGet then
  575.     if self:isModifiedMember(index,"private") then
  576.       return false
  577.     else
  578.       return true
  579.     end
  580.   elseif accessType==self.__accessTypeSet then
  581.     if self:isModifiedMember(index,"private") then
  582.       error("Attempt to modify private member.",4)
  583.     elseif self:isModifiedMember(index,"final") then
  584.       error("Attempt to modify final member.",4)
  585.     else
  586.       return true
  587.     end
  588.   end
  589. end
  590.  
  591. function Object:setValue(index,value,accessLevel)
  592.   if accessLevel~=0 then
  593.     canAccess = self:checkMemberAccess(index,self.__accessTypeSet)
  594.   else
  595.     canAccess=(self:isModifiedMember(index,"final") and false) or (not self:isModifiedMember(index,"final") and true)
  596.     if not canAccess then
  597.       error("Attempt to modify final member.",3)
  598.     end
  599.   end
  600.  
  601.   if canAccess then
  602.     if self:isTypedValue(index) then
  603.       if not Utils.hasType(value,self:getValueType(index)) then
  604.         error("[ValueTypeError] Attempt to set member:"..index.." with wrong type")
  605.       end
  606.     end
  607.     if self:isModifiedMember(index,"shared") then
  608.       self.raw.getClass():setShared(index,value)
  609.     else
  610.       if self:isModifiedMember(index,"property") then
  611.         if not self.propertyFlags[index] then
  612.           self:callSetter(index,value)
  613.         else
  614.           self.raw[index] = value
  615.         end
  616.       else
  617.         self.raw[index]=value
  618.       end
  619.     end
  620.   end
  621. end
  622.  
  623. function Object:isModifiedMember(index,_type)
  624.   if self.modified[index] then
  625.     if _type then
  626.       for k,v in pairs(self.modified[index].modifiers) do
  627.         if v==_type then
  628.           return true
  629.         end
  630.       end
  631.     else
  632.       return true
  633.     end
  634.   end
  635.   return false
  636. end
  637.  
  638. function Object:callGetter(index)
  639.   self.propertyFlags[index] = true
  640.   local getter = "get"..index:sub(1,1):upper()..index:sub(2,#index)
  641.   if type(self.raw[getter]) == "function" then
  642.     return self:proxyGetter(getter)
  643.   else
  644.     return self.raw[index]
  645.   end
  646. end
  647.  
  648. function Object:callSetter(index,value)
  649.   self.propertyFlags[index] = true
  650.   local setter = "set"..index:sub(1,1):upper()..index:sub(2,#index)
  651.   if type(self.raw[setter]) == "function" then
  652.     self:proxySetter(setter,value)
  653.   else
  654.     self.raw[index] = value
  655.   end
  656. end
  657.  
  658. function Object:setInner(proxy)
  659.   self.proxy = proxy
  660. end
  661.  
  662. function Object:setOuter(proxy)
  663.   self.outerProxy = proxy
  664. end
  665.  
  666. function Object:getProxy(_type)
  667.   return (self.proxy and (_type == "inner")) or self.outerProxy
  668. end
  669.  
  670. function Object:proxySetter(setter,...)
  671.   if not type(setter)=="function" then
  672.     error("Can't invoke index: "..setter.." through proxy (not a function)")
  673.   end
  674.   self.raw[setter](self.proxy,...)
  675.   self.propertyFlags = {}
  676. end
  677.  
  678. function Object:proxyGetter(getter)
  679.   if not type(getter)=="function" then
  680.     error("Can't invoke index: "..getter.." through proxy (not a function)")
  681.   end
  682.   local fResult = self.raw[getter](self.proxy)
  683.   self.propertyFlags = {}
  684.   return fResult
  685. end
  686.    
  687. function SecurityContext.new()
  688.   local obj={}
  689.   obj.accessLevel=0
  690.   return setmetatable(obj,{__index=SecurityContext})
  691. end
  692.  
  693. function SecurityContext:setAccessLevel(level)
  694.   self.accessLevel=level
  695. end
  696.  
  697. function SecurityContext:getAccessLevel()
  698.   local level=self.accessLevel
  699.   self.accessLevel=0
  700.   return level
  701. end
  702.  
  703.  
  704.  
  705. setmetatable(Class,{__call=function(self,name) return Class.new(name) end})
  706. setmetatable(Object,{__call=function(self,class) return Object.new(class) end})
  707. setmetatable(ObjectProxy,{__call=createObjectProxy})
  708. setmetatable(Property,{__call=function(self,value) return Property.new(value) end})
  709. setmetatable(SecurityContext,{__call=function(self) return SecurityContext.new() end})
  710.  
  711.  
  712. _G.class=Class
  713. _G.extends=extends
  714. _G.implements=implements
  715. _G.interface=interface
Advertisement
Add Comment
Please, Sign In to add comment