Add is_integer (#243)

This commit is contained in:
qwreey 2024-09-02 10:14:34 +00:00
parent dd6a3861e5
commit 94d8d079c4
No known key found for this signature in database
GPG key ID: D28DB79297A214BD
8 changed files with 52 additions and 18 deletions

View file

@ -12,7 +12,7 @@ use super::{
use crate::ffi::{ffi_association::get_association, NativeConvert, FFI_STATUS_NAMES}; use crate::ffi::{ffi_association::get_association, NativeConvert, FFI_STATUS_NAMES};
// Get the NativeConvert handle from the type UserData // Get the NativeConvert handle from the type UserData
// this is intended to avoid constant table lookups. (eg: struct) // this is intended to avoid lookup userdata and lua table every time. (eg: struct)
// userdata must live longer than the NativeConvert handle. // userdata must live longer than the NativeConvert handle.
// However, c_struct is a strong reference to each field, so this is not a problem. // However, c_struct is a strong reference to each field, so this is not a problem.
pub unsafe fn get_conv(userdata: &LuaAnyUserData) -> LuaResult<*const dyn NativeConvert> { pub unsafe fn get_conv(userdata: &LuaAnyUserData) -> LuaResult<*const dyn NativeConvert> {

View file

@ -1,5 +1,5 @@
use std::boxed::Box; use std::boxed::Box;
use std::sync::LazyLock; use std::sync::OnceLock;
use mlua::prelude::*; use mlua::prelude::*;
@ -10,13 +10,18 @@ use super::{
NativeDataHandle, NativeDataHandle,
}; };
static BOX_REF_FLAGS: LazyLock<FfiRefFlagList> = LazyLock::new(|| { static BOX_REF_FLAGS: OnceLock<FfiRefFlagList> = OnceLock::new();
FfiRefFlagList::new(&[ fn get_box_ref_flags() -> FfiRefFlagList {
FfiRefFlag::Offsetable, BOX_REF_FLAGS
FfiRefFlag::Readable, .get_or_init(|| {
FfiRefFlag::Writable, FfiRefFlagList::new(&[
]) FfiRefFlag::Offsetable,
}); FfiRefFlag::Readable,
FfiRefFlag::Writable,
])
})
.to_owned()
}
// It is an untyped, sized memory area that Lua can manage. // It is an untyped, sized memory area that Lua can manage.
// This area is safe within Lua. Operations have their boundaries checked. // This area is safe within Lua. Operations have their boundaries checked.
@ -93,8 +98,7 @@ impl FfiBox {
// To deref a box space is to allow lua to read any space, // To deref a box space is to allow lua to read any space,
// which has security issues and is ultimately dangerous. // which has security issues and is ultimately dangerous.
// Therefore, box:ref():deref() is not allowed. // Therefore, box:ref():deref() is not allowed.
let luaref = let luaref = lua.create_userdata(FfiRef::new(ptr.cast(), get_box_ref_flags(), bounds))?;
lua.create_userdata(FfiRef::new(ptr.cast(), (*BOX_REF_FLAGS).clone(), bounds))?;
// Makes box alive longer then ref // Makes box alive longer then ref
set_association(lua, REF_INNER, &luaref, &this)?; set_association(lua, REF_INNER, &luaref, &this)?;
@ -114,7 +118,7 @@ impl FfiBox {
// Get raw ptr // Get raw ptr
pub fn get_ptr(&self) -> *mut u8 { pub fn get_ptr(&self) -> *mut u8 {
self.data.as_ptr() as *mut u8 self.data.as_ptr().cast_mut()
} }
} }
@ -134,7 +138,7 @@ impl NativeDataHandle for FfiBox {
true true
} }
unsafe fn get_pointer(&self, offset: isize) -> *mut () { unsafe fn get_pointer(&self, offset: isize) -> *mut () {
self.get_ptr().byte_offset(offset) as *mut () self.get_ptr().byte_offset(offset).cast::<()>()
} }
} }

View file

@ -0,0 +1,3 @@
struct FfiFunc {
args: Vec,
}

View file

