/
OOP.lua
572 lines (456 loc) · 14 KB
/
OOP.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
--[[
Carbon for Lua
#class OOP
#description {
Provides object orientation features for Carbon.
TODO: migrate advanced attributes to `Allocator` attribute.
}
]]
local Carbon = (...)
local Dictionary = Carbon.Collections.Dictionary
local List = Carbon.Collections.List
local ffi
if (Carbon.Support.ffi) then
local ok
ok, ffi = pcall(require, "ffi")
if (not ok) then
ffi = nil
end
end
local OOP = {
__attribute_inheritance = {
-- Inherited attributes
InstanceIndirection = true,
InstancedMetatable = true,
Serialization = true,
-- Not inherited attributes
SparseInstances = false,
PooledInstantiation = false,
PoolSize = false,
Abstract = false,
ExplicitInitialization = false,
Allocator = false,
EXT_LJ_Struct = false
}
}
-- Default attributes for classes and static classes
local default_attributes = {
}
local default_static_attributes = {
}
-- Utility methods
local function handle_indirection(class, instance)
if (class:GetAttribute("InstanceIndirection")) then
local internal = instance
instance = newproxy(true)
local meta = getmetatable(instance)
meta.__index = meta.__index or internal
meta.__newindex = meta.__newindex or internal
end
return instance
end
local function apply_metatable(class, instance)
if (class:GetAttribute("InstanceIndirection")) then
-- If we wrapped the object in a userdata, we need to apply metatables a little differently.
Dictionary.ShallowCopy(class.__metatable, getmetatable(instance))
else
-- The InstancedMetatable attribute determines whether the metatable
-- is class-specific or instance-specific.
if (class.__attributes.InstancedMetatable) then
local target
if (getmetatable(instance)) then
target = Dictionary.ShallowCopy(getmetatable(instance))
end
setmetatable(instance, Dictionary.ShallowCopy(class.__metatable, target))
else
setmetatable(instance, class.__metatable)
end
end
end
OOP.Attributes = {
Class = {},
PreInitialize = {},
PostInitialize = {},
Copy = {}
}
--[[#method {
class public void OOP:RegisterAttribute(@string type, @any identifier, function method)
required type: The type of attribute (Class, PreInitialize, PostInitialize, or Copy).
required identifier: The identifier of the attribute.
required method: The class applicator. For function signatures, see below.
Registers a new custom class attribute.
- Class: `@void function(class)`
Called immediately when the attribute is added to the class.
- PreInitialize: `@void function(class, instance)`
Called after instance allocation but before the object's initializer is called.
- PostInitialize: `@void function(class, instance)`
Called after the object is fully initialized.
- Copy: `@void function(original, copy)`
Called after the copied object has been allocated and filled.
}]]
function OOP:RegisterAttribute(type, identifier, method)
local typeset = self.Attributes[type]
if (not typeset) then
error(("Could not register attribute of type %q: unknown attribute type"):format(type), 2)
end
local out
for i, attribute in ipairs(typeset) do
if (attribute[1] == identifier) then
out = attribute
break
end
end
if (not out) then
out = {identifier}
table.insert(typeset, out)
end
out[2] = method
end
--[[#method {
class public void OOP:SetAttributeInherited(@any identifier, @bool inherited)
required identifier: The identifier of the attribute to define a value for.
required inherited: Whether inheriting a class will also inherit this attribute.
Marks an attribute as inherited or not inherited explicitly.
}]]
function OOP:SetAttributeInherited(identifier, inherited)
self.__attribute_inheritance[identifier] = not not inherited
end
-- PooledInstantiation attribute
-- The rest of the functionality is defined inline, since it modifies allocation behavior.
OOP:RegisterAttribute("Class", "PooledInstantiation",
function(class)
class.__pool = {}
class.__pool_destructor = function(instance)
local pool = class.__pool
if (#pool < (class.__attributes.PoolSize or 20)) then
table.insert(pool, instance)
end
end
class.__members.Destroy = rawget(class.__members, "Destroy") or class.__pool_destructor
class:Attributes {
Allocator = function(self)
return table.remove(self.__pool, #self.__pool) or {}
end
}
end
)
OOP:RegisterAttribute("Class", "SparseInstances",
function(class)
class:Metatable {
__index = class.__members
}
end
)
--[[
#class OOP.BaseClass
#description {
Body for both @OOP.Class and @OOP.StaticClass base classes.
}
#property public set Is {
The typecheck object for this class.
}
#property private dictionary __members {
Holds user-defined class members.
}
#property private dictionary __base_members {
Holds base class members so they can be overridden effectively.
}
#property private dictionary __metatable {
Holds metatable to be applied to instances.
}
#property private dictionary __attributes {
Holds class attributes.
}
]]
OOP.BaseClass = {
__members = {},
__base_members = {},
__metatable = {
__gc = function(self)
if (self.Destroy) then
self:Destroy()
end
end
},
__attributes = {},
Is = {}
}
--[[#method {
class public self BaseClass:Inherits(...)
Inherits from classes, taking on their inheritable attributes, members, metatables, and type information.
}]]
function OOP.BaseClass:Inherits(...)
for i = 1, select("#", ...) do
local object = select(i, ...)
if (type(object) ~= "table") then
error(("Carbon.OOP: Cannot inherit from object #%d, not a class: %s"):format(i, tostring(object)), 2)
end
-- Is this an actual class, or just a collection of members?
if (object.__members and object.__metatable and object.Is and object.__attributes) then
Dictionary.RawDeepCopyMerge(object.__members, self.__members)
Dictionary.DeepCopyMerge(object.__metatable, self.__metatable)
Dictionary.ShallowMerge(object.Is, self.Is)
for key, value in pairs(object.__attributes) do
if (OOP.__attribute_inheritance[key]) then
if (type(value) == "table") then
self.__attributes[key] = Dictionary.DeepCopy(value)
else
self.__attributes[key] = value
end
end
end
for i, attribute in ipairs(OOP.Attributes.Class) do
if (object.__attributes[attribute[1]] and OOP.__attribute_inheritance[attribute[1]]) then
attribute[2](self)
end
end
else
-- This isn't really a class, but we can treat it like one!
Dictionary.RawDeepCopyMerge(object, self.__members)
end
end
return self
end
--[[#method {
class public self BaseClass:Attributes(@dictionary attributes)
required attributes: The attributes to give to the object.
Adds attributes to the class. Overwrites existing attributes.
The attributes parameter is only shallow copied, keep this in mind.
}]]
function OOP.BaseClass:Attributes(attributes)
Dictionary.ShallowCopy(attributes, self.__attributes)
for i, attribute in ipairs(OOP.Attributes.Class) do
if (attributes[attribute[1]]) then
attribute[2](self)
end
end
return self
end
--[[#method {
class public @any? BaseClass:GetAttribute(@any attribute)
required attribute: The attribute identifier to look up.
Returns the value of the given attribute for this class.
}]]
function OOP.BaseClass:GetAttribute(attribute)
return self.__attributes[attribute]
end
--[[#method {
class public self BaseClass:Metatable(@dictionary metatable)
required metatable: The metatable to give to instances of this class.
Adds metatables to the class. Overwrites existing metatable entries.
The metatable parameter is only shallow copied, keep this in mind.
}]]
function OOP.BaseClass:Metatable(metatable)
Dictionary.ShallowCopy(metatable, self.__metatable)
return self
end
--[[#method {
class public self BaseClass:Members(@dictionary members)
required members: The member to give to instances of this class.
Adds members to the class. Overwrites existing member entries.
The members parameter is only shallow copied, keep this in mind.
}]]
function OOP.BaseClass:Members(members)
Dictionary.ShallowCopy(members, self.__members)
return self
end
--[[#method {
class public @any? BaseClass:GetMember(@any key)
required key: The key of the member to get.
Returns a defined member in the class, if it is set.
}]]
function OOP.BaseClass:GetMember(key)
return self.__members[key]
end
--[[
#class OOP.Object
#inherits OOP.BaseClass
#description {
The base object for all instancable classes.
}
]]
OOP.Object = Dictionary.DeepCopy(OOP.BaseClass)
:Attributes(default_attributes)
OOP.Object.Is[OOP.Object] = true
--[[#method 0.9 {
class public Object Class:PlacementNew(@indexable? target, ...)
optional target: Where to place the instance, will be provided if not given.
optional ...: Arguments to pass to the constructor
Creates a new object and puts it into a given indexable object.
}]]
function OOP.Object:PlacementNew(instance, ...)
if (self.__attributes.Abstract) then
error("Cannot create instance of abstract class!", 2)
end
if (not instance) then
if (self.__attributes.Allocator) then
instance = self.__attributes.Allocator(self, ...)
elseif (ffi and self.__attributes.EXT_LJ_Struct) then
if (not self.__ext_ffi_metatype) then
local index = Dictionary.ShallowCopy(self.__base_members)
Dictionary.ShallowCopy(self.__members, index)
if (not index.Class) then
index.Class = self.ClassPointer
end
if (not index.class) then
index.class = self.__class_reference
end
if (not index.Is) then
index.Is = self.__is_reference
end
self.__ext_ffi_metatype = {
__index = index
}
self.__ext_ffi_index = index
Dictionary.ShallowCopy(self.__metatable, self.__ext_ffi_metatype)
ffi.metatype(self.__attributes.EXT_LJ_Struct, self.__ext_ffi_metatype)
end
if (not self.__ext_ffi_constructor) then
self.__ext_ffi_constructor = ffi.typeof(self.__attributes.EXT_LJ_Struct)
end
instance = self.__ext_ffi_constructor()
else
instance = {}
end
end
-- These attributes all disable member copying
if (not (self.__attributes.SparseInstances or self.__attributes.ExplicitInitialization or self.__attributes.EXT_LJ_Struct)) then
Dictionary.DeepCopy(self.__base_members, instance, false)
Dictionary.DeepCopy(self.__members, instance, false)
end
if (not self.__attributes.EXT_LJ_Struct) then
instance.self = Carbon.Deprecated(instance.self or instance)
instance.Class = instance.Class or self.ClassPointer
instance.class = instance.class or self.__class_reference
instance.Is = instance.Is or self.__is_reference
-- InstanceIndirection attribute wraps the object in a userdata
-- This allows a __gc metamethod with Lua 5.1 and LuaJIT.
-- As a side effect, 'self' becomes a userdata
instance = handle_indirection(self, instance)
apply_metatable(self, instance)
end
for i, attribute in ipairs(OOP.Attributes.PreInitialize) do
if (self.__attributes[attribute[1]]) then
attribute[2](self, instance)
end
end
if (self.__members.Init) then
local err, result = self.__members.Init(instance, ...)
if (err == false) then
return err, result
end
end
for i, attribute in ipairs(OOP.Attributes.PostInitialize) do
if (self.__attributes[attribute[1]]) then
attribute[2](self, instance)
end
end
return instance
end
--[[#method 1 {
class public Object Class:New(...)
Creates a new object and passes parameters to its initializer.
}]]
function OOP.Object:New(...)
return self:PlacementNew(nil, ...)
end
--[[#method 0.85 {
object public self Object:Init(...)
Initializes the object with the given parameters.
}]]
OOP.Object.__base_members.Init = function(self)
return self
end
--[[#method 0.86 {
object public @void Object:Destroy()
Destroys the object.
}]]
OOP.Object.__base_members.Destroy = function(self)
end
--[[#method 0.8 {
object public Object Object:Copy()
Copies the given object.
}]]
OOP.Object.__base_members.Copy = function(self, target, datawise, map)
local class_pointer = self.Class
if (not class_pointer) then
return self
end
local class = class_pointer()
if (not target and class.__attributes.Allocator) then
target = class.__attributes.Allocator(self)
else
target = {}
end
-- Ensure that class isn't copied
if (not map) then
map = {}
end
map[class] = class
local source = self
if (type(self) == "userdata") then
source = getmetatable(self).__index
end
local copy = Dictionary.DeepCopy(source, target, false, map)
copy = handle_indirection(class, copy)
apply_metatable(class, copy)
for i, attribute in ipairs(OOP.Attributes.Copy) do
if (class.__attributes[attribute[1]]) then
attribute[2](class, copy)
end
end
return copy
end
--[[
#class OOP.StaticObject
#description {
The base class for objects that cannot be instanced, but use OOP-like functionality like inheritance.
}
]]
OOP.StaticObject = Dictionary.DeepCopy(OOP.BaseClass)
:Attributes(default_static_attributes)
OOP.StaticObject.Is[OOP.StaticObject] = true
-- #class OOP
--[[#method 1 {
class public @Class OOP:Class([Class based_on])
optional based_on: A class to make a direct copy of for the basis of this class.
Creates a new, empty class.
}]]
function OOP:Class(based_on)
based_on = based_on or self.Object
local class = Dictionary.DeepCopy(based_on, nil, true)
setmetatable(class.__members, {
__index = class.__base_members
})
class.Is[class] = true
class.__class_reference = newproxy(true)
getmetatable(class.__class_reference).__index = class
class.__class_reference = Carbon.Deprecated(class.__class_reference)
class.ClassPointer = function()
return class
end
class.__is_reference = newproxy(true)
getmetatable(class.__is_reference).__index = class.Is
setmetatable(class, {
__newindex = class.__members,
__index = class.__members,
__call = function(self, ...)
return self:New(...)
end
})
return class
end
--[[#method 0.9 {
class public @StaticClass OOP:StaticClass()
Creates a static class, enabling it to inherit from other objects without having instantiation capability.
}]]
function OOP:StaticClass()
return OOP:Class(self.StaticObject)
end
Carbon.Metadata:Set(OOP.Object, {
Name = "OOP.Object"
})
Carbon.Metadata:Set(OOP.StaticObject, {
Name = "OOP.StaticObject"
})
return OOP