Skip to content

Commit ac8a1be

Browse files
authored
feat: better enums
2 parents d295f93 + 97a99ef commit ac8a1be

File tree

1 file changed

+97
-7
lines changed

1 file changed

+97
-7
lines changed

class_loader.lua

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ do
6464
local objects = {}
6565

6666
for objectName, object in pairs(packages[packageName]) do
67-
if objectName:sub(2) ~= "__" then
67+
if objectName:sub(0, 2) ~= "__" then
6868
currentFile[objectName] = object
6969
table.insert(objects, object)
7070
end
@@ -113,6 +113,22 @@ do
113113
return setmetatable({}, class)
114114
end
115115

116+
local Enum = {}
117+
Enum.__type = "Enum"
118+
Enum.__isEnum = true
119+
Enum.__index = Enum
120+
121+
function Enum:GetValues()
122+
return self._values
123+
end
124+
125+
function Enum:GetValueOf(name)
126+
local value = self[name]
127+
if istable(value) and value.__type == "EnumValue" then
128+
return value
129+
end
130+
end
131+
116132
local baseEnvironment = {
117133
Import = ClassLoader.ImportObject,
118134
Class = function()
@@ -134,11 +150,7 @@ do
134150
return singleton
135151
end,
136152
Enum = function()
137-
local enum = getCurrentFileObject()
138-
enum.__type = "Enum"
139-
enum.__isEnum = true
140-
141-
return enum
153+
return setmetatable(getCurrentFileObject(), Enum)
142154
end
143155
}
144156

@@ -205,6 +217,80 @@ do
205217
return currentFile
206218
end
207219

220+
local EnumValue = {}
221+
EnumValue.__type = "EnumValue"
222+
EnumValue.__isEnumValue = true
223+
EnumValue.__index = EnumValue
224+
225+
local function newEnumValue(name, value, declaringClass)
226+
local tbl = setmetatable({}, EnumValue)
227+
228+
tbl._name = name
229+
tbl._value = value
230+
tbl._declaringClass = declaringClass
231+
232+
return tbl
233+
end
234+
235+
function EnumValue:GetName()
236+
return self._name
237+
end
238+
239+
function EnumValue:GetValue()
240+
return self._value
241+
end
242+
243+
function EnumValue:GetDeclaringClass()
244+
return self._declaringClass
245+
end
246+
247+
function EnumValue:__add(other)
248+
return self._value + other._value
249+
end
250+
251+
function EnumValue:__sub(other)
252+
return self._value - other._value
253+
end
254+
255+
function EnumValue:__mul(other)
256+
return self._value * other._value
257+
end
258+
259+
function EnumValue:__div(other)
260+
return self._value / other._value
261+
end
262+
263+
function EnumValue:__mod(other)
264+
return self._value % other._value
265+
end
266+
267+
function EnumValue:__eq(other)
268+
return self._value == other._value
269+
end
270+
271+
function EnumValue:__lt(other)
272+
return self._value < other._value
273+
end
274+
275+
function EnumValue:__le(other)
276+
return self._value <= other._value
277+
end
278+
279+
local function setupEnum(enum)
280+
local values = {}
281+
282+
for name, value in pairs(enum) do
283+
if name:sub(0, 2) ~= "__" then --Ignore type, isEnum, etc
284+
local enumValue = newEnumValue(name, value, enum)
285+
286+
enum[name] = enumValue
287+
table.insert(values, enumValue)
288+
end
289+
end
290+
291+
enum._values = values
292+
end
293+
208294
local function registerObject(packageName, objectName, object)
209295
assert(istable(object), "Invalid object returned by: " .. packageName .. "." .. objectName)
210296

@@ -215,6 +301,10 @@ do
215301
object.Extends = nil
216302
end
217303

304+
if object.__type == "Enum" then
305+
setupEnum(object)
306+
end
307+
218308
currentPackage[objectName] = object
219309
end
220310

@@ -285,7 +375,7 @@ do
285375
packages[packageName] = nil
286376

287377
for key, _ in pairs(package) do
288-
if key:sub(2) ~= "__" then --If our package only contains metadata it's safe to delete
378+
if key:sub(0, 2) ~= "__" then --If our package only contains metadata it's safe to delete
289379
packages[packageName] = package
290380
break
291381
end

0 commit comments

Comments
 (0)