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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use lua::{Index, ToLua, FromLua, State, MULTRET};
use types::{LuaStackable, LuaGeneric};
use context::Context;
use error;
pub struct LuaFunction {
index: Index
}
fn get_callback_index(val: &Option<&LuaFunction>) -> Index {
match *val {
Some(val) => val.get_pos(),
None => 0
}
}
impl LuaFunction {
pub fn new(i: Index) -> LuaFunction {
LuaFunction {
index: i
}
}
pub fn pcall(&self, context: &mut Context, args: &[&ToLua], errfunc: Option<&LuaFunction>,
nresults: i32) -> error::Result<Vec<LuaGeneric>> {
let top_prev = context.get_state().get_top();
context.get_state().push_value(self.get_pos());
for arg in args {
arg.to_lua(context.get_state());
}
let threadstatus = context.get_state().pcall(
args.len() as i32, nresults, get_callback_index(&errfunc));
match error::get_status_from_threadstatus(threadstatus) {
Err(status) => {
error::new_luaresult_err(status, error::pop_error_from_state(context.get_state()))
},
Ok(_) => {
let top_post = context.get_state().get_top();
error::new_luaresult_ok((top_prev..top_post)
.map(|i| LuaGeneric::new(i+1))
.collect())
}
}
}
pub fn pcall_multiret(&self, context: &mut Context, args: &[&ToLua],
errfunc: Option<&LuaFunction>) -> error::Result<Vec<LuaGeneric>> {
self.pcall(context, args, errfunc, MULTRET)
}
pub fn pcall_singleret(&self, context: &mut Context, args: &[&ToLua],
errfunc: Option<&LuaFunction>) -> error::Result<Option<LuaGeneric>> {
self.pcall(context, args, errfunc, 1)
.map(|mut v| {
match v.len() {
0 => None,
_ => Some(v.swap_remove(0))
}
})
}
pub fn pcall_noret(&self, context: &mut Context, args: &[&ToLua],
errfunc: Option<&LuaFunction>) -> error::Result<()> {
self.pcall(context, args, errfunc, 0)
.map(|_|())
}
pub fn call(&self, context: &mut Context, args: &[&ToLua], nresults: i32) -> Vec<LuaGeneric> {
let top_prev = context.get_state().get_top();
context.get_state().push_value(self.get_pos());
for arg in args {
arg.to_lua(context.get_state());
}
context.get_state().call(args.len() as i32, nresults);
let top_post = context.get_state().get_top();
(top_prev..top_post)
.map(|i| LuaGeneric::new(i+1))
.collect()
}
pub fn call_singleret(&self, context: &mut Context, args: &[&ToLua]) -> Option<LuaGeneric> {
let mut result = self.call(context, args, 1);
match result.len() {
0 => None,
_ => Some(result.swap_remove(0))
}
}
pub fn call_multiret(&self, context: &mut Context, args: &[&ToLua]) -> Vec<LuaGeneric> {
self.call(context, args, MULTRET)
}
pub fn call_noret(&self, context: &mut Context, args: &[&ToLua]) {
self.call(context, args, 0);
}
}
impl LuaStackable for LuaFunction {
fn get_pos(&self) -> Index {
self.index
}
}
impl ToLua for LuaFunction {
fn to_lua(&self, state: &mut State) {
state.push_value(self.get_pos());
}
}
impl FromLua for LuaFunction {
fn from_lua(state: &mut State, index: Index) -> Option<LuaFunction> {
if state.is_fn(index) {
Some(LuaFunction::new(index))
} else {
None
}
}
}