summaryrefslogtreecommitdiff
path: root/src/lua/mod.rs
blob: 815b38dd119618f80949119ce3f2b85d6c4d6adc (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
use mlua::prelude::*;
use once_cell::sync::Lazy;
use std::{path::Path, ptr::addr_of_mut};

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 runnable;
pub mod runtime;

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

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

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

        if i < args.len() - 1 {
            content += ", ";
        }
    }
    content += "\n";

    writer.print(content);
    Ok(())
}

pub fn get() -> &'static mut Lazy<Lua> {
    static mut LUA: Lazy<Lua> = Lazy::new(|| {
        let lua = Lua::new();

        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();

        lua
    });


    unsafe { &mut *addr_of_mut!(LUA) }
}

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
        )))
    }
}

pub fn source_default_config() -> LuaResult<()> {
    get().load(include_str!("../init.lua")).exec()
}