Limit-Theory-Redux / ltheory

Limit Theory Redux is a fork of the discontinued open-world space simulation game Limit Theory. We have made it our mission to continue the development of the ambitious Limit Theory project as an open source initiative.
Apache License 2.0
62 stars 8 forks source link

Add Slice and Array support to LuaJIT FFI gen, clean up the type generation code. #316

Closed dgavedissian closed 3 months ago

dgavedissian commented 3 months ago

It will probably be easier to review this commit-by-commit.

HaronK commented 3 months ago

Overall looks good! Arrays/slices support will be a nice addition.

dgavedissian commented 3 months ago

@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);
dgavedissian commented 3 months ago

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.

HaronK commented 3 months ago
dgavedissian commented 3 months ago

Done all those things. Thanks for the feedback. I'll merge once CI passes.