diff options
Diffstat (limited to 'src/lua/mod.rs')
| -rw-r--r-- | src/lua/mod.rs | 57 |
1 files changed, 24 insertions, 33 deletions
diff --git a/src/lua/mod.rs b/src/lua/mod.rs index afa6171..e7c3352 100644 --- a/src/lua/mod.rs +++ b/src/lua/mod.rs @@ -1,10 +1,6 @@ -use std::{ - path::Path, - sync::{Mutex, MutexGuard}, -}; - -use lazy_static::lazy_static; use mlua::prelude::*; +use once_cell::sync::Lazy; +use std::{path::Path, ptr::addr_of_mut}; use crate::{ config::{ @@ -18,27 +14,8 @@ use crate::{ pub mod evalsto; pub mod iobuffer; pub mod ownedfunction; -pub mod runtime; pub mod runnable; - -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 - }; -} +pub mod runtime; macro_rules! ud_is_type { ($writer:ident, $ud:ident, $f:ty => $fe:expr $(, $($r:ty => $e:expr),+)? $(,)?) => { @@ -61,11 +38,11 @@ fn print(_: &Lua, args: LuaMultiValue) -> LuaResult<()> { 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, + GlobalConfig => GlobalConfig::get(), + Theme => GlobalConfig::get().theme, + SheetViewTheme => GlobalConfig::get().theme.view, + GlobalState => GlobalState::get(), + SheetViewState => GlobalState::get().view, ); } else { writer.write(format!("{:#?}", arg)); @@ -79,8 +56,22 @@ fn print(_: &Lua, args: LuaMultiValue) -> LuaResult<()> { Ok(()) } -pub fn get() -> MutexGuard<'static, Lua> { - LUA.lock().unwrap() +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<()> { |