SHARE
TWEET

Example compiled

Oeed Apr 24th, 2016 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dofile("serialise")
  2.  
  3. local __CLASS__error = error
  4.  
  5. local __CLASS__metatable_class__index = function(_,k)
  6.     __CLASS__error("attempted to access non-existant class '" .. tostring(_) .. "' enum '" .. k .. "'")
  7. end
  8.  
  9. local __CLASS__metatable_class__newindex = function(_,k,v)
  10.     __CLASS__error("attempted to mutate class '" .. tostring(_) .. "' using key '" .. k .. "'")
  11. end
  12.  
  13. local __CLASS__metatable_instance__index = function(_,k)
  14.     __CLASS__error("attempted to access non-existant instance property '" .. k .. "' of '" .. tostring(_) .. "'")
  15. end
  16.  
  17. local __CLASS__metatable_instance__newindex = function(_,k,v)
  18.     __CLASS__error("attempted to set non-existant instance property '" .. k .. "' of '" .. tostring(_) .. "' to '" .. v .. "'")
  19. end
  20.  
  21. local __CLASS__metatable_enum__index = function(_,k)
  22.     __CLASS__error("attempted to access non-existant key '" .. k .. "' from enum '" .. tostring(_) .. "'")
  23. end
  24.  
  25. local __CLASS__metatable_enum__newindex = function(_,k,v)
  26.     __CLASS__error("attempted to mutate enum '" .. tostring(_) .. "' key '" .. tostring(k) .. "' to '" .. v .. "'")
  27. end
  28.  
  29. local __CLASS__error_instance_readonly = function(_,k,v)
  30.     __CLASS__error("attempted to set read only instance property '" .. k .. "' of '" .. tostring(_) .. "' to '" .. v .. "'")
  31. end
  32.  
  33. local __CLASS__error_type_instance_property = function(_, k, t, v)
  34.     __CLASS__error("attempted to set instance property '" .. k .. "' of '" .. tostring(_) .. "' to an invalid value '" .. tostring(v) .. "', expected '" .. t .. "'")
  35. end
  36.  
  37. local __CLASS__error_type_instance_function = function(_, k, f, t, v)
  38.     __CLASS__error("attempted to pass parameter '" .. k .. "' of '" .. tostring(_) .. "." .. f .. "' an invalid value '" .. tostring(v) .. "', expected '" .. t .. "'")
  39. end
  40.  
  41. local __CLASS__defaultvalue_execute = function()end
  42.  
  43. local __CLASS__string_execute = function(str, env, ...)
  44.     return setfenv(loadstring(str), env)(...)
  45. end
  46.  
  47. local __CLASS__execute = function(func, env, ...)
  48.     return setfenv(func, env)(...)
  49. end
  50.  
  51. local __CLASS__type_check_String = function(v)
  52.     return v == nil or type(v) ~= "string"
  53. end
  54.  
  55. local __CLASS__type_implementation_of = function(self, interface)
  56.     return true
  57. end
  58.  
  59. local __CLASS__type_instance_of = function(self, class)
  60.     return true
  61. end
  62.  
  63. local __CLASS__type_subclass_of = function(self, superclass)
  64.     return true
  65. end
  66.  
  67. local __CLASS__type_type_of = function(self, type)
  68.     return true
  69. end
  70.  
  71. local __CLASS__static_Cat = {}
  72. Cat = {}
  73. local __CLASS__environment_class_Cat = setmetatable({}, { __index = _G })
  74. setfenv(loadstring([[local CONST_VARIABLE = "THING" window = 4]]), __CLASS__environment_class_Cat)()
  75. -- print(serialise(__CLASS__environment_class_Cat))
  76. local __CLASS__initialise_instance_Cat = function(self, arg1, arg2)
  77.     print("init")
  78.     print(self)
  79.     print(arg1)
  80.     print(arg2)
  81.     print(CONST_VARIABLE)
  82.     print(window)
  83.     print(anInstanceVariable)
  84.     print(self)
  85. end
  86. local __CLASS__defaultvalues_instance_Cat = {
  87.     aString = loadstring("return " .. [["hello there" .. " concated!"
  88.     .. "oh but there's more!"]])
  89. }
  90. local __CLASS__readOnly_instance_Cat = {}
  91. local __CLASS__enum_Cat_styles = { LIST = 1; THUMBNAIL = 2; }
  92. local __CLASS__tostring_enum_Cat_styles = "enum '" .. "Cat.styles" .. "': " .. tostring(__CLASS__enum_Cat_styles):sub(8)
  93. local __CLASS__values_class_Cat = setmetatable({
  94.     static = __CLASS__static_Cat,
  95.     styles = setmetatable(__CLASS__enum_Cat_styles,{
  96.         -- TODO: enum values are mutable because a proxy can't be used (for looping over it). in 5.2 there are __pairs and __ipairs. for next there is this: __next = function(t, k) return next(priv, k) end
  97.         __index = __CLASS__metatable_enum__index,
  98.         __newindex = __CLASS__metatable_enum__newindex,
  99.         __tostring = function()return __CLASS__tostring_enum_Cat_styles end,
  100.     })
  101. }, {__index = __CLASS__metatable_class__index})
  102. local __CLASS__function_instance_Cat_capitaliseAndLocation = function()
  103.     -- to allow for custom environments, we need to have a unique version of the function each time
  104.     return function(self,name,char,callback,buttons,defaultButton)
  105.         if __CLASS__type_check_String(name) then
  106.             return __CLASS__error_type_instance_function(self, "name", "capitaliseAndLocation", "String", name)
  107.         elseif __CLASS__type_check_String(char) then
  108.             return __CLASS__error_type_instance_function(self, "char", "capitaliseAndLocation", "String", char)
  109.         -- etc..
  110.         end
  111.  
  112.         local location = name:find( char )
  113.         print(self)
  114.         print(anInstanceVariable)
  115.         return name:upper(), location
  116.     end
  117. end
  118. local __CLASS__function_instance_Animal_capitaliseAndLocation = function(self_) -- TODO: maybe self_ should be used below instead of self?
  119.     -- to allow for custom environments, we need to have a unique version of the function each time
  120.     return function(self,name,char,callback,buttons,defaultButton)
  121.         if __CLASS__type_check_String(name) then
  122.             return __CLASS__error_type_instance_function(self, "name", "capitaliseAndLocation", "String", name)
  123.         elseif __CLASS__type_check_String(char) then
  124.             return __CLASS__error_type_instance_function(self, "char", "capitaliseAndLocation", "String", char)
  125.         -- etc..
  126.         end
  127.         print("Animal vv")
  128.         print(self)
  129.         print(anInstanceVariable)
  130.         return name .. "ani"
  131.     end
  132. end
  133.  
  134. local __CLASS__metatable_instance__index_Cat = setmetatable({
  135.     -- if there is a getter use that purely
  136.     thing = function(self, key, values, environment, locks)
  137.         if locks[key] then
  138.             return values[key]
  139.         else
  140.             locks[key] = true
  141.             local v = {__CLASS__execute(function(self)
  142.                 -- user code
  143.                 print("GETTERRR")
  144.                 print(anInstanceVariable)
  145.                 return self.thing
  146.             end, environment, self)}
  147.             locks[key] = nil
  148.             return unpack(v)
  149.         end
  150.     end,
  151.     -- otherwise, just return the value
  152.     aString = function(self, key, values)
  153.         return values[key]
  154.     end,
  155.     capitaliseAndLocation = function(self, key, values, environment, locks)
  156.         -- TODO: we don't want to have to call setfenv here (it's about 100 times slower)
  157.         return values[key]
  158.     end
  159. }, {__index = __CLASS__metatable_instance__index})
  160. local __CLASS__functions_instance_Cat = {
  161.     capitaliseAndLocation = {__CLASS__function_instance_Cat_capitaliseAndLocation, __CLASS__function_instance_Animal_capitaliseAndLocation}
  162. }
  163. local __CLASS__willSet_instance_Cat_thing = function(self, thing)
  164.     -- user code
  165.     print("will set")
  166. end
  167. local __CLASS__set_instance_Cat_thing = function(self, thing)
  168.     -- user code
  169.     print("oi set")
  170.     print(thing)
  171.     self.thing = "6fsd"
  172. end
  173. local __CLASS__didSet_instance_Cat_thing = function(self, thing)
  174.     -- user code
  175.     print("it was set to " .. tostring(thing))
  176. end
  177. local __CLASS__metatable_instance__newindex_Cat = setmetatable({
  178.     stringTable = __CLASS__error_instance_readonly,
  179.     somethingElseReadonly = __CLASS__error_instance_readonly,
  180.     capitaliseAndLocation = __CLASS__error_instance_set_function,
  181.     thing = function(self, key, value, values, environment, locks)
  182.         if __CLASS__type_check_String(value) then
  183.             __CLASS__error_type_instance_property(self, key, "String", value)
  184.         end
  185.         if locks[key] then
  186.             values[key] = value
  187.         else
  188.             locks[key] = true
  189.             -- if there is a willSet, add this
  190.                 __CLASS__execute(__CLASS__willSet_instance_Cat_thing, environment, self, value)
  191.  
  192.             -- if there is a set, add this
  193.                 __CLASS__execute(__CLASS__set_instance_Cat_thing, environment, self, value)
  194.                 -- if there is a didSet, add this
  195.                 __CLASS__execute(__CLASS__didSet_instance_Cat_thing, environment, self, values[key])
  196.             -- else
  197.                 -- values[key] = value
  198.                 -- if there is a didSet, add this
  199.                 -- __CLASS__execute(__CLASS__didSet_instance_Cat_thing environment, self, value) -- we can use value because we know that it's definintely the value
  200.  
  201.             locks[key] = nil
  202.         end
  203.     end,
  204.     -- otherwise, just set the value
  205.     otherThing = function(self, key, value, values, environment)
  206.         values[key] = value
  207.     end
  208. }, {__index = __CLASS__metatable_instance__index})
  209.  
  210. local __CLASS__instancevariables_instance_Cat = {
  211.     anInstanceVariable = loadstring("return " .. "32\9")
  212. }
  213. local __CLASS__tostring_class_Cat = "class '" .. "Cat" .. "': " .. tostring(Cat):sub(8)
  214. setmetatable( Cat, {
  215.     __index = __CLASS__values_class_Cat,
  216.     __newindex = __CLASS__metatable__newindex,
  217.     __call = function(_, ...)
  218.         local instance = {}
  219.         local instanceEnvironment = setmetatable({}, {
  220.             __index = function(_, k)
  221.                 if not __CLASS__instancevariables_instance_Cat[k] then -- if an instance variable is nil and there is an upvalue local with the same name the instance variable will hide the upvalue, even when nil
  222.                     return __CLASS__environment_class_Cat[k]
  223.                 end
  224.             end,
  225.             __newindex = function(_, k, v)
  226.                 if not __CLASS__instancevariables_instance_Cat[k] then -- all instance variables hide upvalues, regardless of their value
  227.                     __CLASS__environment_class_Cat[k] = v -- TODO: should this be the class environment, or _G?
  228.                 else
  229.                     rawset(_, k, v)
  230.                 end
  231.             end
  232.         })
  233.         for k, v in pairs(__CLASS__instancevariables_instance_Cat) do
  234.             instanceEnvironment[k] = __CLASS__execute(v, __CLASS__environment_class_Cat)
  235.         end
  236.         local values = {}
  237.         for k,v in pairs(__CLASS__defaultvalues_instance_Cat) do
  238.             values[k] = __CLASS__execute(v, __CLASS__environment_class_Cat)
  239.         end
  240.         for k, v in pairs(__CLASS__functions_instance_Cat) do
  241.             -- somehow
  242.             values[k] = v[1](instance)
  243.         end
  244.         local getLocks, setLocks = {}, {}
  245.         local supers = {}
  246.         local supersEnvironments = {}
  247.         local supersValues = {}
  248.         local __tostring = "instance of '" .. "Cat" .. "': " .. tostring(instance):sub(8)
  249.         setmetatable(instance, {
  250.             __index = function(_,k)
  251.                 return __CLASS__metatable_instance__index_Cat[k](instance, k, values, instanceEnvironment, getLocks)
  252.             end,
  253.             __newindex = function(_,k,v)
  254.                 __CLASS__metatable_instance__newindex_Cat[k](instance, k, v, values, instanceEnvironment, setLocks)
  255.             end,
  256.             __tostring = function() return __tostring end
  257.         })
  258.  
  259.         -- for i = 1, 1 do
  260.         --  local super = {}
  261.         --  local superValues = setmetatable({}, {__index = values, __newindex = function(_,k,v)values[k] = v end})
  262.         --  local super__tostring = "super '" .. "Animal" .. "': " .. tostring(instance):sub(8) .. " of " .. __tostring
  263.         --  setmetatable(super, {
  264.         --      __index = function(_,k)
  265.         --          return __CLASS__metatable_instance__index_Cat[k](super, k, superValues, instanceEnvironment, getLocks)
  266.         --      end,
  267.         --      __newindex = function(_,k,v)
  268.         --          __CLASS__metatable_instance__newindex_Cat[k](super, k, v, superValues, instanceEnvironment, setLocks)
  269.         --      end,
  270.         --      __tostring = function() return super__tostring end
  271.         --  })
  272.         --  table.insert(supers, super)
  273.         -- end
  274.  
  275.         -- don't include this if init function wasn't specified. if a self one wasn't defined, call the super one
  276.         __CLASS__execute(__CLASS__initialise_instance_Cat, instanceEnvironment, instance, ...)
  277.         return instance
  278.     end,
  279.     __tostring = function() return __CLASS__tostring_class_Cat end
  280. } )
  281.  
  282. print(Cat)
  283. local cat = Cat("donkey", "apple")
  284. print(cat)
  285. print(cat.aString)
  286. print("^^")
  287. print(cat.thing)
  288. cat.thing = "dg"
  289. print(Cat.styles)
  290. for k, v in pairs(Cat.styles) do
  291.     print(k)
  292. end
  293. print(serialise(Cat.styles))
  294. print(cat.capitaliseAndLocation)
  295. print(cat:capitaliseAndLocation("what", "a"))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top