View difference between Paste ID: 2qPybHqp and zvdvQLCy
SHOW: | | - or go back to the newest paste.
1-
wO={}
1+
local classes={}
2-
wO.global={}
2+
3-
wO.global.clone=function(self) local cloneObj={} for i,v in pairs(self) do cloneObj[i]=v end return cloneObj end
3+
--Objects/proxies
4-
wO.global.isA=function(self, arg)
4+
5-
	if self.className==arg then
5+
local function functionToTable(tab)
6-
		return true
6+
7-
	elseif self.className~="base" then
7+
	mt.__call=
8-
		if self.extends:isA(arg) then
8+
	function(a,...)
9
		local prevSelf=self
10
		self=tab
11
		local src=a.func
12-
		return false
12+
		local ret={pcall(src,...)}
13
		if not ret[1] then
14
			assert(pcall(src))
15-
wO.global.className="base"
15+
16-
wO.global.__settings={new=function(...) return {} end}
16+
		table.remove(ret,1)
17-
wO.global.omt={}
17+
		self=prevSelf
18
		return unpack(ret)
19-
setmetatable(wO.global, {__tostring=function(a) return "Class "..a.className end})
19+
20
	for i,v in pairs(tab) do
21-
wO.classes={base=wO.global}
21+
		if type(v)=="function" then
22
			local oldV=v
23
			tab[i]={func=oldV}
24
			setmetatable(tab[i],mt)
25
		end
26-
	b.extends=(type(arg[2])=="table" and wO.classes[arg[1]] or wO.classes["base"])
26+
27
end
28
29
local function createProxy(o,omt)
30-
	mt.__index=b.extends
30+
	local proxy=newproxy(true)
31
	local mt=getmetatable(proxy)
32
33-
	if not b.mt then
33+
	for i,v in pairs(omt) do
34-
		b.mt=mt
34+
		mt[i]=v
35
	end
36-
		for i,v in pairs(mt) do
36+
	
37-
			table.insert(b.mt)
37+
	mt.__index=
38
	function(tab,ind)
39
		if not ind:find("__") then
40-
	setmetatable(b, b.mt)
40+
			if o[ind]~=nil then
41-
	wO.classes[a]=b
41+
				return o[ind]
42-
end	
42+
			else
43
				error("Value not found : "..ind)
44
			end
45-
	local o=(wO.classes[a].__settings.new(...))
45+
		elseif bypassing and ind=="___bypassing" then
46
			return tab.___self
47-
	for i,v in pairs(wO.classes[a].omt) do
47+
48
			error("Private value : "..ind)
49
		end
50-
	omt.__index=wO.classes[a]
50+
51-
	setmetatable(o, omt)
51+
52-
	return o
52+
	mt.__newindex=
53
	function(tab,ind,val)
54
		if not ind:find("_") then
55
			if o[ind] then
56
				o[ind]=val
57
			else
58
				error("Value not found : "..ind)
59
			end
60
		else
61
			error("Read only or private value : "..ind)
62
		end
63
	end
64
	
65
	mt.__metatable=false
66
	return proxy
67
end
68
69
local function newObj(a,...)
70
	local arg={...}
71
	local o
72-
setmetatable(_G,{__index=function(_,a) return (a=="class" and addClass or (a==("newObj" or "new") and newObj or (wO.classes[a] or nil))) end}) --Ugly piece of code.
72+
	local class
73
	if arg[1]=="bypassNewFunc" then
74
		o={}
75
		class=classes[a] or {}
76
	else
77
		o=((classes[a]) and classes[a].__settings.new(...) or {})
78
		class=classes[a] or {}
79
	end
80
	local omt={}
81
	for i,v in pairs((class.__omt or (classes["base"] or {}))) do
82
		omt[i]=v
83
	end
84
	omt.__index=class
85
	setmetatable(o,omt)
86
	for i,v in pairs(class) do
87
		o[i]=(o[i] or v)
88
	end
89
	o.___self=o
90
	functionToTable(o)
91
	local proxy=createProxy(o,omt)
92
	return proxy
93
end
94
95
--Classes
96
97
local function newClass(a, ...)
98
	arg={...}
99
	b=(type(arg[1])=="table" and arg[1] or arg[2])
100
	b.extends=classes[(type(arg[2])=="table" and arg[1] or "base")] or {}
101
	b.className=a
102
	b.name=a.."Obj"
103
	local mt=(b.__mt or {})
104
	mt.__tostring=function(a) return "Class "..a.className end
105
	mt.__call=function(a,...) local arg={...} return newObj(a.className,...) end
106
	mt.__concat=function(a,b) return tostring(a)..tostring(b) end
107
	if not b.__mt then
108
		b.__mt=mt
109
	end
110
	setmetatable(b, b.__mt)
111
	classes[a]=b
112
	for i,v in pairs(b.extends) do
113
		if not b[i] then
114
			b[i]=v
115
		end
116
	end
117
	b.extends=newObj(b.extends.className,"bypassNewFunc")
118
	_G[a]=b
119
end
120
121
local function addClassInheritance(name, b)
122
	return function(...)
123
		newClass(name, b, arg[1])
124
	end
125
end
126
127
local function addClass(name)
128
	return function(...)
129
		local arg={...}
130
		if type(arg[1])=="table" then
131
			newClass(name,...)
132
		else
133
			return addClassInheritance(name,arg[1])
134
		end
135
	end
136
end
137
138
function class(name)
139
	return addClass(name)
140
end
141
142
--load default classes and such
143
144
class "base"
145
{
146
	isA=
147
	function(arg)
148
		if self.className==arg then
149
			return true
150
		elseif self.className~="base" then
151
			if self.extends.isA(arg) then
152
				return true
153
			end
154
		else
155
			return false
156
		end
157
	end,
158
	
159
	className="base",
160
	__settings={new=function(...) return {} end},
161
	__omt={__tostring=function(a) return "Object "..a.className end,__concat=function(a,b) return tostring(a)..tostring(b) end},
162
	__mt={__tostring=function(a) return "Class "..a.className end}
163
}