@ -64,7 +64,7 @@ impl FfiLib {
impl LuaUserData for FfiLib { impl LuaUserData for FfiLib {
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_function("dlsym", |lua, (this, name): (LuaAnyUserData, String)| { methods.add_function("find", |lua, (this, name): (LuaAnyUserData, String)| {
let luasym = FfiLib::get_sym(lua, this, name)?; let luasym = FfiLib::get_sym(lua, this, name)?;
Ok(luasym) Ok(luasym)
}); });

View file

@ -0,0 +1,19 @@
use std::cell::Ref;
use mlua::prelude::*;
use super::NativeDataHandle;
// Handle native data, provide type conversion between luavalue and native types
pub trait NativeCall {
// Call native function
unsafe fn call_native(
&self,
lua: &Lua,
arg: LuaMultiValue,
ret: &Ref<dyn NativeDataHandle>,
) -> LuaResult<()>;
// Call lua closure
unsafe fn call_lua(&self, lua: &Lua, arg: LuaMultiValue, ret: *mut ()) -> LuaResult<()>;
}

View file

@ -1,3 +1,4 @@
mod call;
mod cast; mod cast;
mod convert; mod convert;
mod readwrite; mod readwrite;
@ -13,6 +14,6 @@ pub trait NativeSignedness {
} }
pub use self::{ pub use self::{
cast::native_num_cast, convert::NativeConvert, readwrite::GetNativeDataHandle, call::NativeCall, cast::native_num_cast, convert::NativeConvert,
readwrite::NativeDataHandle, readwrite::GetNativeDataHandle, readwrite::NativeDataHandle,
}; };

View file

@ -5,6 +5,8 @@ mod ffi_native;
mod ffi_raw; mod ffi_raw;
mod ffi_ref; mod ffi_ref;
use mlua::prelude::*;
pub use self::{ pub use self::{
ffi_box::FfiBox, ffi_box::FfiBox,
ffi_lib::FfiLib, ffi_lib::FfiLib,
@ -48,3 +50,7 @@ pub mod bit_mask {
pub(crate) use U8_TEST; pub(crate) use U8_TEST;
} }
pub fn is_integer(num: LuaValue) -> bool {
num.is_integer()
}

View file

@ -8,7 +8,7 @@ mod ffi;
use crate::{ use crate::{
c::{create_all_c_types, create_all_types, CFn, CStruct}, c::{create_all_c_types, create_all_types, CFn, CStruct},
ffi::{create_nullptr, FfiBox, FfiLib}, ffi::{create_nullptr, is_integer, FfiBox, FfiLib},
}; };
/** /**
@ -25,7 +25,7 @@ pub fn module(lua: &Lua) -> LuaResult<LuaTable> {
.with_value("nullptr", create_nullptr(lua)?)? .with_value("nullptr", create_nullptr(lua)?)?
.with_function("box", |_, size: usize| Ok(FfiBox::new(size)))? .with_function("box", |_, size: usize| Ok(FfiBox::new(size)))?
// TODO: discuss about function name. matching with io.open is better? // TODO: discuss about function name. matching with io.open is better?
.with_function("dlopen", |_, name: String| { .with_function("open", |_, name: String| {
let lib = FfiLib::new(name)?; let lib = FfiLib::new(name)?;
Ok(lib) Ok(lib)
})? })?
@ -33,6 +33,7 @@ pub fn module(lua: &Lua) -> LuaResult<LuaTable> {
let cstruct = CStruct::new_from_lua_table(lua, types)?; let cstruct = CStruct::new_from_lua_table(lua, types)?;
Ok(cstruct) Ok(cstruct)
})? })?
.with_function("isInteger", |_lua, num: LuaValue| Ok(is_integer(num)))?
.with_function("fn", |lua, (args, ret): (LuaTable, LuaAnyUserData)| { .with_function("fn", |lua, (args, ret): (LuaTable, LuaAnyUserData)| {
let cfn = CFn::new_from_lua_table(lua, args, ret)?; let cfn = CFn::new_from_lua_table(lua, args, ret)?;
Ok(cfn) Ok(cfn)