|
25 | 25 |
|
26 | 26 | local ClassLoader = {} |
27 | 27 |
|
| 28 | +do |
| 29 | + local type = type |
| 30 | + local getmetatable = getmetatable |
| 31 | + |
| 32 | + function ClassLoader.EnhancedType(object) |
| 33 | + local typ = type(object) |
| 34 | + |
| 35 | + if typ == "table" then |
| 36 | + local objectType = object.__type |
| 37 | + if objectType == "EnumValue" then |
| 38 | + local declaringClass = object._declaringClass |
| 39 | + if declaringClass then |
| 40 | + typ = declaringClass.__name or typ |
| 41 | + end |
| 42 | + elseif objectType == "Enum" then |
| 43 | + typ = object.__name .. "#Template" |
| 44 | + elseif getmetatable(object) == nil then --This is an uninitialised class. |
| 45 | + typ = object.__name and (object.__name .. "#Template") or typ |
| 46 | + else |
| 47 | + typ = object.__name or typ |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + return typ |
| 52 | + end |
| 53 | +end |
| 54 | + |
28 | 55 | local packages = {} |
29 | 56 | local currentPackage, currentFile |
30 | 57 | local baseDirectory |
|
109 | 136 | end |
110 | 137 |
|
111 | 138 | local function instantiateClass(class) |
112 | | - assert(class, "The class constructor must be called with a colon") |
| 139 | + assert(istable(class), "The class constructor must be called with a colon") |
113 | 140 | return setmetatable({}, class) |
114 | 141 | end |
115 | 142 |
|
|
139 | 166 |
|
140 | 167 | class.New = instantiateClass |
141 | 168 | class.Extends = ClassLoader.ExtendObject |
| 169 | + class.Property = ClassLoader.CreateProperty |
142 | 170 |
|
143 | 171 | return class |
144 | 172 | end, |
|
154 | 182 | end |
155 | 183 | } |
156 | 184 |
|
157 | | - local type = type |
158 | | - local getmetatable = getmetatable |
159 | | - local function enhancedType(object) |
160 | | - local typ = type(object) |
161 | | - |
162 | | - if typ == "table" then |
163 | | - local objectType = object.__type |
164 | | - if objectType == "EnumValue" then |
165 | | - local declaringClass = object._declaringClass |
166 | | - if declaringClass then |
167 | | - typ = declaringClass.__name or typ |
168 | | - end |
169 | | - elseif objectType == "Enum" then |
170 | | - typ = object.__name .. "#Template" |
171 | | - elseif getmetatable(object) == nil then --This is an uninitialised class. |
172 | | - typ = object.__name and (object.__name .. "#Template") or typ |
173 | | - else |
174 | | - typ = object.__name or typ |
175 | | - end |
176 | | - end |
177 | | - |
178 | | - return typ |
179 | | - end |
180 | | - |
181 | 185 | baseEnvironment.__index = baseEnvironment |
182 | 186 | setmetatable(baseEnvironment, {__index = _G}) |
183 | 187 |
|
|
193 | 197 | __type = "Package", |
194 | 198 | __isPackage = true, |
195 | 199 | _G = _G, |
196 | | - type = enhancedType, |
| 200 | + type = ClassLoader.EnhancedType, |
197 | 201 | pairs = pairs, |
198 | 202 | ipairs = ipairs |
199 | 203 | } |
@@ -222,6 +226,37 @@ function ClassLoader.ExtendObject(class, super) |
222 | 226 | return class |
223 | 227 | end |
224 | 228 |
|
| 229 | +function ClassLoader.CreateProperty(class, name, typ, default) |
| 230 | + assert(istable(class), "The property builder must be called with a colon") |
| 231 | + assert(isstring(name), "The property name must be a string") |
| 232 | + assert(typ == nil or isstring(typ), "The property type must be a string or nil") |
| 233 | + |
| 234 | + local privateName = "_" .. name:gsub("^%u", string.lower) |
| 235 | + |
| 236 | + class["Get" .. name] = function(self) |
| 237 | + return self[privateName] |
| 238 | + end |
| 239 | + |
| 240 | + if typ then |
| 241 | + class["Set" .. name] = function(self, value) |
| 242 | + local actualType = ClassLoader.EnhancedType(value) |
| 243 | + assert(actualType == typ, "Unexpected value of type \"" .. actualType .. "\" given for property: " .. name) |
| 244 | + |
| 245 | + self[privateName] = value |
| 246 | + end |
| 247 | + else |
| 248 | + class["Set" .. name] = function(self, value) |
| 249 | + self[privateName] = value |
| 250 | + end |
| 251 | + end |
| 252 | + |
| 253 | + if default then |
| 254 | + class["Set" .. name](class, default) |
| 255 | + end |
| 256 | + |
| 257 | + return class |
| 258 | +end |
| 259 | + |
225 | 260 | do |
226 | 261 | local function filePathToObjectPackageName(filePath) |
227 | 262 | return filePath |
|
329 | 364 | object.Extends = nil |
330 | 365 | end |
331 | 366 |
|
| 367 | + if object.Property == ClassLoader.CreateProperty then |
| 368 | + object.Property = nil |
| 369 | + end |
| 370 | + |
332 | 371 | if object.__type == "Enum" then |
333 | 372 | setupEnum(object) |
334 | 373 | end |
|
0 commit comments