-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmeta-events5.2.lua
46 lines (39 loc) · 1.03 KB
/
meta-events5.2.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
-- from incredible-gmod.ru with <3
-- /~https://github.com/Be1zebub/Small-GLua-Things/blob/master/meta-events5.2.lua
-- __len, __pairs, __ipairs meta table events support for lua 5.1 (this meta-events added in 5.2)
if tonumber((_VERSION:gsub("Lua ", ""))) < 5.2 and not lua_52_compat then
lua_52_compat = true
local __pairs = pairs
function pairs(tbl)
local mt = getmetatable(tbl)
if mt and mt.__pairs then
return mt:__pairs()
else
return __pairs(tbl)
end
end
local __ipairs = ipairs
function ipairs(tbl)
local mt = getmetatable(tbl)
if mt and mt.__ipairs then
return mt:__ipairs()
else
return __ipairs(tbl)
end
end
end
-- to add __len support, make your instances with newproxy(true)
--[[ Example:
local class = {}
function class:__len()
return 123
end
function class:new()
local instance = newproxy(true)
debug.setmetatable(instance, self)
return instance
end
local instance = class:new()
print(#instance)
]]--
-- Usage example: /~https://github.com/Be1zebub/Small-GLua-Things/blob/master/nwtable.lua