summaryrefslogtreecommitdiff
path: root/class.lua
blob: fda1e24df478f2025c8ab4f11fc26586f3673d6a (plain)
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
-- currently a class is a table T with T.__index = T
-- then to make an instance of this class, we do
--		setmetatable(instance,T)
-- this should be fine for anything we wish to do.
-- it is possible we will eventually split this into two separate
-- tables perhaps? i don't see why we would ever do that though

local function class()
	local T = {}
	T.__index = T
	setmetatable(T, {__call = function(cls, ...) return cls.make(cls, ...) end})
	return T
end

local function extend(Base)
	local T = class()
	for k,v in pairs(Base) do
		if k:sub(1,2) == "__" and k~="__index" then
			T[k]=v
		end
	end
	getmetatable(T).__index = Base
	return T
end

local function is(obj, cls)
	if not type(obj) == 'table' then return false end
	local mt = getmetatable(obj).__index
	while mt do
		if rawequal(cls, mt) then return true end
		mt = (getmetatable(mt) or {}).__index
	end
	return false
end

return setmetatable({
	class=class,
	extend=extend,
	is=is,
},{__call=class})