Skip to content

Commit 07cf801

Browse files
committed
Reuse Scope::create_any_userdata in Scope::create_userdata.
The methods are identical.
1 parent 9a512ae commit 07cf801

1 file changed

Lines changed: 1 addition & 32 deletions

File tree

src/scope.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -156,38 +156,7 @@ impl<'scope, 'env: 'scope> Scope<'scope, 'env> {
156156
where
157157
T: UserData + 'env,
158158
{
159-
let state = self.lua.state();
160-
unsafe {
161-
let _sg = StackGuard::new(state);
162-
check_stack(state, 3)?;
163-
164-
// We don't write the data to the userdata until pushing the metatable
165-
let protect = !self.lua.unlikely_memory_error();
166-
#[cfg(feature = "luau")]
167-
let ud_ptr = {
168-
let data = UserDataStorage::new_scoped(data);
169-
util::push_userdata(state, data, protect)?
170-
};
171-
#[cfg(not(feature = "luau"))]
172-
let ud_ptr = util::push_uninit_userdata::<UserDataStorage<T>>(state, protect)?;
173-
174-
// Push the metatable and register it with no TypeId
175-
let mut registry = UserDataRegistry::new_unique(self.lua.lua(), ud_ptr as *mut _);
176-
T::register(&mut registry);
177-
self.lua.push_userdata_metatable(registry.into_raw())?;
178-
let mt_ptr = ffi::lua_topointer(state, -1);
179-
self.lua.register_userdata_metatable(mt_ptr, None);
180-
181-
// Write data to the pointer and attach metatable
182-
#[cfg(not(feature = "luau"))]
183-
std::ptr::write(ud_ptr, UserDataStorage::new_scoped(data));
184-
ffi::lua_setmetatable(state, -2);
185-
186-
let ud = AnyUserData(self.lua.pop_ref());
187-
self.seal_userdata::<T>(&ud);
188-
189-
Ok(ud)
190-
}
159+
self.create_any_userdata(data, T::register)
191160
}
192161

193162
/// Creates a Lua userdata object from a custom Rust type.

0 commit comments

Comments
 (0)