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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use std::cell::RefCell;
use std::marker::PhantomData;
use std::mem;
use crate::error::{Error, Result};
use crate::function::Function;
use crate::state::{Lua, LuaGuard, RawLua};
use crate::traits::{FromLuaMulti, IntoLuaMulti};
use crate::types::{Callback, CallbackUpvalue, ScopedCallback, ValueRef};
use crate::userdata::{AnyUserData, UserData, UserDataRegistry, UserDataStorage};
use crate::util::{
self, assert_stack, check_stack, get_metatable_ptr, get_userdata, take_userdata, StackGuard,
};
/// Constructed by the [`Lua::scope`] method, allows temporarily creating Lua userdata and
/// callbacks that are not required to be `Send` or `'static`.
///
/// See [`Lua::scope`] for more details.
pub struct Scope<'scope, 'env: 'scope> {
lua: LuaGuard,
// Internal destructors run first, then user destructors (based on the declaration order)
destructors: Destructors<'env>,
user_destructors: UserDestructors<'env>,
_scope_invariant: PhantomData<&'scope mut &'scope ()>,
_env_invariant: PhantomData<&'env mut &'env ()>,
}
type DestructorCallback<'a> = Box<dyn FnOnce(&RawLua, ValueRef) -> Vec<Box<dyn FnOnce() + 'a>>>;
// Implement Drop on Destructors instead of Scope to avoid compilation error
struct Destructors<'a>(RefCell<Vec<(ValueRef, DestructorCallback<'a>)>>);
struct UserDestructors<'a>(RefCell<Vec<Box<dyn FnOnce() + 'a>>>);
impl<'scope, 'env: 'scope> Scope<'scope, 'env> {
pub(crate) fn new(lua: LuaGuard) -> Self {
Scope {
lua,
destructors: Destructors(RefCell::new(Vec::new())),
user_destructors: UserDestructors(RefCell::new(Vec::new())),
_scope_invariant: PhantomData,
_env_invariant: PhantomData,
}
}
/// Wraps a Rust function or closure, creating a callable Lua function handle to it.
///
/// This is a version of [`Lua::create_function`] that creates a callback which expires on
/// scope drop. See [`Lua::scope`] for more details.
pub fn create_function<F, A, R>(&'scope self, func: F) -> Result<Function>
where
F: Fn(&Lua, A) -> Result<R> + 'scope,
A: FromLuaMulti,
R: IntoLuaMulti,
{
unsafe {
self.create_callback(Box::new(move |rawlua, nargs| {
let args = A::from_stack_args(nargs, 1, None, rawlua)?;
func(rawlua.lua(), args)?.push_into_stack_multi(rawlua)
}))
}
}
/// Wraps a Rust mutable closure, creating a callable Lua function handle to it.
///
/// This is a version of [`Lua::create_function_mut`] that creates a callback which expires
/// on scope drop. See [`Lua::scope`] and [`Scope::create_function`] for more details.
pub fn create_function_mut<F, A, R>(&'scope self, func: F) -> Result<Function>
where
F: FnMut(&Lua, A) -> Result<R> + 'scope,
A: FromLuaMulti,
R: IntoLuaMulti,
{
let func = RefCell::new(func);
self.create_function(move |lua, args| {
(*func.try_borrow_mut().map_err(|_| Error::RecursiveMutCallback)?)(lua, args)
})
}
/// Creates a Lua userdata object from a reference to custom userdata type.
///
/// This is a version of [`Lua::create_userdata`] that creates a userdata which expires on
/// scope drop, and does not require that the userdata type be Send. This method takes
/// non-'static reference to the data. See [`Lua::scope`] for more details.
///
/// Userdata created with this method will not be able to be mutated from Lua.
pub fn create_userdata_ref<T>(&'scope self, data: &'env T) -> Result<AnyUserData>
where
T: UserData + 'static,
{
let ud = unsafe { self.lua.make_userdata(UserDataStorage::new_ref(data)) }?;
self.seal_userdata::<T>(&ud);
Ok(ud)
}
/// Creates a Lua userdata object from a mutable reference to custom userdata type.
///
/// This is a version of [`Lua::create_userdata`] that creates a userdata which expires on
/// scope drop, and does not require that the userdata type be Send. This method takes
/// non-'static mutable reference to the data. See [`Lua::scope`] for more details.
pub fn create_userdata_ref_mut<T>(&'scope self, data: &'env mut T) -> Result<AnyUserData>
where
T: UserData + 'static,
{
let ud = unsafe { self.lua.make_userdata(UserDataStorage::new_ref_mut(data)) }?;
self.seal_userdata::<T>(&ud);
Ok(ud)
}
/// Creates a Lua userdata object from a reference to custom Rust type.
///
/// This is a version of [`Lua::create_any_userdata`] that creates a userdata which expires on
/// scope drop, and does not require that the Rust type be Send. This method takes non-'static
/// reference to the data. See [`Lua::scope`] for more details.
///
/// Userdata created with this method will not be able to be mutated from Lua.
pub fn create_any_userdata_ref<T>(&'scope self, data: &'env T) -> Result<AnyUserData>
where
T: 'static,
{
let ud = unsafe { self.lua.make_any_userdata(UserDataStorage::new_ref(data)) }?;
self.seal_userdata::<T>(&ud);
Ok(ud)
}
/// Creates a Lua userdata object from a mutable reference to custom Rust type.
///
/// This is a version of [`Lua::create_any_userdata`] that creates a userdata which expires on
/// scope drop, and does not require that the Rust type be Send. This method takes non-'static
/// mutable reference to the data. See [`Lua::scope`] for more details.
pub fn create_any_userdata_ref_mut<T>(&'scope self, data: &'env mut T) -> Result<AnyUserData>
where
T: 'static,
{
let ud = unsafe { self.lua.make_any_userdata(UserDataStorage::new_ref_mut(data)) }?;
self.seal_userdata::<T>(&ud);
Ok(ud)
}
/// Creates a Lua userdata object from a custom userdata type.
///
/// This is a version of [`Lua::create_userdata`] that creates a userdata which expires on
/// scope drop, and does not require that the userdata type be `Send` or `'static`. See
/// [`Lua::scope`] for more details.
///
/// The main limitation that comes from using non-'static userdata is that the produced userdata
/// will no longer have a [`TypeId`] associated with it, because [`TypeId`] can only work for
/// `'static` types. This means that it is impossible, once the userdata is created, to get a
/// reference to it back *out* of an [`AnyUserData`] handle. This also implies that the
/// "function" type methods that can be added via [`UserDataMethods`] (the ones that accept
/// [`AnyUserData`] as a first parameter) are vastly less useful. Also, there is no way to
/// re-use a single metatable for multiple non-'static types, so there is a higher cost
/// associated with creating the userdata metatable each time a new userdata is created.
///
/// [`TypeId`]: std::any::TypeId
/// [`UserDataMethods`]: crate::UserDataMethods
pub fn create_userdata<T>(&'scope self, data: T) -> Result<AnyUserData>
where
T: UserData + 'env,
{
let state = self.lua.state();
unsafe {
let _sg = StackGuard::new(state);
check_stack(state, 3)?;
// We don't write the data to the userdata until pushing the metatable
let protect = !self.lua.unlikely_memory_error();
#[cfg(feature = "luau")]
let ud_ptr = {
let data = UserDataStorage::new_scoped(data);
util::push_userdata(state, data, protect)?
};
#[cfg(not(feature = "luau"))]
let ud_ptr = util::push_uninit_userdata::<UserDataStorage<T>>(state, protect)?;
// Push the metatable and register it with no TypeId
let mut registry = UserDataRegistry::new_unique(self.lua.lua(), ud_ptr as *mut _);
T::register(&mut registry);
self.lua.push_userdata_metatable(registry.into_raw())?;
let mt_ptr = ffi::lua_topointer(state, -1);
self.lua.register_userdata_metatable(mt_ptr, None);
// Write data to the pointer and attach metatable
#[cfg(not(feature = "luau"))]
std::ptr::write(ud_ptr, UserDataStorage::new_scoped(data));
ffi::lua_setmetatable(state, -2);
let ud = AnyUserData(self.lua.pop_ref());
self.seal_userdata::<T>(&ud);
Ok(ud)
}
}
/// Creates a Lua userdata object from a custom Rust type.
///
/// Since the Rust type is not required to be static and implement [`UserData`] trait,
/// you need to provide a function to register fields or methods for the object.
///
/// See also [`Scope::create_userdata`] for more details about non-static limitations.
pub fn create_any_userdata<T>(
&'scope self,
data: T,
register: impl FnOnce(&mut UserDataRegistry<T>),
) -> Result<AnyUserData>
where
T: 'env,
{
let state = self.lua.state();
let ud = unsafe {
let _sg = StackGuard::new(state);
check_stack(state, 3)?;
// We don't write the data to the userdata until pushing the metatable
let protect = !self.lua.unlikely_memory_error();
#[cfg(feature = "luau")]
let ud_ptr = {
let data = UserDataStorage::new_scoped(data);
util::push_userdata(state, data, protect)?
};
#[cfg(not(feature = "luau"))]
let ud_ptr = util::push_uninit_userdata::<UserDataStorage<T>>(state, protect)?;
// Push the metatable and register it with no TypeId
let mut registry = UserDataRegistry::new_unique(self.lua.lua(), ud_ptr as *mut _);
register(&mut registry);
self.lua.push_userdata_metatable(registry.into_raw())?;
let mt_ptr = ffi::lua_topointer(state, -1);
self.lua.register_userdata_metatable(mt_ptr, None);
// Write data to the pointer and attach metatable
#[cfg(not(feature = "luau"))]
std::ptr::write(ud_ptr, UserDataStorage::new_scoped(data));
ffi::lua_setmetatable(state, -2);
AnyUserData(self.lua.pop_ref())
};
self.seal_userdata::<T>(&ud);
Ok(ud)
}
/// Adds a destructor function to be run when the scope ends.
///
/// This functionality is useful for cleaning up any resources after the scope ends.
///
/// # Example
///
/// ```rust
/// # use mlua::{Error, Lua, Result};
/// # fn main() -> Result<()> {
/// let lua = Lua::new();
/// let ud = lua.create_any_userdata(String::from("hello"))?;
/// lua.scope(|scope| {
/// scope.add_destructor(|| {
/// _ = ud.take::<String>();
/// });
/// // Run the code that uses `ud` here
/// Ok(())
/// })?;
/// assert!(matches!(ud.borrow::<String>(), Err(Error::UserDataDestructed)));
/// # Ok(())
/// # }
pub fn add_destructor(&'scope self, destructor: impl FnOnce() + 'env) {
self.user_destructors.0.borrow_mut().push(Box::new(destructor));
}
unsafe fn create_callback(&'scope self, f: ScopedCallback<'scope>) -> Result<Function> {
let f = mem::transmute::<ScopedCallback, Callback>(f);
let f = self.lua.create_callback(f)?;
let destructor: DestructorCallback = Box::new(|rawlua, vref| {
let ref_thread = rawlua.ref_thread();
ffi::lua_getupvalue(ref_thread, vref.index, 1);
let upvalue = get_userdata::<CallbackUpvalue>(ref_thread, -1);
let data = (*upvalue).data.take();
ffi::lua_pop(ref_thread, 1);
vec![Box::new(move || drop(data))]
});
self.destructors.0.borrow_mut().push((f.0.clone(), destructor));
Ok(f)
}
/// Shortens the lifetime of the userdata to the lifetime of the scope.
fn seal_userdata<T: 'env>(&self, ud: &AnyUserData) {
let destructor: DestructorCallback = Box::new(|rawlua, vref| unsafe {
let state = rawlua.state();
let _sg = StackGuard::new(state);
assert_stack(state, 2);
// Ensure that userdata is not destructed
match rawlua.push_userdata_ref(&vref) {
Ok(Some(_)) => {}
Ok(None) => {
// Deregister metatable
let mt_ptr = get_metatable_ptr(state, -1);
rawlua.deregister_userdata_metatable(mt_ptr);
}
Err(_) => return vec![],
}
let data = take_userdata::<UserDataStorage<T>>(state);
vec![Box::new(move || drop(data))]
});
self.destructors.0.borrow_mut().push((ud.0.clone(), destructor));
}
}
impl Drop for Destructors<'_> {
fn drop(&mut self) {
// We separate the action of invalidating the userdata in Lua and actually dropping the
// userdata type into two phases. This is so that, in the event a userdata drop panics,
// we can be sure that all of the userdata in Lua is actually invalidated.
let destructors = mem::take(&mut *self.0.borrow_mut());
if let Some(lua) = destructors.first().map(|(vref, _)| vref.lua.lock()) {
// All destructors are non-panicking, so this is fine
let to_drop = destructors
.into_iter()
.flat_map(|(vref, destructor)| destructor(&lua, vref))
.collect::<Vec<_>>();
drop(to_drop);
}
}
}
impl Drop for UserDestructors<'_> {
fn drop(&mut self) {
let destructors = mem::take(&mut *self.0.borrow_mut());
for destructor in destructors {
destructor();
}
}
}