Skip to content

Commit

Permalink
Move association names(const &str) into mod association_names
Browse files Browse the repository at this point in the history
  • Loading branch information
qwreey committed Aug 24, 2024
1 parent 0eb9326 commit 975723f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
3 changes: 1 addition & 2 deletions crates/lune-std-ffi/src/carr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions crates/lune-std-ffi/src/cptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -22,7 +21,7 @@ impl CPtr {
) -> LuaResult<LuaValue<'lua>> {
let value = Self().into_lua(lua)?;

set_association(lua, POINTER_INNER, value.borrow(), inner)?;
set_association(lua, CPTR_INNER, value.borrow(), inner)?;

Ok(value)
}
Expand Down Expand Up @@ -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::<usize>()));
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)
});
Expand Down
3 changes: 1 addition & 2 deletions crates/lune-std-ffi/src/cstruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,8 +24,6 @@ pub struct CStruct {
size: usize,
}

const CSTRUCT_INNER: &str = "__cstruct_inner";

impl CStruct {
pub fn new(fields: Vec<Type>) -> LuaResult<Self> {
let libffi_type = Type::structure(fields.clone());
Expand Down
7 changes: 7 additions & 0 deletions crates/lune-std-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ 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",
"ffi_status_FFI_BAD_ABI",
"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.
Expand Down

0 comments on commit 975723f

Please sign in to comment.