diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-10 19:06:46 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-10 19:06:46 +0200 |
| commit | 63cfcbe7a7745b276de58ec92e0141b958c44feb (patch) | |
| tree | 990e33a83756e27187033579ee2f85d5c79169d5 /src/lua | |
| parent | b747ca8af52129876b577a4f20f7105a05c6b002 (diff) | |
use unsafe blocks instead of mutexes
Diffstat (limited to 'src/lua')
| -rw-r--r-- | src/lua/mod.rs | 57 | ||||
| -rw-r--r-- | src/lua/runtime.rs | 8 |
2 files changed, 28 insertions, 37 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<()> { diff --git a/src/lua/runtime.rs b/src/lua/runtime.rs index 6a0a230..71bf21a 100644 --- a/src/lua/runtime.rs +++ b/src/lua/runtime.rs @@ -50,10 +50,10 @@ pub fn package(lua: &Lua) -> Result<()> { "path", format!( "{}/?.lua;{}/?/init.lua;{}/lib/?.lua;{}/lib/?/init.lua;{};./?.lua", - config::constants::USER_CONFIG_DIR.as_str(), - config::constants::USER_CONFIG_DIR.as_str(), - config::constants::USER_CONFIG_DIR.as_str(), - config::constants::USER_CONFIG_DIR.as_str(), + config::env::USER_CONFIG_DIR.as_str(), + config::env::USER_CONFIG_DIR.as_str(), + config::env::USER_CONFIG_DIR.as_str(), + config::env::USER_CONFIG_DIR.as_str(), path.split(';') .filter(|s| s.starts_with('/')) .collect::<Vec<&str>>() |