1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::{path::Path, sync::Mutex};

use mlua::{Lua, MultiValue, Table, UserData};
use once_cell::sync::OnceCell;

use crate::{FrameworkError, FrameworkGlobal};

pub mod lua {
    pub use mlua::*;
}

pub struct ScriptHost {
    lua: Mutex<Lua>,
}

impl FrameworkGlobal for ScriptHost {
    fn cell() -> &'static OnceCell<Self> {
        static INSTANCE: OnceCell<ScriptHost> = OnceCell::new();
        &INSTANCE
    }

    fn create() -> Result<Self, FrameworkError> {
        Ok(ScriptHost {
            lua: Mutex::new(Lua::new()),
        })
    }
}

pub trait ScriptType: UserData + Send + Sync {}
impl<T: UserData + Send + Sync> ScriptType for T {}

impl ScriptHost {
    pub fn eval<
        S: AsRef<str>,
        F: FnOnce(Result<MultiValue, mlua::Error>) -> Result<T, FrameworkError>,
        T,
    >(
        &'_ self,
        code: S,
        result_handler: F,
    ) -> Result<T, FrameworkError> {
        result_handler(
            self.lua
                .lock()
                .unwrap()
                .load(code.as_ref())
                .eval::<MultiValue>(),
        )
    }

    pub fn load_script<P>(&self, path: P) -> Result<(), FrameworkError>
    where
        P: AsRef<Path>,
    {
        let script_text = std::fs::read_to_string(path.as_ref())?;
        let _: mlua::Value = self
            .lua
            .lock()
            .expect("lua lock was poisoned")
            .load(&script_text)
            .eval()
            .unwrap(); // TODO: propagate error

        Ok(())
    }

    pub fn set_table<S, F>(&self, name: S, constructor: F) -> Result<(), FrameworkError>
    where
        S: AsRef<str>,
        F: FnOnce(Table) -> Result<Table, mlua::Error>,
    {
        let lua = self.lua.lock().expect("lua lock was poisoned");
        let globals = lua.globals();

        let _ = globals.set(
            name.as_ref(),
            constructor(
                lua.create_table()
                    .expect("failed basic lua operation: create_table"),
            )?,
        );

        Ok(())
    }

    pub fn set<S, T>(&self, name: S, value: T)
    where
        S: AsRef<str>,
        T: ScriptType + 'static,
    {
        let lua = self.lua.lock().expect("lua lock was poisoned");
        let globals = lua.globals();

        // TODO: propagate this error?
        let _ = globals.set(name.as_ref(), value);
    }
}