Skip to content

Commit

Permalink
Merge pull request #119 from Etto48/fix/mlua_update
Browse files Browse the repository at this point in the history
Fix/mlua update
  • Loading branch information
Etto48 authored Nov 3, 2024
2 parents f404d94 + 1fb4d1f commit 398594c
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 170 deletions.
189 changes: 138 additions & 51 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ HexPatch is a binary patcher and editor with terminal user interface (TUI),
it's capable of disassembling instructions and assembling patches.
It supports a variety of architectures and file formats.
Also, it can edit remote files via SSH."""
version = "1.9.1"
version = "1.9.2"
authors = ["Ettore Ricci"]
edition = "2021"
readme = "docs/README.md"
Expand All @@ -31,13 +31,13 @@ dirs = "5.0"
is-terminal = "0.4"
keystone-engine = "0.1"
macro_rules_attribute = "0.2"
mlua = { version = "0.9", features = ["lua54", "vendored", "serialize"] }
mlua = { version = "0.10", features = ["lua54", "vendored", "serialize"] }
object = "0.36"
pdb = "0.8"
ratatui = { version = "0.29", features = ["serde"] }
regex = "1.11"
russh = "0.45"
russh-keys = "0.45"
russh = "0.46"
russh-keys = "0.46"
russh-sftp = "2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ args = ["fmt", "--all", "--check"]

[tasks.lint-typos]
description = "Check for typos"
install_crate = { crate_name = "typos_cli", binary = "typos", test_arg = "--version" }
install_crate = { crate_name = "typos-cli", binary = "typos", test_arg = "--version" }
command = "typos"

[tasks.clippy]
Expand Down
8 changes: 4 additions & 4 deletions src/app/asm/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,13 @@ impl App {
if global_byte_index >= self.assembly_offsets.len() {
return None;
}
let current_istruction_index = self.assembly_offsets[global_byte_index];
Some(&self.assembly_instructions[current_istruction_index])
let current_instruction_index = self.assembly_offsets[global_byte_index];
Some(&self.assembly_instructions[current_instruction_index])
}

pub(in crate::app) fn get_instruction_at(&self, index: usize) -> &AssemblyLine {
let current_istruction_index = self.assembly_offsets[index];
&self.assembly_instructions[current_istruction_index]
let current_instruction_index = self.assembly_offsets[index];
&self.assembly_instructions[current_instruction_index]
}

pub(in crate::app) fn edit_assembly(&mut self, modifyied_bytes: usize) {
Expand Down
16 changes: 8 additions & 8 deletions src/app/plugins/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ impl<'app> AppContext<'app> {
self.exported_header_parsers.lock().unwrap().take()
}

pub fn to_lua<'lua>(
&'lua mut self,
lua: &'lua Lua,
scope: &Scope<'lua, '_>,
) -> mlua::Table<'lua> {
pub fn to_lua<'scope, 'env>(
&'env mut self,
lua: &'scope Lua,
scope: &'scope Scope<'scope, 'env>,
) -> mlua::Table {
let context = lua.create_table().unwrap();
context
.set(
Expand All @@ -151,7 +151,7 @@ impl<'app> AppContext<'app> {
"add_command",
scope
.create_function_mut(move |lua, (command, description): (String, String)| {
if let Ok(_command_fn) = lua.globals().get::<_, Function>(command.clone()) {
if let Ok(_command_fn) = lua.globals().get::<Function>(command.clone()) {
exported_commands
.lock()
.unwrap()
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<'app> AppContext<'app> {
scope
.create_function_mut(move |lua, callback: String| {
if let Ok(_header_parser_fn) =
lua.globals().get::<_, Function>(callback.clone())
lua.globals().get::<Function>(callback.clone())
{
exported_header_parsers
.lock()
Expand Down Expand Up @@ -243,7 +243,7 @@ impl<'app> AppContext<'app> {
let mut popup = self.popup.lock().unwrap();
if popup.is_some() {
Err(mlua::Error::external("Popup already open"))
} else if lua.globals().get::<_, Function>(callback.clone()).is_err() {
} else if lua.globals().get::<Function>(callback.clone()).is_err() {
Err(mlua::Error::external(format!(
"Function '{}' not found but needed to open the popup",
callback
Expand Down
2 changes: 1 addition & 1 deletion src/app/plugins/header_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl HeaderContext {
}

impl UserData for HeaderContext {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
fn add_methods<'lua, M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_method_mut("set_bitness", |_, this, bitness: u8| {
if this.bitness.is_some() {
Err(mlua::Error::external("bitness already set"))
Expand Down
2 changes: 1 addition & 1 deletion src/app/plugins/instruction_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl From<&AssemblyLine> for InstructionInfo {
}

impl UserData for InstructionInfo {
fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
fn add_fields<'lua, F: mlua::UserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("instruction", |_lua, this| Ok(this.instruction.clone()));
fields.add_field_method_get("physical_address", |_lua, this| Ok(this.physical_address));
fields.add_field_method_get("virtual_address", |_lua, this| Ok(this.virtual_address));
Expand Down
72 changes: 36 additions & 36 deletions src/app/plugins/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ impl Plugin {
register_usize(&lua)?;

app_context.reset_exported_commands();
if let Ok(init) = lua.globals().get::<_, Function>("init") {
if let Ok(init) = lua.globals().get::<Function>("init") {
lua.scope(|scope| {
let context = app_context.to_lua(&lua, scope);
init.call::<_, ()>(context)
init.call::<()>(context)
})?;
}

Expand All @@ -66,31 +66,31 @@ impl Plugin {

pub fn get_event_handlers(&self) -> Events {
let mut handlers = Events::NONE;
if self.lua.globals().get::<_, Function>("on_open").is_ok() {
if self.lua.globals().get::<Function>("on_open").is_ok() {
handlers |= Events::ON_OPEN;
}
if self.lua.globals().get::<_, Function>("on_edit").is_ok() {
if self.lua.globals().get::<Function>("on_edit").is_ok() {
handlers |= Events::ON_EDIT;
}
if self.lua.globals().get::<_, Function>("on_save").is_ok() {
if self.lua.globals().get::<Function>("on_save").is_ok() {
handlers |= Events::ON_SAVE;
}
if self.lua.globals().get::<_, Function>("on_key").is_ok() {
if self.lua.globals().get::<Function>("on_key").is_ok() {
handlers |= Events::ON_KEY;
}
if self.lua.globals().get::<_, Function>("on_mouse").is_ok() {
if self.lua.globals().get::<Function>("on_mouse").is_ok() {
handlers |= Events::ON_MOUSE;
}
if self.lua.globals().get::<_, Function>("on_focus").is_ok() {
if self.lua.globals().get::<Function>("on_focus").is_ok() {
handlers |= Events::ON_FOCUS;
}
if self.lua.globals().get::<_, Function>("on_blur").is_ok() {
if self.lua.globals().get::<Function>("on_blur").is_ok() {
handlers |= Events::ON_BLUR;
}
if self.lua.globals().get::<_, Function>("on_paste").is_ok() {
if self.lua.globals().get::<Function>("on_paste").is_ok() {
handlers |= Events::ON_PASTE;
}
if self.lua.globals().get::<_, Function>("on_resize").is_ok() {
if self.lua.globals().get::<Function>("on_resize").is_ok() {
handlers |= Events::ON_RESIZE;
}
handlers
Expand All @@ -107,74 +107,74 @@ impl Plugin {
let ret = match event {
Event::Open => {
// Call the on_open function
let on_open = self.lua.globals().get::<_, Function>("on_open").unwrap();
let on_open = self.lua.globals().get::<Function>("on_open").unwrap();
self.lua.scope(|scope| {
let context = app_context.to_lua(&self.lua, scope);
on_open.call::<_, ()>(context)
on_open.call::<()>(context)
})
}
Event::Edit { new_bytes } => {
// Call the on_edit function
let on_edit = self.lua.globals().get::<_, Function>("on_edit").unwrap();
let on_edit = self.lua.globals().get::<Function>("on_edit").unwrap();
self.lua.scope(|scope| {
let new_bytes = scope.create_any_userdata_ref_mut(new_bytes)?;
let context = app_context.to_lua(&self.lua, scope);
on_edit.call::<_, ()>((new_bytes, context))
on_edit.call::<()>((new_bytes, context))
})
}
Event::Save => {
// Call the on_save function
let on_save = self.lua.globals().get::<_, Function>("on_save").unwrap();
let on_save = self.lua.globals().get::<Function>("on_save").unwrap();
self.lua.scope(|scope| {
let context = app_context.to_lua(&self.lua, scope);
on_save.call::<_, ()>(context)
on_save.call::<()>(context)
})
}
Event::Key { event } => {
// Call the on_key function
let on_key = self.lua.globals().get::<_, Function>("on_key").unwrap();
let on_key = self.lua.globals().get::<Function>("on_key").unwrap();
let event = key_event_to_lua(&self.lua, &event).unwrap();
self.lua.scope(|scope| {
let context = app_context.to_lua(&self.lua, scope);
on_key.call::<_, ()>((event, context))
on_key.call::<()>((event, context))
})
}
Event::Mouse { event, location } => {
// Call the on_mouse function
let on_mouse = self.lua.globals().get::<_, Function>("on_mouse").unwrap();
let on_mouse = self.lua.globals().get::<Function>("on_mouse").unwrap();
let event = mouse_event_to_lua(&self.lua, &event, location).unwrap();
self.lua.scope(|scope| {
let context = app_context.to_lua(&self.lua, scope);
on_mouse.call::<_, ()>((event, context))
on_mouse.call::<()>((event, context))
})
}
Event::Focus => {
let on_focus = self.lua.globals().get::<_, Function>("on_focus").unwrap();
let on_focus = self.lua.globals().get::<Function>("on_focus").unwrap();
self.lua.scope(|scope| {
let context = app_context.to_lua(&self.lua, scope);
on_focus.call::<_, ()>(context)
on_focus.call::<()>(context)
})
}
Event::Blur => {
let on_blur = self.lua.globals().get::<_, Function>("on_blur").unwrap();
let on_blur = self.lua.globals().get::<Function>("on_blur").unwrap();
self.lua.scope(|scope| {
let context = app_context.to_lua(&self.lua, scope);
on_blur.call::<_, ()>(context)
on_blur.call::<()>(context)
})
}
Event::Paste { text } => {
let on_paste = self.lua.globals().get::<_, Function>("on_paste").unwrap();
let on_paste = self.lua.globals().get::<Function>("on_paste").unwrap();
self.lua.scope(|scope| {
let text = self.lua.create_string(text).unwrap();
let context = app_context.to_lua(&self.lua, scope);
on_paste.call::<_, ()>((text, context))
on_paste.call::<()>((text, context))
})
}
Event::Resize { width, height } => {
let on_resize = self.lua.globals().get::<_, Function>("on_resize").unwrap();
let on_resize = self.lua.globals().get::<Function>("on_resize").unwrap();
self.lua.scope(|scope| {
let context = app_context.to_lua(&self.lua, scope);
on_resize.call::<_, ()>((width, height, context))
on_resize.call::<()>((width, height, context))
})
}
};
Expand All @@ -191,12 +191,12 @@ impl Plugin {
}

pub fn run_command(&mut self, command: &str, app_context: &mut AppContext) -> mlua::Result<()> {
let command_fn = self.lua.globals().get::<_, Function>(command)?;
let command_fn = self.lua.globals().get::<Function>(command)?;
app_context.set_exported_commands(self.commands.take());
app_context.set_exported_header_parsers(self.header_parsers.take());
let ret = self.lua.scope(|scope| {
let context = app_context.to_lua(&self.lua, scope);
command_fn.call::<_, ()>(context)
command_fn.call::<()>(context)
});
self.commands = app_context.take_exported_commands();
self.header_parsers = app_context.take_exported_header_parsers();
Expand All @@ -216,12 +216,12 @@ impl Plugin {
let callback = self
.lua
.globals()
.get::<_, Function>(callback.as_ref())
.get::<Function>(callback.as_ref())
.unwrap();
self.lua.scope(|scope| {
let popup_context = popup_context.to_lua(&self.lua, scope);
let context = app_context.to_lua(&self.lua, scope);
callback.call::<_, ()>((popup_context, context))
callback.call::<()>((popup_context, context))
})
}

Expand All @@ -232,12 +232,12 @@ impl Plugin {
let parser_fn = self
.lua
.globals()
.get::<&str, Function>(parser.parser.as_ref())
.get::<Function>(parser.parser.clone())
.unwrap();
let result = self.lua.scope(|scope| {
let context = app_context.to_lua(&self.lua, scope);
let header_context = scope.create_userdata_ref_mut(&mut header_context)?;
parser_fn.call::<_, ()>((header_context, context))
parser_fn.call::<()>((header_context, context))
});
self.commands = app_context.take_exported_commands();
match result {
Expand Down Expand Up @@ -286,7 +286,7 @@ mod test {
let mut app_context = get_app_context!(app);
let plugin = Plugin::new_from_source(&source, &mut app_context).unwrap();
assert_eq!(
plugin.lua.globals().get::<_, i32>("test_value").unwrap(),
plugin.lua.globals().get::<i32>("test_value").unwrap(),
test_value
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/plugins/plugin_instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl PluginInstant {
}

impl UserData for PluginInstant {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
fn add_methods<'lua, M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_method("elapsed", |_, this, ()| {
Ok(this.inner.elapsed().as_secs_f64())
});
Expand Down
6 changes: 5 additions & 1 deletion src/app/plugins/popup_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ impl<'app> PopupContext<'app> {
}
}

pub fn to_lua<'lua>(&'lua mut self, lua: &'lua Lua, scope: &Scope<'lua, '_>) -> Table<'lua> {
pub fn to_lua<'scope, 'env>(
&'env mut self,
lua: &Lua,
scope: &'scope Scope<'scope, 'env>,
) -> Table {
let table = lua.create_table().unwrap();
table
.set(
Expand Down
2 changes: 1 addition & 1 deletion src/app/plugins/ui_location/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Point {
}
}

impl<'lua> IntoLua<'lua> for Point {
impl IntoLua for Point {
fn into_lua(self, lua: &mlua::Lua) -> mlua::Result<mlua::Value> {
let ret = lua.create_table()?;
ret.set("x", self.x)?;
Expand Down
4 changes: 2 additions & 2 deletions src/app/plugins/ui_location/ui_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub struct UiLocation {
pub relative_location: Point,
}

impl<'lua> IntoLua<'lua> for UiLocation {
fn into_lua(self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>> {
impl IntoLua for UiLocation {
fn into_lua(self, lua: &mlua::Lua) -> mlua::Result<mlua::Value> {
let ret = lua.create_table()?;
ret.set("info", self.info)?;
ret.set("relative_location", self.relative_location)?;
Expand Down
4 changes: 2 additions & 2 deletions src/app/plugins/ui_location/ui_location_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub enum UiLocationInfo {
},
}

impl<'lua> IntoLua<'lua> for UiLocationInfo {
fn into_lua(self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>> {
impl IntoLua for UiLocationInfo {
fn into_lua(self, lua: &mlua::Lua) -> mlua::Result<mlua::Value> {
let ret = lua.create_table()?;
match self {
UiLocationInfo::AddressView { file_address } => {
Expand Down
Loading

0 comments on commit 398594c

Please sign in to comment.