Skip to content

Commit 94d8d07

Browse files
committed
Add is_integer (#243)
1 parent dd6a386 commit 94d8d07

File tree

8 files changed

+52
-18
lines changed

8 files changed

+52
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::{
1212
use crate::ffi::{ffi_association::get_association, NativeConvert, FFI_STATUS_NAMES};
1313

1414
// Get the NativeConvert handle from the type UserData
15-
// this is intended to avoid constant table lookups. (eg: struct)
15+
// this is intended to avoid lookup userdata and lua table every time. (eg: struct)
1616
// userdata must live longer than the NativeConvert handle.
1717
// However, c_struct is a strong reference to each field, so this is not a problem.
1818
pub unsafe fn get_conv(userdata: &LuaAnyUserData) -> LuaResult<*const dyn NativeConvert> {

crates/lune-std-ffi/src/ffi/ffi_box.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::boxed::Box;
2-
use std::sync::LazyLock;
2+
use std::sync::OnceLock;
33

44
use mlua::prelude::*;
55

@@ -10,13 +10,18 @@ use super::{
1010
NativeDataHandle,
1111
};
1212

13-
static BOX_REF_FLAGS: LazyLock<FfiRefFlagList> = LazyLock::new(|| {
14-
FfiRefFlagList::new(&[
15-
FfiRefFlag::Offsetable,
16-
FfiRefFlag::Readable,
17-
FfiRefFlag::Writable,
18-
])
19-
});
13+
static BOX_REF_FLAGS: OnceLock<FfiRefFlagList> = OnceLock::new();
14+
fn get_box_ref_flags() -> FfiRefFlagList {
15+
BOX_REF_FLAGS
16+
.get_or_init(|| {
17+
FfiRefFlagList::new(&[
18+
FfiRefFlag::Offsetable,
19+
FfiRefFlag::Readable,
20+
FfiRefFlag::Writable,
21+
])
22+
})
23+
.to_owned()
24+
}
2025

2126
// It is an untyped, sized memory area that Lua can manage.
2227
// This area is safe within Lua. Operations have their boundaries checked.
@@ -93,8 +98,7 @@ impl FfiBox {
9398
// To deref a box space is to allow lua to read any space,
9499
// which has security issues and is ultimately dangerous.
95100
// Therefore, box:ref():deref() is not allowed.
96-
let luaref =
97-
lua.create_userdata(FfiRef::new(ptr.cast(), (*BOX_REF_FLAGS).clone(), bounds))?;
101+
let luaref = lua.create_userdata(FfiRef::new(ptr.cast(), get_box_ref_flags(), bounds))?;
98102

99103
// Makes box alive longer then ref
100104
set_association(lua, REF_INNER, &luaref, &this)?;
@@ -114,7 +118,7 @@ impl FfiBox {
114118

115119
// Get raw ptr
116120
pub fn get_ptr(&self) -> *mut u8 {
117-
self.data.as_ptr() as *mut u8
121+
self.data.as_ptr().cast_mut()
118122
}
119123
}
120124

@@ -134,7 +138,7 @@ impl NativeDataHandle for FfiBox {
134138
true
135139
}
136140
unsafe fn get_pointer(&self, offset: isize) -> *mut () {
137-
self.get_ptr().byte_offset(offset) as *mut ()
141+
self.get_ptr().byte_offset(offset).cast::<()>()
138142
}
139143
}
140144

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct FfiFunc {
2+
args: Vec,
3+
}

crates/lune-std-ffi/src/ffi/ffi_lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl FfiLib {
6464

6565
impl LuaUserData for FfiLib {
6666
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
67-
methods.add_function("dlsym", |lua, (this, name): (LuaAnyUserData, String)| {
67+
methods.add_function("find", |lua, (this, name): (LuaAnyUserData, String)| {
6868
let luasym = FfiLib::get_sym(lua, this, name)?;
6969
Ok(luasym)
7070
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::cell::Ref;
2+
3+
use mlua::prelude::*;
4+
5+
use super::NativeDataHandle;
6+
7+
// Handle native data, provide type conversion between luavalue and native types
8+
pub trait NativeCall {
9+
// Call native function
10+
unsafe fn call_native(
11+
&self,
12+
lua: &Lua,
13+
arg: LuaMultiValue,
14+
ret: &Ref<dyn NativeDataHandle>,
15+
) -> LuaResult<()>;
16+
17+
// Call lua closure
18+
unsafe fn call_lua(&self, lua: &Lua, arg: LuaMultiValue, ret: *mut ()) -> LuaResult<()>;
19+
}

crates/lune-std-ffi/src/ffi/ffi_native/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod call;
12
mod cast;
23
mod convert;
34
mod readwrite;
@@ -13,6 +14,6 @@ pub trait NativeSignedness {
1314
}
1415

1516
pub use self::{
16-
cast::native_num_cast, convert::NativeConvert, readwrite::GetNativeDataHandle,
17-
readwrite::NativeDataHandle,
17+
call::NativeCall, cast::native_num_cast, convert::NativeConvert,
18+
readwrite::GetNativeDataHandle, readwrite::NativeDataHandle,
1819
};

crates/lune-std-ffi/src/ffi/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ mod ffi_native;
55
mod ffi_raw;
66
mod ffi_ref;
77

8+
use mlua::prelude::*;
9+
810
pub use self::{
911
ffi_box::FfiBox,
1012
ffi_lib::FfiLib,
@@ -48,3 +50,7 @@ pub mod bit_mask {
4850

4951
pub(crate) use U8_TEST;
5052
}
53+
54+
pub fn is_integer(num: LuaValue) -> bool {
55+
num.is_integer()
56+
}

crates/lune-std-ffi/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod ffi;
88

99
use crate::{
1010
c::{create_all_c_types, create_all_types, CFn, CStruct},
11-
ffi::{create_nullptr, FfiBox, FfiLib},
11+
ffi::{create_nullptr, is_integer, FfiBox, FfiLib},
1212
};
1313

1414
/**
@@ -25,14 +25,15 @@ pub fn module(lua: &Lua) -> LuaResult<LuaTable> {
2525
.with_value("nullptr", create_nullptr(lua)?)?
2626
.with_function("box", |_, size: usize| Ok(FfiBox::new(size)))?
2727
// TODO: discuss about function name. matching with io.open is better?
28-
.with_function("dlopen", |_, name: String| {
28+
.with_function("open", |_, name: String| {
2929
let lib = FfiLib::new(name)?;
3030
Ok(lib)
3131
})?
3232
.with_function("struct", |lua, types: LuaTable| {
3333
let cstruct = CStruct::new_from_lua_table(lua, types)?;
3434
Ok(cstruct)
3535
})?
36+
.with_function("isInteger", |_lua, num: LuaValue| Ok(is_integer(num)))?
3637
.with_function("fn", |lua, (args, ret): (LuaTable, LuaAnyUserData)| {
3738
let cfn = CFn::new_from_lua_table(lua, args, ret)?;
3839
Ok(cfn)

0 commit comments

Comments
 (0)