Replies: 1 comment 1 reply
-
There are few options to solve this:
let lua = Lua::new();
let data = Rc::new(RefCell::new(SharedUserData::default()));
lua.load(chunk! {
function incr(data)
print("[Lua]: Before = " .. data:get_val());
data:incr_val();
print("[Lua]: After = " .. data:get_val());
end
})
.exec()?;
println!("[Rust]: Before = {}", data.borrow().get_val());
lua.globals()
.get::<_, Function>("incr")?
.call::<_, ()>(data.clone())?;
println!("[Rust]: After = {}", data.borrow().get_val());
let lua = Lua::new();
let data = SharedUserData::default();
lua.load(chunk! {
function incr(data)
print("[Lua]: Before = " .. data:get_val());
data:incr_val();
print("[Lua]: After = " .. data:get_val());
end
})
.exec()?;
println!("[Rust]: Before = {}", data.get_val());
let data_ud = lua.create_userdata(data)?;
lua.globals()
.get::<_, Function>("incr")?
.call::<_, ()>(data_ud.clone())?;
let data = data_ud.take::<SharedUserData>()?;
println!("[Rust]: After = {}", data.get_val());
let lua = Lua::new();
let data = lua.create_userdata(SharedUserData::default())?;
lua.load(chunk! {
function incr(data)
print("[Lua]: Before = " .. data:get_val());
data:incr_val();
print("[Lua]: After = " .. data:get_val());
end
})
.exec()?;
println!("[Rust]: Before = {}", data.borrow::<SharedUserData>()?.get_val());
lua.globals()
.get::<_, Function>("incr")?
.call::<_, ()>(data.clone())?;
println!("[Rust]: After = {}", data.borrow::<SharedUserData>()?.get_val()); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there any way to make this example work?
I have it cloning right now because it's the only way I can get the UserData to pass across the interface, but obviously that means the increment in the Lua function doesn't propagate back to Rust. I believe the UserData can't cross the boundary as a reference because it's not static, but I'm not sure how to really resolve this.
Beta Was this translation helpful? Give feedback.
All reactions