Skip to content

Commit 658b5ef

Browse files
committed
Move fixed size types into ffi, Fix test cases (#243)
1 parent b31f814 commit 658b5ef

File tree

32 files changed

+248
-161
lines changed

32 files changed

+248
-161
lines changed

crates/lune-std-ffi/README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# `lune-std-ffi`
22

3+
## Tests
4+
5+
See [tests/ffi](../../tests/ffi/README.md)
6+
37
## Code structure
48

59
### /c
@@ -83,16 +87,19 @@ Implememt type-casting for all CTypes
8387

8488
## TODO
8589

86-
Add `CTypeInfo:add(target, from1, from2, ...)` and `:sub` `:mul` `:div` `:mod` `:pow` for math operation.
90+
- CString
91+
92+
- Add buffer for owned data support
8793

88-
> Luau cannot handle i64 or i128
94+
- Add math operation.
8995

90-
Add bor band and such bit-related operation
96+
> `CTypeInfo:add(target, from1, from2, ...)` and `:sub` `:mul` `:div` `:mod` `:pow`
97+
> Luau cannot handle f64, i64 or i128, so we should provide math operation for it
9198
92-
> Luau only supports 32bit bit operations
99+
- Add bit operation
93100

94-
wchar and wstring support
101+
> Luau only supports 32bit bit operations
95102
96-
string(null ending) / buffer support
103+
- Add wchar and wstring support
97104

98-
void support
105+
> For windows API

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,17 @@ impl FfiConvert for CArrInfo {
140140
dst: &Ref<dyn FfiData>,
141141
src: &Ref<dyn FfiData>,
142142
) -> LuaResult<()> {
143-
dst.get_inner_pointer()
144-
.byte_offset(dst_offset)
145-
.copy_from(src.get_inner_pointer().byte_offset(src_offset), self.get_size());
143+
dst.get_inner_pointer().byte_offset(dst_offset).copy_from(
144+
src.get_inner_pointer().byte_offset(src_offset),
145+
self.get_size(),
146+
);
146147
Ok(())
147148
}
148149
}
149150

