Skip to content
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

Problem when change a inner field in a mutable struct #835

Open
ramon-bernardo opened this issue Oct 17, 2024 · 0 comments
Open

Problem when change a inner field in a mutable struct #835

ramon-bernardo opened this issue Oct 17, 2024 · 0 comments

Comments

@ramon-bernardo
Copy link
Contributor

ramon-bernardo commented Oct 17, 2024

The inner field value of a struct is not being changed, requiring extraction and reinsertion.

Minimal example:

pub fn main(player) {
    dbg!(player.position.x);
    player.position.x = 0;
    dbg!(player.position.x);
}
use rune::alloc::clone::TryClone;
use rune::termcolor::{ColorChoice, StandardStream};
use rune::{Any, ContextError, Diagnostics, Module, Vm};

use std::sync::Arc;

#[derive(Any, TryClone, Debug)]
pub struct Position {
    #[rune(get, set)]
    pub x: u16,
    #[rune(get, set)]
    pub y: u16,
}

#[derive(Any, Debug)]
pub struct Player {
    #[rune(get, set)]
    pub position: Position,
}

pub fn module() -> Result<Module, ContextError> {
    let mut module = Module::new();
    module.ty::<Position>()?;
    module.ty::<Player>()?;
    Ok(module)
}

fn main() -> rune::support::Result<()> {
    let mut context = rune_modules::default_context()?;
    context.install(module()?)?;

    let mut sources = rune::sources!(
        entry => {
            pub fn main(player) {
                dbg!(player.position.x);
                player.position.x = 0;
                dbg!(player.position.x);
            }
        }
    );

    let mut diagnostics = Diagnostics::new();

    let result = rune::prepare(&mut sources)
        .with_context(&context)
        .with_diagnostics(&mut diagnostics)
        .build();

    if !diagnostics.is_empty() {
        let mut writer = StandardStream::stderr(ColorChoice::Always);
        diagnostics.emit(&mut writer, &sources)?;
    }

    let mut vm = Vm::new(Arc::new(context.runtime()?), Arc::new(result?));

    let mut player = Player {
        position: Position { x: 10, y: 10 },
    };

    vm.call(["main"], (&mut player,))?;
    Ok(())
}

Output

10
10

Expected result

10
0

Note: when the field is extracted, it works.

pub fn main(player) {
    let position = player.position;
    dbg!(position.x);
    position.x = 0;
    dbg!(position.x);
    player.position = position;
    dbg!(player.position.x);
}

Ouput

10
0
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant