-
Notifications
You must be signed in to change notification settings - Fork 0
/
classified.lua
269 lines (209 loc) · 6.61 KB
/
classified.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
--[[
classified.lua
version 1.1.2
changelog:
+ 1.1.0: getters and setters
+ 1.1.1: made errors bubble up properly because yeah
+ 1.1.2: first github release - added error for indexing
a class without a static block
Copyright (C) 2021 Rin <lostkagamine@outlook.com>
This file is part of classified.
classified is non-violent software: you can use, redistribute,
and/or modify it under the terms of the NVPLv7+ as found
in the LICENSE file in the source code root directory or
at <https://git.pixie.town/thufie/npl-builder>.
classified comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law. See the NVPL for details.
]]
local _CLASSIFIED_VERSION = '1.1.1'
local _ISSUE_REPORT_LINK = 'https://github.com/ry00001/classified/issues/new'
-- disabling _PEDANTIC will disable the error
-- for indexing a class without a static block
local _PEDANTIC = true
local _classes = {}
local _supers = {}
local _properties = {}
local forbidden = {
"__new"
}
local function contains(tb,v)
for _,l in pairs(tb) do
if l==v then return true end
end
return false
end
local function ierror(msg, add)
if not add then add = 0 end
local slug = string.format('v%s: %s', _CLASSIFIED_VERSION, msg)
print('*************************************')
print('** classified.lua: INTERNAL ERROR **')
print('** ------------------------------- **')
print('** if you are seeing this message, **')
print('** you have found a bug, report it **')
print('*************************************')
print()
print('** please report this error at this link:')
print('** '.._ISSUE_REPORT_LINK)
print('** please include a full stack trace and the error message below')
print()
print(slug)
print()
error(slug, 2+add)
end
local function iassert(cond, msg)
if not cond then
if not msg then
msg = 'assertion failed'
else
msg = 'assertion failed: '..msg
end
ierror(msg, 1)
end
end
local function build(t, ignore, custforbidden)
local o = {}
if not custforbidden then
custforbidden = forbidden
end
for i, j in pairs(t) do
if not ignore and contains(forbidden,i) then goto please_5_5_add_continue_i_beg_you end
if i == "__THIS_IS_A_PROPERTY_ACCESSOR__" then
return nil -- Don't.
end
if type(j) == "table" then
local rv = build(j)
o[i] = rv
else
o[i] = j
end
local mt = getmetatable(o[i])
if mt ~= nil and type(o[i]) == "table" then
setmetatable(o[i], mt)
end
::please_5_5_add_continue_i_beg_you::
end
return o
end
local function _construct(n, args)
iassert(_classes[n] ~= nil, '_classes[n] ~= nil')
local cls = _classes[n]
local mem = build(cls.definition)
if _supers[n] then
if not cls.ctor then
-- Call the constructor
_supers[n](mem, table.unpack(args))
else
mem.super = _supers[n]
end
end
if cls.ctor then
cls.ctor(mem, table.unpack(args))
mem.super = nil -- Destroy it
end
return mem
end
local function _extends(og, new, tb)
local n = build(_classes[og].definition, true, {})
if _classes[og].ctor and tb.__new then
_supers[new] = _classes[og].ctor
n.__new = tb.__new
end
if _classes[og].static then
local newstatics = build(_classes[og].static, true)
for a, b in pairs(_classes[new].static or {}) do
newstatics[a] = b
end
_classes[new].static = newstatics
end
for i, j in pairs(tb) do
n[i] = j
end
class(new)(n)
end
local _current_class = nil
function static(the)
if not _classes[_current_class] then
_classes[_current_class] = {}
end
_classes[_current_class].static = the
end
function property(tb)
if not tb.get and not tb.set then
error("cannot define empty property", 2)
end
local ret = {
__THIS_IS_A_PROPERTY_ACCESSOR__ = "don't touch this"
}
if tb.get then
ret.get = tb.get
end
if tb.set then
ret.set = tb.set
end
return ret
end
function class(name)
-- lord
local the = {}
_current_class = name
local theCall = function(_, theTable)
if not _classes[name] then
_classes[name] = {}
end
_classes[name].definition = build(theTable, false, {"__static"})
_classes[name].ctor = theTable.__new
_properties[name] = {}
for a, b in pairs(theTable) do
if type(b) == "table" and b.__THIS_IS_A_PROPERTY_ACCESSOR__ then
-- It is in fact a property accessor
_properties[name][a] = b
end
end
local obj = {}
setmetatable(obj, {
__call = function(self, ...)
local c = _construct(name, {...})
setmetatable(c, {
__index = function(self, key)
if _properties[name][key] then
if not _properties[name][key].get then
error("attempt to read a property '"..key.."' with no getter defined", 2)
end
return _properties[name][key].get(self)
end
return rawget(self, key)
end,
__newindex = function(self, key, value)
if _properties[name][key] then
if not _properties[name][key].set then
error("attempt to write to a property '"..key.."' with no setter defined", 2)
end
return _properties[name][key].set(self, value)
end
rawset(self, key, value)
end
})
return c
end,
__index = function(self, key)
if not _classes[name].static then
if _PEDANTIC then
error("attempt to index a class with no static block '"..name.."'", 2)
end
return nil
end
return _classes[name].static[key]
end
})
_G[name] = obj
end
the.extends = function(self, og)
return function(t)
_extends(og, name, t)
end
end
setmetatable(the, {
__call = theCall
})
return the
end