5
5
6
6
zlib License
7
7
8
- Copyright (c) 2020 - 2022 Miqueas Martinez
8
+ Copyright (c) 2020 - 2024 Miqueas Martinez
9
9
10
10
This software is provided 'as-is', without any express or implied
11
11
warranty. In no event will the authors be held liable for any damages
89
89
90
90
--- Parent class for all classes
91
91
--- @class Object
92
- local Object = {}
92
+ local Class = {}
93
93
94
94
--- Creates a class
95
95
--- @param def table Class " template"
96
96
--- @return table
97
- function Class (def )
97
+ local function createClass (def )
98
98
check_arg (1 , def , " table" )
99
99
100
- local class = setmt (def , Object )
100
+ local class = setmt (def , Class )
101
101
102
102
return class
103
103
end
108
108
--- @param instance table The object
109
109
--- @param class table The class
110
110
--- @return boolean | string
111
- function Object .is (instance , class )
111
+ function Class .is (instance , class )
112
112
check_arg (1 , instance , " table" )
113
113
opt_arg (2 , class , " table" )
114
114
@@ -118,25 +118,25 @@ function Object.is(instance, class)
118
118
return " Object"
119
119
end
120
120
121
- return mt == class or getmt (mt ) == Object
121
+ return mt == class or getmt (mt ) == Class
122
122
end
123
123
124
124
--- Implements all functions from one or more classes/tables given.
125
125
--- Can't use this from an instance.
126
126
--- @param class table The class where implement functions
127
127
--- @param ... table Tables /classes with functions to import from
128
128
--- @return nil
129
- function Object .implements (class , ...)
129
+ function Class .implements (class , ...)
130
130
check_arg (1 , class , " table" )
131
131
132
132
-- Prevent using this method from an instance
133
133
err (
134
- getmt (class ) == Object ,
134
+ getmt (class ) == Class ,
135
135
" Trying to call 'implements' method from an instance"
136
136
)
137
137
138
- for iface_index , iface in ipairs ({ ... }) do
139
- check_arg (iface_index , iface , " table" )
138
+ for index , iface in ipairs ({ ... }) do
139
+ check_arg (index , iface , " table" )
140
140
141
141
for name , func in pairs (iface ) do
142
142
if not get (class , name ) then
@@ -150,14 +150,16 @@ end
150
150
--- @param class table The class to use to create the object
151
151
--- @param ... any Additional arguments to pass to the class constructor
152
152
--- @return table
153
- function Object .__call (class , ...)
153
+ function Class .__call (class , ...)
154
154
check_arg (1 , class , " table" )
155
155
156
156
-- At the moment, these two meta-fields aren't writables
157
157
set (class , " __index" , class )
158
- set (class , " __newindex" , Object .__newindex )
158
+ set (class , " __newindex" , Class .__newindex )
159
159
160
160
local o = setmt ({}, class )
161
+
162
+ err (o .new ~= nil , " This class doesn't have a constructor method" )
161
163
o :new (... )
162
164
163
165
return o
@@ -167,50 +169,28 @@ end
167
169
--- @param class table The object
168
170
--- @param key string The key
169
171
--- @return any
170
- function Object .__index (class , key )
171
- return get (class , key ) or get (Object , key )
172
+ function Class .__index (class , key )
173
+ return get (class , key ) or get (Class , key )
172
174
end
173
175
174
176
--- Setter
175
177
--- @param class table The object
176
178
--- @param key string The key
177
179
--- @param val any The value
178
180
--- @return nil
179
- function Object .__newindex (class , key , val )
181
+ function Class .__newindex (class , key , val )
180
182
local mt = getmt (class )
181
183
local _val = get (class , key ) or get (mt , key )
182
184
183
- if mt == Object then
185
+ if mt == Class then
184
186
-- From a class
185
187
set (class , key , val )
186
188
else
187
189
-- From an instance
188
- err (_val , " Field '%s' doesn't exists." , key )
190
+ err (_val ~= nil , " Field '%s' doesn't exists." , key )
189
191
err (type (_val ) ~= " function" , " Trying to overwrite method '%s'." , key )
190
192
set (class , key , val )
191
193
end
192
194
end
193
195
194
- Object .new = Object .__call
195
-
196
- --- Handles what will be returned when "require()" this library.
197
- --- See the documentation for more details.
198
- --- @param g_constructor boolean Enable /disable global constructor
199
- --- @param g_object boolean Enable /disable global ` Object`
200
- --- @return function , Object | nil
201
- return function (g_constructor , g_object )
202
- opt_arg (1 , g_constructor , " boolean" )
203
- opt_arg (2 , g_object , " boolean" )
204
-
205
- if g_constructor then
206
- if not _G .New then
207
- _G .New = Object .new
208
- end
209
- end
210
-
211
- if g_object then
212
- return Class , Object
213
- end
214
-
215
- return Class
216
- end
196
+ return createClass
0 commit comments