Skip to content

Commit da30dfb

Browse files
committed
Refactor and documenting code structure (#243)
1 parent a2a8176 commit da30dfb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+803
-701
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/lune-std-ffi/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# `lune-std-ffi`
2+
3+
## Code structure
4+
5+
### /c
6+
7+
Define C-ABI type information and provide conversion and casting
8+
9+
- [**Struct ` CArrInfo`:**](./src/c/struct_info.rs) Represents C Array type
10+
- [**Struct ` CPtrInfo`:**](./src/c/ptr_info.rs) Represents C Pointer type
11+
- [**Struct ` CFnInfo`:**](./src/c/fn_info.rs) Represents C Function signature
12+
provide CallableData and ClosureData creation
13+
- [**Struct ` CStructInfo`:**](./src/c/struct_info.rs) Represents C Struct type
14+
- [**Struct ` CTypeInfo<T>`:**](./src/c/type_info.rs) Represents C type, extended in `/c/types`
15+
16+
#### /c/types
17+
18+
Export fixed-size source time known types and non-fixed compile time known types
19+
Implememt type-casting for all CTypes
20+
21+
**Mod `ctype_helper`:**
22+
23+
- **Function `get_conv`:**
24+
get _FfiConvert_ from some ctype userdata, used for struct and array conversion
25+
- **Function `get_size`:**
26+
get size from some ctype userdata, used for call return and arguments boundary checking
27+
- **Function `get_name`:**
28+
get type name from some ctype userdata, used for pretty-print
29+
- **Function `get_middle_type`:**
30+
get **`libffi::middle::Type`:** from some ctype userdata
31+
- **Function `is_ctype`:** check userdata is ctype
32+
33+
---
34+
35+
### /data
36+
37+
**Structs:** Provide memory userdata
38+
39+
- [**Struct `BoxData`:**](./src/data/box_data/mod.rs) A heap allocated memory with user definable lifetime
40+
- [**Struct `LibData`:**](./src/data/lib_data.rs) A dynamic opened library
41+
- [**Struct `RefData`:**](./src/data/ref_data/mod.rs) A reference that can be used for receiving return data from external function or pass pointer arguments
42+
43+
**Structs:** Provide function(pointer) userdata
44+
45+
- [**Struct `CallableData`:**](./src/data/callable_data.rs) A callable function, which can be created from function pointer
46+
- [**Struct `ClosureData`:**](./src/data/closure_data.rs) A closure pointer, which can be created from lua function and can be used for callback
47+
48+
---
49+
50+
### /ffi
51+
52+
**Traits:** Provide ABI shared common type information trait
53+
54+
- **Trait `FfiSize`**
55+
- **Trait `FfiSignedness`**
56+
- **Trait `FfiConvert`:** Provide read LuaValue from FfiData or write LuaValue into FfiData
57+
58+
**Trait `FfiData`:** Provide common data handle, including methods below
59+
60+
- **Method `check_boundary`:** check boundary with offset and size
61+
- **Method `get_pointer`:** returns raw pointer `*mut ()`
62+
- **Method `is_writable`**
63+
- **Method `is_readable`**
64+
65+
> Note: `GetFfiData` trait in `data/mod.rs` provides `AnyUserData.get_data_handle() -> FfiData` method
66+
67+
**Mods:** Provide common helper functions
68+
69+
- **`association.rs`:** GC utility, used for inner, ret and arg type holding in subtype
70+
- **`bit_mask.rs`:** u8 bitfield helper
71+
- **`cast.rs`:** library
72+
- **Function `num_cast<From, Into>(from: FfiData, from: FfiData)`:**
73+
Cast number type value inno another number type
74+
- **`libffi_helper.rs`:**
75+
- **Const `FFI_STATUS_NAMES`:** Used for ffi_status stringify
76+
- **Function `get_ensured_size`:** Returns ensured ffi_type size
77+
- **Const `SIEE_OF_POINTER`:** Platform specific pointer size (Compile time known)
78+
79+
## TODO

crates/lune-std-ffi/src/c/c_arr.rs renamed to crates/lune-std-ffi/src/c/arr_info.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ use std::cell::Ref;
33
use libffi::middle::Type;
44
use mlua::prelude::*;
55

6-
use super::{association_names::CARR_INNER, c_helper, method_provider};
7-
use crate::ffi::{
8-
ffi_association::{get_association, set_association},
9-
NativeConvert, NativeData, NativeSize,
6+
use super::{association_names::CARR_INNER, helper, method_provider};
7+
use crate::{
8+
data::{FfiConvert, FfiData, FfiSize},
9+
ffi::{association, libffi_helper::get_ensured_size},
1010
};
11-
use crate::libffi_helper::get_ensured_size;
1211

1312
// This is a series of some type.
1413
// It provides the final size and the offset of the index,
@@ -19,19 +18,19 @@ use crate::libffi_helper::get_ensured_size;
1918
// We can simply provide array type with struct.
2019
// See: https://stackoverflow.com/a/43525176
2120

22-
pub struct CArr {
21+
pub struct CArrInfo {
2322
struct_type: Type,
2423
length: usize,
2524
size: usize,
2625
inner_size: usize,
27-
inner_conv: *const dyn NativeConvert,
26+
inner_conv: *const dyn FfiConvert,
2827
}
2928

30-
impl CArr {
29+
impl CArrInfo {
3130
pub fn new(
3231
element_type: Type,
3332
length: usize,
34-
inner_conv: *const dyn NativeConvert,
33+
inner_conv: *const dyn FfiConvert,
3534
) -> LuaResult<Self> {
3635
let inner_size = get_ensured_size(element_type.as_raw_ptr())?;
3736
let struct_type = Type::structure(vec![element_type.clone(); length]);
@@ -51,11 +50,11 @@ impl CArr {
5150
type_userdata: &LuaAnyUserData<'lua>,
5251
length: usize,
5352
) -> LuaResult<LuaAnyUserData<'lua>> {
54-
let fields = c_helper::get_middle_type(type_userdata)?;
55-
let conv = unsafe { c_helper::get_conv(type_userdata)? };
53+
let fields = helper::get_middle_type(type_userdata)?;
54+
let conv = unsafe { helper::get_conv(type_userdata)? };
5655
let carr = lua.create_userdata(Self::new(fields, length, conv)?)?;
5756

58-
set_association(lua, CARR_INNER, &carr, type_userdata)?;
57+
association::set(lua, CARR_INNER, &carr, type_userdata)?;
5958
Ok(carr)
6059
}
6160

@@ -70,13 +69,13 @@ impl CArr {
7069
// Stringify for pretty printing like:
7170
// <CArr( u8, length = 8 )>
7271
pub fn stringify(lua: &Lua, userdata: &LuaAnyUserData) -> LuaResult<String> {
73-
let this = userdata.borrow::<CArr>()?;
72+
let this = userdata.borrow::<CArrInfo>()?;
7473
if let Some(LuaValue::UserData(inner_userdata)) =
75-
get_association(lua, CARR_INNER, userdata)?
74+
association::get(lua, CARR_INNER, userdata)?
7675
{
7776
Ok(format!(
7877
" {}, length = {} ",
79-
c_helper::pretty_format(lua, &inner_userdata)?,
78+
helper::pretty_format(lua, &inner_userdata)?,
8079
this.length,
8180
))
8281
} else {
@@ -85,18 +84,18 @@ impl CArr {
8584
}
8685
}
8786

88-
impl NativeSize for CArr {
87+
impl FfiSize for CArrInfo {
8988
fn get_size(&self) -> usize {
9089
self.size
9190
}
9291
}
93-
impl NativeConvert for CArr {
92+
impl FfiConvert for CArrInfo {
9493
// FIXME: FfiBox, FfiRef support required
95-
unsafe fn luavalue_into<'lua>(
94+
unsafe fn value_into_data<'lua>(
9695
&self,
9796
lua: &'lua Lua,
9897
offset: isize,
99-
data_handle: &Ref<dyn NativeData>,
98+
data_handle: &Ref<dyn FfiData>,
10099
value: LuaValue<'lua>,
101100
) -> LuaResult<()> {
102101
let LuaValue::Table(ref table) = value else {
@@ -106,7 +105,7 @@ impl NativeConvert for CArr {
106105
let field_offset = (i * self.inner_size) as isize;
107106
let data: LuaValue = table.get(i + 1)?;
108107

109-
self.inner_conv.as_ref().unwrap().luavalue_into(
108+
self.inner_conv.as_ref().unwrap().value_into_data(
110109
lua,
111110
field_offset + offset,
112111
data_handle,
@@ -116,18 +115,18 @@ impl NativeConvert for CArr {
116115
Ok(())
117116
}
118117

119-
unsafe fn luavalue_from<'lua>(
118+
unsafe fn value_from_data<'lua>(
120119
&self,
121120
lua: &'lua Lua,
122121
offset: isize,
123-
data_handle: &Ref<dyn NativeData>,
122+
data_handle: &Ref<dyn FfiData>,
124123
) -> LuaResult<LuaValue<'lua>> {
125124
let table = lua.create_table_with_capacity(self.length, 0)?;
126125
for i in 0..self.length {
127126
let field_offset = (i * self.inner_size) as isize;
128127
table.set(
129128
i + 1,
130-
self.inner_conv.as_ref().unwrap().luavalue_from(
129+
self.inner_conv.as_ref().unwrap().value_from_data(
131130
lua,
132131
field_offset + offset,
133132
data_handle,
@@ -138,12 +137,12 @@ impl NativeConvert for CArr {
138137
}
139138
}
140139

141-
impl LuaUserData for CArr {
140+
impl LuaUserData for CArrInfo {
142141
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
143142
fields.add_field_method_get("size", |_, this| Ok(this.get_size()));
144143
fields.add_field_method_get("length", |_, this| Ok(this.get_length()));
145144
fields.add_field_function_get("inner", |lua, this: LuaAnyUserData| {
146-
let inner: LuaValue = get_association(lua, CARR_INNER, this)?
145+
let inner: LuaValue = association::get(lua, CARR_INNER, this)?
147146
// It shouldn't happen.
148147
.ok_or_else(|| LuaError::external("inner field not found"))?;
149148
Ok(inner)

crates/lune-std-ffi/src/c/c_string.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)