fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosBugfixes + Dark Mode skina8034e9

v2.1.1a
4.5 KiB190 linesraw
1local class = require("class")
2
3local OBJECT = {}
4
5function OBJECT:__call(...)
6	local class = rawget(self, "class")
7	local classname = rawget(class, "classname")
8	local constructor = rawget(class, classname)
9	if not constructor then
10		-- constructor doesn't exist, error
11		return error(string.format("can not create object for class '%s' (no constructor)", classname))
12	end
13	-- call our constructor method
14	constructor(self, ...)
15	-- return the object
16	return self
17end
18
19local function recursiveIndex(class, key)
20	local value = rawget(class, key)
21	if value ~= nil then
22		return value
23	else
24		class = rawget(class, "class")
25		if class then
26			-- Check the inherited class
27			return recursiveIndex(class, key)
28		end
29	end
30	return nil
31end
32
33function OBJECT:__index(key)
34	-- Check for any specific, class-based, methods like "super"
35	local meta_index = rawget(getmetatable(self), key) -- OBJECT
36	if meta_index ~= nil then
37		-- Only return if not nil
38		return meta_index
39	end
40
41	-- Get our "self" class structure
42	local class = rawget(self, "__self")
43
44	-- Recursive lookup down the class line
45	return recursiveIndex(class, key)
46end
47
48function OBJECT:instanceof(other)
49	local class = rawget(self, "class")
50	local name
51	local tp = type(other)
52
53	if tp == "table" then
54		-- User passed another object, get the classname
55		name = rawget(rawget(self, "class"), "classname")
56	elseif tp == "string" then
57		-- User passed the classname
58		name = other
59	end
60
61	-- Handle invalid arguments
62	if not name then return error(string.format("unexpected argument #2 (string or object expected, got %s", tp)) end
63
64	while class do
65		-- Loop through the class tree and see if any match
66		if class:getName() == name then
67			return true
68		end
69		class = rawget(class, "class")
70	end
71
72	-- Nothing found
73	return false
74end
75
76function OBJECT:super(method, ...)
77	local this = rawget(self, "__self")
78	local class = rawget(this, "class")
79
80	-- Error if the scope of the super call has no inheritance (AKA the main class)
81	if not class then return error("attempted to call method 'super' in class with no baseclass") end
82
83	-- Use the method name or the class name (for constructor) if nil
84	method = method or class.classname
85
86	-- Recursively lookup the method starting at our "self" class
87	local func = recursiveIndex(class, method)
88
89	if func == nil or type(func) ~= "function" then
90		-- Method doesn't exist, throw an error
91		return error(string.format("attempted to call method '%s' (a %s value)", method, type(func)))
92	end
93
94	-- The "self" is now inside the inherited class structure
95	rawset(self, "__self", class)
96
97	-- Call the method
98	-- This can trigger another super call, so thats why we need "self" as our defined scope
99	local ret = func(self, ...)
100
101	-- The "self" is back to the main class structure
102	rawset(self, "__self", this)
103
104	-- return whatever our method returned
105	return ret
106end
107
108function OBJECT:getBaseClass()
109	return rawget(rawget(self, "class"), "class")
110end
111
112function OBJECT:getClass()
113	return rawget(self, "class")
114end
115
116function OBJECT:type()
117	return "object"
118end
119
120function OBJECT:__tostring()
121	return string.format("%s: %p", rawget(rawget(self, "class"), "classname"), self)
122end
123
124local CLASS = {}
125CLASS.__index = CLASS
126
127function CLASS:getBaseClass()
128	return rawget(self, "class")
129end
130
131function CLASS:getName()
132	return rawget(self, "classname")
133end
134
135function OBJECT:type()
136	return "class"
137end
138
139function CLASS:__tostring()
140	return string.format("Class[%q]: %p", self:getName(), self)
141end
142
143local object = {}
144
145do
146	local function copy(object)
147		local lookup_table = {}
148		local function _copy(object)
149			if type(object) ~= "table" then
150				return object
151			elseif lookup_table[object] then
152				return lookup_table[object]
153			end
154			local new_table = {}
155			lookup_table[object] = new_table
156			for index, value in pairs(object) do
157				new_table[_copy(index)] = _copy(value)
158			end
159			return setmetatable(new_table, getmetatable(object))
160		end
161		return _copy(object)
162	end
163
164	local function createObjectClass(name)
165		local main = class.get(name)
166		local base = class.getBase(name)
167		local struct = setmetatable(copy(main), CLASS)
168		if base then
169			struct.class = createObjectClass(base)
170		end
171		return struct
172	end
173
174	function object.new(name, ...)
175		if not class.get(name) then
176			return error(("failed to create object for unknown class '%s'"):format(name)) 
177		end
178
179		-- Create a copy of the class structure, with metatable and all
180		local obj = setmetatable({class=createObjectClass(name)}, OBJECT)
181
182		-- Set our current "self" to our class structure
183		obj.__self = obj.class
184
185		-- Return our new object and call constructor
186		return obj(...)
187	end
188end
189
190return object