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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use std::marker::PhantomData;
use ffi;
use LuaContext;
use AsLua;
use AsMutLua;
use Push;
use PushGuard;
use LuaRead;
pub struct LuaTable<L> {
table: L,
}
unsafe impl<L> AsLua for LuaTable<L> where L: AsLua {
fn as_lua(&self) -> LuaContext {
self.table.as_lua()
}
}
unsafe impl<L> AsMutLua for LuaTable<L> where L: AsMutLua {
fn as_mut_lua(&mut self) -> LuaContext {
self.table.as_mut_lua()
}
}
impl<L> LuaRead<L> for LuaTable<L> where L: AsMutLua {
fn lua_read_at_position(mut lua: L, index: i32) -> Result<LuaTable<L>, L> {
assert!(index == -1);
if unsafe { ffi::lua_istable(lua.as_mut_lua().0, index) } {
Ok(LuaTable { table: lua })
} else {
Err(lua)
}
}
}
pub struct LuaTableIterator<'t, L: 't, K, V> where L: AsMutLua {
table: &'t mut LuaTable<L>,
finished: bool,
marker: PhantomData<(K, V)>,
}
unsafe impl<'t, L, K, V> AsLua for LuaTableIterator<'t, L, K, V> where L: AsMutLua {
fn as_lua(&self) -> LuaContext {
self.table.as_lua()
}
}
unsafe impl<'t, L, K, V> AsMutLua for LuaTableIterator<'t, L, K, V> where L: AsMutLua {
fn as_mut_lua(&mut self) -> LuaContext {
self.table.as_mut_lua()
}
}
impl<L> LuaTable<L> where L: AsMutLua {
pub fn into_inner(self) -> L {
self.table
}
pub fn iter<K, V>(&mut self) -> LuaTableIterator<L, K, V> {
unsafe { ffi::lua_pushnil(self.table.as_mut_lua().0) };
LuaTableIterator {
table: self,
finished: false,
marker: PhantomData,
}
}
pub fn get<'a, R, I>(&'a mut self, index: I) -> Option<R>
where R: LuaRead<PushGuard<&'a mut LuaTable<L>>>,
I: for<'b> Push<&'b mut &'a mut LuaTable<L>>
{
let mut me = self;
index.push_to_lua(&mut me).forget();
unsafe { ffi::lua_gettable(me.as_mut_lua().0, -2); }
if unsafe { ffi::lua_isnil(me.as_lua().0, -1) } {
let _guard = PushGuard { lua: me, size: 1 };
return None;
}
let guard = PushGuard { lua: me, size: 1 };
LuaRead::lua_read(guard).ok()
}
pub fn into_get<'a, R, I>(self, index: I) -> Result<R, PushGuard<Self>>
where R: LuaRead<PushGuard<LuaTable<L>>>,
I: for<'b> Push<&'b mut LuaTable<L>>
{
let mut me = self;
index.push_to_lua(&mut me).forget();
unsafe { ffi::lua_gettable(me.as_mut_lua().0, -2); }
let is_nil = unsafe { ffi::lua_isnil(me.as_mut_lua().0, -1) };
let guard = PushGuard { lua: me, size: 1 };
if is_nil {
Err(guard)
} else {
LuaRead::lua_read(guard)
}
}
pub fn set<'s, I, V>(&'s mut self, index: I, value: V)
where I: for<'a> Push<&'a mut &'s mut LuaTable<L>>,
V: for<'a> Push<&'a mut &'s mut LuaTable<L>>
{
let mut me = self;
index.push_to_lua(&mut me).forget();
value.push_to_lua(&mut me).forget();
unsafe { ffi::lua_settable(me.as_mut_lua().0, -3); }
}
pub fn empty_array<'s, I>(&'s mut self, index: I) -> LuaTable<PushGuard<&'s mut LuaTable<L>>>
where I: for<'a> Push<&'a mut &'s mut LuaTable<L>> + Clone
{
let mut me = self;
index.clone().push_to_lua(&mut me).forget();
Vec::<u8>::with_capacity(0).push_to_lua(&mut me).forget();
unsafe { ffi::lua_settable(me.as_mut_lua().0, -3); }
me.get(index).unwrap()
}
pub fn get_or_create_metatable(mut self) -> LuaTable<PushGuard<L>> {
let result = unsafe { ffi::lua_getmetatable(self.table.as_mut_lua().0, -1) };
if result == 0 {
unsafe {
ffi::lua_newtable(self.table.as_mut_lua().0);
ffi::lua_setmetatable(self.table.as_mut_lua().0, -2);
let r = ffi::lua_getmetatable(self.table.as_mut_lua().0, -1);
assert!(r != 0);
}
}
LuaTable {
table: PushGuard { lua: self.table, size: 1 }
}
}
}
impl<'t, L, K, V> Iterator for LuaTableIterator<'t, L, K, V>
where L: AsMutLua + 't,
K: for<'i, 'j> LuaRead<&'i mut &'j mut LuaTableIterator<'t, L, K, V>> + 'static,
V: for<'i, 'j> LuaRead<&'i mut &'j mut LuaTableIterator<'t, L, K, V>> + 'static
{
type Item = Option<(K, V)>;
fn next(&mut self) -> Option<Option<(K,V)>> {
if self.finished {
return None;
}
if unsafe { ffi::lua_next(self.table.as_mut_lua().0, -2) } == 0 {
self.finished = true;
return None;
}
let mut me = self;
let key = LuaRead::lua_read_at_position(&mut me, -2).ok();
let value = LuaRead::lua_read_at_position(&mut me, -1).ok();
unsafe { ffi::lua_pop(me.table.as_mut_lua().0, 1) };
if key.is_none() || value.is_none() {
Some(None)
} else {
Some(Some((key.unwrap(), value.unwrap())))
}
}
}
impl<'t, L, K, V> Drop for LuaTableIterator<'t, L, K, V> where L: AsMutLua + 't {
fn drop(&mut self) {
if !self.finished {
unsafe { ffi::lua_pop(self.table.table.as_mut_lua().0, 1) }
}
}
}