Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions crates/liblovely/lovely.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ typedef void (*lua_pushvalue_ptr)(lua_State *state, int index);
typedef void (*lua_pushcclosure_ptr)(lua_State *state, lua_CFunction f, int n);
typedef const char* (*lua_tolstring_ptr)(lua_State *state, int index, size_t *len);
typedef int (*lua_type_ptr)(lua_State *state, int index);
typedef void (*luaL_register_ptr)(lua_State *state, const char *libname, const luaL_Reg *l);
typedef void (*lua_pushstring_ptr)(lua_State *state, const char *string);
typedef void (*lua_pushnumber_ptr)(lua_State *state, double number);
typedef void (*lua_pushboolean_ptr)(lua_State *state, int b);
typedef void (*lua_settable_ptr)(lua_State *state, int index);
typedef void (*lua_createtable_ptr)(lua_State *state, int narr, int nrec);
typedef int (*lua_error_ptr)(lua_State *state);
typedef void (*luaL_register_ptr)(lua_State *state, const char *libname, const luaL_Reg *l);
typedef const char* (*luaL_checklstring_ptr)(lua_State *state, int index, size_t *len);

struct LuaLib {
Expand All @@ -33,12 +34,13 @@ struct LuaLib {
lua_pushcclosure_ptr lua_pushcclosure;
lua_tolstring_ptr lua_tolstring;
lua_type_ptr lua_type;
luaL_register_ptr luaL_register;
lua_pushstring_ptr lua_pushstring;
lua_pushnumber_ptr lua_pushnumber;
lua_pushboolean_ptr lua_pushboolean;
lua_settable_ptr lua_settable;
lua_createtable_ptr lua_createtable;
lua_error_ptr lua_error;
luaL_register_ptr luaL_register;
luaL_checklstring_ptr luaL_checklstring;
};

Expand Down
18 changes: 10 additions & 8 deletions crates/liblovely/luajit.patch
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ index a44f0272..f2541a5b 100644
const char *name, const char *mode);
LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
diff --git a/src/lib_aux.c b/src/lib_aux.c
index 4b4664a7..a9debce9 100644
index bef7bdab..31c82110 100644
--- a/src/lib_aux.c
+++ b/src/lib_aux.c
@@ -22,6 +22,7 @@
Expand All @@ -27,7 +27,7 @@ index 4b4664a7..a9debce9 100644