150151
impl LuaUserData for CArrInfo {
151152
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
153+
fields.add_meta_field(LuaMetaMethod::Type, "CArr");
152154
fields.add_field_method_get("size", |_, this| Ok(this.get_size()));
153155
fields.add_field_method_get("length", |_, this| Ok(this.get_length()));
154156
fields.add_field_function_get("inner", |lua, this: LuaAnyUserData| {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::ptr;
2-
31
use libffi::middle::{Cif, Type};
42
use mlua::prelude::*;
53

@@ -155,19 +153,18 @@ impl CFnInfo {
155153
this: &LuaAnyUserData,
156154
lua_function: LuaFunction<'lua>,
157155
) -> LuaResult<LuaAnyUserData<'lua>> {
158-
let closure = ClosureData::new(
159-
ptr::from_ref(lua),
156+
let closure_data = ClosureData::alloc(
157+
lua,
160158
self.cif.as_raw_ptr(),
161159
self.arg_info_list.clone(),
162160
self.result_info.clone(),
163161
lua.create_registry_value(&lua_function)?,
164162
)?;
165-
let closure_userdata = lua.create_userdata(closure)?;
166163

167-
association::set(lua, CLOSURE_CFN, &closure_userdata, this)?;
168-
association::set(lua, CLOSURE_FUNC, &closure_userdata, lua_function)?;
164+
association::set(lua, CLOSURE_CFN, &closure_data, this)?;
165+
association::set(lua, CLOSURE_FUNC, &closure_data, lua_function)?;
169166

170-
Ok(closure_userdata)
167+
Ok(closure_data)
171168
}
172169

173170
pub fn create_callable<'lua>(
@@ -206,6 +203,9 @@ impl CFnInfo {
206203
}
207204

208205
impl LuaUserData for CFnInfo {
206+
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
207+
fields.add_meta_field(LuaMetaMethod::Type, "CFn");
208+
}
209209
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
210210
// Subtype
211211
method_provider::provide_ptr(methods);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ pub fn stringify(lua: &Lua, userdata: &LuaAnyUserData) -> LuaResult<String> {
302302
CPtrInfo::stringify(lua, userdata)
303303
} else if userdata.is::<CFnInfo>() {
304304
CFnInfo::stringify(lua, userdata)
305+
} else if userdata.is::<CVoidInfo>() {
306+
CVoidInfo::stringify()
305307
} else if let Some(name) = ctype_helper::get_name(userdata)? {
306308
Ok(String::from(name))
307309
} else {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::{
1717
ptr_info::CPtrInfo,
1818
struct_info::CStructInfo,
1919
type_info::{CTypeCast, CTypeInfo},
20-
types::{ctype_helper, export_ctypes},
20+
types::{ctype_helper, export_c_types, export_fixed_types},
2121
void_info::CVoidInfo,
2222
};
2323

@@ -34,10 +34,10 @@ mod association_names {
3434
pub const CLOSURE_CFN: &str = "__closure_cfn";
3535
}
3636

37-
pub fn export(lua: &Lua) -> LuaResult<LuaTable> {
37+
pub fn export_c(lua: &Lua) -> LuaResult<LuaTable> {
3838
TableBuilder::new(lua)?
3939
.with_value("void", CVoidInfo::new())?
40-
.with_values(export_ctypes(lua)?)?
40+
.with_values(export_c_types(lua)?)?
4141
.with_function("struct", |lua, types: LuaTable| {
4242
CStructInfo::from_table(lua, types)
4343
})?

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl CPtrInfo {
124124

125125
impl LuaUserData for CPtrInfo {
126126
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
127+
fields.add_meta_field(LuaMetaMethod::Type, "CPtr");
127128
fields.add_field_method_get("size", |_, _| Ok(size_of::<usize>()));
128129
fields.add_field_function_get("inner", |lua, this| {
129130
let inner = association::get(lua, CPTR_INNER, this)?

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ impl FfiConvert for CStructInfo {
182182

183183
impl LuaUserData for CStructInfo {
184184
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
185+
fields.add_meta_field(LuaMetaMethod::Type, "CStruct");
185186
fields.add_field_method_get("size", |_, this| Ok(this.get_size()));
186187
}
187188
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ where
9191
Self: CTypeCast + FfiSignedness + FfiConvert + FfiSize,
9292
{
9393
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
94+
fields.add_meta_field(LuaMetaMethod::Type, "CType");
9495
fields.add_field_method_get("size", |_, this| Ok(this.get_size()));
9596
fields.add_meta_field(LuaMetaMethod::Type, "CType");
9697
fields.add_field_method_get("signedness", |_, this| Ok(this.get_signedness()));

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ macro_rules! create_ctypes {
3434
),)*])
3535
};
3636
}
37-
pub fn export_ctypes(lua: &Lua) -> LuaResult<Vec<(&'static str, LuaAnyUserData)>> {
37+
pub fn export_c_types(lua: &Lua) -> LuaResult<Vec<(&'static str, LuaAnyUserData)>> {
3838
create_ctypes!(
3939
lua,
4040
// Export Compile-time known c-types
@@ -55,6 +55,11 @@ pub fn export_ctypes(lua: &Lua) -> LuaResult<Vec<(&'static str, LuaAnyUserData)>
5555
("ulong", c_ulong, Type::c_ulong()),
5656
("longlong", c_longlong, Type::c_longlong()),
5757
("ulonglong", c_ulonglong, Type::c_ulonglong()),
58+
)
59+
}
60+
pub fn export_fixed_types(lua: &Lua) -> LuaResult<Vec<(&'static str, LuaAnyUserData)>> {
61+
create_ctypes!(
62+
lua,
5863
// Export Source-time known c-types (fixed)
5964
("u8", u8, Type::u8()),
6065
("u16", u16, Type::u16()),
@@ -70,10 +75,8 @@ pub fn export_ctypes(lua: &Lua) -> LuaResult<Vec<(&'static str, LuaAnyUserData)>
7075
("f32", f32, Type::f32()),
7176
("usize", usize, Type::usize()),
7277
("isize", isize, Type::isize()),
73-
// TODO: c_float and c_double sometime can be half and single,
74-
// TODO: but libffi-rs doesn't support it. need work-around or drop support
75-
("float", f32, Type::f32()),
76-
("double", f64, Type::f64()),
78+
("f32", f32, Type::f32()),
79+
("f64", f64, Type::f64()),
7780
)
7881
}
7982

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,25 @@ impl FfiSize for CVoidInfo {
1717
0
1818
}
1919
}
20+
2021
impl CVoidInfo {
2122
pub fn new() -> Self {
2223
Self()
2324
}
2425
pub fn get_middle_type() -> Type {
2526
Type::void()
2627
}
28+
pub fn stringify() -> LuaResult<String> {
29+
Ok(String::from("CVoid"))
30+
}
2731
}
2832

29-
impl LuaUserData for CVoidInfo {}
33+
impl LuaUserData for CVoidInfo {
34+
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
35+
fields.add_meta_field(LuaMetaMethod::Type, "CVoid");
36+
}
37+
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
38+
method_provider::provide_to_string(methods);
39+
method_provider::provide_ptr(methods);
40+
}
41+
}

0 commit comments

Comments
 (0)