Skip to content

Commit

Permalink
Fix casting and add casting test (lune-org#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwreey committed Oct 24, 2024
1 parent 154c68a commit 14d2b60
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
22 changes: 16 additions & 6 deletions crates/lune-std-ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ See [tests/ffi](../../tests/ffi/README.md)

- Array argument in cfn

- Ref boundary fix

## Code structure

### /c
Expand Down Expand Up @@ -84,19 +86,26 @@ Implememt type-casting for all CTypes

- **Trait `FfiSize`**
- **Trait `FfiSignedness`**
- **Trait `FfiConvert`:** Provide read LuaValue from FfiData or write LuaValue into FfiData

**Traits:** Provide call information trait
**Structs:** Provide call information trait

- **Trait `FfiArg`:** Used for argument boundary checking and callback argument ref flag
- **Trait `FfiResult`:** Used for result boundary checking
- **Struct `FfiArg`:** Used for argument boundary checking and callback argument ref flag
- **Struct `FfiResult`:** Used for result boundary checking

**Trait `FfiData`:** Provide common data handle, including methods below

- **Method `check_inner_boundary`:** check boundary with offset and size
- **Method `get_inner_pointer`:** returns raw pointer `*mut ()`
- **Method `is_writable`**
- **Method `is_readable`**
- **Method `copy_from`** copy data from another data

- **Trait `FfiConvert`:** Provide methods for read LuaValue from FfiData or write LuaValue into FfiData

- **Method `value_into_data`:** set data with lua value
- **Method `value_from_data`:** get lua value from data
- **Method `copy_data`:** copy sized data into another data
- **Method `stringify_data`:** stringify data with specific type

> Note: `GetFfiData` trait in `data/mod.rs` provides `AnyUserData.get_data_handle() -> FfiData` method
Expand All @@ -108,6 +117,7 @@ Implememt type-casting for all CTypes
- **Function `num_cast<From, Into>(from: FfiData, from: FfiData)`:**
Cast number type value inno another number type
- [**Mod `libffi_helper.rs`:**](./src/ffi/libffi_helper.rs)
- **Const `FFI_STATUS_NAMES`:** Used for ffi_status stringify
- **Function `get_ensured_size`:** Returns ensured ffi_type size
- **Const `FFI_STATUS_NAMES`:** Used for `ffi_status` stringify
- **Function `get_ensured_size`:** Returns ensured `ffi_type` size
- **Const `SIZE_OF_POINTER`:** Platform specific pointer size (Compile time known)
- **Function `ffi_status_assert`:** Convert `ffi_status` to `LuaResult<()>`
2 changes: 1 addition & 1 deletion crates/lune-std-ffi/src/c/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ where
into: &Ref<dyn FfiData>,
) -> LuaResult<()> {
define_cast_num!(
From, self, into_info, from_info, from, into,
From, self, from_info, into_info, from, into,
u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64 usize isize
)
}
Expand Down
16 changes: 7 additions & 9 deletions tests/ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ gcc for library compiling (for external-\*)

**Luau-side**

- [x] [isInteger](./isInteger)
- [x] [cast](./cast.luau)

- [ ] [pretty_print](./pretty_print)

> need box, ref test
- [x] [isInteger](./isInteger)
- [ ] [into_boundary](./into_boundary)

> need assertion
- [ ] [from_boundary](./from_boundary)
- [ ] [write_boundary](./write_boundary.luau)

> need assertion
> Failed, need fix
- [ ] [cast](./cast)
- [ ] [read_boundary](./read_boundary.luau)

> need assertion
> Failed, need fix
## Benchmark Results

Expand Down
13 changes: 13 additions & 0 deletions tests/ffi/cast.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local ffi = require("@lune/ffi")

local floatBox = ffi.f32:box(1.2)
local intBox = ffi.box(ffi.i32.size)

ffi.f32:cast(ffi.i32, floatBox, intBox)

local castedInt = ffi.i32:readData(intBox)

assert(
castedInt == 1 and ffi.isInteger(castedInt),
"castedInt == 1 and ffi.isInteger(castedInt) assersion failed"
)
7 changes: 2 additions & 5 deletions tests/ffi/isInteger.luau
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
--!nocheck
--!nolint

local ffi = require("@lune/ffi")

local int = 0b1
local float = 0.5

assert(ffi.isInteger(int) == true, "ffi.isInteger(int) == true failed")
assert(ffi.isInteger(float) == false, "ffi.isInteger(float) == false failed")
assert(ffi.isInteger(int) == true, "ffi.isInteger(int) == true assersion failed")
assert(ffi.isInteger(float) == false, "ffi.isInteger(float) == false assersion failed")

0 comments on commit 14d2b60

Please sign in to comment.