summaryrefslogtreecommitdiff
path: root/src/lua/mod.rs
blob: e10d83bd69c58cfb57682d0d1bbc7a5fec14bcd4 (plain)
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, MutexGuard},
};

use lazy_static::lazy_static;
use mlua::prelude::*;

use crate::{
    config::{
        theme::{view::SheetViewTheme, Theme},
        GlobalConfig,
    },
    sheet::cell::CellRef,
    state::{view::SheetViewState, GlobalState},
};

pub mod evalsto;
pub mod iobuffer;
pub mod ownedfunction;
pub mod runtime;

lazy_static! {
    static ref LUA: Mutex<Lua> = {
        let lock = Mutex::new(Lua::new());

        {
            let lua = lock.lock().unwrap();

            let print_binding = lua.create_function(print).unwrap();
            lua.globals().set("print", print_binding).unwrap();

            runtime::math(&lua).unwrap();
            runtime::neosheet(&lua).unwrap();
            runtime::package(&lua).unwrap();
        }

        lock
    };
}

macro_rules! ud_is_type {
    ($writer:ident, $ud:ident, $f:ty => $fe:expr $(, $($r:ty => $e:expr),+)? $(,)?) => {
        if $ud.is::<$f>() {
            $writer.write(format!("{:#?}", $fe))
        } $($( else if $ud.is::<$r>() {
            $writer.write(format!("{:#?}", $e))
        })*)? else {
            $writer.write(format!("{:#?}", $ud));
        }
    };
}

fn print(_: &Lua, args: LuaMultiValue) -> LuaResult<()> {
    let mut writer = iobuffer::iobuffer().write().unwrap();

    for (i, arg) in args.iter().enumerate() {
        if let Some(ud) = arg.as_userdata() {
            ud_is_type!(
                writer,
                ud,
                CellRef => ud.borrow::<CellRef>(),
                GlobalConfig => GlobalConfig::instance(),
                Theme => GlobalConfig::instance().theme,
                SheetViewTheme => GlobalConfig::instance().theme.view,
                GlobalState => GlobalState::instance(),
                SheetViewState => GlobalState::instance().view,
            );
        } else {
            writer.write(format!("{:#?}", arg));
        }

        if i < args.len() - 1 {
            writer.write(", ");
        }
    }
    writer.writeln("");
    Ok(())
}

pub fn get() -> MutexGuard<'static, Lua> {
    LUA.lock().unwrap()
}

pub fn source(path: &str) -> LuaResult<()> {
    let path = Path::new(path);

    if path.exists() && path.is_file() {
        get().load(path).exec()?;
        Ok(())
    } else {
        Err(LuaError::runtime(format!(
            "could not source {:?} (file not found)",
            path
        )))
    }
}