diff --git a/crates/lune-std-ffi/src/carr.rs b/crates/lune-std-ffi/src/carr.rs index 3ec5094a..fbee29b1 100644 --- a/crates/lune-std-ffi/src/carr.rs +++ b/crates/lune-std-ffi/src/carr.rs @@ -2,6 +2,7 @@ use libffi::middle::Type; use mlua::prelude::*; use crate::association::{get_association, set_association}; +use crate::association_names::CARR_INNER; use crate::chelper::{get_ensured_size, stringify_userdata, type_from_userdata}; use crate::cptr::CPtr; @@ -16,8 +17,6 @@ use crate::cptr::CPtr; // Padding after each field inside the struct is set to next field can follow the alignment. // There is no problem even if you create a struct with n fields of a single type within the struct. Array adheres to the condition that there is no additional padding between each element. Padding to a struct is padding inside the struct. Simply think of the padding byte as a trailing unnamed field. -const CARR_INNER: &str = "__carr_inner"; - pub struct CArr { libffi_type: Type, struct_type: Type, diff --git a/crates/lune-std-ffi/src/cptr.rs b/crates/lune-std-ffi/src/cptr.rs index 60759e9f..a2663703 100644 --- a/crates/lune-std-ffi/src/cptr.rs +++ b/crates/lune-std-ffi/src/cptr.rs @@ -6,11 +6,10 @@ use libffi::middle::Type; use mlua::prelude::*; use crate::association::{get_association, set_association}; +use crate::association_names::CPTR_INNER; use crate::carr::CArr; use crate::chelper::{name_from_userdata, stringify_userdata}; -const POINTER_INNER: &str = "__pointer_inner"; - pub struct CPtr(); impl CPtr { @@ -22,7 +21,7 @@ impl CPtr { ) -> LuaResult> { let value = Self().into_lua(lua)?; - set_association(lua, POINTER_INNER, value.borrow(), inner)?; + set_association(lua, CPTR_INNER, value.borrow(), inner)?; Ok(value) } @@ -55,7 +54,7 @@ impl LuaUserData for CPtr { fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) { fields.add_field_method_get("size", |_, _| Ok(size_of::())); fields.add_field_function_get("inner", |lua, this| { - let inner = get_association(lua, POINTER_INNER, this)? + let inner = get_association(lua, CPTR_INNER, this)? .ok_or(LuaError::external("inner type not found"))?; Ok(inner) }); diff --git a/crates/lune-std-ffi/src/cstruct.rs b/crates/lune-std-ffi/src/cstruct.rs index 701769da..20380adc 100644 --- a/crates/lune-std-ffi/src/cstruct.rs +++ b/crates/lune-std-ffi/src/cstruct.rs @@ -10,6 +10,7 @@ use libffi::{ use mlua::prelude::*; use crate::association::{get_association, set_association}; +use crate::association_names::CSTRUCT_INNER; use crate::chelper::{name_from_userdata, stringify_userdata, type_list_from_table}; use crate::ctype::CType; use crate::FFI_STATUS_NAMES; @@ -23,8 +24,6 @@ pub struct CStruct { size: usize, } -const CSTRUCT_INNER: &str = "__cstruct_inner"; - impl CStruct { pub fn new(fields: Vec) -> LuaResult { let libffi_type = Type::structure(fields.clone()); diff --git a/crates/lune-std-ffi/src/lib.rs b/crates/lune-std-ffi/src/lib.rs index 235153b5..1cbc0b8c 100644 --- a/crates/lune-std-ffi/src/lib.rs +++ b/crates/lune-std-ffi/src/lib.rs @@ -23,6 +23,7 @@ use crate::ctype::create_all_types; use crate::ffibox::FfiBox; use crate::ffilib::FfiLib; +// Converts ffi status into &str pub const FFI_STATUS_NAMES: [&str; 4] = [ "ffi_status_FFI_OK", "ffi_status_FFI_BAD_TYPEDEF", @@ -30,6 +31,12 @@ pub const FFI_STATUS_NAMES: [&str; 4] = [ "ffi_status_FFI_BAD_ARGTYPE", ]; +mod association_names { + pub const CPTR_INNER: &str = "__cptr_inner"; + pub const CARR_INNER: &str = "__carr_inner"; + pub const CSTRUCT_INNER: &str = "__cstruct_inner"; +} + /** Creates the `ffi` standard library module.