Closed dgavedissian closed 3 months ago
Overall looks good! Arrays/slices support will be a nice addition.
@HaronK I just pushed another commit which adds support for [T; N]
, &[T; N]
and &mut [T; N]
. It works by creating a slice from the pointer passed from FFI by first converting it into a slice with a hardcoded size of N, then assumes the length is correct by using .try_into().unwrap()
. That will obviously cause crashes if the length passed from Lua is wrong, but I think that's a sensible compromise to allow fixed size arrays.
If you would prefer, an alternative is to still pass the size to FFI, and simply let the FFI crash with a panic if the size doesn't match the expected one.
pub fn move_primitive_array(&mut self, data: [f32; 3]) {
}
pub fn set_primitive_array(&mut self, data: &[f32; 3]) {
}
pub fn get_primitive_array(&self, out: &mut [f32; 3]) {
}
pub fn move_custom_array(&mut self, data: [Data; 3]) {
}
pub fn set_custom_array(&mut self, data: &[Data; 3]) {
}
pub fn get_custom_array(&self, out: &mut [Data; 3]) {
}
void TestStructArrays_MovePrimitiveArray (TestStructArrays*, float const* data);
void TestStructArrays_SetPrimitiveArray (TestStructArrays*, float const* data);
void TestStructArrays_GetPrimitiveArray (TestStructArrays const*, float* out);
void TestStructArrays_MoveCustomArray (TestStructArrays*, Data const* data);
void TestStructArrays_SetCustomArray (TestStructArrays*, Data const* data);
void TestStructArrays_GetCustomArray (TestStructArrays const*, Data* out);
OK, I got it to a state where this happens:
thread 'test_primitive_array' panicked at engine/lib/luajit-ffi-gen/tests/test_impl_arrays.rs:21:1:
expected an array of 3 elements, got 2
When calling:
TestStructArrays_MovePrimitiveArray(&mut ts, data_array.as_ptr(), 2);
This also does the right thing if the index is too large.
assert_eq
check before calling from_raw_parts_mut
to prevent potential undefined behavior.Done all those things. Thanks for the feedback. I'll merge once CI passes.
It will probably be easier to review this commit-by-commit.