Skip to content

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

Description

@ramon-bernardo

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions