-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlava.lua
1273 lines (1130 loc) · 32.2 KB
/
lava.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local is51 = _VERSION == "Lua 5.1"
local isJit = jit ~= nil
-- Function copies
local setmetatable = setmetatable
local getmetatable = getmetatable
local pairs = pairs
local ipairs = ipairs
local type = type
local error = error
local assert = assert
local rawget = rawget
local rawset = rawset
local getfenv = getfenv
local setfenv = setfenv
local tostring = tostring
local newproxy = newproxy
local dofile = dofile
local loadfile = loadfile
local string = {
len = string.len,
find = string.find,
sub = string.sub,
format = string.format,
rep = string.rep,
match = string.match,
}
local table = {
insert = table.insert,
}
local debug = {
getinfo = debug.getinfo,
getupvalue = debug.getupvalue,
upvaluejoin = debug.upvaluejoin,
}
local m = {
classRegistry = {}, -- Registry of all class definitions
instanceRegistry = { -- Registry of all class instances
classes = {},
singletons = {},
},
classIndex = 0, -- Global counter for assigning class id numbers
g = is51 and getfenv() or _G,
classTypes = {
TYPE_CLASS = 0,
TYPE_SINGLETON = 1,
TYPE_ABSTRACT = 2,
TYPE_INTERFACE = 3,
TYPE_MIXIN = 4,
}
}
-- Weak values to let the garbage collector do it's thing correctly
setmetatable( m.instanceRegistry.singletons, {__mode = "v"} )
local utils
utils = {
trimString = function( str )
return string.match( str, "^%s*(.-)%s*$" )
end,
splitString = function( str, sep )
local ret, pos = {}, 1
for i = 1, string.len( str ) do
local front, back = string.find( str, sep, pos, true )
if not front then break end
ret[i] = utils.trimString( string.sub(str, pos, front -1) )
pos = back +1
end
ret[#ret +1] = utils.trimString( string.sub(str, pos) )
return ret
end,
emptyTable = function( tbl )
for k, _ in pairs( tbl ) do
tbl[k] = nil
end
end,
copyTable = function( tblSrc, seen )
local copy = {}
setmetatable( copy, getmetatable(tblSrc) )
for k, v in pairs( tblSrc ) do
if type( v ) ~= "table" then
copy[k] = v
else
seen = seen or {}
seen[tblSrc] = copy
if seen[v] then
copy[k] = seen[v]
else
copy[k] = utils.copyTable( v, seen )
end
end
end
return copy
end,
mergeTable = function( tblSrc, tblDest )
for k, v in pairs( tblSrc ) do
if type( v ) == "table" and type( tblDest[k] ) == "table" then
utils.mergeTable( v, tblDest[k] )
else
tblDest[k] = v
end
end
return tblDest
end,
mergeTablePreserveDest = function( tblSrc, tblDest )
for k, v in pairs( tblSrc ) do
if type( v ) == "table" and type( tblDest[k] ) == "table" then
utils.mergeTable( v, tblDest[k] )
else
if tblDest[k] == nil then -- Don't overwrite anything in dest, only add absent things
tblDest[k] = v
end
end
end
return tblDest
end,
mergeTableErrorConflicts = function( tblSrc, tblDest, strErrFmt, fnErrCB )
for k, v in pairs( tblSrc ) do
if type( v ) == "table" and type( tblDest[k] ) == "table" then
utils.mergeTable( v, tblDest[k], strErrFmt )
else
if tblDest[k] ~= nil then -- Error
if fnErrCB then fnErrCB() end
error( string.format(strErrFmt, k) )
else
tblDest[k] = v
end
end
end
return tblDest
end,
classGCRouter = function( pClassInstance )
local meta = getmetatable(pClassInstance)
if meta.__removed then
return -- Lua is cleaning up fragmented userdata
end
local v = rawget( meta.__classDef.methods, "__GC" )
if v then
v( pClassInstance )
end
meta.__classDef:OnGC( pClassInstance )
end,
}
function m:GetOrCreateNamespace( strNamespace )
local t = m.g
for _, v in ipairs( utils.splitString(strNamespace, ".") ) do
t[v] = t[v] or {}
t = t[v]
end
return t
end
--[[
Class declaration object
Created by calls to class creation methods (ex: class(), abstract(), singleton(), interface())
]]--
do
local classDecMT = {}
local classDecMTDef = {
__index = classDecMT,
__call = function( dec, ... )
return m:CreateClassDefinition( dec, ... )
end,
}
-- A bit weird here, to allow for proper method chaining me may end up wanting to call methods like final()
-- after 'extend "" : from ""', we can't easily reconcile this so we must act as a proxy type in such instances
local extendMT = {
from = function( self, strNamespace )
self.dec:SetParentNamespace( strNamespace )
return self
end,
}
local extendMTObj = {
__index = function( t, k )
if k == "from" then
return extendMT.from
else
return rawget( t, "dec" )[k]
end
end,
__newindex = function( t, k, v )
rawget( t, "dec" )[k] = v
end,
__call = function( t, ... )
return classDecMTDef.__call( rawget(t, "dec"), ... )
end,
}
-- Pretty much the same story as above with extends, except we must 'finalize' implements() in edge case
-- (when from() is never called)
local implementsMT = {
from = function( self, strNamespace )
self.ns = strNamespace
return self
end,
}
local implementsMTObj = {
__index = function( t, k )
if not rawget( t, "finalized" ) and k == "from" then
return implementsMT.from
else
-- Finalize first
if not rawget( t, "finalized" ) then
rawget( t, "dec" ):AddInterface( rawget(t, "iface"), rawget(t, "ns") )
rawset( t, "finalized", true )
end
return rawget( t, "dec" )[k]
end
end,
__newindex = function( t, k, v )
if k == "ns" then
rawset( t, k, v )
else
rawget( t, "dec" )[k] = v
end
end,
__call = function( t, ... )
-- Finalize first
if not rawget( t, "finalized" ) then
rawget( t, "dec" ):AddInterface( rawget(t, "iface"), rawget(t, "ns") )
rawset( t, "finalized", true )
end
return classDecMTDef.__call( rawget(t, "dec"), ... )
end,
}
local mixinMTObj = {
__index = function( t, k )
if not rawget( t, "finalized" ) and k == "from" then
return implementsMT.from
else
-- Finalize first
if not rawget( t, "finalized" ) then
rawget( t, "dec" ):AddMixin( rawget(t, "name"), rawget(t, "ns") )
rawset( t, "finalized", true )
end
return rawget( t, "dec" )[k]
end
end,
__newindex = function( t, k, v )
if k == "ns" then
rawset( t, k, v )
else
rawget( t, "dec" )[k] = v
end
end,
__call = function( t, ... )
-- Finalize first
if not rawget( t, "finalized" ) then
rawget( t, "dec" ):AddMixin( rawget(t, "name"), rawget(t, "ns") )
rawset( t, "finalized", true )
end
return classDecMTDef.__call( rawget(t, "dec"), ... )
end,
}
local classDecInst = {
type = m.classTypes.TYPE_CLASS,
className = nil,
m_namespace = nil,
parentClass = nil,
parentNamespace = nil,
m_final = false,
interfaces = {},
mixins = {},
}
function classDecMT:SetClassType( nClassType )
self.type = nClassType
end
function classDecMT:SetClassName( strName )
self.className = strName
end
function classDecMT:SetNamespace( strNamespace )
self.m_namespace = strNamespace
end
function classDecMT:SetParentClass( strClass )
self.parentClass = strClass
end
function classDecMT:SetParentNamespace( strNamespace )
self.parentNamespace = strNamespace
end
function classDecMT:AddInterface( strInterface, strNamespace )
table.insert( self.interfaces, {
iface = strInterface,
ns = strNamespace,
} )
end
function classDecMT:AddMixin( strMixin, strNamespace )
local name = (strNamespace and (strNamespace.. ".") or "").. strMixin
if self.mixins[name] then
error( string.format("Attempt to add a mixin that has already been added! (Mixin: %s)\n", name) )
return
end
self.mixins[name] = { name = strMixin, namespace = strNamespace, path = name }
end
function classDecMT:extends( strClassName )
if self.type == m.classTypes.TYPE_INTERFACE then
error( "Extending an interface is not allowed.\n" )
return
elseif self.type == m.classTypes.TYPE_MIXIN then
error( "Extending a mixin is not allowed.\n" )
return
end
self:SetParentClass( strClassName )
local extendObj = { dec = self }
setmetatable( extendObj, extendMTObj )
return extendObj
end
function classDecMT:namespace( strNamespace )
self:SetNamespace( strNamespace )
return self
end
function classDecMT:final()
if self.type == m.classTypes.TYPE_INTERFACE then
error( "Marking an interface as final has no meaning, this is not allowed.\n" )
return
elseif self.type == m.classTypes.TYPE_MIXIN then
error( "Marking a mixin as final has no meaning, this is not allowed.\n" )
return
end
self.m_final = true
return self
end
function classDecMT:implements( strInterface )
if self.type == m.classTypes.TYPE_INTERFACE then
error( "An interface may not implement another interface.\n" )
return
elseif self.type == m.classTypes.TYPE_MIXIN then
error( "A mixin may not implement an interface.\n" )
return
end
local implementsObj = { dec = self, iface = strInterface }
setmetatable( implementsObj, implementsMTObj )
return implementsObj
end
function classDecMT:mixin( strMixin )
if self.type == m.classTypes.TYPE_INTERFACE then
error( "Applying mixins to an interface is not allowed.\n" )
return
elseif self.type == m.classTypes.TYPE_MIXIN then
error( "Applying mixins to another mixin is not allowed.\n" )
return
end
local mixinObj = { dec = self, name = strMixin }
setmetatable( mixinObj, mixinMTObj )
return mixinObj
end
function m:CreateClassDeclaration()
local inst = utils.copyTable( classDecInst )
setmetatable( inst, classDecMTDef )
return inst
end
end
--[[
Class definition object
Created by calls to class declaration objects (declaring members at the end of the call chain via {})
]]--
do
local classDefMT = {}
local classDefMTDef = {
__index = classDefMT,
}
local classDefInst = {
declaration = nil,
classID = 0,
refCount = 0,
-- namespace and parent info
namespace = nil,
parent = nil,
parentNamespace = nil,
instanceProto = {}, -- Final instance prototype
-- member data
members = {}, -- Normal member vars that are copied to all new instances
-- method data
methods = {},
-- interface info
interfaces = {},
-- mixins
mixins = {
list = {},
methods = {},
members = {},
},
-- shared data
shared_proto = nil,
shared_instance = nil,
-- meta methods
metaMethods = {},
meta = nil,
finalized = false,
}
local metaLookup = {
["__close"] = true,
["__tostring"] = true,
["__name"] = true,
["__call"] = true,
["__add"] = true,
["__sub"] = true,
["__mul"] = true,
["__div"] = true,
["__mod"] = true,
["__pow"] = true,
["__unm"] = true,
["__idiv"] = true,
["__band"] = true,
["__bor"] = true,
["__bxor"] = true,
["__bnot"] = true,
["__shl"] = true,
["__shr"] = true,
["__concat"] = true,
["__len"] = true,
["__eq"] = true,
["__lt"] = true,
["__le"] = true,
}
local removedMT = {
__index = function( t, k ) error( "Attempt to index a removed class!\n".. tostring(t).. ":".. tostring(k) ) end,
__newindex = function( t, k ) error( "Attempt to index a removed class!\n".. tostring(t).. ":".. tostring(k) ) end,
__removed = true,
}
function classDefMT:GetMembers()
return self.members
end
function classDefMT:GetMethods()
return self.methods
end
function classDefMT:GetMixins()
return self.mixins
end
function classDefMT:GetParent()
return self.parent
end
function classDefMT:GetNamespace()
return self.namespace
end
function classDefMT:GetParentNamespace()
return self.parentNamespace
end
function classDefMT:GetClassDeclaration()
return self.declaration
end
function classDefMT:GetInstances()
if self.declaration.type ~= m.classTypes.TYPE_CLASS then return end
return m.instanceRegistry.classes[self.classID] and m.instanceRegistry.classes[self.classID] or {}
end
function classDefMT:FindSuperMethod( pInstance, strFunc )
local cur = pInstance
while type( cur.parent ) == "table" do
if type( cur.parent.methods[strFunc] ) == "function" then
return cur.super.methods[strFunc]
end
if cur == cur.parent then break end
cur = cur.parent
end
return
end
function classDefMT:CallConstructor( pInstance, ... )
if type( pInstance.Initialize ) == "function" then
pInstance:Initialize( ... )
else
local method = self:FindSuperMethod( pInstance, "Initialize" )
if not method then error( string.format("Class %s is missing a constructor.\n", self.declaration.className) )
return
end
method( pInstance, ... )
end
return pInstance
end
function classDefMT:IncRefCount()
self.refCount = self.refCount +1
if self.refCount == 1 then
-- Setup shared instance data
if self.shared_proto then
utils.emptyTable( self.shared_instance )
self.shared_instance = utils.mergeTable( self.shared_proto, self.shared_instance )
end
end
end
function classDefMT:DecRefCount()
assert( self.refCount > 0, "Class destructor error!\n" )
self.refCount = self.refCount -1
if self.refCount == 0 then
-- Clear shared instance data
if self.shared_proto and self.shared_instance then
utils.emptyTable( self.shared_instance )
end
end
end
local RemoveInstance
do
local def, t, meta
RemoveInstance = function( pClassInstance )
meta = getmetatable( pClassInstance )
if meta.__removed then return end
meta.__removed = true -- Guard against the garbage collector invoking during removal
if type( pClassInstance.OnRemove ) == "function" then
pClassInstance:OnRemove()
end
def = meta.__classDef
if def.declaration.type == m.classTypes.TYPE_SINGLETON then
m.instanceRegistry.singletons[def.classID] = nil
else
t = m.instanceRegistry.classes[def.classID]
if t then
t[pClassInstance] = nil
end
end
meta.__index = removedMT.__index
meta.__newindex = removedMT.__newindex
meta.__removed = true
meta.__close = nil
meta.__gc = nil
meta.__tostring = nil
meta.__name = nil
meta.__call = nil
meta.__add = nil
meta.__sub = nil
meta.__mul = nil
meta.__div = nil
meta.__mod = nil
meta.__pow = nil
meta.__unm = nil
meta.__idiv = nil
meta.__band = nil
meta.__bor = nil
meta.__bxor = nil
meta.__bnot = nil
meta.__shl = nil
meta.__shr = nil
meta.__concat = nil
meta.__len = nil
meta.__eq = nil
meta.__lt = nil
meta.__le = nil
end
end
do
local p
function classDefMT:OnGC( pClassInstance )
self:DecRefCount()
p = self.parent
while p ~= nil do
p:DecRefCount()
p = p.parent
end
RemoveInstance( pClassInstance )
end
end
function classDefMT:New( ... )
if self.declaration.type == m.classTypes.TYPE_SINGLETON then
if m.instanceRegistry.singletons[self.classID] then
-- If we already have a singleton instance, return that instance
return m.instanceRegistry.singletons[self.classID]
end
end
if self.declaration.type == m.classTypes.TYPE_ABSTRACT or
self.declaration.type == m.classTypes.TYPE_INTERFACE or
self.declaration.type == m.classTypes.TYPE_MIXIN then
error( "Cannot create an instance of an abstract, interface or mixin class.\n" )
end
-- Construct the instance
local meta = {
__classDef = self.meta.__classDef,
__close = self.meta.__close,
__gc = self.meta.__gc,
__tostring = self.meta.__tostring,
__name = self.meta.__name,
__call = self.meta.__call,
__add = self.meta.__add,
__sub = self.meta.__sub,
__mul = self.meta.__mul,
__div = self.meta.__div,
__mod = self.meta.__mod,
__pow = self.meta.__pow,
__unm = self.meta.__unm,
__idiv = self.meta.__idiv,
__band = self.meta.__band,
__bor = self.meta.__bor,
__bxor = self.meta.__bxor,
__bnot = self.meta.__bnot,
__shl = self.meta.__shl,
__shr = self.meta.__shr,
__concat = self.meta.__concat,
__len = self.meta.__len,
__eq = self.meta.__eq,
__lt = self.meta.__lt,
__le = self.meta.__le,
}
meta.__mt = meta
local instance
if is51 then
-- Make a userdata proxy to get __gc functionality
local realInstance = utils.copyTable( self.instanceProto ) -- copy member vars into the instance
instance = newproxy( true )
local mt = getmetatable( instance )
mt.__instance = realInstance -- Hard ref to keep the instance alive for the lifetime of the proxy
mt.__index = realInstance
mt.__newindex = realInstance
mt.__classDef = meta.__classDef
mt.__close = meta.__close
mt.__gc = meta.__gc
mt.__tostring = meta.__tostring
mt.__name = meta.__name
mt.__call = meta.__call
mt.__add = meta.__add
mt.__sub = meta.__sub
mt.__mul = meta.__mul
mt.__div = meta.__div
mt.__mod = meta.__mod
mt.__pow = meta.__pow
mt.__unm = meta.__unm
mt.__idiv = meta.__idiv
mt.__band = meta.__band
mt.__bor = meta.__bor
mt.__bxor = meta.__bxor
mt.__bnot = meta.__bnot
mt.__shl = meta.__shl
mt.__shr = meta.__shr
mt.__concat = meta.__concat
mt.__len = meta.__len
mt.__eq = meta.__eq
mt.__lt = meta.__lt
mt.__le = meta.__le
mt.__mt = mt
else
local realInstance = utils.copyTable( self.instanceProto ) -- copy member vars into the instance
-- we need a proxy table due to metatable changes in 5.2+ (__index only being invoked when a var isn't found)
instance = {}
local mt = {
__instance = realInstance,
__index = realInstance,
__newindex = realInstance,
__classDef = meta.__classDef,
__close = meta.__close,
__gc = meta.__gc,
__tostring = meta.__tostring,
__name = meta.__name,
__call = meta.__call,
__add = meta.__add,
__sub = meta.__sub,
__mul = meta.__mul,
__div = meta.__div,
__mod = meta.__mod,
__pow = meta.__pow,
__unm = meta.__unm,
__idiv = meta.__idiv,
__band = meta.__band,
__bor = meta.__bor,
__bxor = meta.__bxor,
__bnot = meta.__bnot,
__shl = meta.__shl,
__shr = meta.__shr,
__concat = meta.__concat,
__len = meta.__len,
__eq = meta.__eq,
__lt = meta.__lt,
__le = meta.__le,
}
mt.__mt = mt
setmetatable( instance, mt )
end
self:IncRefCount()
local p = self.parent
while p ~= nil do
p:IncRefCount()
p = p.parent
end
-- If we are a singleton, add to the singleton list
if self.declaration.type == m.classTypes.TYPE_SINGLETON then
m.instanceRegistry.singletons[self.classID] = instance
else
-- Add the class to the instance registry
if not m.instanceRegistry.classes[self.classID] then
m.instanceRegistry.classes[self.classID] = {}
--Weak values to let the garbage collector do it's thing correctly
setmetatable( m.instanceRegistry.classes[self.classID], {__mode = "kv"} )
end
m.instanceRegistry.classes[self.classID][instance] = instance
end
return self:CallConstructor( instance, ... )
end
function classDefMT:FinalizeMixins()
for _, v in pairs( self.declaration.mixins ) do
local ns = m.g
if v.namespace then
ns = m:GetOrCreateNamespace( v.namespace )
end
local mixin = ns[v.name]
if not mixin then
m:CleanupFailedDef( self )
error( string.format("Unable to find mixin '%s'\n", (v.namespace and (v.namespace.. ".") or "").. v.name) )
return
end
self.mixins.list[v.path] = mixin
self.mixins.members = utils.mergeTableErrorConflicts(
mixin.members,
self.mixins.members,
"Mixin member conflict! (%s)\n",
function()
m:CleanupFailedDef( self )
end
)
self.mixins.methods = utils.mergeTableErrorConflicts(
mixin.methods,
self.mixins.methods,
"Mixin method conflict! (%s)\n",
function()
m:CleanupFailedDef( self )
end
)
end
-- Parent mixins
if self.parent then
for _, v in pairs( self.parent.declaration.mixins ) do
if self.mixins.list[v.path] then
local cns = self.declaration.m_namespace
cns = cns and (cns.. ".") or ""
m:CleanupFailedDef( self )
error( string.format(
"Parent class '%s' contains the same mixin as the child class '%s' (mixin '%s')\n",
self.parent.declaration.className,
cns.. self.declaration.className,
(v.namespace and (v.namespace.. ".") or "").. v.name
) )
end
end
end
-- Merge mixins with class data
self.members = utils.mergeTableErrorConflicts(
self.mixins.members,
self.members,
"Mixin member conflict! (%s)\n",
function()
m:CleanupFailedDef( self )
end
)
self.methods = utils.mergeTableErrorConflicts(
self.mixins.methods,
self.methods,
"Mixin method conflict! (%s)\n",
function()
m:CleanupFailedDef( self )
end
)
end
function classDefMT:FinalizeInterfaces()
-- Merge parent interfaces
if self.parent then
for id, iface in pairs( self.parent.interfaces ) do
local dec = m.classRegistry[iface.classID].declaration
if self.interfaces[id] then
local cns = self.declaration.m_namespace
cns = cns and (cns.. ".") or ""
local ins = dec.m_namespace
ins = ins and (ins.. ".") or ""
m:CleanupFailedDef( self )
error( string.format(
"Interface re-implementation in class '%s' from parent '%s' - interface '%s'",
cns.. self.declaration.className,
self.parent.declaration.className,
ins.. dec.className
) )
return
end
self.interfaces[id] = iface
end
end
-- Check that interfaces are all implemented
for _, iface in pairs( self.interfaces ) do
for k, v in pairs( iface.methods ) do
if type( self.methods[k] ) ~= "function" or self.methods[k] == v then
local dec = m.classRegistry[iface.classID].declaration
m:CleanupFailedDef( self )
error( string.format(
"Class '%s' uses interface '%s' but does not implement method '%s'!\n",
(self.declaration.m_namespace and (self.declaration.m_namespace.. ".") or "").. self.declaration.className,
dec.className,
k
) )
end
end
end
end
function classDefMT:Finalize()
-- Register mixins
self:FinalizeMixins()
-- Interfaces
self:FinalizeInterfaces()
-- Inherit metamethods if we have any
if self.parent then
for name, _ in pairs( metaLookup ) do
if self.parent.metaMethods[name] then
if not self.metaMethods[name] then
self.metaMethods[name] = self.parent.metaMethods[name]
end
end
end
end
-- Build our metatable
self.meta = {
__gc = utils.classGCRouter,
__close = self.metaMethods.__close,
__tostring = self.metaMethods.__tostring,
__name = self.metaMethods.__name,
__call = self.metaMethods.__call,
__add = self.metaMethods.__add,
__sub = self.metaMethods.__sub,
__mul = self.metaMethods.__mul,
__div = self.metaMethods.__div,
__mod = self.metaMethods.__mod,
__pow = self.metaMethods.__pow,
__unm = self.metaMethods.__unm,
__idiv = self.metaMethods.__idiv,
__band = self.metaMethods.__band,
__bor = self.metaMethods.__bor,
__bxor = self.metaMethods.__bxor,
__bnot = self.metaMethods.__bnot,
__shl = self.metaMethods.__shl,
__shr = self.metaMethods.__shr,
__concat = self.metaMethods.__concat,
__len = self.metaMethods.__len,
__eq = self.metaMethods.__eq,
__lt = self.metaMethods.__lt,
__le = self.metaMethods.__le,
__classDef = self,
}
-- Build the instance prototype
self.instanceProto = utils.copyTable( self.members )
self.instanceProto = utils.mergeTableErrorConflicts(
self.methods,
self.instanceProto,
"Method/member name conflict!\n",
function()
m:CleanupFailedDef( self )
end
)
if not self.instanceProto.Remove then
self.instanceProto.Remove = RemoveInstance
end
m.classRegistry[self.classID] = self
self.finalized = true
if self.finalizer then
self.finalizer()
end
end
function m:CleanupFailedDef( pClassDef )
pClassDef.namespace[pClassDef.declaration.className] = nil
m.classRegistry[pClassDef.classID] = nil
m.classIndex = m.classIndex -1
end
function m:CreateClassDefinition( pClassDec, tblMembers )
-- Get the namespace
local ns = m.g
if pClassDec.m_namespace then
ns = self:GetOrCreateNamespace( pClassDec.m_namespace )
end
-- Create a new class def
local def = utils.copyTable( classDefInst )
setmetatable( def, classDefMTDef )
-- set declaration and namespace
def.classID = m.classIndex; m.classIndex = m.classIndex +1
def.declaration = pClassDec
def.namespace = ns
-- Get the parent namespace (if we have a parent)
if pClassDec.parentClass then
pClassDec.parentNamespace = pClassDec.parentNamespace or pClassDec.m_namespace
if pClassDec.parentClass == pClassDec.className and pClassDec.parentNamespace == pClassDec.m_namespace then
m:CleanupFailedDef( def )
error( string.format(
"Class %s is trying to extend itself!\n",
(pClassDec.m_namespace and (pClassDec.m_namespace.. ".") or "").. pClassDec.className
) )
return
end
local parentNS = ns
if pClassDec.parentNamespace then
parentNS = self:GetOrCreateNamespace( pClassDec.parentNamespace )
end
-- Set parent info
def.parent = parentNS[pClassDec.parentClass]
def.parentNamespace = parentNS
if not def.parent then
m:CleanupFailedDef( def )
error( string.format(
"Parent class '%s' is nil - child class '%s'!\n",
pClassDec.parentClass,
(pClassDec.m_namespace and (pClassDec.m_namespace.. ".") or "").. pClassDec.className
) )
return
end
-- Make sure we aren't trying to extend from a final class
if def.parent.declaration.m_final then