mirror of
https://github.com/lune-org/lune.git
synced 2025-04-18 10:53:46 +01:00
Reducing and organizing duplicated codes (#243)
This commit is contained in:
parent
95258e1b51
commit
ba074d9a28
27 changed files with 108 additions and 74 deletions
|
@ -145,7 +145,7 @@ impl LuaUserData for CArr {
|
||||||
fields.add_field_function_get("inner", |lua, this: LuaAnyUserData| {
|
fields.add_field_function_get("inner", |lua, this: LuaAnyUserData| {
|
||||||
let inner: LuaValue = get_association(lua, CARR_INNER, this)?
|
let inner: LuaValue = get_association(lua, CARR_INNER, this)?
|
||||||
// It shouldn't happen.
|
// It shouldn't happen.
|
||||||
.ok_or(LuaError::external("inner field not found"))?;
|
.ok_or_else(|| LuaError::external("inner field not found"))?;
|
||||||
Ok(inner)
|
Ok(inner)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,7 +158,7 @@ impl LuaUserData for CFunc {
|
||||||
this.cif.as_raw_ptr(),
|
this.cif.as_raw_ptr(),
|
||||||
ptr::from_ref(&this.arg_info_list),
|
ptr::from_ref(&this.arg_info_list),
|
||||||
ptr::from_ref(&this.result_info),
|
ptr::from_ref(&this.result_info),
|
||||||
ffi_ref.get_pointer(0),
|
ffi_ref.get_pointer(),
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,9 @@ pub mod method_provider {
|
||||||
if !data_handle.check_boundary(offset, this.get_size()) {
|
if !data_handle.check_boundary(offset, this.get_size()) {
|
||||||
return Err(LuaError::external("Out of bounds"));
|
return Err(LuaError::external("Out of bounds"));
|
||||||
}
|
}
|
||||||
|
if !data_handle.is_readable() {
|
||||||
|
return Err(LuaError::external("Unreadable data handle"));
|
||||||
|
}
|
||||||
|
|
||||||
unsafe { this.luavalue_from(lua, offset, data_handle) }
|
unsafe { this.luavalue_from(lua, offset, data_handle) }
|
||||||
},
|
},
|
||||||
|
@ -65,6 +68,7 @@ pub mod method_provider {
|
||||||
let offset = offset.unwrap_or(0);
|
let offset = offset.unwrap_or(0);
|
||||||
|
|
||||||
let data_handle = &userdata.get_data_handle()?;
|
let data_handle = &userdata.get_data_handle()?;
|
||||||
|
// use or functions
|
||||||
if !data_handle.check_boundary(offset, this.get_size()) {
|
if !data_handle.check_boundary(offset, this.get_size()) {
|
||||||
return Err(LuaError::external("Out of bounds"));
|
return Err(LuaError::external("Out of bounds"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,17 @@ use libffi::middle::Type;
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
use super::{association_names::CPTR_INNER, c_helper, c_type_helper, method_provider};
|
use super::{association_names::CPTR_INNER, c_helper, c_type_helper, method_provider};
|
||||||
use crate::ffi::{
|
use crate::{
|
||||||
|
ffi::{
|
||||||
ffi_association::{get_association, set_association},
|
ffi_association::{get_association, set_association},
|
||||||
NativeConvert, NativeData, NativeSignedness, NativeSize,
|
FfiRef, NativeConvert, NativeData, NativeSignedness, NativeSize,
|
||||||
|
},
|
||||||
|
libffi_helper::SIEE_OF_POINTER,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct CPtr();
|
pub struct CPtr {
|
||||||
|
inner_size: usize,
|
||||||
|
}
|
||||||
|
|
||||||
impl NativeSignedness for CPtr {
|
impl NativeSignedness for CPtr {
|
||||||
fn get_signedness(&self) -> bool {
|
fn get_signedness(&self) -> bool {
|
||||||
|
@ -18,7 +23,7 @@ impl NativeSignedness for CPtr {
|
||||||
}
|
}
|
||||||
impl NativeSize for CPtr {
|
impl NativeSize for CPtr {
|
||||||
fn get_size(&self) -> usize {
|
fn get_size(&self) -> usize {
|
||||||
size_of::<*mut ()>()
|
SIEE_OF_POINTER
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl NativeConvert for CPtr {
|
impl NativeConvert for CPtr {
|
||||||
|
@ -26,12 +31,29 @@ impl NativeConvert for CPtr {
|
||||||
unsafe fn luavalue_into<'lua>(
|
unsafe fn luavalue_into<'lua>(
|
||||||
&self,
|
&self,
|
||||||
_lua: &'lua Lua,
|
_lua: &'lua Lua,
|
||||||
_offset: isize,
|
offset: isize,
|
||||||
_data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
_value: LuaValue<'lua>,
|
value: LuaValue<'lua>,
|
||||||
) -> LuaResult<()> {
|
) -> LuaResult<()> {
|
||||||
|
if let LuaValue::UserData(value_userdata) = value {
|
||||||
|
if value_userdata.is::<FfiRef>() {
|
||||||
|
let value_ref = value_userdata.borrow::<FfiRef>()?;
|
||||||
|
value_ref
|
||||||
|
.check_boundary(0, self.inner_size)
|
||||||
|
.then_some(())
|
||||||
|
.ok_or_else(|| LuaError::external("boundary check failed"))?;
|
||||||
|
*data_handle
|
||||||
|
.get_pointer()
|
||||||
|
.byte_offset(offset)
|
||||||
|
.cast::<*mut ()>() = value_ref.get_pointer();
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(LuaError::external("Ptr:into only allows FfiRef"))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
Err(LuaError::external("Conversion of pointer is not allowed"))
|
Err(LuaError::external("Conversion of pointer is not allowed"))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Read data from ptr, then convert into luavalue
|
// Read data from ptr, then convert into luavalue
|
||||||
unsafe fn luavalue_from<'lua>(
|
unsafe fn luavalue_from<'lua>(
|
||||||
|
@ -51,7 +73,9 @@ impl CPtr {
|
||||||
lua: &'lua Lua,
|
lua: &'lua Lua,
|
||||||
inner: &LuaAnyUserData,
|
inner: &LuaAnyUserData,
|
||||||
) -> LuaResult<LuaAnyUserData<'lua>> {
|
) -> LuaResult<LuaAnyUserData<'lua>> {
|
||||||
let value = lua.create_userdata(Self())?;
|
let value = lua.create_userdata(Self {
|
||||||
|
inner_size: c_helper::get_size(inner)?,
|
||||||
|
})?;
|
||||||
|
|
||||||
set_association(lua, CPTR_INNER, &value, inner)?;
|
set_association(lua, CPTR_INNER, &value, inner)?;
|
||||||
|
|
||||||
|
@ -83,7 +107,7 @@ impl LuaUserData for CPtr {
|
||||||
fields.add_field_method_get("size", |_, _| Ok(size_of::<usize>()));
|
fields.add_field_method_get("size", |_, _| Ok(size_of::<usize>()));
|
||||||
fields.add_field_function_get("inner", |lua, this| {
|
fields.add_field_function_get("inner", |lua, this| {
|
||||||
let inner = get_association(lua, CPTR_INNER, this)?
|
let inner = get_association(lua, CPTR_INNER, this)?
|
||||||
.ok_or(LuaError::external("inner type not found"))?;
|
.ok_or_else(|| LuaError::external("inner type not found"))?;
|
||||||
Ok(inner)
|
Ok(inner)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::{cell::Ref, vec::Vec};
|
||||||
use libffi::{low, middle::Type, raw};
|
use libffi::{low, middle::Type, raw};
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
use super::{association_names::CSTRUCT_INNER, c_helper, method_provider, CArr, CPtr};
|
use super::{association_names::CSTRUCT_INNER, c_helper, method_provider};
|
||||||
use crate::ffi::{
|
use crate::ffi::{
|
||||||
ffi_association::{get_association, set_association},
|
ffi_association::{get_association, set_association},
|
||||||
NativeConvert, NativeData, NativeSignedness, NativeSize, FFI_STATUS_NAMES,
|
NativeConvert, NativeData, NativeSignedness, NativeSize, FFI_STATUS_NAMES,
|
||||||
|
@ -56,7 +56,7 @@ impl CStruct {
|
||||||
|
|
||||||
// Create new CStruct UserData with LuaTable.
|
// Create new CStruct UserData with LuaTable.
|
||||||
// Lock and hold table for .inner ref
|
// Lock and hold table for .inner ref
|
||||||
pub fn new_from_table<'lua>(
|
pub fn from_table<'lua>(
|
||||||
lua: &'lua Lua,
|
lua: &'lua Lua,
|
||||||
table: LuaTable<'lua>,
|
table: LuaTable<'lua>,
|
||||||
) -> LuaResult<LuaAnyUserData<'lua>> {
|
) -> LuaResult<LuaAnyUserData<'lua>> {
|
||||||
|
@ -74,7 +74,7 @@ impl CStruct {
|
||||||
// <CStruct( u8, i32, size = 8 )>
|
// <CStruct( u8, i32, size = 8 )>
|
||||||
pub fn stringify(lua: &Lua, userdata: &LuaAnyUserData) -> LuaResult<String> {
|
pub fn stringify(lua: &Lua, userdata: &LuaAnyUserData) -> LuaResult<String> {
|
||||||
if let LuaValue::Table(fields) = get_association(lua, CSTRUCT_INNER, userdata)?
|
if let LuaValue::Table(fields) = get_association(lua, CSTRUCT_INNER, userdata)?
|
||||||
.ok_or(LuaError::external("Field table not found"))?
|
.ok_or_else(|| LuaError::external("Field table not found"))?
|
||||||
{
|
{
|
||||||
let mut result = String::from(" ");
|
let mut result = String::from(" ");
|
||||||
for i in 0..fields.raw_len() {
|
for i in 0..fields.raw_len() {
|
||||||
|
@ -97,7 +97,7 @@ impl CStruct {
|
||||||
let offset = self
|
let offset = self
|
||||||
.inner_offset_list
|
.inner_offset_list
|
||||||
.get(index)
|
.get(index)
|
||||||
.ok_or(LuaError::external("Out of index"))?
|
.ok_or_else(|| LuaError::external("Out of index"))?
|
||||||
.to_owned();
|
.to_owned();
|
||||||
Ok(offset)
|
Ok(offset)
|
||||||
}
|
}
|
||||||
|
@ -185,7 +185,7 @@ impl LuaUserData for CStruct {
|
||||||
// By referencing the table to struct, the types inside do not disappear
|
// By referencing the table to struct, the types inside do not disappear
|
||||||
methods.add_function("field", |lua, (this, field): (LuaAnyUserData, usize)| {
|
methods.add_function("field", |lua, (this, field): (LuaAnyUserData, usize)| {
|
||||||
if let LuaValue::Table(fields) = get_association(lua, CSTRUCT_INNER, this)?
|
if let LuaValue::Table(fields) = get_association(lua, CSTRUCT_INNER, this)?
|
||||||
.ok_or(LuaError::external("Field table not found"))?
|
.ok_or_else(|| LuaError::external("Field table not found"))?
|
||||||
{
|
{
|
||||||
let value: LuaValue = fields.raw_get(field + 1)?;
|
let value: LuaValue = fields.raw_get(field + 1)?;
|
||||||
Ok(value)
|
Ok(value)
|
||||||
|
|
|
@ -6,7 +6,6 @@ use libffi::middle::Type;
|
||||||
use lune_utils::fmt::{pretty_format_value, ValueFormatConfig};
|
use lune_utils::fmt::{pretty_format_value, ValueFormatConfig};
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
use super::{CArr, CPtr};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
c::method_provider,
|
c::method_provider,
|
||||||
ffi::{GetNativeData, NativeConvert, NativeData, NativeSignedness, NativeSize},
|
ffi::{GetNativeData, NativeConvert, NativeData, NativeSignedness, NativeSize},
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<f32> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<f32>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<f32>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,9 @@ impl NativeConvert for CType<f32> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<f32>()).into_lua(lua)? };
|
let value = unsafe {
|
||||||
|
(*data_handle.get_pointer().byte_offset(offset).cast::<f32>()).into_lua(lua)?
|
||||||
|
};
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<f64> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<f64>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<f64>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,9 @@ impl NativeConvert for CType<f64> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<f64>()).into_lua(lua)? };
|
let value = unsafe {
|
||||||
|
(*data_handle.get_pointer().byte_offset(offset).cast::<f64>()).into_lua(lua)?
|
||||||
|
};
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<i128> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<i128>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<i128>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl NativeConvert for CType<i128> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<i128>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<i128>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<i16> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<i16>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<i16>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl NativeConvert for CType<i16> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<i16>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<i16>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<i32> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<i32>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<i32>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl NativeConvert for CType<i32> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<i32>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<i32>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<i64> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<i64>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<i64>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl NativeConvert for CType<i64> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<i64>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<i64>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ impl NativeConvert for CType<i8> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<i8>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<i8>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ impl NativeConvert for CType<i8> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<i8>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<i8>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<isize> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<isize>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<isize>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl NativeConvert for CType<isize> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<isize>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<isize>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<u128> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<u128>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<u128>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl NativeConvert for CType<u128> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<u128>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<u128>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ impl NativeConvert for CType<u16> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<u16>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<u16>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ impl NativeConvert for CType<u16> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<u16>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<u16>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<u32> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<u32>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<u32>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl NativeConvert for CType<u32> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<u32>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<u32>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<u64> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<u64>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<u64>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl NativeConvert for CType<u64> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<u64>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<u64>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ impl NativeConvert for CType<u8> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<u8>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<u8>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,8 @@ impl NativeConvert for CType<u8> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<u8>()).into_lua(lua)? };
|
let value =
|
||||||
|
unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<u8>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl NativeConvert for CType<usize> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
*(data_handle.get_pointer(offset).cast::<usize>()) = value;
|
*(data_handle.get_pointer().byte_offset(offset).cast::<usize>()) = value;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl NativeConvert for CType<usize> {
|
||||||
offset: isize,
|
offset: isize,
|
||||||
data_handle: &Ref<dyn NativeData>,
|
data_handle: &Ref<dyn NativeData>,
|
||||||
) -> LuaResult<LuaValue<'lua>> {
|
) -> LuaResult<LuaValue<'lua>> {
|
||||||
let value = unsafe { (*data_handle.get_pointer(offset).cast::<usize>()).into_lua(lua)? };
|
let value = unsafe { (*data_handle.get_pointer().byte_offset(offset).cast::<usize>()).into_lua(lua)? };
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,7 +77,7 @@ impl FfiBox {
|
||||||
) -> LuaResult<LuaAnyUserData<'lua>> {
|
) -> LuaResult<LuaAnyUserData<'lua>> {
|
||||||
let target = this.borrow::<FfiBox>()?;
|
let target = this.borrow::<FfiBox>()?;
|
||||||
let mut bounds = FfiRefBounds::new(0, target.size());
|
let mut bounds = FfiRefBounds::new(0, target.size());
|
||||||
let mut ptr = unsafe { target.get_pointer(0) };
|
let mut ptr = unsafe { target.get_pointer() };
|
||||||
|
|
||||||
// Calculate offset
|
// Calculate offset
|
||||||
if let Some(t) = offset {
|
if let Some(t) = offset {
|
||||||
|
@ -88,7 +88,7 @@ impl FfiBox {
|
||||||
t
|
t
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
ptr = unsafe { target.get_pointer(t) };
|
ptr = unsafe { ptr.byte_offset(t) };
|
||||||
bounds = bounds.offset(t);
|
bounds = bounds.offset(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,12 +130,8 @@ impl NativeData for FfiBox {
|
||||||
}
|
}
|
||||||
self.size() - (offset as usize) >= size
|
self.size() - (offset as usize) >= size
|
||||||
}
|
}
|
||||||
unsafe fn get_pointer(&self, offset: isize) -> *mut () {
|
unsafe fn get_pointer(&self) -> *mut () {
|
||||||
self.data
|
self.data.as_ptr().cast_mut().cast::<()>()
|
||||||
.as_ptr()
|
|
||||||
.byte_offset(offset)
|
|
||||||
.cast_mut()
|
|
||||||
.cast::<()>()
|
|
||||||
}
|
}
|
||||||
fn is_readable(&self) -> bool {
|
fn is_readable(&self) -> bool {
|
||||||
true
|
true
|
||||||
|
|
|
@ -31,6 +31,8 @@ impl FfiCallable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO? async call: if have no lua closure in arguments, fficallble can be called with async way
|
||||||
|
|
||||||
pub unsafe fn call(&self, result: &Ref<dyn NativeData>, args: LuaMultiValue) -> LuaResult<()> {
|
pub unsafe fn call(&self, result: &Ref<dyn NativeData>, args: LuaMultiValue) -> LuaResult<()> {
|
||||||
result
|
result
|
||||||
.check_boundary(0, self.result_info.as_ref().unwrap().size)
|
.check_boundary(0, self.result_info.as_ref().unwrap().size)
|
||||||
|
@ -54,7 +56,7 @@ impl FfiCallable {
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
LuaError::external(format!("argument {index} boundary check failed"))
|
LuaError::external(format!("argument {index} boundary check failed"))
|
||||||
})?;
|
})?;
|
||||||
data_handle.get_pointer(0)
|
data_handle.get_pointer()
|
||||||
} else {
|
} else {
|
||||||
return Err(LuaError::external("unimpl"));
|
return Err(LuaError::external("unimpl"));
|
||||||
};
|
};
|
||||||
|
@ -64,7 +66,7 @@ impl FfiCallable {
|
||||||
ffi_call(
|
ffi_call(
|
||||||
self.cif,
|
self.cif,
|
||||||
Some(*self.code.as_safe_fun()),
|
Some(*self.code.as_safe_fun()),
|
||||||
result.get_pointer(0).cast::<c_void>(),
|
result.get_pointer().cast::<c_void>(),
|
||||||
arg_list.as_mut_ptr(),
|
arg_list.as_mut_ptr(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@ where
|
||||||
T: AsPrimitive<U>,
|
T: AsPrimitive<U>,
|
||||||
U: 'static + Copy,
|
U: 'static + Copy,
|
||||||
{
|
{
|
||||||
let from_ptr = unsafe { from.get_pointer(0).cast::<T>() };
|
let from_ptr = unsafe { from.get_pointer().cast::<T>() };
|
||||||
let into_ptr = unsafe { into.get_pointer(0).cast::<U>() };
|
let into_ptr = unsafe { into.get_pointer().cast::<U>() };
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
*into_ptr = (*from_ptr).as_();
|
*into_ptr = (*from_ptr).as_();
|
||||||
|
|
|
@ -7,7 +7,7 @@ use super::super::{FfiBox, FfiRef};
|
||||||
|
|
||||||
pub trait NativeData {
|
pub trait NativeData {
|
||||||
fn check_boundary(&self, offset: isize, size: usize) -> bool;
|
fn check_boundary(&self, offset: isize, size: usize) -> bool;
|
||||||
unsafe fn get_pointer(&self, offset: isize) -> *mut ();
|
unsafe fn get_pointer(&self) -> *mut ();
|
||||||
fn is_writable(&self) -> bool;
|
fn is_writable(&self) -> bool;
|
||||||
fn is_readable(&self) -> bool;
|
fn is_readable(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,12 +19,12 @@ pub use self::{
|
||||||
|
|
||||||
// Box:ref():ref() should not be able to modify, Only for external
|
// Box:ref():ref() should not be able to modify, Only for external
|
||||||
const BOX_REF_REF_FLAGS: u8 = 0;
|
const BOX_REF_REF_FLAGS: u8 = 0;
|
||||||
const UNINIT_REF_FLAGS: u8 = FfiRefFlag::Uninit.value()
|
const UNINIT_REF_FLAGS: u8 = FfiRefFlag::Uninit.value();
|
||||||
| FfiRefFlag::Writable.value()
|
// | FfiRefFlag::Writable.value()
|
||||||
| FfiRefFlag::Readable.value()
|
// | FfiRefFlag::Readable.value()
|
||||||
| FfiRefFlag::Dereferenceable.value()
|
// | FfiRefFlag::Dereferenceable.value()
|
||||||
| FfiRefFlag::Offsetable.value()
|
// | FfiRefFlag::Offsetable.value()
|
||||||
| FfiRefFlag::Function.value();
|
// | FfiRefFlag::Function.value();
|
||||||
|
|
||||||
// A referenced space. It is possible to read and write through types.
|
// A referenced space. It is possible to read and write through types.
|
||||||
// This operation is not safe. This may cause a memory error in Lua
|
// This operation is not safe. This may cause a memory error in Lua
|
||||||
|
@ -80,14 +80,16 @@ impl FfiRef {
|
||||||
pub unsafe fn deref(&self) -> LuaResult<Self> {
|
pub unsafe fn deref(&self) -> LuaResult<Self> {
|
||||||
u8_test(self.flags, FfiRefFlag::Dereferenceable.value())
|
u8_test(self.flags, FfiRefFlag::Dereferenceable.value())
|
||||||
.then_some(())
|
.then_some(())
|
||||||
.ok_or(LuaError::external("This pointer is not dereferenceable."))?;
|
.ok_or_else(|| LuaError::external("This pointer is not dereferenceable."))?;
|
||||||
|
|
||||||
self.boundary
|
self.boundary
|
||||||
.check_sized(0, size_of::<usize>())
|
.check_sized(0, size_of::<usize>())
|
||||||
.then_some(())
|
.then_some(())
|
||||||
.ok_or(LuaError::external(
|
.ok_or_else(|| {
|
||||||
|
LuaError::external(
|
||||||
"Offset is out of bounds. Dereferencing pointer requires size of usize",
|
"Offset is out of bounds. Dereferencing pointer requires size of usize",
|
||||||
))?;
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
// FIXME flags
|
// FIXME flags
|
||||||
Ok(Self::new(
|
Ok(Self::new(
|
||||||
|
@ -106,7 +108,7 @@ impl FfiRef {
|
||||||
pub unsafe fn offset(&self, offset: isize) -> LuaResult<Self> {
|
pub unsafe fn offset(&self, offset: isize) -> LuaResult<Self> {
|
||||||
u8_test(self.flags, FfiRefFlag::Offsetable.value())
|
u8_test(self.flags, FfiRefFlag::Offsetable.value())
|
||||||
.then_some(())
|
.then_some(())
|
||||||
.ok_or(LuaError::external("This pointer is not offsetable."))?;
|
.ok_or_else(|| LuaError::external("This pointer is not offsetable."))?;
|
||||||
|
|
||||||
// Check boundary, if exceed, return error
|
// Check boundary, if exceed, return error
|
||||||
self.boundary
|
self.boundary
|
||||||
|
@ -142,8 +144,8 @@ impl NativeData for FfiRef {
|
||||||
fn check_boundary(&self, offset: isize, size: usize) -> bool {
|
fn check_boundary(&self, offset: isize, size: usize) -> bool {
|
||||||
self.boundary.check_sized(offset, size)
|
self.boundary.check_sized(offset, size)
|
||||||
}
|
}
|
||||||
unsafe fn get_pointer(&self, offset: isize) -> *mut () {
|
unsafe fn get_pointer(&self) -> *mut () {
|
||||||
self.ptr.byte_offset(offset)
|
**self.ptr
|
||||||
}
|
}
|
||||||
fn is_readable(&self) -> bool {
|
fn is_readable(&self) -> bool {
|
||||||
u8_test(self.flags, FfiRefFlag::Readable.value())
|
u8_test(self.flags, FfiRefFlag::Readable.value())
|
||||||
|
|
|
@ -27,7 +27,7 @@ pub fn module(lua: &Lua) -> LuaResult<LuaTable> {
|
||||||
.with_function("box", |_lua, size: usize| Ok(FfiBox::new(size)))?
|
.with_function("box", |_lua, size: usize| Ok(FfiBox::new(size)))?
|
||||||
.with_function("open", |_lua, name: String| FfiLib::new(name))?
|
.with_function("open", |_lua, name: String| FfiLib::new(name))?
|
||||||
.with_function("structInfo", |lua, types: LuaTable| {
|
.with_function("structInfo", |lua, types: LuaTable| {
|
||||||
CStruct::new_from_table(lua, types)
|
CStruct::from_table(lua, types)
|
||||||
})?
|
})?
|
||||||
.with_function("uninitRef", |_lua, ()| Ok(FfiRef::new_uninit()))?
|
.with_function("uninitRef", |_lua, ()| Ok(FfiRef::new_uninit()))?
|
||||||
.with_function("isInteger", |_lua, num: LuaValue| Ok(is_integer(num)))?
|
.with_function("isInteger", |_lua, num: LuaValue| Ok(is_integer(num)))?
|
||||||
|
|
|
@ -27,3 +27,5 @@ pub fn get_ensured_size(ffi_type: *mut raw::ffi_type) -> LuaResult<usize> {
|
||||||
}
|
}
|
||||||
unsafe { Ok((*ffi_type).size) }
|
unsafe { Ok((*ffi_type).size) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const SIEE_OF_POINTER: usize = size_of::<*mut ()>();
|
||||||
|
|
Loading…
Add table
Reference in a new issue