Skip to content

Commit

Permalink
More pretty-prints (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwreey committed Nov 9, 2024
1 parent c3f255d commit 7a7ccf7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
3 changes: 2 additions & 1 deletion crates/lune-std-ffi/src/data/box_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl BoxData {
if self.size() > FFI_BOX_PRINT_MAX_LENGTH * 2 {
return String::from("length limit exceed");
}
let mut buff: String = String::with_capacity(self.size() * 2);
let mut buff: String = String::with_capacity(self.size() * 2 + 2);
buff.push_str("0x");
for value in self.data.iter() {
buff.push_str(format!("{:x}", value.to_be()).as_str());
}
Expand Down
8 changes: 8 additions & 0 deletions crates/lune-std-ffi/src/data/callable_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ impl CallableData {
}
}

// Stringify for pretty-print, with hex format address
pub fn stringify(&self) -> String {
format!("0x{:x}", self.code.as_ptr() as usize)
}

pub unsafe fn call(&self, result: LuaValue, args: LuaMultiValue) -> LuaResult<()> {
let arg_len = self.arg_info_list.len();
// Optimization: use sized caller when possible
Expand Down Expand Up @@ -178,5 +183,8 @@ impl LuaUserData for CallableData {
unsafe { this.call(result, args) }
},
);
methods.add_meta_method(LuaMetaMethod::ToString, |_lua, this, ()| {
Ok(this.stringify())
});
}
}
8 changes: 8 additions & 0 deletions crates/lune-std-ffi/src/data/closure_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ impl ClosureData {

Ok(closure_data)
}

// Stringify for pretty-print, with hex format address
pub fn stringify(&self) -> String {
format!("0x{:x}", unsafe { self.get_inner_pointer() } as usize)
}
}

impl FfiData for ClosureData {
Expand Down Expand Up @@ -148,5 +153,8 @@ impl LuaUserData for ClosureData {
association::set(lua, REF_INNER, &ref_data, &this)?;
Ok(ref_data)
});
methods.add_meta_method(LuaMetaMethod::ToString, |_lua, this, ()| {
Ok(this.stringify())
});
}
}
21 changes: 17 additions & 4 deletions crates/lune-std-ffi/src/data/lib_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ const LIB_REF_FLAGS: u8 = RefFlag::Offsetable.value()
| RefFlag::Function.value();

// Runtime dynamic loaded libraries
pub struct LibData(Library);
pub struct LibData {
name: String,
lib: Library,
}

impl LibData {
// Open library then return library handle
pub fn new(libname: String) -> LuaResult<Self> {
match Library::open(libname) {
Ok(t) => Ok(Self(t)),
match Library::open(&libname) {
Ok(t) => Ok(Self {
lib: t,
name: libname.clone(),
}),
Err(err) => Err(err.into_lua_err()),
}
}
Expand All @@ -32,7 +38,7 @@ impl LibData {
) -> LuaResult<LuaAnyUserData<'lua>> {
let lib = this.borrow::<LibData>()?;
let sym = unsafe {
lib.0
lib.lib
.symbol::<*const ()>(name.as_str())
.map_err(LuaError::external)?
};
Expand All @@ -44,12 +50,19 @@ impl LibData {

Ok(ffi_ref)
}

pub fn stringify(&self) -> String {
self.name.clone()
}
}

impl LuaUserData for LibData {
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_function("find", |lua, (this, name): (LuaAnyUserData, String)| {
LibData::find_symbol(lua, this, name)
});
methods.add_meta_method(LuaMetaMethod::ToString, |_lua, this, ()| {
Ok(this.stringify())
});
}
}
2 changes: 1 addition & 1 deletion crates/lune-std-ffi/src/data/ref_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl RefData {

// Stringify for pretty-print, with hex format address
pub fn stringify(&self) -> String {
format!("{:x}", **self.ptr as usize)
format!("0x{:x}", **self.ptr as usize)
}
}

Expand Down
8 changes: 6 additions & 2 deletions tests/ffi/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# tests/ffi
<!-- markdownlint-disable MD036 -->

# `tests/ffi`

## Requirements

Expand All @@ -25,7 +27,9 @@ gcc for library compiling (for external-\*)
**Pretty Print**

- [x] [arr](./pretty_print/arr.luau)
- [ ] [box](./pretty_print/box.luau)
- [ ] [box](./pretty_print/box.luau) Need assertion
- [ ] [ref](./pretty_print/ref.luau) Need assertion
- [ ] [lib](./pretty_print/lib.luau) Need assertion
- [x] [fn](./pretty_print/fn.luau)
- [x] [ptr](./pretty_print/ptr.luau)
- [x] [struct](./pretty_print/struct.luau)
Expand Down

0 comments on commit 7a7ccf7

Please sign in to comment.