Git Product home page Git Product logo

Comments (4)

jugglerchris avatar jugglerchris commented on July 19, 2024 1

Thanks for letting us know, and hope you find the issue!

from rlua.

makspll avatar makspll commented on July 19, 2024

And i think what makes me think this is a bug is that if I change the signature of the metamethod to force idx to be a String instead of a Value calling comp.vec[0] = 5.4 results in a runtime error.

from rlua.

jugglerchris avatar jugglerchris commented on July 19, 2024

Hi,
I agree this doesn't sound right.
The only Lua behaviour that comes to mind is that if you have an ordinary table, then the __newindex or __index metamethods aren't called if the table already contains that key - but if it's a userdata that shouldn't be an issue.

Are you able to reproduce this in an ideally smaller rlua-only example (or at least something I can run and try out)?

from rlua.

makspll avatar makspll commented on July 19, 2024

Apologies for the delay, left for holiday very soon after posting this issue,

I've just gotten back and attempted to reproduce with a minimal example. I was unable to reproduce so this makes me think that this may very well be UB from my side (playing with bevy and pointers), I will close this for now but if i fix things on my side and still bump into this I will re-open and link to the full example.

Sorry!

For context this is what I tried and it works:

use rlua::{Value,UserData,MetaMethod,Function,prelude::*};

#[derive(Debug)]
pub struct MyUserData {
    v : [u32;3]
}

impl UserData for MyUserData {
    fn add_methods<'lua, T: LuaUserDataMethods<'lua, Self>>(methods: &mut T) {
        methods.add_meta_method(MetaMethod::ToString, |_,s,()| {
            Ok(format!("{:?}",s))
        });

        methods.add_meta_method_mut(MetaMethod::NewIndex, |_,s,(idx,val) : (Value,u32)|{
            let idx = match idx {
                Value::Integer(i) => i as usize,
                Value::String(s) => match s.to_str().unwrap() {
                    "x" => 0,
                    "y" => 1,
                    "z" => 2,
                    _ => panic!()
                },
                _ => panic!(),
            };

            Ok(s.v[idx] = val)
        })
    }
}


fn main() {
    let lua = Lua::new();
    lua.context(|lua_ctx| {
        lua_ctx
            .load("
            function on_update(my_user_data)
                print(my_user_data)
                my_user_data[0] = 69
                my_user_data.y = 42
                print(my_user_data)
            end
            ")
            .exec()
    }).unwrap();

    lua.context(|lua_ctx| {
        let g = lua_ctx.globals();
        let f : Function = g.get("on_update").unwrap();

        f.call::<MyUserData,()>(MyUserData{
            v: [0,1,2],
        }).unwrap();
    })

}

from rlua.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.