-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCore.lua
More file actions
196 lines (152 loc) · 4.92 KB
/
Core.lua
File metadata and controls
196 lines (152 loc) · 4.92 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
local _, Internals = ...;
local GetMetadata = function( object )
local metatable = getmetatable( object );
if ( metatable ) then
return metatable.metadata;
end
return nil;
end
local GetType = function( object )
local objectType = type( object );
if ( objectType ~= "table" ) then return objectType; end
local metadata = GetMetadata( object );
if ( metadata ) then
if ( metadata.isInstance ) then
return "instance";
elseif ( metadata.isClass ) then
return metadata.isAbstract and "abstractclass" or "class";
else
error( "Object has metadata but is neither class nor instance" );
end
end
return objectType;
end
local CreateClass;
do
local SetupMetadata = function( class )
local metadata = {
isInstance = true,
};
local globalMetadata = class.globalMetadata or { };
class.globalMetadata = globalMetadata;
setmetatable( metadata, { __index = globalMetadata } );
class.metatable.metadata = metadata;
class.metadata = metadata;
end
local SetupInstanceFactory;
do
local DefaultInstanceFactory = function( class )
return { };
end
SetupInstanceFactory = function( class, instanceFactory )
local instanceFactoryType = GetType( instanceFactory );
if ( instanceFactoryType == "class" or instanceFactoryType == "abstractclass" ) then
class.instanceFactory = instanceFactory.instanceFactory;
return;
elseif ( instanceFactoryType == "nil" ) then
class.instanceFactory = DefaultInstanceFactory;
return;
end
assert( instanceFactoryType == "function", "Parameter 'instanceFactory' must be function or nil" );
class.instanceFactory = instanceFactory;
end
end
local SetupCtor
do
local AbstractClass = function( self )
error( "Cannot instantiate abstract class" );
end
local NoClass = function( self )
error( "Cannot call base constructor of class with no inheritance" );
end
SetupCtor = function( class, ctor )
local ctorType = type( ctor );
assert( ctorType == "nil" or ctorType == "function", "Parameter 'ctor' must be function or nil" );
if ( ctor ) then
class.ctor = function( self, ... )
ctor( self, class.inherit and class.inherit.ctor or NoClass, ... );
end
else
class.ctor = AbstractClass;
end
end
end
local SetupPrototype;
do
local ApplyInheritance = function( prototype, inherit )
for key, value in pairs( inherit ) do
if ( not prototype[ key ] ) then
prototype[ key ] = value;
end
end
end
SetupPrototype = function( class, prototype, inherit, ... )
local prototypeType = type( prototype );
assert( prototypeType == "nil" or prototypeType == "table", "Parameter 'prototype' must be table or nil" );
local inheritType = GetType( inherit );
assert( inheritType == "nil" or inheritType == "function" or inheritType == "class" or inheritType == "abstractclass", "Class can inherit only from another class. Given '" .. tostring( inheritType ) .. "'" );
if ( not prototype ) then
prototype = { };
end
if ( inherit and inheritType ~= "function" ) then
setmetatable( prototype, inherit.metatable );
class.inherit = inherit;
class.globalMetadata = inherit.globalMetadata;
end
for i = 1, select( "#", ... ) do
local current = select( i, ... );
assert( GetType( current ) == "abstractclass", "Class can inherit only from abstract classes" );
ApplyInheritance( prototype, current.prototype );
end
class.prototype = prototype;
class.metatable = {
__index = prototype,
};
end
end
local New
do
New = function( class, ... )
local instance = class:instanceFactory( ... );
setmetatable( instance, class.metatable );
class.ctor( instance, ... );
return instance;
end
end
CreateClass = function( ctor, prototype, inherit, ... )
local class = { };
setmetatable( class, {
__call = New,
metadata = { isClass = true, isAbstract = not ctor },
} );
SetupInstanceFactory( class, inherit );
SetupPrototype( class, prototype, inherit, ... );
SetupMetadata( class );
SetupCtor( class, ctor );
return class, class.inherit and class.inherit.prototype or nil;
end
end
local Register, Import;
do
local classes = { };
Register = function( name, class )
local classType = GetType( class );
assert( type( name ) == "string", "Parameter 'name' must be string" );
assert( classType == "class" or classType == "abstractclass", "Parameter 'class' must be class or abstract class" );
assert( classes[ name ] == nil, "Class with name '" .. name .. "' is already registered" );
classes[ name ] = class;
end
Import = function( name )
assert( type( name ) == "string", "Parameter 'name' must be string" );
local class = classes[ name ];
assert( class ~= nil, "Class '" .. name .. "' wasn't found" );
return class;
end
end
kCore = {
GetMetadata = GetMetadata,
GetType = GetType,
CreateClass = CreateClass,
Register = Register,
Import = Import,
};