bones_scripting/lua/bindings/
world.rs

1use super::*;
2
3pub fn metatable(ctx: Context) -> Table {
4    let metatable = Table::new(&ctx);
5    metatable
6        .set(
7            ctx,
8            "__tostring",
9            Callback::from_fn(&ctx, |ctx, _fuel, mut stack| {
10                stack.push_front(
11                    piccolo::String::from_static(&ctx, "World { resources, components, assets }")
12                        .into(),
13                );
14                Ok(CallbackReturn::Return)
15            }),
16        )
17        .unwrap();
18
19    metatable
20        .set(ctx, "__newindex", ctx.singletons().get(ctx, no_newindex))
21        .unwrap();
22    metatable
23        .set(
24            ctx,
25            "__index",
26            Callback::from_fn(&ctx, move |ctx, _fuel, mut stack| {
27                let (world, key): (&WorldRef, lua::String) = stack.consume(ctx)?;
28
29                let singletons = ctx.singletons();
30                let resources_metatable = singletons.get(ctx, super::resources::metatable);
31                let components_metatable = singletons.get(ctx, super::components::metatable);
32                let assets_metatable = singletons.get(ctx, super::assets::metatable);
33
34                match key.as_bytes() {
35                    b"resources" => {
36                        let resources = UserData::new_static(&ctx, world.clone());
37                        resources.set_metatable(&ctx, Some(resources_metatable));
38                        stack.push_front(resources.into());
39                    }
40                    b"components" => {
41                        let components = UserData::new_static(&ctx, world.clone());
42                        components.set_metatable(&ctx, Some(components_metatable));
43                        stack.push_front(components.into());
44                    }
45                    b"assets" => {
46                        let assets = UserData::new_static(&ctx, world.clone());
47                        assets.set_metatable(&ctx, Some(assets_metatable));
48                        stack.push_front(assets.into());
49                    }
50                    _ => (),
51                }
52
53                Ok(CallbackReturn::Return)
54            }),
55        )
56        .unwrap();
57
58    metatable
59}