Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dofile("serialise")
- local __CLASS__error = error
- local __CLASS__metatable_class__index = function(_,k)
- __CLASS__error("attempted to access non-existant class '" .. tostring(_) .. "' enum '" .. k .. "'")
- end
- local __CLASS__metatable_class__newindex = function(_,k,v)
- __CLASS__error("attempted to mutate class '" .. tostring(_) .. "' using key '" .. k .. "'")
- end
- local __CLASS__metatable_instance__index = function(_,k)
- __CLASS__error("attempted to access non-existant instance property '" .. k .. "' of '" .. tostring(_) .. "'")
- end
- local __CLASS__metatable_instance__newindex = function(_,k,v)
- __CLASS__error("attempted to set non-existant instance property '" .. k .. "' of '" .. tostring(_) .. "' to '" .. v .. "'")
- end
- local __CLASS__metatable_enum__index = function(_,k)
- __CLASS__error("attempted to access non-existant key '" .. k .. "' from enum '" .. tostring(_) .. "'")
- end
- local __CLASS__metatable_enum__newindex = function(_,k,v)
- __CLASS__error("attempted to mutate enum '" .. tostring(_) .. "' key '" .. tostring(k) .. "' to '" .. v .. "'")
- end
- local __CLASS__error_instance_readonly = function(_,k,v)
- __CLASS__error("attempted to set read only instance property '" .. k .. "' of '" .. tostring(_) .. "' to '" .. v .. "'")
- end
- local __CLASS__error_type_instance_property = function(_, k, t, v)
- __CLASS__error("attempted to set instance property '" .. k .. "' of '" .. tostring(_) .. "' to an invalid value '" .. tostring(v) .. "', expected '" .. t .. "'")
- end
- local __CLASS__error_type_instance_function = function(_, k, f, t, v)
- __CLASS__error("attempted to pass parameter '" .. k .. "' of '" .. tostring(_) .. "." .. f .. "' an invalid value '" .. tostring(v) .. "', expected '" .. t .. "'")
- end
- local __CLASS__defaultvalue_execute = function()end
- local __CLASS__string_execute = function(str, env, ...)
- return setfenv(loadstring(str), env)(...)
- end
- local __CLASS__execute = function(func, env, ...)
- return setfenv(func, env)(...)
- end
- local __CLASS__type_check_String = function(v)
- return v == nil or type(v) ~= "string"
- end
- local __CLASS__type_implementation_of = function(self, interface)
- return true
- end
- local __CLASS__type_instance_of = function(self, class)
- return true
- end
- local __CLASS__type_subclass_of = function(self, superclass)
- return true
- end
- local __CLASS__type_type_of = function(self, type)
- return true
- end
- local __CLASS__static_Cat = {}
- Cat = {}
- local __CLASS__environment_class_Cat = setmetatable({}, { __index = _G })
- setfenv(loadstring([[local CONST_VARIABLE = "THING" window = 4]]), __CLASS__environment_class_Cat)()
- -- print(serialise(__CLASS__environment_class_Cat))
- local __CLASS__initialise_instance_Cat = function(self, arg1, arg2)
- print("init")
- print(self)
- print(arg1)
- print(arg2)
- print(CONST_VARIABLE)
- print(window)
- print(anInstanceVariable)
- print(self)
- end
- local __CLASS__defaultvalues_instance_Cat = {
- aString = loadstring("return " .. [["hello there" .. " concated!"
- .. "oh but there's more!"]])
- }
- local __CLASS__readOnly_instance_Cat = {}
- local __CLASS__enum_Cat_styles = { LIST = 1; THUMBNAIL = 2; }
- local __CLASS__tostring_enum_Cat_styles = "enum '" .. "Cat.styles" .. "': " .. tostring(__CLASS__enum_Cat_styles):sub(8)
- local __CLASS__values_class_Cat = setmetatable({
- static = __CLASS__static_Cat,
- styles = setmetatable(__CLASS__enum_Cat_styles,{
- -- 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
- __index = __CLASS__metatable_enum__index,
- __newindex = __CLASS__metatable_enum__newindex,
- __tostring = function()return __CLASS__tostring_enum_Cat_styles end,
- })
- }, {__index = __CLASS__metatable_class__index})
- local __CLASS__function_instance_Cat_capitaliseAndLocation = function()
- -- to allow for custom environments, we need to have a unique version of the function each time
- return function(self,name,char,callback,buttons,defaultButton)
- if __CLASS__type_check_String(name) then
- return __CLASS__error_type_instance_function(self, "name", "capitaliseAndLocation", "String", name)
- elseif __CLASS__type_check_String(char) then
- return __CLASS__error_type_instance_function(self, "char", "capitaliseAndLocation", "String", char)
- -- etc..
- end
- local location = name:find( char )
- print(self)
- print(anInstanceVariable)
- return name:upper(), location
- end
- end
- local __CLASS__function_instance_Animal_capitaliseAndLocation = function(self_) -- TODO: maybe self_ should be used below instead of self?
- -- to allow for custom environments, we need to have a unique version of the function each time
- return function(self,name,char,callback,buttons,defaultButton)
- if __CLASS__type_check_String(name) then
- return __CLASS__error_type_instance_function(self, "name", "capitaliseAndLocation", "String", name)
- elseif __CLASS__type_check_String(char) then
- return __CLASS__error_type_instance_function(self, "char", "capitaliseAndLocation", "String", char)
- -- etc..
- end
- print("Animal vv")
- print(self)
- print(anInstanceVariable)
- return name .. "ani"
- end
- end
- local __CLASS__metatable_instance__index_Cat = setmetatable({
- -- if there is a getter use that purely
- thing = function(self, key, values, environment, locks)
- if locks[key] then
- return values[key]
- else
- locks[key] = true
- local v = {__CLASS__execute(function(self)
- -- user code
- print("GETTERRR")
- print(anInstanceVariable)
- return self.thing
- end, environment, self)}
- locks[key] = nil
- return unpack(v)
- end
- end,
- -- otherwise, just return the value
- aString = function(self, key, values)
- return values[key]
- end,
- capitaliseAndLocation = function(self, key, values, environment, locks)
- -- TODO: we don't want to have to call setfenv here (it's about 100 times slower)
- return values[key]
- end
- }, {__index = __CLASS__metatable_instance__index})
- local __CLASS__functions_instance_Cat = {
- capitaliseAndLocation = {__CLASS__function_instance_Cat_capitaliseAndLocation, __CLASS__function_instance_Animal_capitaliseAndLocation}
- }
- local __CLASS__willSet_instance_Cat_thing = function(self, thing)
- -- user code
- print("will set")
- end
- local __CLASS__set_instance_Cat_thing = function(self, thing)
- -- user code
- print("oi set")
- print(thing)
- self.thing = "6fsd"
- end
- local __CLASS__didSet_instance_Cat_thing = function(self, thing)
- -- user code
- print("it was set to " .. tostring(thing))
- end
- local __CLASS__metatable_instance__newindex_Cat = setmetatable({
- stringTable = __CLASS__error_instance_readonly,
- somethingElseReadonly = __CLASS__error_instance_readonly,
- capitaliseAndLocation = __CLASS__error_instance_set_function,
- thing = function(self, key, value, values, environment, locks)
- if __CLASS__type_check_String(value) then
- __CLASS__error_type_instance_property(self, key, "String", value)
- end
- if locks[key] then
- values[key] = value
- else
- locks[key] = true
- -- if there is a willSet, add this
- __CLASS__execute(__CLASS__willSet_instance_Cat_thing, environment, self, value)
- -- if there is a set, add this
- __CLASS__execute(__CLASS__set_instance_Cat_thing, environment, self, value)
- -- if there is a didSet, add this
- __CLASS__execute(__CLASS__didSet_instance_Cat_thing, environment, self, values[key])
- -- else
- -- values[key] = value
- -- if there is a didSet, add this
- -- __CLASS__execute(__CLASS__didSet_instance_Cat_thing environment, self, value) -- we can use value because we know that it's definintely the value
- locks[key] = nil
- end
- end,
- -- otherwise, just set the value
- otherThing = function(self, key, value, values, environment)
- values[key] = value
- end
- }, {__index = __CLASS__metatable_instance__index})
- local __CLASS__instancevariables_instance_Cat = {
- anInstanceVariable = loadstring("return " .. "32\9")
- }
- local __CLASS__tostring_class_Cat = "class '" .. "Cat" .. "': " .. tostring(Cat):sub(8)
- setmetatable( Cat, {
- __index = __CLASS__values_class_Cat,
- __newindex = __CLASS__metatable__newindex,
- __call = function(_, ...)
- local instance = {}
- local instanceEnvironment = setmetatable({}, {
- __index = function(_, k)
- 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
- return __CLASS__environment_class_Cat[k]
- end
- end,
- __newindex = function(_, k, v)
- if not __CLASS__instancevariables_instance_Cat[k] then -- all instance variables hide upvalues, regardless of their value
- __CLASS__environment_class_Cat[k] = v -- TODO: should this be the class environment, or _G?
- else
- rawset(_, k, v)
- end
- end
- })
- for k, v in pairs(__CLASS__instancevariables_instance_Cat) do
- instanceEnvironment[k] = __CLASS__execute(v, __CLASS__environment_class_Cat)
- end
- local values = {}
- for k,v in pairs(__CLASS__defaultvalues_instance_Cat) do
- values[k] = __CLASS__execute(v, __CLASS__environment_class_Cat)
- end
- for k, v in pairs(__CLASS__functions_instance_Cat) do
- -- somehow
- values[k] = v[1](instance)
- end
- local getLocks, setLocks = {}, {}
- local supers = {}
- local supersEnvironments = {}
- local supersValues = {}
- local __tostring = "instance of '" .. "Cat" .. "': " .. tostring(instance):sub(8)
- setmetatable(instance, {
- __index = function(_,k)
- return __CLASS__metatable_instance__index_Cat[k](instance, k, values, instanceEnvironment, getLocks)
- end,
- __newindex = function(_,k,v)
- __CLASS__metatable_instance__newindex_Cat[k](instance, k, v, values, instanceEnvironment, setLocks)
- end,
- __tostring = function() return __tostring end
- })
- -- for i = 1, 1 do
- -- local super = {}
- -- local superValues = setmetatable({}, {__index = values, __newindex = function(_,k,v)values[k] = v end})
- -- local super__tostring = "super '" .. "Animal" .. "': " .. tostring(instance):sub(8) .. " of " .. __tostring
- -- setmetatable(super, {
- -- __index = function(_,k)
- -- return __CLASS__metatable_instance__index_Cat[k](super, k, superValues, instanceEnvironment, getLocks)
- -- end,
- -- __newindex = function(_,k,v)
- -- __CLASS__metatable_instance__newindex_Cat[k](super, k, v, superValues, instanceEnvironment, setLocks)
- -- end,
- -- __tostring = function() return super__tostring end
- -- })
- -- table.insert(supers, super)
- -- end
- -- don't include this if init function wasn't specified. if a self one wasn't defined, call the super one
- __CLASS__execute(__CLASS__initialise_instance_Cat, instanceEnvironment, instance, ...)
- return instance
- end,
- __tostring = function() return __CLASS__tostring_class_Cat end
- } )
- print(Cat)
- local cat = Cat("donkey", "apple")
- print(cat)
- print(cat.aString)
- print("^^")
- print(cat.thing)
- cat.thing = "dg"
- print(Cat.styles)
- for k, v in pairs(Cat.styles) do
- print(k)
- end
- print(serialise(Cat.styles))
- print(cat.capitaliseAndLocation)
- print(cat:capitaliseAndLocation("what", "a"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement