-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
293 lines (240 loc) · 7.5 KB
/
init.lua
File metadata and controls
293 lines (240 loc) · 7.5 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
--- Yet Another Object Implementation, for Lua.
--- Copyright (C) 2024 icyselec
---
--- This library is free software; you can redistribute it and/or
--- modify it under the terms of the GNU Lesser General Public
--- License as published by the Free Software Foundation; either
--- version 2.1 of the License, or (at your option) any later version.
---
--- This library is distributed in the hope that it will be useful,
--- but WITHOUT ANY WARRANTY; without even the implied warranty of
--- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--- Lesser General Public License for more details.
---
--- You should have received a copy of the GNU Lesser General Public
--- License along with this library; if not, write to the Free Software
--- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
local newproxy = newproxy
local getmetatable, setmetatable = getmetatable, setmetatable
local type = type
local rawget, rawset = rawget, rawset
local assert = assert
local isSupportedGc = not newproxy
local debug = (not isSupportedGc) and require 'debug'
local cachingIgnores = {
new = true,
final = true,
}
--- Origin of all objects.
------
--- The most recommended coding convention in our framework is to define only one class in one file.
--- Of course, it is not a must.
--- Here is the template file, copy and use it.
--[[
local Object = require 'Yaoi'
---@class SampleObject: Yaoi
---@field mustneed any
---@field optional? any
local SampleObject = Object:def()
function SampleObject:new (o)
o = self:base(o)
assert(o.mustneed)
o.optional = o.optional or 42
return o
end
return SampleObject
--]]
---@class Yaoi
---@field final? fun(self: self)
local Yaoi = {}
---@generic T
---@param self T
---@param o any
---@return T
function Yaoi:def (o)
assert(rawget(self, 'def'), "Attempt to extend a sealed object.")
if not o then
o = {
__index = Yaoi.__index,
__gc = Yaoi.__gc,
new = Yaoi.new,
def = Yaoi.def,
base = Yaoi.base,
}
elseif not getmetatable(o) then
o = rawset(rawset(rawset(rawset(o, '__index', Yaoi.__index), '__gc', Yaoi.__gc), 'new', Yaoi.new), 'def', Yaoi.def)
end
return setmetatable(o, self)
end
local function cache (self, this, index)
if not index then
return this
end
if not rawget(this, index) then
if not cachingIgnores[index] then
return cache(self, rawset(this, index, rawget(self, index)), next(self, index))
end
end
return cache(self, this, next(self, index))
end
--- To quickly access fields in the base class, copy them.
---@param this Yaoi
function Yaoi:cache (this)
if this then
cache(self, this, next(self, nil))
end
if getmetatable(self) then
getmetatable(self):cache(this or self)
end
end
---@return nil
local function propagateFinalization (self, base)
if base then
if rawget(base, 'final') then
rawget(base, 'final')(self)
end
return propagateFinalization(self, getmetatable(base))
end
end
--- Automatically invokes the finalizer defined in the inheritance chain.
--- ## function Yaoi:final ()
--- self must be an `Yaoi`
---
--- When the inheritance chain contains finalizers, there is considerable performance degradation when an instance is finalized. (or GC-ed.)
--- Therefore, careful consideration is important when defining finalizers.
---@private
---@param self Yaoi | userdata
function Yaoi:__gc ()
self = (type(self) == 'userdata') and getmetatable(self) or self ---@cast self Yaoi
-- Finalizer only invokes on an instance.
if not self:typeof(self) then
return propagateFinalization(self, getmetatable(self))
end
end
--- Called when there is no value in the hash table. If the type is cyclic, it can also cause an infinite recursive.
--- It is used internally, it shall not be redefined, so it is not documented.
---@private
---@param index any
---@return any
function Yaoi:__index (index)
if self then
local value = rawget(self, index)
if value then
return value
end
return Yaoi.__index(getmetatable(self), index)
end
end
--- Front-end function that fires the constructor chain
--- Because defining a class using annotations is like writing the constructor's parameters, this method should not have any annotations. All information is included in the class definition.
function Yaoi:new (o)
o = self:base(o or {})
return o
end
local function tails (self, o)
if self then
return rawget(self, 'new')(self, tails(getmetatable(self), o))
end
return o
end
--- This method is used to define a constructor; therefore, it should not be called arbitrarily.
--- The correct way to call this method is to call directly from the newly defined constructor.
--- Invoking this method outside of the class's constructor definition is an undefined-behavior.
---
--- If the inheritance chain is too long and takes too long to initialize the object, you can try to 'Constructor chain reconstruction'.
--- Tips: To enable this feature, pass the `true` value to the second parameter of `base` method.
---
--- ***Note*** this feature allows you to use it under the condition that you know everything about all base types.
--- If there's anything you don't know at all, We recommend you not to use it because it's an unsafe feature.
---
--- The following code is an example using constructor chain reconstruction.
--[[
local Yaoi = require 'Yaoi'
---@class First: Yaoi
---@field x number
local First = Yaoi:def()
function First:new (o)
o = self:base(o)
assert(o.x)
return o
end
---@class Second: First
---@field y number
local Second = First:def()
function Second:new (o)
o = self:base(o)
assert(o.y)
return o
end
---@class Third: Second
---@field z number
local Third = Second:def()
function Third:new (o)
o = self:base(o, true)
-- You can do all the required initializations in `First` and `Second` in one place.
assert(o.x and o.y and o.z)
return o
end
local third = Third:new{x = 1, y = 2, z = 3,}
--]]
--- when using this feature, Dramatic performance improvements can be achieved with longer inheritance chains.
--- but it is more important not to create such a long inheritance chain in the first place.
---@protected
---@generic T: Yaoi
---@param self T
---@param o table
---@param recons? boolean
---@return T
---@nodiscard
function Yaoi:base (o, recons)
o = o or {}
if not getmetatable(o) then
assert(rawget(self, 'new'), "Attempt to instantiate an instance.")
if rawget(self, 'final') and not isSupportedGc then
o = rawset(rawset(o, debug.setmetatable(newproxy(false), o), not nil), '__gc', Yaoi.__gc)
end
o = setmetatable(o, self)
if not recons then
return tails(getmetatable(self), o)
end
end
return o
end
--- Find out which type the object belongs to.
------
--- It can be used as follows.
---* Examine which type inherits a particular type: SomeType:typeof(Yaoi) -- is SomeType inherit Yaoi?
---* Instance check: print(Type:typeof(Type), Instance:typeof(Instance)) -- it prints "true, false"
--- Distinguish which objects are datatypes(classes) or instances.
--- local Yaoi = require 'Yaoi'
---
--- local objA = Yaoi:def() -- is datatype
--- local objB = Yaoi:new() -- is instance
---
--- local function printKind (t)
--- if t:typeof(t) then
--- print("datatype")
--- else
--- print("instance")
--- end
--- end
---
--- printKind(objA) -- "datatype"
--- printKind(objB) -- "instance"
---@param that any
---@return boolean | nil
function Yaoi:typeof (that)
that = that or self
if self then
if rawget(self, 'new') then
if self == that and rawget(that, 'new') then
return true
else
return self.typeof(getmetatable(self), that)
end
end
return false
end
return nil
end
return Yaoi