-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathinit.lua
287 lines (230 loc) · 6.84 KB
/
init.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
local argcheck = require 'argcheck'
local argcheckenv = require 'argcheck.env'
local doc = require 'argcheck.doc'
local ffi
pcall(function()
ffi = require 'ffi'
end)
doc[[
Object Classes for Lua
----------------------
This package provide simple object-oriented capabilities to Lua.
Each class is defined with a metatable, which contains methods.
Inheritance is achieved by setting metatables over metatables.
An efficient type checking is provided.
## Typical Example
```lua
local class = require 'class'
-- define some dummy A class
local A = class('A')
function A:__init(stuff)
self.stuff = stuff
end
function A:run()
print(self.stuff)
end
-- define some dummy B class, inheriting from A
local B = class('B', 'A')
function B:__init(stuff)
A.__init(self, stuff) -- call the parent init
end
function B:run5()
for i=1,5 do
print(self.stuff)
end
end
-- create some instances of both classes
local a = A('hello world from A')
local b = B('hello world from B')
-- run stuff
a:run()
b:run()
b:run5()
```
## Documentation
First, require the package
```lua
local class = require 'class'
```
Note that `class` does not clutter the global namespace.
Class metatables are then created with `class(name)` or equivalently `class.new(name)`.
```lua
local A = class('A')
local B = class('B', 'A') -- B inherit from A
```
You then have to fill-up the returned metatable with methods.
```lua
function A:myMethod()
-- do something
end
```
There are two special methods: `new()`, which already exists when the class is created and _should not be overrided_
and `__init()` which is called by `new()` at the creation of the class.
```lua
function A:__init(args)
-- do something with args
-- note that self exists
end
```
Creation of an instance is then achieved with the `new()` function or (equivalently) using the Lua `__call` metamethod:
```lua
local a = A('blah blah') -- an instance of A
local aa = A.new('blah blah') -- equivalent of the above
```
]]
local class = {}
local classes = {}
local isofclass = {}
local ctypes = {}
-- create a constructor table
local function constructortbl(metatable)
local ct = {}
setmetatable(ct, {
__index=metatable,
__newindex=metatable,
__metatable=metatable,
__call=function(self, ...)
return self.new(...)
end
})
return ct
end
class.new = argcheck{
doc = [[
### `class.new(@ARGP)`
Creates a new class called `name`, which might optionally inherit from `parentname`.
Returns a table, in which methods should be defined.
Note that the returned table is not the metatable, but a _constructor_ table (with a `__call`
function defined). In that respect, one can use the following shorthand:
```lua
local A = class.new('A')
local a = A.new() -- instance.
local aa = A() -- another instance (shorthand).
```
There is also a shorthand `class.new()`, which is `class()`.
]],
{name="name", type="string", doc="class name"},
{name="parentname", type="string", opt=true, doc="parent class name"},
{name="ctype", type="cdata", opt=true, doc="ctype which should be considered as of class name"},
call =
function(name, parentname, ctype)
local class = {__typename = name}
assert(not classes[name], string.format('class <%s> already exists', name))
if ctype then
local ctype_id = tonumber(ctype)
assert(ctype_id, 'invalid ffi ctype')
assert(not ctypes[ctype_id], string.format('ctype <%s> already considered as <%s>', tostring(ctype), ctypes[ctype_id]))
ctypes[ctype_id] = name
end
class.__index = class
class.__factory =
function()
local self = {}
setmetatable(self, class)
return self
end
class.__init =
function()
end
class.new =
function(...)
local self = class.__factory()
self:__init(...)
return self
end
classes[name] = class
isofclass[name] = {[name]=true}
if parentname then
local parent = classes[parentname]
assert(parent, string.format('parent class <%s> does not exist', parentname))
setmetatable(class, parent)
-- consider as type of parent
while parent do
isofclass[parent.__typename][name] = true
parent = getmetatable(parent)
end
return constructortbl(class), classes[parentname]
else
return constructortbl(class)
end
end
}
class.factory = argcheck{
doc = [[
### `class.factory(name)`
Return a new (empty) instance of the class `name`. No `__init` method will be called.
]],
{name="name", type="string"},
call =
function(name)
assert(classes[name], string.format('unknown class <%s>', name))
return class[name].__factory()
end
}
class.metatable = argcheck{
doc = [[
### `class.metatable(name)`
Return the metatable (i.e. the table containing all methods) related to class `name`.
]],
{name="name", type="string"},
call =
function(name)
return classes[name]
end
}
doc[[
### `class.type(obj)`
Return the type of the object `obj` (if this is a known class), or the type
returned by the standard lua `type()` function (if it is not known).
]]
function class.type(obj)
local tname = type(obj)
local objname
if tname == 'cdata' then
objname = ctypes[tonumber(ffi.typeof(obj))]
elseif tname == 'userdata' or tname == 'table' then
local mt = getmetatable(obj)
if mt then
objname = rawget(mt, '__typename')
end
end
if objname then
return objname
else
return tname
end
end
doc[[
### `class.istype(obj, name)`
Check is `obj` is an instance (or a child) of class `name`. Returns a boolean.
]]
function class.istype(obj, typename)
local tname = type(obj)
local objname
if tname == 'cdata' then
objname = ctypes[tonumber(ffi.typeof(obj))]
elseif tname == 'userdata' or tname == 'table' then
local mt = getmetatable(obj)
if mt then
objname = rawget(mt, '__typename')
end
end
if objname then -- we are now sure it is one of our object
local valid = rawget(isofclass, typename)
if valid then
return rawget(valid, objname) or false
else
return objname == typename -- it might be some other type system
end
else
return tname == typename
end
end
-- make sure argcheck understands those types
argcheckenv.istype = class.istype
-- allow class() instead of class.new()
setmetatable(class, {__call=
function(self, ...)
return self.new(...)
end})
return class