-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.lua
54 lines (47 loc) · 1.39 KB
/
utils.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
--
-- Objective-C Classes
--
local Class = {}
setmetatable(Class, {__index = function(tbl, key)
local cls = objc.getclass(key)
tbl[key] = cls
return cls
end})
objc.class = Class
--
-- Context
--
local Context = {}
function Context:create ()
local s = {stack = objc.newstack()}
setmetatable(s, {__index = self})
return s
end
function Context:sendMesg (target, selector, ...)
local stack = self.stack
local n = select("#", ...)
for i = 1, n do
local arg = select(-i, ...)
objc.push(stack, arg)
end
objc.push(stack, target, selector)
objc.operate(stack, "call")
return objc.pop(stack)
end
function Context:wrap(obj)
local o = {}
setmetatable(o, {__call = function (func, ...)
-- print("obj called!", func, obj)
local ret = self:sendMesg(obj, ...)
if type(ret) == "userdata" then
return self:wrap(ret)
else
return ret
end
end,
__unm = function (op)
return obj
end})
return o
end
objc.context = Context