-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmetatable.lua
61 lines (55 loc) · 1.13 KB
/
metatable.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
values = {
useInteger = function(meta, method)
meta[method] = 1
end,
useString = function(meta, method)
meta[method] = "Hello"
end,
useBoolean = function(meta, method)
meta[method] = true
end,
useFunction = function(meta, method)
meta[method] = function()
return "Used Function"
end
end,
useObject = function(meta, method)
meta[method] = {}
end,
useCallableObject = function(meta, method)
local object = {}
local objectMeta = {
__call = function()
return "Used Callable Object"
end
}
setmetatable(object, objectMeta)
meta[method] = object
end
}
metamethods = {
__index = function(object)
return object.test
end,
__newindex = function(object)
object.test = "Value"
end,
__call = function(object)
return object("Hello")
end
}
for method, test in pairs(metamethods) do
for typeName, type in pairs(values) do
local value = {}
local meta = {}
setmetatable(value, meta)
type(meta, method)
local success, result = pcall(test, value)
if success then
print(method, typeName, "SUCCESS", result)
else
print(method, typeName, "ERROR", result)
end
end
end
loadstring("b = 5"); print(b)