-
-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't AnyUserData::borrow()
userdata created by Scope::create_userdata()
in mlua 0.10.0
#476
Comments
This is probably the same issue discussed in #475. |
This issue stems from #475, since I need this because I'm trying to build an iterator, and here's the full code (mlua 0.9.9): https://github.com/sxyazi/yazi/blob/main/yazi-fm/src/lives/yanked.rs#L32 |
|
Yes, In my real scenario,
This code can print "hello from foo" normally in 0.9.9, but not in 0.10.0: fn main() -> mlua::Result<()> {
struct Foo(String);
impl UserData for Foo {}
let lua = Lua::new();
lua.scope(|scope| {
let foo = Foo("hello from foo".to_string());
lua.globals().set("foo", scope.create_userdata(foo)?)?;
lua.globals().set(
"test",
lua.create_function(|_, ud: AnyUserData| {
let foo = ud.borrow::<Foo>()?;
Ok(foo.0.clone())
})?,
)?;
lua.load("print(test(foo))").exec()
})
}
I noticed that pub fn create_userdata<T>(&self, data: T) -> Result<AnyUserData<'lua>>
where
T: UserData + 'static, Function signature for 0.10.0: pub fn create_userdata<T>(&'scope self, data: T) -> Result<AnyUserData>
where
T: UserData + 'env, |
If you used |
Sorry for the confusion, let me explain it again. I'm fixing an issue where my iterator doesn't work in 0.10.0, but it works in 0.9.9, and here, So I'm finding it hard to describe my case as it also involves some unsafe code, but I've written a new example where you can just think of each fn main() -> mlua::Result<()> {
struct Iter;
impl Iterator for Iter {
type Item = &'static usize;
fn next(&mut self) -> Option<Self::Item> { Some(&123) }
}
impl UserData for Iter {}
let lua = Lua::new();
lua.scope(|scope| {
lua.globals().set("iter", scope.create_userdata(Iter)?)?;
lua.globals().set(
"test",
// mlua 0.9.9 - works
lua.create_function(|_, mut iter: UserDataRefMut<Iter>| Ok(iter.next().copied()))?,
// mlua 0.10.0 - doesn't work
// lua.create_function(|_, iter: AnyUserData| {
// iter.borrow_mut_scoped(|iter: &mut Iter| iter.next().copied())
// })?,
)?;
lua.load("print(test(iter))").exec()
})
} Hope this helps with understanding! |
Thanks for the information, I'll try to sort it out soon. |
@sxyazi Would making |
If I understood correctly, you mean using the combination of I like this idea, as it allows Also, I noticed that the implementation of Lines 204 to 206 in 4f56575
Should I implement this deregistration myself when manually calling |
It allow to create non-static userdata objects (that does not implement I removed the original Your case is a bit unusual as you use unsafe and moved |
If you used previously So, |
Thanks for the clarification! I think I understand what's happening with the
|
@sxyazi I added Eg: let ud = lua.create_any_userdata(String::from("foo"))?;
lua.scope(|scope| {
scope.add_destructor(|| {
_ = ud.take::<String>();
});
Ok(())
})?;
// ud is destroyed now Unfortunately, exposing let ud = lua.create_any_userdata(String::from("hello"))?;
ud.borrow_scoped::<StdString, _>(|s| {
lua.scope(|scope| {
scope.seal_userdata(&ud)?;
Ok(())
})
.unwrap();
assert_eq!(s, "foo"); // Use after free!
})?; |
Is there a way to drop I want to store all my |
I'm working on it |
I added |
I switched Huge thanks for your perfect support on this niche user case, |
I have the following code that worked in 0.9.9:
which prints:
But with
mlua
0.10.0, I got this error:Change
borrow()
toborrow_scoped()
doesn't make any difference either:The text was updated successfully, but these errors were encountered: