diff --git a/crates/lune-std-ffi/src/data/box_data/mod.rs b/crates/lune-std-ffi/src/data/box_data/mod.rs index 88ab7bae..70ce3b38 100644 --- a/crates/lune-std-ffi/src/data/box_data/mod.rs +++ b/crates/lune-std-ffi/src/data/box_data/mod.rs @@ -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()); } diff --git a/crates/lune-std-ffi/src/data/callable_data.rs b/crates/lune-std-ffi/src/data/callable_data.rs index 2e95a27b..47f47953 100644 --- a/crates/lune-std-ffi/src/data/callable_data.rs +++ b/crates/lune-std-ffi/src/data/callable_data.rs @@ -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 @@ -178,5 +183,8 @@ impl LuaUserData for CallableData { unsafe { this.call(result, args) } }, ); + methods.add_meta_method(LuaMetaMethod::ToString, |_lua, this, ()| { + Ok(this.stringify()) + }); } } diff --git a/crates/lune-std-ffi/src/data/closure_data.rs b/crates/lune-std-ffi/src/data/closure_data.rs index 1a5dfca6..26595d1c 100644 --- a/crates/lune-std-ffi/src/data/closure_data.rs +++ b/crates/lune-std-ffi/src/data/closure_data.rs @@ -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 { @@ -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()) + }); } } diff --git a/crates/lune-std-ffi/src/data/lib_data.rs b/crates/lune-std-ffi/src/data/lib_data.rs index 9c92ce59..b8819f9b 100644 --- a/crates/lune-std-ffi/src/data/lib_data.rs +++ b/crates/lune-std-ffi/src/data/lib_data.rs @@ -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 { - 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()), } } @@ -32,7 +38,7 @@ impl LibData { ) -> LuaResult> { let lib = this.borrow::()?; let sym = unsafe { - lib.0 + lib.lib .symbol::<*const ()>(name.as_str()) .map_err(LuaError::external)? }; @@ -44,6 +50,10 @@ impl LibData { Ok(ffi_ref) } + + pub fn stringify(&self) -> String { + self.name.clone() + } } impl LuaUserData for LibData { @@ -51,5 +61,8 @@ impl LuaUserData for LibData { 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()) + }); } } diff --git a/crates/lune-std-ffi/src/data/ref_data/mod.rs b/crates/lune-std-ffi/src/data/ref_data/mod.rs index 2c15d5ec..3de26137 100644 --- a/crates/lune-std-ffi/src/data/ref_data/mod.rs +++ b/crates/lune-std-ffi/src/data/ref_data/mod.rs @@ -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) } } diff --git a/tests/ffi/README.md b/tests/ffi/README.md index f42576b6..47d2779f 100644 --- a/tests/ffi/README.md +++ b/tests/ffi/README.md @@ -1,4 +1,6 @@ -# tests/ffi + + +# `tests/ffi` ## Requirements @@ -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)