Remove unnecessary Box around Context
This commit is contained in:
1 parent
6fe15c63fe
commit
d4ca16f07e
9 files changed
+36
-36
No files matched your search
+3
-3
@@ -135,13 +135,13 @@ fn exit(l: &Lua, exit_msg: Option<String>) -> LuaResult<()> {
|
||||
if let Some(exit_msg) = exit_msg {
|
||||
println!("{exit_msg}");
|
||||
}
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
*ctx.should_exit() = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn restart(l: &Lua, _: ()) -> LuaResult<()> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
*ctx.needs_restart() = true;
|
||||
Ok(())
|
||||
}
|
||||
@@ -154,7 +154,7 @@ fn open_url(l: &Lua, url: String) -> LuaResult<MultiValue> {
|
||||
}
|
||||
|
||||
fn render_init(l: &Lua, features: Variadic<String>) -> LuaResult<()> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
for feature in features {
|
||||
if feature == "DPI_AWARE" {
|
||||
*ctx.is_dpi_aware() = true;
|
||||
|
||||
@@ -2,12 +2,12 @@ use crate::lua::Context;
|
||||
use mlua::{Lua, Result as LuaResult};
|
||||
|
||||
pub fn copy(l: &Lua, text: String) -> LuaResult<()> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
ctx.window().set_clipboard_text(text);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn paste(l: &Lua, _: ()) -> LuaResult<Option<String>> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
Ok(ctx.window().get_clipboard_text())
|
||||
}
|
||||
@@ -178,7 +178,7 @@ unsafe extern "C-unwind" fn image_handle_load(state: *mut ffi::lua_State) -> c_i
|
||||
let handle = unsafe { get_image_handle(state, 1) };
|
||||
|
||||
let lua = unsafe { Lua::get_or_init_from_ptr(state) };
|
||||
let ctx = lua.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = lua.app_data_ref::<Context>().unwrap();
|
||||
|
||||
// Path
|
||||
let path_ptr = unsafe { ffi::luaL_checklstring(state, 2, ptr::null_mut()) };
|
||||
|
||||
+2
-2
@@ -5,13 +5,13 @@ use crate::{
|
||||
use mlua::{Lua, Result as LuaResult};
|
||||
|
||||
pub fn get_cursor_pos(l: &Lua, _: ()) -> LuaResult<(u32, u32)> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
let pos = ctx.input().mouse_pos();
|
||||
Ok((pos.x as u32, pos.y as u32))
|
||||
}
|
||||
|
||||
pub fn is_key_down(l: &Lua, key_name: String) -> LuaResult<bool> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
|
||||
if let Some(key) = str_as_key(&key_name) {
|
||||
Ok(ctx.input().key_pressed(key))
|
||||
|
||||
+2
-2
@@ -12,7 +12,7 @@ pub fn protected_call(l: &Lua, (func, args): (Function, MultiValue)) -> LuaResul
|
||||
}
|
||||
|
||||
pub fn load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult<MultiValue> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
let mut module_path = ctx.script_dir().join(name);
|
||||
if module_path.extension().is_none() {
|
||||
module_path.set_extension("lua");
|
||||
@@ -26,7 +26,7 @@ pub fn load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult<Mul
|
||||
}
|
||||
|
||||
pub fn protected_load_module(l: &Lua, (name, args): (String, MultiValue)) -> LuaResult<MultiValue> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
let mut module_path = ctx.script_dir().join(name);
|
||||
if module_path.extension().is_none() {
|
||||
module_path.set_extension("lua");
|
||||
|
||||
+4
-4
@@ -7,13 +7,13 @@ use crate::{
|
||||
};
|
||||
|
||||
pub fn get_user_path(l: &Lua, _: ()) -> LuaResult<PathBuf> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
Ok(ctx.script_dir().join("userdata"))
|
||||
}
|
||||
|
||||
// parent directory of Launch.lua script
|
||||
pub fn get_script_path(l: &Lua, _: ()) -> LuaResult<PathBuf> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
Ok(ctx.script_dir().to_owned())
|
||||
}
|
||||
|
||||
@@ -26,13 +26,13 @@ pub fn get_runtime_path(_: &Lua, _: ()) -> LuaResult<PathBuf> {
|
||||
}
|
||||
|
||||
pub fn get_work_dir(l: &Lua, _: ()) -> LuaResult<PathBuf> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
Ok(ctx.current_working_dir().clone())
|
||||
}
|
||||
|
||||
// NOTE: unused
|
||||
pub fn set_work_dir(l: &Lua, path: String) -> LuaResult<()> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
if change_working_directory(&path).is_ok() {
|
||||
*ctx.current_working_dir() = path.into();
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ macro_rules! i32_from_stack {
|
||||
unsafe extern "C-unwind" fn set_draw_color(state: *mut ffi::lua_State) -> c_int {
|
||||
//profiling::scope!("set_draw_color");
|
||||
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
|
||||
let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
|
||||
|
||||
let nargs = unsafe { ffi::lua_gettop(state) };
|
||||
match nargs {
|
||||
@@ -116,7 +116,7 @@ unsafe extern "C-unwind" fn set_draw_color(state: *mut ffi::lua_State) -> c_int
|
||||
unsafe extern "C-unwind" fn get_draw_color(state: *mut ffi::lua_State) -> c_int {
|
||||
//profiling::scope!("get_draw_color");
|
||||
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
|
||||
let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
|
||||
|
||||
let color: [f32; 4] = ctx.recorder().get_draw_color().into();
|
||||
unsafe { ffi::lua_pushnumber(state, color[0] as f64) };
|
||||
@@ -130,7 +130,7 @@ unsafe extern "C-unwind" fn get_draw_color(state: *mut ffi::lua_State) -> c_int
|
||||
unsafe extern "C-unwind" fn set_viewport(state: *mut ffi::lua_State) -> c_int {
|
||||
//profiling::scope!("set_viewport");
|
||||
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
|
||||
let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
|
||||
|
||||
let nargs = unsafe { ffi::lua_gettop(state) };
|
||||
match nargs {
|
||||
@@ -154,7 +154,7 @@ unsafe extern "C-unwind" fn set_viewport(state: *mut ffi::lua_State) -> c_int {
|
||||
unsafe extern "C-unwind" fn set_draw_layer(state: *mut ffi::lua_State) -> c_int {
|
||||
//profiling::scope!("set_draw_layer");
|
||||
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
|
||||
let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
|
||||
|
||||
let nargs = unsafe { ffi::lua_gettop(state) };
|
||||
|
||||
@@ -188,7 +188,7 @@ unsafe extern "C-unwind" fn set_draw_layer(state: *mut ffi::lua_State) -> c_int
|
||||
unsafe extern "C-unwind" fn draw_image(state: *mut ffi::lua_State) -> c_int {
|
||||
//profiling::scope!("draw_image");
|
||||
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
|
||||
let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
|
||||
|
||||
let nargs = unsafe { ffi::lua_gettop(state) };
|
||||
if !matches!(nargs, 5 | 6 | 7 | 9 | 10 | 11) {
|
||||
@@ -236,7 +236,7 @@ unsafe extern "C-unwind" fn draw_image(state: *mut ffi::lua_State) -> c_int {
|
||||
unsafe extern "C-unwind" fn draw_image_quad(state: *mut ffi::lua_State) -> c_int {
|
||||
//profiling::scope!("draw_image_quad");
|
||||
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
|
||||
let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
|
||||
|
||||
let nargs = unsafe { ffi::lua_gettop(state) };
|
||||
if !matches!(nargs, 9 | 10 | 11 | 17 | 18 | 19) {
|
||||
@@ -303,7 +303,7 @@ unsafe extern "C-unwind" fn draw_image_quad(state: *mut ffi::lua_State) -> c_int
|
||||
unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int {
|
||||
//profiling::scope!("draw_string");
|
||||
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
|
||||
let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
|
||||
|
||||
let nargs = unsafe { ffi::lua_gettop(state) };
|
||||
|
||||
@@ -380,7 +380,7 @@ unsafe extern "C-unwind" fn draw_string(state: *mut ffi::lua_State) -> c_int {
|
||||
unsafe extern "C-unwind" fn get_string_width(state: *mut ffi::lua_State) -> c_int {
|
||||
//profiling::scope!("get_string_width");
|
||||
let lua_instance = unsafe { Lua::get_or_init_from_ptr(state) };
|
||||
let ctx = lua_instance.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = lua_instance.app_data_ref::<Context>().unwrap();
|
||||
|
||||
let nargs = unsafe { ffi::lua_gettop(state) };
|
||||
|
||||
@@ -405,7 +405,7 @@ fn get_index_at_cur(
|
||||
(line_height, font_type, text, cur_x, cur_y): (i32, String, String, f32, f32),
|
||||
) -> LuaResult<usize> {
|
||||
//profiling::scope!("get_char_index_at_cur");
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
|
||||
let font_type = font_type.parse::<PoBFontType>()?;
|
||||
|
||||
|
||||
+6
-6
@@ -5,7 +5,7 @@ use crate::{
|
||||
use mlua::{Lua, Result as LuaResult};
|
||||
|
||||
pub fn get_screen_size(l: &Lua, _: ()) -> LuaResult<(u32, u32)> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
let size = if *ctx.is_dpi_aware() {
|
||||
let PhysicalSize { width, height, .. } = ctx.window().size;
|
||||
(width, height)
|
||||
@@ -17,25 +17,25 @@ pub fn get_screen_size(l: &Lua, _: ()) -> LuaResult<(u32, u32)> {
|
||||
}
|
||||
|
||||
pub fn get_screen_scale(l: &Lua, _: ()) -> LuaResult<f32> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
let scale_factor = ctx.window().scale_factor();
|
||||
Ok(scale_factor)
|
||||
}
|
||||
|
||||
pub fn set_window_title(l: &Lua, title: String) -> LuaResult<()> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
ctx.window().set_window_title(&title);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_foreground(l: &Lua, _: ()) -> LuaResult<()> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
ctx.window().focus();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_dpi_scale_override(l: &Lua, percent: i32) -> LuaResult<()> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
match percent {
|
||||
0 => ctx.window().scale_factor_override = None,
|
||||
p if p > 0 => ctx.window().scale_factor_override = Some(p as f32 / 100.0),
|
||||
@@ -45,7 +45,7 @@ pub fn set_dpi_scale_override(l: &Lua, percent: i32) -> LuaResult<()> {
|
||||
}
|
||||
|
||||
pub fn get_dpi_scale_override(l: &Lua, _: ()) -> LuaResult<i32> {
|
||||
let ctx = l.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
match ctx.window().scale_factor_override {
|
||||
Some(scale_factor) => Ok((scale_factor * 100.0) as i32),
|
||||
None => Ok(0),
|
||||
|
||||
+7
-7
@@ -57,8 +57,8 @@ pub struct Context {
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new() -> &'static Self {
|
||||
Box::leak(Box::new(Self {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
window: Cell::new(std::ptr::null_mut()),
|
||||
input: Cell::new(std::ptr::null()),
|
||||
fonts: Cell::new(std::ptr::null_mut()),
|
||||
@@ -69,7 +69,7 @@ impl Context {
|
||||
needs_restart: Cell::new(std::ptr::null_mut()),
|
||||
should_exit: Cell::new(std::ptr::null_mut()),
|
||||
is_dpi_aware: Cell::new(std::ptr::null_mut()),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&self, ctx: &mut PoBContext) {
|
||||
@@ -199,7 +199,7 @@ impl LuaInstance {
|
||||
|
||||
/// Loads and executes PoB's Launch.lua script
|
||||
pub fn launch(&self, pob_ctx: &mut PoBContext) -> LuaResult<()> {
|
||||
let ctx = self.lua.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = self.lua.app_data_ref::<Context>().unwrap();
|
||||
ctx.set(pob_ctx);
|
||||
|
||||
let script_dir = pob_ctx.app.script_dir.as_path();
|
||||
@@ -221,7 +221,7 @@ impl LuaInstance {
|
||||
pub fn handle_subscripts(&self, pob_ctx: &mut PoBContext) {
|
||||
profiling::scope!("handle_subscripts");
|
||||
|
||||
let ctx = self.lua.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = self.lua.app_data_ref::<Context>().unwrap();
|
||||
ctx.set(pob_ctx);
|
||||
let subscript_events = self.subscript_manager.borrow_mut().process(self);
|
||||
ctx.clear();
|
||||
@@ -265,7 +265,7 @@ impl LuaInstance {
|
||||
}
|
||||
|
||||
pub fn can_exit(&self, pob_ctx: &mut PoBContext) -> bool {
|
||||
let ctx = self.lua.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = self.lua.app_data_ref::<Context>().unwrap();
|
||||
ctx.set(pob_ctx);
|
||||
|
||||
let can_exit = get_callback(&self.lua, "CanExit")
|
||||
@@ -280,7 +280,7 @@ impl LuaInstance {
|
||||
profiling::scope!("handle_event", format!("{}", event));
|
||||
|
||||
// "Plug" references into context
|
||||
let ctx = self.lua.app_data_ref::<&'static Context>().unwrap();
|
||||
let ctx = self.lua.app_data_ref::<Context>().unwrap();
|
||||
ctx.set(pob_ctx);
|
||||
|
||||
// Call event handler in PoB application code
|
||||
|
||||
Reference in new issue
Block a user