summaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorkyren <kerriganw@gmail.com>2018-02-07 16:42:03 -0500
committerkyren <kerriganw@gmail.com>2018-02-07 16:42:03 -0500
commit98ee4e9492b626268b7170d713686da6a258cbb9 (patch)
tree50e25aec3243ce94d1469aaf3521b7da64a017eb /src/util.rs
parentab9841a02fb0c9ebe76e15247e68401fd13474cb (diff)
downloadmlua-98ee4e9492b626268b7170d713686da6a258cbb9.zip
More correct scope drop behavior
now no longer aborts if a Drop impl panics
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/util.rs b/src/util.rs
index 1de9b9f..aba6ba6 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -248,15 +248,9 @@ pub unsafe fn get_userdata<T>(state: *mut ffi::lua_State, index: c_int) -> *mut
ud
}
-pub unsafe extern "C" fn userdata_destructor<T>(state: *mut ffi::lua_State) -> c_int {
- callback_error(state, || {
- destruct_userdata::<T>(state);
- Ok(0)
- })
-}
-
-// Pops the userdata off of the top of the stack and drops it
-pub unsafe fn destruct_userdata<T>(state: *mut ffi::lua_State) {
+// Pops the userdata off of the top of the stack and returns it to rust, invalidating the lua
+// userdata.
+pub unsafe fn take_userdata<T>(state: *mut ffi::lua_State) -> T {
// We set the metatable of userdata on __gc to a special table with no __gc method and with
// metamethods that trigger an error on access. We do this so that it will not be double
// dropped, and also so that it cannot be used or identified as any particular userdata type
@@ -265,7 +259,14 @@ pub unsafe fn destruct_userdata<T>(state: *mut ffi::lua_State) {
ffi::lua_setmetatable(state, -2);
let ud = &mut *(ffi::lua_touserdata(state, -1) as *mut T);
ffi::lua_pop(state, 1);
- mem::replace(ud, mem::uninitialized());
+ mem::replace(ud, mem::uninitialized())
+}
+
+pub unsafe extern "C" fn userdata_destructor<T>(state: *mut ffi::lua_State) -> c_int {
+ callback_error(state, || {
+ take_userdata::<T>(state);
+ Ok(0)
+ })
}
// In the context of a lua callback, this will call the given function and if the given function