LUALIB_API lua_State *luaL_newstate(void)
{
+ struct LuaLib lua = {lua_call, lua_pcall, lua_getfield, lua_setfield, lua_gettop, lua_settop, lua_pushvalue, lua_pushcclosure, lua_tolstring, lua_type, luaL_register, lua_pushstring, lua_pushnumber, lua_pushboolean, lua_settable, lua_createtable, luaL_checklstring};
+ struct LuaLib lua = {lua_call, lua_pcall, lua_getfield, lua_setfield, lua_gettop, lua_settop, lua_pushvalue, lua_pushcclosure, lua_tolstring, lua_type, lua_pushstring, lua_pushnumber, lua_pushboolean, lua_settable, lua_createtable, lua_error, luaL_register, luaL_checklstring};
+ lovely_init(lovely_loadbufferx, lua);
lua_State *L = lua_newstate(mem_alloc, NULL);
if (L) {
Expand All @@ -36,13 +36,13 @@ index 4b4664a7..a9debce9 100644

LUALIB_API lua_State *luaL_newstate(void)
{
+ struct LuaLib lua = {lua_call, lua_pcall, lua_getfield, lua_setfield, lua_gettop, lua_settop, lua_pushvalue, lua_pushcclosure, lua_tolstring, lua_type, luaL_register, lua_pushstring, lua_pushnumber, lua_pushboolean, lua_settable, lua_createtable, luaL_checklstring};
+ struct LuaLib lua = {lua_call, lua_pcall, lua_getfield, lua_setfield, lua_gettop, lua_settop, lua_pushvalue, lua_pushcclosure, lua_tolstring, lua_type, lua_pushstring, lua_pushnumber, lua_pushboolean, lua_settable, lua_createtable, lua_error, luaL_register, luaL_checklstring};
+ lovely_init(lovely_loadbufferx, lua);
lua_State *L;
#if LJ_64 && !LJ_GC64
L = lj_state_newstate(LJ_ALLOCF_INTERNAL, NULL);
diff --git a/src/lj_load.c b/src/lj_load.c
index 24b660a8..daa8ef29 100644
index ce7814bc..a6494233 100644
--- a/src/lj_load.c
+++ b/src/lj_load.c
@@ -22,6 +22,7 @@
Expand Down Expand Up @@ -76,10 +76,10 @@ index 24b660a8..daa8ef29 100644
{
diff --git a/src/lovely.h b/src/lovely.h
new file mode 100644
index 00000000..10f14b29
index 00000000..79d668ef
--- /dev/null
+++ b/src/lovely.h
@@ -0,0 +1,48 @@
@@ -0,0 +1,50 @@
+// This file was generated using gen-h.lua
+
+#ifndef LOVELY_H
Expand All @@ -96,12 +96,13 @@ index 00000000..10f14b29
+typedef void (*lua_pushcclosure_ptr)(lua_State *state, lua_CFunction f, int n);
+typedef const char* (*lua_tolstring_ptr)(lua_State *state, int index, size_t *len);
+typedef int (*lua_type_ptr)(lua_State *state, int index);
+typedef void (*luaL_register_ptr)(lua_State *state, const char *libname, const luaL_Reg *l);
+typedef void (*lua_pushstring_ptr)(lua_State *state, const char *string);
+typedef void (*lua_pushnumber_ptr)(lua_State *state, double number);
+typedef void (*lua_pushboolean_ptr)(lua_State *state, int b);
+typedef void (*lua_settable_ptr)(lua_State *state, int index);
+typedef void (*lua_createtable_ptr)(lua_State *state, int narr, int nrec);
+typedef int (*lua_error_ptr)(lua_State *state);
+typedef void (*luaL_register_ptr)(lua_State *state, const char *libname, const luaL_Reg *l);
+typedef const char* (*luaL_checklstring_ptr)(lua_State *state, int index, size_t *len);
+
+struct LuaLib {
Expand All @@ -115,12 +116,13 @@ index 00000000..10f14b29
+ lua_pushcclosure_ptr lua_pushcclosure;
+ lua_tolstring_ptr lua_tolstring;
+ lua_type_ptr lua_type;
+ luaL_register_ptr luaL_register;
+ lua_pushstring_ptr lua_pushstring;
+ lua_pushnumber_ptr lua_pushnumber;
+ lua_pushboolean_ptr lua_pushboolean;
+ lua_settable_ptr lua_settable;
+ lua_createtable_ptr lua_createtable;
+ lua_error_ptr lua_error;
+ luaL_register_ptr luaL_register;
+ luaL_checklstring_ptr luaL_checklstring;
+};
+
Expand Down
1 change: 1 addition & 0 deletions crates/lovely-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ libc = "0.2.153"
libloading = "0.8.3"
toml = "0.8.10"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0"
sha2 = "0.10.8"
walkdir = "2.5.0"
wildmatch = "2.3.0"
Expand Down
162 changes: 162 additions & 0 deletions crates/lovely-core/src/dump.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use std::fs;
use std::path::Path;

use serde::Serialize;

// Sidecar debug entry. Written to the dump dir.
#[derive(Serialize, Debug)]
pub struct PatchDebugEntry {
pub patch_source: PatchSource,
pub regions: Vec<PatchRegion>,
#[serde(skip_serializing_if = "Option::is_none")]
pub warnings: Option<Vec<String>>,
}

#[derive(Serialize, Debug, Clone)]
pub enum DebugPatchType {
#[serde(rename = "pattern")]
Pattern,
#[serde(rename = "regex")]
Regex,
#[serde(rename = "copy")]
Copy,
}

#[derive(Serialize, Debug, Clone)]
pub struct PatchSource {
pub file: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub pattern: Option<String>,
pub patch_type: DebugPatchType,
}

#[derive(Serialize, Debug)]
pub struct PatchRegion {
pub start_line: usize,
pub end_line: usize,
}

#[derive(Debug, Clone)]
pub struct ByteRegion {
pub start: usize,
pub end: usize,
pub delta: isize,
}

impl ByteRegion {
/// Adjust this region based on an edit that occurred elsewhere.
pub fn adjust(&mut self, edit_pos: usize, delta: isize) {
if edit_pos <= self.start {
self.start = self.start.saturating_add_signed(delta);
self.end = self.end.saturating_add_signed(delta);
} else if edit_pos < self.end {
self.end = self.end.saturating_add_signed(delta);
}
}
}

/// Dirty byte-based debug entry.
#[derive(Debug)]
pub struct ByteDebugEntry {
pub patch_source: PatchSource,
pub regions: Vec<ByteRegion>,
pub warnings: Option<Vec<String>>,
}

impl ByteDebugEntry {
/// Adjust all regions in this entry based on the edit that occurred.
pub fn adjust(&mut self, edit_pos: usize, delta: isize) {
for region in &mut self.regions {
region.adjust(edit_pos, delta);
}
}
}

#[derive(Serialize, Debug)]
pub struct PatchDebug {
pub buffer_name: String,
pub entries: Vec<PatchDebugEntry>,
}

impl PatchDebug {
pub fn new(buffer_name: &str) -> Self {
Self {
buffer_name: buffer_name.to_string(),
entries: Vec::new(),
}
}

/// Convert from byte-based to line-based.
pub fn from_byte_entries(buffer_name: &str, byte_entries: Vec<ByteDebugEntry>, rope: &crop::Rope) -> Self {
let entries = byte_entries
.into_iter()
.map(|entry| PatchDebugEntry {
patch_source: entry.patch_source,
regions: entry
.regions
.into_iter()
.map(|r| PatchRegion {
start_line: rope.line_of_byte(r.start) + 1,
end_line: rope.line_of_byte(r.end.saturating_sub(1)) + 1,
})
.collect(),
warnings: entry.warnings,
})
.collect();

Self {
buffer_name: buffer_name.to_string(),
entries,
}
}
}

/// Dump the buffer and its sidecar.
pub fn write_dump(
mod_dir: &Path,
dir_name: &str,
name: &str,
buffer: &str,
debug: &PatchDebug,
) {
if name.chars().count() > 100 {
return;
}

let dump_path = mod_dir.join("lovely").join(dir_name).join(name);
if fs::exists(&dump_path).unwrap_or(false) {
return;
}

if let Some(parent) = dump_path.parent() {
if !parent.is_dir() {
if let Err(e) = fs::create_dir_all(parent) {
log::error!("Failed to create directory at {parent:?}: {e:?}");
return;
}
}
}

if let Err(e) = fs::write(&dump_path, buffer) {
log::error!("Failed to write dump to {dump_path:?}: {e:?}");
return;
}

let mut json_path = dump_path;
json_path.add_extension("json");

if fs::exists(&json_path).unwrap_or(false) {
return;
}

match serde_json::to_string_pretty(debug) {
Ok(json) => {
if let Err(e) = fs::write(&json_path, json) {
log::error!("Failed to write debug JSON to {json_path:?}: {e:?}");
}
}
Err(e) => {
log::error!("Failed to serialize debug info: {e:?}");
}
}
}
Loading
Loading