aboutsummaryrefslogtreecommitdiffstats
path: root/src/theming/lua.rs
blob: 22ad4d6b97a63171dfcee6b2ba69c8f3b88a15a5 (plain) (blame)
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
// SPDX-License-Identifier: AGPL-3.0-only
use std::sync::LazyLock;
use std::collections::BTreeMap;
use std::ops::Deref;

use tokio::sync::Mutex;
use mlua::*;

use crate::CONFIG;

static LUA: LazyLock<Mutex<Lua>> = LazyLock::new(|| {
  let stdlib = StdLib::TABLE | StdLib::STRING | StdLib::UTF8 | StdLib::BIT | StdLib::MATH;
  let lua = Lua::new_with(stdlib, LuaOptions::new().thread_pool_size(2)).expect("lua initialization");

  lua.sandbox(true).expect("lua initialization");

  lua.set_compiler(
    if CONFIG.theme_debug { Compiler::new().set_optimization_level(0).set_debug_level(2) }
    else { Compiler::new().set_optimization_level(2).set_debug_level(1) }
  );

  let expect: Value = lua.load(include_str!("lua-lib/expect.lua"))
    .set_compiler(Compiler::new().set_optimization_level(2).set_debug_level(1))
    .eval()
    .expect("expect.lua loading");
  lua.globals().set("expect", expect);

  let html: Value = lua.load(include_str!("lua-lib/html.lua"))
    .set_compiler(Compiler::new().set_optimization_level(2).set_debug_level(1))
    .eval()
    .expect("html.lua loading");
  lua.globals().set("html", html);

  Mutex::new(lua)
});